From b648f604c25749ad494a851a221990bb87109c3a Mon Sep 17 00:00:00 2001 From: Alexandre ABRIOUX Date: Thu, 28 Nov 2024 19:43:19 +0100 Subject: [PATCH 1/2] feat(ethereum-storage): custom gas limit --- package.json | 3 ++- .../src/ethereum-tx-submitter.ts | 18 ++++++++++++++-- .../test/ethereum-tx-submitter.test.ts | 21 +++++++++++++++++++ 3 files changed, 39 insertions(+), 3 deletions(-) diff --git a/package.json b/package.json index 0745da420..a465df7d6 100644 --- a/package.json +++ b/package.json @@ -66,5 +66,6 @@ "semver": "https://github.com/RequestNetwork/requestNetwork/security/dependabot/197", "json-schema": "https://github.com/RequestNetwork/requestNetwork/security/dependabot/51", "json5": "https://github.com/RequestNetwork/requestNetwork/security/dependabot/165" - } + }, + "packageManager": "yarn@1.22.21+sha1.1959a18351b811cdeedbd484a8f86c3cc3bbaf72" } diff --git a/packages/ethereum-storage/src/ethereum-tx-submitter.ts b/packages/ethereum-storage/src/ethereum-tx-submitter.ts index e87d4d712..3045f595a 100644 --- a/packages/ethereum-storage/src/ethereum-tx-submitter.ts +++ b/packages/ethereum-storage/src/ethereum-tx-submitter.ts @@ -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; @@ -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; @@ -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, @@ -44,6 +50,7 @@ export class EthereumTransactionSubmitter implements StorageTypes.ITransactionSu gasPriceMin, gasPriceMax, gasPriceMultiplier, + gasLimit, debugProvider, }: SubmitterProps) { this.logger = logger || new SimpleLogger(); @@ -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); @@ -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, + }; } } diff --git a/packages/ethereum-storage/test/ethereum-tx-submitter.test.ts b/packages/ethereum-storage/test/ethereum-tx-submitter.test.ts index 7409fb7ba..965efae8d 100644 --- a/packages/ethereum-storage/test/ethereum-tx-submitter.test.ts +++ b/packages/ethereum-storage/test/ethereum-tx-submitter.test.ts @@ -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) }), + ); + }); }); From 329c3b4de5d12503de0b75c6a8f71b9c764f9983 Mon Sep 17 00:00:00 2001 From: Alexandre ABRIOUX Date: Thu, 28 Nov 2024 19:44:39 +0100 Subject: [PATCH 2/2] reset package.json --- package.json | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/package.json b/package.json index a465df7d6..0745da420 100644 --- a/package.json +++ b/package.json @@ -66,6 +66,5 @@ "semver": "https://github.com/RequestNetwork/requestNetwork/security/dependabot/197", "json-schema": "https://github.com/RequestNetwork/requestNetwork/security/dependabot/51", "json5": "https://github.com/RequestNetwork/requestNetwork/security/dependabot/165" - }, - "packageManager": "yarn@1.22.21+sha1.1959a18351b811cdeedbd484a8f86c3cc3bbaf72" + } }