Skip to content

Commit

Permalink
feat(ethereum-storage): custom gas limit
Browse files Browse the repository at this point in the history
  • Loading branch information
alexandre-abrioux committed Nov 28, 2024
1 parent d54df7f commit b648f60
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 3 deletions.
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
}
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) }),
);
});
});

0 comments on commit b648f60

Please sign in to comment.