Skip to content

Commit

Permalink
fix gas controller + remove extra deps
Browse files Browse the repository at this point in the history
  • Loading branch information
arch1995 committed Oct 31, 2023
1 parent 9786a36 commit eb3c2bf
Show file tree
Hide file tree
Showing 7 changed files with 3,601 additions and 254 deletions.
3,743 changes: 3,500 additions & 243 deletions package-lock.json

Large diffs are not rendered by default.

1 change: 0 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,6 @@
"bignumber.js": "^9.1.2",
"bn.js": "^5.2.1",
"bowser": "^2.11.0",
"clone": "^2.1.2",
"copy-to-clipboard": "^3.3.3",
"core-js": "^3.33.1",
"das-sdk": "^1.9.3",
Expand Down
11 changes: 9 additions & 2 deletions src/controllers/gas/GasFeeController.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import {
calculateTimeEstimate,
fetchEthGasPriceEstimate as defaultFetchEthGasPriceEstimate,
fetchGasEstimates as defaultFetchGasEstimates,
fetchGasEstimatesViaEthFeeHistory as defaultFetchGasEstimatesViaEthFeeHistory,
fetchLegacyGasPriceEstimates as defaultFetchLegacyGasPriceEstimates,
} from './gas-util'

Expand Down Expand Up @@ -59,6 +60,7 @@ class GasFeeController {
fetchGasEstimates = defaultFetchGasEstimates,
fetchEthGasPriceEstimate = defaultFetchEthGasPriceEstimate,
fetchLegacyGasPriceEstimates = defaultFetchLegacyGasPriceEstimates,
fetchGasEstimatesViaEthFeeHistory = defaultFetchGasEstimatesViaEthFeeHistory,
getCurrentNetworkEIP1559Compatibility,
getCurrentAccountEIP1559Compatibility,
getChainId,
Expand All @@ -72,6 +74,7 @@ class GasFeeController {
this.fetchGasEstimates = fetchGasEstimates
this.fetchEthGasPriceEstimate = fetchEthGasPriceEstimate
this.fetchLegacyGasPriceEstimates = fetchLegacyGasPriceEstimates
this.fetchGasEstimatesViaEthFeeHistory = fetchGasEstimatesViaEthFeeHistory
this.getProvider = getProvider

this.getCurrentNetworkEIP1559Compatibility = getCurrentNetworkEIP1559Compatibility
Expand Down Expand Up @@ -139,7 +142,12 @@ class GasFeeController {

try {
if (isEIP1559Compatible) {
const estimates = await this.fetchGasEstimates(this.EIP1559APIEndpoint.replace('<chain_id>', `${chainId}`))
let estimates
try {
estimates = await this.fetchGasEstimates(this.config.EIP1559APIEndpoint.replace('<chain_id>', `${chainId}`))
} catch {
estimates = await this.fetchGasEstimatesViaEthFeeHistory(this.ethQuery)
}
const { suggestedMaxPriorityFeePerGas, suggestedMaxFeePerGas } = estimates.medium
const estimatedGasFeeTimeBounds = this.getTimeEstimate(suggestedMaxPriorityFeePerGas, suggestedMaxFeePerGas)
newState = {
Expand All @@ -159,7 +167,6 @@ class GasFeeController {
}
} catch {
try {
log.log('fetching gas fee estimates from dqwd API')
const estimates = await this.fetchEthGasPriceEstimate(this.ethQuery)
newState = {
gasFeeEstimates: estimates,
Expand Down
44 changes: 44 additions & 0 deletions src/controllers/gas/gas-util.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import BigNumber from 'bignumber.js'
import BN from 'bn.js'

import { decGWEIToHexWEI, hexWEIToDecGWEI } from '../../utils/conversionUtils'
Expand Down Expand Up @@ -32,6 +33,49 @@ export async function fetchGasEstimates(url) {
return normalizedEstimates
}

export async function fetchGasEstimatesViaEthFeeHistory(ethQuery) {
const noOfBlocks = 10
const newestBlock = 'latest'
// get the 10, 50 and 95th percentile of the tip fees from the last 10 blocks
const percentileValues = [10, 50, 95]
const feeHistory = await ethQuery.sendAsync({
method: 'eth_feeHistory',
params: [noOfBlocks, newestBlock, percentileValues],
})
// this is in hex wei
const finalBaseFeePerGas = feeHistory.baseFeePerGas.at(-1)
// this is in hex wei
const priorityFeeCalcs = feeHistory.reward.reduce(
(acc, curr) => ({
slow: acc.slow.plus(new BigNumber(curr[0], 16)),
average: acc.average.plus(new BigNumber(curr[1], 16)),
fast: acc.fast.plus(new BigNumber(curr[2], 16)),
}),
{ slow: new BigNumber(0), average: new BigNumber(0), fast: new BigNumber(0) }
)
return {
estimatedBaseFee: hexWEIToDecGWEI(finalBaseFeePerGas).toString(10),
high: {
maxWaitTimeEstimate: 30_000,
minWaitTimeEstimate: 15_000,
suggestedMaxFeePerGas: hexWEIToDecGWEI(priorityFeeCalcs.fast.plus(finalBaseFeePerGas).toString(16)).toString(),
suggestedMaxPriorityFeePerGas: hexWEIToDecGWEI(priorityFeeCalcs.fast.toString(16)).toString(),
},
medium: {
maxWaitTimeEstimate: 45_000,
minWaitTimeEstimate: 15_000,
suggestedMaxFeePerGas: hexWEIToDecGWEI(priorityFeeCalcs.average.plus(finalBaseFeePerGas).toString(16)).toString(),
suggestedMaxPriorityFeePerGas: hexWEIToDecGWEI(priorityFeeCalcs.average.toString(16)).toString(),
},
low: {
maxWaitTimeEstimate: 60_000,
minWaitTimeEstimate: 15_000,
suggestedMaxFeePerGas: hexWEIToDecGWEI(priorityFeeCalcs.slow.plus(finalBaseFeePerGas).toString(16)).toString(),
suggestedMaxPriorityFeePerGas: hexWEIToDecGWEI(priorityFeeCalcs.slow.toString(16)).toString(),
},
}
}

/**
* Hit the legacy MetaSwaps gasPrices estimate api and return the low, medium
* high values from that API.
Expand Down
6 changes: 0 additions & 6 deletions test/setup.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,4 @@ dotenv.config({ path: '.env' })
// eslint-disable-next-line import/first
import './helper.mjs'

globalThis.fetch =
globalThis.fetch ||
function fetch() {
return Promise.resolve()
}

globalThis.indexedDB = {}
46 changes: 46 additions & 0 deletions test/unit/controllers/gas-fee-controller-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,29 @@ describe('GasFeeController', () => {

it('should getGasFeeEstimatesAndStartPolling', async () => {
sandbox.stub(gasFeeController, 'getCurrentAccountEIP1559Compatibility').returns(true)
sandbox.stub(gasFeeController, 'fetchGasEstimatesViaEthFeeHistory').returns(
Promise.resolve({
estimatedBaseFee: '1',
high: {
maxWaitTimeEstimate: 30_000,
minWaitTimeEstimate: 15_000,
suggestedMaxFeePerGas: '1',
suggestedMaxPriorityFeePerGas: '1',
},
medium: {
maxWaitTimeEstimate: 45_000,
minWaitTimeEstimate: 15_000,
suggestedMaxFeePerGas: '1',
suggestedMaxPriorityFeePerGas: '1',
},
low: {
maxWaitTimeEstimate: 60_000,
minWaitTimeEstimate: 15_000,
suggestedMaxFeePerGas: '1',
suggestedMaxPriorityFeePerGas: '1',
},
})
)
assert.deepStrictEqual(gasFeeController.state.gasFeeEstimates, {})
await gasFeeController.getGasFeeEstimatesAndStartPolling(undefined)
assert('low' in gasFeeController.state.gasFeeEstimates)
Expand Down Expand Up @@ -138,6 +161,29 @@ describe('GasFeeController', () => {
sandbox.stub(gasFeeController, 'getCurrentAccountEIP1559Compatibility').returns(true)
sandbox.stub(gasFeeController, 'getCurrentNetworkEIP1559Compatibility').returns(() => Promise.resolve(true))
sandbox.stub(gasFeeController, 'getCurrentNetworkLegacyGasAPICompatibility').returns(true)
sandbox.stub(gasFeeController, 'fetchGasEstimatesViaEthFeeHistory').returns(
Promise.resolve({
estimatedBaseFee: '1',
high: {
maxWaitTimeEstimate: 30_000,
minWaitTimeEstimate: 15_000,
suggestedMaxFeePerGas: '1',
suggestedMaxPriorityFeePerGas: '1',
},
medium: {
maxWaitTimeEstimate: 45_000,
minWaitTimeEstimate: 15_000,
suggestedMaxFeePerGas: '1',
suggestedMaxPriorityFeePerGas: '1',
},
low: {
maxWaitTimeEstimate: 60_000,
minWaitTimeEstimate: 15_000,
suggestedMaxFeePerGas: '1',
suggestedMaxPriorityFeePerGas: '1',
},
})
)
assert.deepStrictEqual(gasFeeController.state.gasFeeEstimates, {})
const estimates = await gasFeeController._fetchGasFeeEstimateData()
assert('gasFeeEstimates' in estimates)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/* eslint-disable */
import assert from 'assert'
import clone from 'clone'
import { cloneDeep } from 'lodash-es'
import EthQuery from 'eth-query'
import sinon from 'sinon'
import { obj as createThoughStream } from 'through2'
Expand Down Expand Up @@ -125,7 +125,7 @@ describe('MetaMaskController', () => {
return Promise.resolve(this.object)
},
},
initState: clone(firstTimeState),
initState: cloneDeep(firstTimeState),
platform: { showTransactionNotification: () => {} },
requestTkeyInput: noop,
requestTkeySeedPhraseInput: noop,
Expand Down

0 comments on commit eb3c2bf

Please sign in to comment.