Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: LoggingStaticJsonRpcProvider #2495

Merged
merged 5 commits into from
Apr 10, 2024
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions packages/client/src/Ethereum.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,12 @@
* Config and utilities for interating with identity & Ethereum chain.
*/
import { Wallet } from '@ethersproject/wallet'
import { StaticJsonRpcProvider } from '@ethersproject/providers'
import type { Provider } from '@ethersproject/providers'
import type { ConnectionInfo } from '@ethersproject/web'
import type { Overrides } from '@ethersproject/contracts'
import type { BigNumber } from '@ethersproject/bignumber'
import { ChainConnectionInfo, StrictStreamrClientConfig } from './Config'
import { LoggingStaticJsonRpcProvider } from './utils/LoggingStaticJsonRpcProvider'

export const generateEthereumAccount = (): { address: string, privateKey: string } => {
const wallet = Wallet.createRandom()
Expand All @@ -23,7 +23,7 @@ export const getStreamRegistryChainProviders = (config: Pick<StrictStreamrClient

const getRpcProviders = (connectionInfo: ChainConnectionInfo, pollInterval?: number): Provider[] => {
return connectionInfo.rpcs.map((c: ConnectionInfo) => {
const provider = new StaticJsonRpcProvider(c)
const provider = new LoggingStaticJsonRpcProvider(c)
if (pollInterval !== undefined) {
provider.pollingInterval = pollInterval
}
Expand Down
34 changes: 34 additions & 0 deletions packages/client/src/utils/LoggingStaticJsonRpcProvider.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import { StaticJsonRpcProvider } from '@ethersproject/providers'
import { Logger, randomString } from '@streamr/utils'

export class LoggingStaticJsonRpcProvider extends StaticJsonRpcProvider {
private readonly logger = new Logger(module)

override async send(method: string, params: any[]): Promise<any> {
const traceId = randomString(5)
const startTime = Date.now()
const logContext = {
traceId,
method,
params,
connection: this.connection
harbu marked this conversation as resolved.
Show resolved Hide resolved
}
this.logger.debug('Send request', logContext)
let result
try {
result = await super.send(method, params)
} catch (err) {
this.logger.debug('Encountered error while requesting', {
...logContext,
err,
elapsedTime: Date.now() - startTime
})
throw err
}
this.logger.debug('Received response', {
...logContext,
elapsedTime: Date.now() - startTime
})
return result
}
}
Loading