Skip to content
Merged
Show file tree
Hide file tree
Changes from all 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
18 changes: 16 additions & 2 deletions packages/ethereum-storage/src/ethereum-tx-submitter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { CurrencyTypes, LogTypes, StorageTypes } from '@requestnetwork/types';
import { requestHashSubmitterArtifact } from '@requestnetwork/smart-contracts';
import { RequestOpenHashSubmitter } from '@requestnetwork/smart-contracts/types';
import { GasFeeDefiner } from './gas-fee-definer';
import { SimpleLogger, isEip1559Supported } from '@requestnetwork/utils';
import { isEip1559Supported, SimpleLogger } from '@requestnetwork/utils';

export type SubmitterProps = {
signer: Signer;
Expand All @@ -22,6 +22,11 @@ export type SubmitterProps = {
* The default is 100, which does not change the value (100 is equal to x1, 200 is equal to x2).
*/
gasPriceMultiplier?: number;
/**
* If set, the gas limit will be used when submitting transactions.
* If not, the gas limit will be estimated on each submission.
*/
gasLimit?: BigNumber;
network: CurrencyTypes.EvmChainName;
logger?: LogTypes.ILogger;
debugProvider?: boolean;
Expand All @@ -36,6 +41,7 @@ export class EthereumTransactionSubmitter implements StorageTypes.ITransactionSu
private readonly hashSubmitter: RequestOpenHashSubmitter;
private readonly provider: providers.JsonRpcProvider;
private readonly gasFeeDefiner: GasFeeDefiner;
private readonly gasLimit: BigNumber | undefined;

constructor({
network,
Expand All @@ -44,6 +50,7 @@ export class EthereumTransactionSubmitter implements StorageTypes.ITransactionSu
gasPriceMin,
gasPriceMax,
gasPriceMultiplier,
gasLimit,
debugProvider,
}: SubmitterProps) {
this.logger = logger || new SimpleLogger();
Expand All @@ -60,6 +67,7 @@ export class EthereumTransactionSubmitter implements StorageTypes.ITransactionSu
gasPriceMultiplier,
logger: this.logger,
});
this.gasLimit = gasLimit;
if (debugProvider) {
this.provider.on('debug', (event) => {
this.logger.debug('JsonRpcProvider debug event', event);
Expand Down Expand Up @@ -96,6 +104,12 @@ export class EthereumTransactionSubmitter implements StorageTypes.ITransactionSu
utils.hexZeroPad(utils.hexlify(ipfsSize), 32),
]);

return { to: this.hashSubmitter.address, data: tx, value: fee, ...gasFees };
return {
to: this.hashSubmitter.address,
data: tx,
value: fee,
gasLimit: this.gasLimit,
...gasFees,
};
}
}
21 changes: 21 additions & 0 deletions packages/ethereum-storage/test/ethereum-tx-submitter.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,4 +49,25 @@ describe(EthereumTransactionSubmitter, () => {
expect.objectContaining({ action: 'request' }),
);
});

it('should not use gas limit by default', async () => {
const sendTransactionSpy = jest.spyOn(signer, 'sendTransaction');
await txSubmitter.submit('hash', 1);
expect(sendTransactionSpy).toHaveBeenCalledWith(
expect.objectContaining({ gasLimit: undefined }),
);
});

it('can use a custom gas limit', async () => {
const txSubmitterWithGasLimit = new EthereumTransactionSubmitter({
network: 'private',
signer,
gasLimit: BigNumber.from(1000000),
});
const sendTransactionSpy = jest.spyOn(signer, 'sendTransaction');
await txSubmitterWithGasLimit.submit('hash', 1);
expect(sendTransactionSpy).toHaveBeenCalledWith(
expect.objectContaining({ gasLimit: BigNumber.from(1000000) }),
);
});
});