Skip to content

Commit

Permalink
Merge branch 'master' into feat/contract-update-with-safe
Browse files Browse the repository at this point in the history
  • Loading branch information
leoslr committed Jan 11, 2024
2 parents 814ed96 + aafe4df commit 5393949
Show file tree
Hide file tree
Showing 8 changed files with 118 additions and 85 deletions.
32 changes: 30 additions & 2 deletions packages/ethereum-storage/src/ethereum-tx-submitter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,21 @@ import { SimpleLogger, isEip1559Supported } from '@requestnetwork/utils';

export type SubmitterProps = {
signer: Signer;
/**
* The minimum value for maxPriorityFeePerGas and maxFeePerGas.
* The default is zero.
*/
gasPriceMin?: BigNumber;
/**
* The maximum value for maxFeePerGas.
* There is no limit if no value is set.
*/
gasPriceMax?: BigNumber;
/**
* A multiplier for the computed maxFeePerGas.
* The default is 100, which does not change the value (100 is equal to x1, 200 is equal to x2).
*/
gasPriceMultiplier?: number;
network: CurrencyTypes.EvmChainName;
logger?: LogTypes.ILogger;
debugProvider?: boolean;
Expand All @@ -23,15 +37,29 @@ export class EthereumTransactionSubmitter implements StorageTypes.ITransactionSu
private readonly provider: providers.JsonRpcProvider;
private readonly gasFeeDefiner: GasFeeDefiner;

constructor({ network, signer, logger, gasPriceMin, debugProvider }: SubmitterProps) {
constructor({
network,
signer,
logger,
gasPriceMin,
gasPriceMax,
gasPriceMultiplier,
debugProvider,
}: SubmitterProps) {
this.logger = logger || new SimpleLogger();
const provider = signer.provider as providers.JsonRpcProvider;
this.provider = provider;
this.hashSubmitter = requestHashSubmitterArtifact.connect(
network,
signer,
) as RequestOpenHashSubmitter; // type mismatch with ethers.
this.gasFeeDefiner = new GasFeeDefiner({ provider, gasPriceMin, logger: this.logger });
this.gasFeeDefiner = new GasFeeDefiner({
provider,
gasPriceMin,
gasPriceMax,
gasPriceMultiplier,
logger: this.logger,
});
if (debugProvider) {
this.provider.on('debug', (event) => {
this.logger.debug('JsonRpcProvider debug event', event);
Expand Down
16 changes: 13 additions & 3 deletions packages/ethereum-storage/src/gas-fee-definer.ts
Original file line number Diff line number Diff line change
@@ -1,31 +1,41 @@
import { suggestFees } from '@rainbow-me/fee-suggestions';
import { BigNumber, providers, constants } from 'ethers';
import { BigNumber, providers } from 'ethers';
import { normalizeGasFees } from '@requestnetwork/utils';
import { FeeTypes, LogTypes } from '@requestnetwork/types';

export class GasFeeDefiner {
private readonly logger: LogTypes.ILogger;
private readonly provider: providers.JsonRpcProvider;
private readonly gasPriceMin: BigNumber;
private readonly gasPriceMin?: BigNumber;
private readonly gasPriceMax?: BigNumber;
private readonly gasPriceMultiplier?: number;

constructor({
logger,
provider,
gasPriceMin,
gasPriceMax,
gasPriceMultiplier,
}: {
logger: LogTypes.ILogger;
gasPriceMin?: BigNumber;
gasPriceMax?: BigNumber;
gasPriceMultiplier?: number;
provider: providers.JsonRpcProvider;
}) {
this.logger = logger;
this.provider = provider;
this.gasPriceMin = gasPriceMin || constants.Zero;
this.gasPriceMin = gasPriceMin;
this.gasPriceMax = gasPriceMax;
this.gasPriceMultiplier = gasPriceMultiplier;
}

public async getGasFees(): Promise<FeeTypes.EstimatedGasFees> {
return normalizeGasFees({
logger: this.logger,
gasPriceMin: this.gasPriceMin,
gasPriceMax: this.gasPriceMax,
gasPriceMultiplier: this.gasPriceMultiplier,
suggestFees: async () => {
const { baseFeeSuggestion, maxPriorityFeeSuggestions } = await suggestFees(this.provider);
return {
Expand Down
2 changes: 1 addition & 1 deletion packages/payment-processor/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@
"test:watch": "yarn test --watch"
},
"dependencies": {
"@openzeppelin/contracts": "4.7.3",
"@openzeppelin/contracts": "4.9.5",
"@requestnetwork/currency": "0.12.0",
"@requestnetwork/payment-detection": "0.39.0",
"@requestnetwork/smart-contracts": "0.32.0",
Expand Down
2 changes: 1 addition & 1 deletion packages/smart-contracts/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@
"@nomiclabs/hardhat-ethers": "2.0.2",
"@nomiclabs/hardhat-waffle": "2.0.1",
"@nomiclabs/hardhat-web3": "2.0.0",
"@openzeppelin/contracts": "4.4.2",
"@openzeppelin/contracts": "4.9.5",
"@rainbow-me/fee-suggestions": "2.1.0",
"@requestnetwork/currency": "0.12.0",
"@requestnetwork/types": "0.39.0",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,14 +61,6 @@ export const batchPaymentsArtifact = new ContractArtifact<BatchPayments>(
address: '0x0DD57FFe83a53bCbd657e234B16A3e74fEDb8fBA',
creationBlockNumber: 35498688,
},
zksyncera: {
address: '0x0C41700ee1B363DB2ebC1a985f65cAf6eC4b1023',
creationBlockNumber: 19545614,
},
zksynceratestnet: {
address: '0x276A92F78aA527b8e157B0E47cEB63b4239dfa0b',
creationBlockNumber: 13814862,
},
},
},
},
Expand Down
17 changes: 13 additions & 4 deletions packages/utils/src/normalize-gas-fees.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { BigNumber, constants } from 'ethers';

import { maxBigNumber } from './index';
import { maxBigNumber, minBigNumber } from './index';
import { LogTypes, FeeTypes } from '@requestnetwork/types';

/**
Expand All @@ -14,22 +14,31 @@ import { LogTypes, FeeTypes } from '@requestnetwork/types';
async function normalizeGasFees({
logger,
gasPriceMin,
gasPriceMax,
gasPriceMultiplier,
suggestFees,
}: {
logger: LogTypes.ILogger;
gasPriceMin?: BigNumber;
gasPriceMax?: BigNumber;
gasPriceMultiplier?: number;
suggestFees: () => Promise<FeeTypes.SuggestedFees>;
}): Promise<FeeTypes.EstimatedGasFees> {
try {
const suggestedFee = await suggestFees();

const baseFee = maxBigNumber(suggestedFee.baseFee, gasPriceMin || constants.Zero);

const maxPriorityFeePerGas = maxBigNumber(
suggestedFee.maxPriorityFee,
gasPriceMin || constants.Zero,
);
const maxFeePerGas = baseFee.add(maxPriorityFeePerGas);

const maxFeePerGasInit = baseFee
.add(maxPriorityFeePerGas)
.mul(gasPriceMultiplier || 100)
.div(100);
const maxFeePerGas = gasPriceMax
? minBigNumber(maxFeePerGasInit, gasPriceMax)
: maxFeePerGasInit;

if (maxPriorityFeePerGas.eq(0) || maxFeePerGas.eq(0)) {
logger.warn(
Expand Down
26 changes: 26 additions & 0 deletions packages/utils/test/normalize-gas-fees.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,30 @@ describe('Normalize Gas Fees', () => {
expect(normalizedGasFees.maxPriorityFeePerGas?.toString()).toBe('1000000000');
expect(normalizedGasFees.maxFeePerGas?.toString()).toBe('2000000000');
});

it('should respect maximum gas fee', async () => {
const normalizedGasFees = await normalizeGasFees({
logger: console,
suggestFees: async () => ({
baseFee: '400000000000', // 400 Gwei
maxPriorityFee: '2000000000', // 2 Gwei
}),
gasPriceMax: BigNumber.from('250000000000'), // 250 Gwei
});
expect(normalizedGasFees.maxPriorityFeePerGas?.toString()).toBe('2000000000'); // 2 Gwei
expect(normalizedGasFees.maxFeePerGas?.toString()).toBe('250000000000'); // 250 Gwei
});

it('should respect gas multiplier', async () => {
const normalizedGasFees = await normalizeGasFees({
logger: console,
suggestFees: async () => ({
baseFee: '20000000000', // 20 Gwei
maxPriorityFee: '2000000000', // 2 Gwei
}),
gasPriceMultiplier: 200, // x2
});
expect(normalizedGasFees.maxPriorityFeePerGas?.toString()).toBe('2000000000'); // 2 Gwei
expect(normalizedGasFees.maxFeePerGas?.toString()).toBe('44000000000'); // (20 + 2) x 2 = 44 Gwei
});
});
100 changes: 34 additions & 66 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,7 @@
"@jridgewell/trace-mapping" "^0.3.17"
jsesc "^2.5.1"

"@babel/generator@^7.22.7", "@babel/generator@^7.22.9":
"@babel/generator@^7.22.9":
version "7.22.9"
resolved "https://registry.yarnpkg.com/@babel/generator/-/generator-7.22.9.tgz#572ecfa7a31002fa1de2a9d91621fd895da8493d"
integrity sha512-KtLMbmicyuK2Ak/FTCJVbDnkN1SlT8/kceFTiuDiiRUUSMnHMidxSCdG4ndkTOHHpoomWe/4xkvHkEOncwjYIw==
Expand All @@ -178,6 +178,16 @@
"@jridgewell/trace-mapping" "^0.3.17"
jsesc "^2.5.1"

"@babel/generator@^7.23.6":
version "7.23.6"
resolved "https://registry.yarnpkg.com/@babel/generator/-/generator-7.23.6.tgz#9e1fca4811c77a10580d17d26b57b036133f3c2e"
integrity sha512-qrSfCYxYQB5owCmGLbl8XRpX1ytXlpueOb0N0UmQwA073KZxejgQTzAmJezxvpwQD9uGtK2shHdi55QT+MbjIw==
dependencies:
"@babel/types" "^7.23.6"
"@jridgewell/gen-mapping" "^0.3.2"
"@jridgewell/trace-mapping" "^0.3.17"
jsesc "^2.5.1"

"@babel/helper-annotate-as-pure@^7.14.5":
version "7.14.5"
resolved "https://registry.npmjs.org/@babel/helper-annotate-as-pure/-/helper-annotate-as-pure-7.14.5.tgz"
Expand Down Expand Up @@ -239,7 +249,7 @@
resolved "https://registry.yarnpkg.com/@babel/helper-environment-visitor/-/helper-environment-visitor-7.22.5.tgz#f06dd41b7c1f44e1f8da6c4055b41ab3a09a7e98"
integrity sha512-XGmhECfVA/5sAt+H+xpSg0mfrHq6FzNr9Oxh7PSEBBRUb/mL7Kz3NICXb194rCqAEdxkhPT1a88teizAFyvk8Q==

"@babel/helper-function-name@^7.14.5", "@babel/helper-function-name@^7.22.5":
"@babel/helper-function-name@^7.14.5":
version "7.22.5"
resolved "https://registry.yarnpkg.com/@babel/helper-function-name/-/helper-function-name-7.22.5.tgz#ede300828905bb15e582c037162f99d5183af1be"
integrity sha512-wtHSq6jMRE3uF2otvfuD3DIvVhOsSNshQl0Qrd7qC9oQJzHvOL4qQXlQn2916+CXGywIjpGuIkoyZRRxHPiNQQ==
Expand Down Expand Up @@ -487,6 +497,11 @@
resolved "https://registry.yarnpkg.com/@babel/parser/-/parser-7.23.0.tgz#da950e622420bf96ca0d0f2909cdddac3acd8719"
integrity sha512-vvPKKdMemU85V9WE/l5wZEmImpCtLqbnTvqDS2U1fJ96KrxoW7KrXhNsNCblQlg8Ck4b85yxdTyelsMUgFUXiw==

"@babel/parser@^7.23.6":
version "7.23.6"
resolved "https://registry.yarnpkg.com/@babel/parser/-/parser-7.23.6.tgz#ba1c9e512bda72a47e285ae42aff9d2a635a9e3b"
integrity sha512-Z2uID7YJ7oNvAI20O9X0bblw7Qqs8Q2hFy0R9tAfnfLkp5MW0UH9eUvnDSnFwKZ0AvgS1ucqR4KzvVHgnke1VQ==

"@babel/plugin-proposal-class-properties@^7.0.0":
version "7.14.5"
resolved "https://registry.npmjs.org/@babel/plugin-proposal-class-properties/-/plugin-proposal-class-properties-7.14.5.tgz"
Expand Down Expand Up @@ -814,52 +829,20 @@
"@babel/parser" "^7.22.5"
"@babel/types" "^7.22.5"

"@babel/traverse@^7.14.0", "@babel/traverse@^7.16.8", "@babel/traverse@^7.23.5":
version "7.23.5"
resolved "https://registry.yarnpkg.com/@babel/traverse/-/traverse-7.23.5.tgz#f546bf9aba9ef2b042c0e00d245990c15508e7ec"
integrity sha512-czx7Xy5a6sapWWRx61m1Ke1Ra4vczu1mCTtJam5zRTBOonfdJ+S/B6HYmGYu3fJtr8GGET3si6IhgWVBhJ/m8w==
"@babel/traverse@^7.14.0", "@babel/traverse@^7.14.5", "@babel/traverse@^7.16.8", "@babel/traverse@^7.22.6", "@babel/traverse@^7.22.8", "@babel/traverse@^7.23.2", "@babel/traverse@^7.23.5":
version "7.23.7"
resolved "https://registry.yarnpkg.com/@babel/traverse/-/traverse-7.23.7.tgz#9a7bf285c928cb99b5ead19c3b1ce5b310c9c305"
integrity sha512-tY3mM8rH9jM0YHFGyfC0/xf+SB5eKUu7HPj7/k3fpi9dAlsMc5YbQvDi0Sh2QTPXqMhyaAtzAr807TIyfQrmyg==
dependencies:
"@babel/code-frame" "^7.23.5"
"@babel/generator" "^7.23.5"
"@babel/generator" "^7.23.6"
"@babel/helper-environment-visitor" "^7.22.20"
"@babel/helper-function-name" "^7.23.0"
"@babel/helper-hoist-variables" "^7.22.5"
"@babel/helper-split-export-declaration" "^7.22.6"
"@babel/parser" "^7.23.5"
"@babel/types" "^7.23.5"
debug "^4.1.0"
globals "^11.1.0"

"@babel/traverse@^7.14.5", "@babel/traverse@^7.22.6", "@babel/traverse@^7.22.8":
version "7.22.8"
resolved "https://registry.yarnpkg.com/@babel/traverse/-/traverse-7.22.8.tgz#4d4451d31bc34efeae01eac222b514a77aa4000e"
integrity sha512-y6LPR+wpM2I3qJrsheCTwhIinzkETbplIgPBbwvqPKc+uljeA5gP+3nP8irdYt1mjQaDnlIcG+dw8OjAco4GXw==
dependencies:
"@babel/code-frame" "^7.22.5"
"@babel/generator" "^7.22.7"
"@babel/helper-environment-visitor" "^7.22.5"
"@babel/helper-function-name" "^7.22.5"
"@babel/helper-hoist-variables" "^7.22.5"
"@babel/helper-split-export-declaration" "^7.22.6"
"@babel/parser" "^7.22.7"
"@babel/types" "^7.22.5"
debug "^4.1.0"
globals "^11.1.0"

"@babel/traverse@^7.23.2":
version "7.23.2"
resolved "https://registry.yarnpkg.com/@babel/traverse/-/traverse-7.23.2.tgz#329c7a06735e144a506bdb2cad0268b7f46f4ad8"
integrity sha512-azpe59SQ48qG6nu2CzcMLbxUudtN+dOM9kDbUqGq3HXUJRlo7i8fvPoxQUzYgLZ4cMVmuZgm8vvBpNeRhd6XSw==
dependencies:
"@babel/code-frame" "^7.22.13"
"@babel/generator" "^7.23.0"
"@babel/helper-environment-visitor" "^7.22.20"
"@babel/helper-function-name" "^7.23.0"
"@babel/helper-hoist-variables" "^7.22.5"
"@babel/helper-split-export-declaration" "^7.22.6"
"@babel/parser" "^7.23.0"
"@babel/types" "^7.23.0"
debug "^4.1.0"
"@babel/parser" "^7.23.6"
"@babel/types" "^7.23.6"
debug "^4.3.1"
globals "^11.1.0"

"@babel/types@^7.0.0", "@babel/types@^7.14.5", "@babel/types@^7.22.5", "@babel/types@^7.3.0", "@babel/types@^7.3.3":
Expand All @@ -871,7 +854,7 @@
"@babel/helper-validator-identifier" "^7.22.5"
to-fast-properties "^2.0.0"

"@babel/types@^7.16.7":
"@babel/types@^7.16.7", "@babel/types@^7.23.6":
version "7.23.6"
resolved "https://registry.yarnpkg.com/@babel/types/-/types-7.23.6.tgz#be33fdb151e1f5a56877d704492c240fc71c7ccd"
integrity sha512-+uarb83brBzPKN38NX1MkB6vb6+mwvR6amUulqAE7ccQw1pEl+bCia9TbdG1lsnFP7lZySvUn37CHyXQdfTwzg==
Expand Down Expand Up @@ -4782,20 +4765,15 @@
dependencies:
"@octokit/openapi-types" "^5.3.2"

"@openzeppelin/contracts@4.4.2":
version "4.4.2"
resolved "https://registry.npmjs.org/@openzeppelin/contracts/-/contracts-4.4.2.tgz"
integrity sha512-NyJV7sJgoGYqbtNUWgzzOGW4T6rR19FmX1IJgXGdapGPWsuMelGJn9h03nos0iqfforCbCB0iYIR0MtIuIFLLw==

"@openzeppelin/contracts@4.5.0":
version "4.5.0"
resolved "https://registry.npmjs.org/@openzeppelin/contracts/-/contracts-4.5.0.tgz"
integrity sha512-fdkzKPYMjrRiPK6K4y64e6GzULR7R7RwxSigHS8DDp7aWDeoReqsQI+cxHV1UuhAqX69L1lAaWDxenfP+xiqzA==

"@openzeppelin/contracts@4.7.3":
version "4.7.3"
resolved "https://registry.npmjs.org/@openzeppelin/contracts/-/contracts-4.7.3.tgz"
integrity sha512-dGRS0agJzu8ybo44pCIf3xBaPQN/65AIXNgK8+4gzKd5kbvlqyxryUYVLJv7fK98Seyd2hDZzVEHSWAh0Bt1Yw==
"@openzeppelin/contracts@4.9.5":
version "4.9.5"
resolved "https://registry.yarnpkg.com/@openzeppelin/contracts/-/contracts-4.9.5.tgz#1eed23d4844c861a1835b5d33507c1017fa98de8"
integrity sha512-ZK+W5mVhRppff9BE6YdR8CC52C8zAvsVAiWhEtQ5+oNxFE6h1WdeWo+FJSF8KKvtxxVYZ7MTP/5KoVpAU3aSWg==

"@parcel/watcher-android-arm64@2.3.0":
version "2.3.0"
Expand Down Expand Up @@ -12391,20 +12369,10 @@ flush-write-stream@^1.0.0:
inherits "^2.0.3"
readable-stream "^2.3.6"

follow-redirects@^1.12.1:
version "1.14.9"
resolved "https://registry.npmjs.org/follow-redirects/-/follow-redirects-1.14.9.tgz"
integrity sha512-MQDfihBQYMcyy5dhRDJUHcw7lb2Pv/TuE6xP1vyraLukNDHKbDxDNaOE3NbCAdKQApno+GPRyo1YAp89yCjK4w==

follow-redirects@^1.14.9:
version "1.15.1"
resolved "https://registry.npmjs.org/follow-redirects/-/follow-redirects-1.15.1.tgz"
integrity sha512-yLAMQs+k0b2m7cVxpS1VKJVvoz7SS9Td1zss3XRwXj+ZDH00RJgnuLx7E44wx02kQLrdM3aOOy+FpzS7+8OizA==

follow-redirects@^1.15.0:
version "1.15.3"
resolved "https://registry.yarnpkg.com/follow-redirects/-/follow-redirects-1.15.3.tgz#fe2f3ef2690afce7e82ed0b44db08165b207123a"
integrity sha512-1VzOtuEM8pC9SFU1E+8KfTjZyMztRsgEfwQl44z8A25uy13jSzTj6dyK2Df52iV0vgHCfBwLhDWevLn95w5v6Q==
follow-redirects@^1.12.1, follow-redirects@^1.14.9, follow-redirects@^1.15.0:
version "1.15.4"
resolved "https://registry.yarnpkg.com/follow-redirects/-/follow-redirects-1.15.4.tgz#cdc7d308bf6493126b17ea2191ea0ccf3e535adf"
integrity sha512-Cr4D/5wlrb0z9dgERpUL3LrmPKVDsETIJhaCMeDfuFYcqa5bldGV6wBsAN6X/vxlXQtFBMrXdXxdL8CbDTGniw==

fontkit@^1.8.0:
version "1.8.1"
Expand Down

0 comments on commit 5393949

Please sign in to comment.