1
0
mirror of https://github.com/kremalicious/metamask-extension.git synced 2024-11-29 23:58:06 +01:00

Fix 9874 - Improve gas maximum estimation (#10043)

This commit is contained in:
David Walsh 2020-12-15 11:16:51 -06:00 committed by Mark Stacey
parent 62b00f62db
commit b801ccdb35
4 changed files with 18 additions and 29 deletions

View File

@ -39,7 +39,6 @@ import { calcGasTotal } from '../../pages/send/send.utils'
import { import {
decimalToHex, decimalToHex,
getValueFromWeiHex, getValueFromWeiHex,
hexMax,
decGWEIToHexWEI, decGWEIToHexWEI,
hexToDecimal, hexToDecimal,
hexWEIToDecGWEI, hexWEIToDecGWEI,
@ -67,6 +66,8 @@ const GAS_PRICES_LOADING_STATES = {
COMPLETED: 'COMPLETED', COMPLETED: 'COMPLETED',
} }
export const FALLBACK_GAS_MULTIPLIER = 1.5
const initialState = { const initialState = {
aggregatorMetadata: null, aggregatorMetadata: null,
approveTxId: null, approveTxId: null,
@ -593,20 +594,16 @@ export const signAndSendTransactions = (history, metaMetricsEvent) => {
const usedQuote = getUsedQuote(state) const usedQuote = getUsedQuote(state)
const usedTradeTxParams = usedQuote.trade const usedTradeTxParams = usedQuote.trade
const estimatedGasLimit = new BigNumber( const estimatedGasLimit = new BigNumber(usedQuote?.gasEstimate || `0x0`, 16)
usedQuote?.gasEstimate || decimalToHex(usedQuote?.averageGas || 0),
16,
)
const estimatedGasLimitWithMultiplier = estimatedGasLimit const estimatedGasLimitWithMultiplier = estimatedGasLimit
.times(1.4, 10) .times(usedQuote?.gasMultiplier || FALLBACK_GAS_MULTIPLIER, 10)
.round(0) .round(0)
.toString(16) .toString(16)
const maxGasLimit = const maxGasLimit =
customSwapsGas || customSwapsGas ||
hexMax( (usedQuote?.gasEstimate
`0x${decimalToHex(usedQuote?.maxGas || 0)}`, ? estimatedGasLimitWithMultiplier
estimatedGasLimitWithMultiplier, : usedQuote?.maxGas)
)
const usedGasPrice = getUsedSwapsGasPrice(state) const usedGasPrice = getUsedSwapsGasPrice(state)
usedTradeTxParams.gas = maxGasLimit usedTradeTxParams.gas = maxGasLimit

View File

@ -1,4 +1,3 @@
import BigNumber from 'bignumber.js'
import { ETH, GWEI, WEI } from '../constants/common' import { ETH, GWEI, WEI } from '../constants/common'
import { addHexPrefix } from '../../../../app/scripts/lib/util' import { addHexPrefix } from '../../../../app/scripts/lib/util'
import { import {
@ -163,16 +162,6 @@ export function hexWEIToDecETH(hexWEI) {
}) })
} }
export function hexMax(...hexNumbers) {
let max = hexNumbers[0]
hexNumbers.slice(1).forEach((hexNumber) => {
if (new BigNumber(hexNumber, 16).gt(max, 16)) {
max = hexNumber
}
})
return max
}
export function addHexes(aHexWEI, bHexWEI) { export function addHexes(aHexWEI, bHexWEI) {
return addCurrencies(aHexWEI, bHexWEI, { return addCurrencies(aHexWEI, bHexWEI, {
aBase: 16, aBase: 16,

View File

@ -112,6 +112,11 @@ const QUOTE_VALIDATORS = [
property: 'maxGas', property: 'maxGas',
type: 'number', type: 'number',
}, },
{
property: 'gasEstimate',
type: 'number|undefined',
validator: (gasEstimate) => gasEstimate === undefined || gasEstimate > 0,
},
] ]
const TOKEN_VALIDATORS = [ const TOKEN_VALIDATORS = [

View File

@ -12,6 +12,7 @@ import { useSwapsEthToken } from '../../../hooks/useSwapsEthToken'
import { MetaMetricsContext } from '../../../contexts/metametrics.new' import { MetaMetricsContext } from '../../../contexts/metametrics.new'
import FeeCard from '../fee-card' import FeeCard from '../fee-card'
import { import {
FALLBACK_GAS_MULTIPLIER,
getQuotes, getQuotes,
getSelectedQuote, getSelectedQuote,
getApproveTxParams, getApproveTxParams,
@ -57,7 +58,6 @@ import {
} from '../../../helpers/utils/token-util' } from '../../../helpers/utils/token-util'
import { import {
decimalToHex, decimalToHex,
hexMax,
hexToDecimal, hexToDecimal,
getValueFromWeiHex, getValueFromWeiHex,
} from '../../../helpers/utils/conversions.util' } from '../../../helpers/utils/conversions.util'
@ -123,18 +123,16 @@ export default function ViewQuote() {
usedQuote?.gasEstimateWithRefund || usedQuote?.gasEstimateWithRefund ||
`0x${decimalToHex(usedQuote?.averageGas || 0)}` `0x${decimalToHex(usedQuote?.averageGas || 0)}`
const gasLimitForMax = const gasLimitForMax = usedQuote?.gasEstimate || `0x0`
usedQuote?.gasEstimate || `0x${decimalToHex(usedQuote?.averageGas || 0)}`
const usedGasLimitWithMultiplier = new BigNumber(gasLimitForMax, 16) const usedGasLimitWithMultiplier = new BigNumber(gasLimitForMax, 16)
.times(1.4, 10) .times(usedQuote?.gasMultiplier || FALLBACK_GAS_MULTIPLIER, 10)
.round(0) .round(0)
.toString(16) .toString(16)
const nonCustomMaxGasLimit = hexMax( const nonCustomMaxGasLimit = usedQuote?.gasEstimate
`0x${decimalToHex(usedQuote?.maxGas || 0)}`, ? usedGasLimitWithMultiplier
usedGasLimitWithMultiplier, : `0x${decimalToHex(usedQuote?.maxGas || 0)}`
)
const maxGasLimit = customMaxGas || nonCustomMaxGasLimit const maxGasLimit = customMaxGas || nonCustomMaxGasLimit
const gasTotalInWeiHex = calcGasTotal(maxGasLimit, gasPrice) const gasTotalInWeiHex = calcGasTotal(maxGasLimit, gasPrice)