Skip to content

Commit cf4c125

Browse files
feat(ethereum-storage): custom gas limit (#1495)
1 parent dd294d8 commit cf4c125

File tree

2 files changed

+37
-2
lines changed

2 files changed

+37
-2
lines changed

packages/ethereum-storage/src/ethereum-tx-submitter.ts

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import { CurrencyTypes, LogTypes, StorageTypes } from '@requestnetwork/types';
33
import { requestHashSubmitterArtifact } from '@requestnetwork/smart-contracts';
44
import { RequestOpenHashSubmitter } from '@requestnetwork/smart-contracts/types';
55
import { GasFeeDefiner } from './gas-fee-definer';
6-
import { SimpleLogger, isEip1559Supported } from '@requestnetwork/utils';
6+
import { isEip1559Supported, SimpleLogger } from '@requestnetwork/utils';
77

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

4046
constructor({
4147
network,
@@ -44,6 +50,7 @@ export class EthereumTransactionSubmitter implements StorageTypes.ITransactionSu
4450
gasPriceMin,
4551
gasPriceMax,
4652
gasPriceMultiplier,
53+
gasLimit,
4754
debugProvider,
4855
}: SubmitterProps) {
4956
this.logger = logger || new SimpleLogger();
@@ -60,6 +67,7 @@ export class EthereumTransactionSubmitter implements StorageTypes.ITransactionSu
6067
gasPriceMultiplier,
6168
logger: this.logger,
6269
});
70+
this.gasLimit = gasLimit;
6371
if (debugProvider) {
6472
this.provider.on('debug', (event) => {
6573
this.logger.debug('JsonRpcProvider debug event', event);
@@ -96,6 +104,12 @@ export class EthereumTransactionSubmitter implements StorageTypes.ITransactionSu
96104
utils.hexZeroPad(utils.hexlify(ipfsSize), 32),
97105
]);
98106

99-
return { to: this.hashSubmitter.address, data: tx, value: fee, ...gasFees };
107+
return {
108+
to: this.hashSubmitter.address,
109+
data: tx,
110+
value: fee,
111+
gasLimit: this.gasLimit,
112+
...gasFees,
113+
};
100114
}
101115
}

packages/ethereum-storage/test/ethereum-tx-submitter.test.ts

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,4 +49,25 @@ describe(EthereumTransactionSubmitter, () => {
4949
expect.objectContaining({ action: 'request' }),
5050
);
5151
});
52+
53+
it('should not use gas limit by default', async () => {
54+
const sendTransactionSpy = jest.spyOn(signer, 'sendTransaction');
55+
await txSubmitter.submit('hash', 1);
56+
expect(sendTransactionSpy).toHaveBeenCalledWith(
57+
expect.objectContaining({ gasLimit: undefined }),
58+
);
59+
});
60+
61+
it('can use a custom gas limit', async () => {
62+
const txSubmitterWithGasLimit = new EthereumTransactionSubmitter({
63+
network: 'private',
64+
signer,
65+
gasLimit: BigNumber.from(1000000),
66+
});
67+
const sendTransactionSpy = jest.spyOn(signer, 'sendTransaction');
68+
await txSubmitterWithGasLimit.submit('hash', 1);
69+
expect(sendTransactionSpy).toHaveBeenCalledWith(
70+
expect.objectContaining({ gasLimit: BigNumber.from(1000000) }),
71+
);
72+
});
5273
});

0 commit comments

Comments
 (0)