From 4861f36c56a12c8618141adcc55028d976fad7cd Mon Sep 17 00:00:00 2001 From: Pasha8914 Date: Fri, 14 Oct 2022 16:30:19 +1000 Subject: [PATCH] :ambulance: ability to configure min maxPriorityFeePerGas --- README.md | 1 + package.json | 2 +- src/services/gas-estimation/eip1559.ts | 7 +++++-- src/services/gas-estimation/types.ts | 2 +- src/services/gas-price-oracle/types.ts | 1 + src/services/legacy-gas-price/constants.ts | 6 +++--- src/tests/eip1559.test.ts | 16 +++++++++++++--- src/tests/legacy.test.ts | 4 ++-- 8 files changed, 27 insertions(+), 12 deletions(-) diff --git a/README.md b/README.md index e4b89ef..aa9cdd4 100644 --- a/README.md +++ b/README.md @@ -90,6 +90,7 @@ const options: GasOracleOptions = { blockTime: 10, // seconds shouldCache: false, timeout: 10000, // specifies the number of milliseconds before the request times out. + minPriority: 0, // specifies the min maxPriorityFeePerGas. fallbackGasPrices: { gasPrices: { instant: 28, diff --git a/package.json b/package.json index ce544f7..bab541a 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "gas-price-oracle", - "version": "0.5.1", + "version": "0.5.2", "description": "Gas Price Oracle library for Ethereum dApps.", "homepage": "https://github.com/peppersec/gas-price-oracle", "main": "./lib/index.js", diff --git a/src/services/gas-estimation/eip1559.ts b/src/services/gas-estimation/eip1559.ts index 4ca0464..1e539f6 100644 --- a/src/services/gas-estimation/eip1559.ts +++ b/src/services/gas-estimation/eip1559.ts @@ -16,6 +16,7 @@ export class Eip1559GasPriceOracle implements EstimateOracle { shouldCache: false, chainId: ChainId.MAINNET, fallbackGasPrices: undefined, + minPriority: DEFAULT_PRIORITY_FEE, blockTime: DEFAULT_BLOCK_DURATION, blocksCount: NETWORKS[ChainId.MAINNET].blocksCount, percentile: NETWORKS[ChainId.MAINNET].percentile, @@ -135,8 +136,10 @@ export class Eip1559GasPriceOracle implements EstimateOracle { private async calculateFees({ baseFee, feeHistory }: CalculateFeesParams): Promise { const estimatedPriorityFee = await this.getPriorityFromChain(feeHistory) - const { highest: maxPriorityFeePerGas } = findMax([estimatedPriorityFee ?? BG_ZERO, new BigNumber(DEFAULT_PRIORITY_FEE)]) - + const { highest: maxPriorityFeePerGas } = findMax([ + estimatedPriorityFee ?? BG_ZERO, + new BigNumber(this.configuration.minPriority), + ]) const maxFeePerGas = baseFee.plus(maxPriorityFeePerGas) if (this.checkIsGreaterThanMax(maxFeePerGas) || this.checkIsGreaterThanMax(maxPriorityFeePerGas)) { diff --git a/src/services/gas-estimation/types.ts b/src/services/gas-estimation/types.ts index 951cd87..e9644d5 100644 --- a/src/services/gas-estimation/types.ts +++ b/src/services/gas-estimation/types.ts @@ -32,7 +32,7 @@ export type GasEstimationOptionsPayload = Options & { fetcher: RpcFetcher } -export type Config = Required & { fallbackGasPrices?: EstimatedGasPrice } +export type Config = Required & { fallbackGasPrices?: EstimatedGasPrice; minPriority: number } export abstract class EstimateOracle { public configuration: Config public abstract estimateFees(fallbackGasPrices?: EstimatedGasPrice): Promise diff --git a/src/services/gas-price-oracle/types.ts b/src/services/gas-price-oracle/types.ts index 180a50d..7e01a26 100644 --- a/src/services/gas-price-oracle/types.ts +++ b/src/services/gas-price-oracle/types.ts @@ -38,6 +38,7 @@ export type GasOracleOptions = { percentile?: number blockTime?: number shouldCache?: boolean + minPriority?: number fallbackGasPrices?: FallbackGasPrices } diff --git a/src/services/legacy-gas-price/constants.ts b/src/services/legacy-gas-price/constants.ts index 993f717..02aff0e 100644 --- a/src/services/legacy-gas-price/constants.ts +++ b/src/services/legacy-gas-price/constants.ts @@ -2,9 +2,9 @@ const DEFAULT_GAS_PRICE = { instant: 0, fast: 0, standard: 0, low: 0 } const MULTIPLIERS = { instant: 1.3, - standard: 0.85, - low: 0.5, - fast: 1, + fast: 1.2, + standard: 1.1, + low: 1, } export { MULTIPLIERS, DEFAULT_GAS_PRICE } diff --git a/src/tests/eip1559.test.ts b/src/tests/eip1559.test.ts index 3bb74a7..23824e6 100644 --- a/src/tests/eip1559.test.ts +++ b/src/tests/eip1559.test.ts @@ -33,7 +33,7 @@ beforeEach('beforeEach', function () { oracle = new GasPriceOracle() }) -const INJECTED_RPC_URL = 'https://ethereum-rpc.trustwalletapp.com' +const INJECTED_RPC_URL = 'https://cloudflare-eth.com' describe('eip-1559 gasOracle', function () { describe('eip constructor', function () { it('should set default values', function () { @@ -62,14 +62,14 @@ describe('eip-1559 gasOracle', function () { describe(`estimateGas ${chainId}`, function () { it('should return error if not eip-1559 not supported', async function () { - if (chainId === ChainId.OPTIMISM || chainId === ChainId.ARBITRUM || chainId === ChainId.BSC) { + if (chainId === ChainId.OPTIMISM || chainId === ChainId.BSC) { await eipOracle.eip1559 .estimateFees() .should.be.rejectedWith('An error occurred while fetching current base fee, falling back') } }) - if (chainId === ChainId.OPTIMISM || chainId === ChainId.ARBITRUM || chainId === ChainId.BSC) { + if (chainId === ChainId.OPTIMISM || chainId === ChainId.BSC) { return } @@ -143,6 +143,16 @@ describe('eip-1559 gasOracle', function () { }) }) }) + + describe('estimate ARBITRUM', function () { + it('should be priority 0', async function () { + const eipOracle = new GasPriceOracle({ minPriority: 0, chainId: ChainId.ARBITRUM }) + const estimateGas: EstimatedGasPrice = await eipOracle.eip1559.estimateFees(FALLBACK_ESTIMATE) + + console.log('estimateGas.maxPriorityFeePerGas', estimateGas.maxPriorityFeePerGas) + estimateGas.maxPriorityFeePerGas.should.be.at.equal(0) + }) + }) }) after('after', function () { diff --git a/src/tests/legacy.test.ts b/src/tests/legacy.test.ts index 5ce31ca..bc0563c 100644 --- a/src/tests/legacy.test.ts +++ b/src/tests/legacy.test.ts @@ -42,7 +42,7 @@ beforeEach('beforeEach', function () { ;({ onChainOracles, offChainOracles } = oracle.legacy) }) -const INJECTED_RPC_URL = 'https://ethereum-rpc.trustwalletapp.com' +const INJECTED_RPC_URL = 'https://cloudflare-eth.com' describe('legacy gasOracle', function () { describe('legacy constructor', function () { @@ -191,7 +191,7 @@ describe('legacy gasOracle', function () { const gas = (await oracle.gasPrices({ isLegacy: true })) as unknown as GasPrice - const shouldBe = LegacyGasPriceOracle.getMultipliedPrices(NETWORKS[ChainId.MAINNET].defaultGasPrice) + const shouldBe = LegacyGasPriceOracle.getCategorize(NETWORKS[ChainId.MAINNET].defaultGasPrice) gas.instant.should.be.equal(shouldBe.instant) gas.fast.should.be.equal(shouldBe.fast)