Skip to content

Commit

Permalink
chore(ethereum-storage): log JSON-RPC requests (#1288)
Browse files Browse the repository at this point in the history
  • Loading branch information
alexandre-abrioux committed Dec 11, 2023
1 parent e3e5446 commit febf862
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 1 deletion.
8 changes: 7 additions & 1 deletion packages/ethereum-storage/src/ethereum-tx-submitter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ export type SubmitterProps = {
gasPriceMin?: BigNumber;
network: CurrencyTypes.EvmChainName;
logger?: LogTypes.ILogger;
debugProvider?: boolean;
};

/**
Expand All @@ -22,7 +23,7 @@ export class EthereumTransactionSubmitter implements StorageTypes.ITransactionSu
private readonly provider: providers.JsonRpcProvider;
private readonly gasFeeDefiner: GasFeeDefiner;

constructor({ network, signer, logger, gasPriceMin }: SubmitterProps) {
constructor({ network, signer, logger, gasPriceMin, debugProvider }: SubmitterProps) {
this.logger = logger || new SimpleLogger();
const provider = signer.provider as providers.JsonRpcProvider;
this.provider = provider;
Expand All @@ -31,6 +32,11 @@ export class EthereumTransactionSubmitter implements StorageTypes.ITransactionSu
signer,
) as RequestOpenHashSubmitter; // type mismatch with ethers.
this.gasFeeDefiner = new GasFeeDefiner({ provider, gasPriceMin, logger: this.logger });
if (debugProvider) {
this.provider.on('debug', (event) => {
this.logger.debug('JsonRpcProvider debug event', event);
});
}
}

get hashSubmitterAddress(): string {
Expand Down
20 changes: 20 additions & 0 deletions packages/ethereum-storage/test/ethereum-tx-submitter.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { JsonRpcProvider } from '@ethersproject/providers';
import { BigNumber, Wallet } from 'ethers';
import { EthereumTransactionSubmitter } from '../src';
import { LogTypes } from '@requestnetwork/types';

const mnemonic = 'candy maple cake sugar pudding cream honey rich smooth crumble sweet treat';
const signer = Wallet.fromMnemonic(mnemonic).connect(new JsonRpcProvider('http://localhost:8545'));
Expand All @@ -23,4 +24,23 @@ describe(EthereumTransactionSubmitter, () => {
const tx = await txSubmitter.submit('hash', 1);
expect(tx.hash).toMatch(/^0x.+/);
});
it('can debug transactions', async () => {
const debugMock = jest.fn();
const logger = {
debug: debugMock,
warn: jest.fn(),
error: jest.fn(),
} as any as LogTypes.ILogger;
const txSubmitter = new EthereumTransactionSubmitter({
network: 'private',
signer,
logger,
debugProvider: true,
});
await txSubmitter.submit('hash', 1);
expect(debugMock).toHaveBeenCalledWith(
'JsonRpcProvider debug event',
expect.objectContaining({ action: 'request' }),
);
});
});

0 comments on commit febf862

Please sign in to comment.