From b801ccdb359dcfb6729504d1b1d68c50ec124e6b Mon Sep 17 00:00:00 2001 From: David Walsh Date: Tue, 15 Dec 2020 11:16:51 -0600 Subject: [PATCH] Fix 9874 - Improve gas maximum estimation (#10043) --- ui/app/ducks/swaps/swaps.js | 17 +++++++---------- ui/app/helpers/utils/conversions.util.js | 11 ----------- ui/app/pages/swaps/swaps.util.js | 5 +++++ ui/app/pages/swaps/view-quote/view-quote.js | 14 ++++++-------- 4 files changed, 18 insertions(+), 29 deletions(-) diff --git a/ui/app/ducks/swaps/swaps.js b/ui/app/ducks/swaps/swaps.js index d09afd648..dd7fab0a1 100644 --- a/ui/app/ducks/swaps/swaps.js +++ b/ui/app/ducks/swaps/swaps.js @@ -39,7 +39,6 @@ import { calcGasTotal } from '../../pages/send/send.utils' import { decimalToHex, getValueFromWeiHex, - hexMax, decGWEIToHexWEI, hexToDecimal, hexWEIToDecGWEI, @@ -67,6 +66,8 @@ const GAS_PRICES_LOADING_STATES = { COMPLETED: 'COMPLETED', } +export const FALLBACK_GAS_MULTIPLIER = 1.5 + const initialState = { aggregatorMetadata: null, approveTxId: null, @@ -593,20 +594,16 @@ export const signAndSendTransactions = (history, metaMetricsEvent) => { const usedQuote = getUsedQuote(state) const usedTradeTxParams = usedQuote.trade - const estimatedGasLimit = new BigNumber( - usedQuote?.gasEstimate || decimalToHex(usedQuote?.averageGas || 0), - 16, - ) + const estimatedGasLimit = new BigNumber(usedQuote?.gasEstimate || `0x0`, 16) const estimatedGasLimitWithMultiplier = estimatedGasLimit - .times(1.4, 10) + .times(usedQuote?.gasMultiplier || FALLBACK_GAS_MULTIPLIER, 10) .round(0) .toString(16) const maxGasLimit = customSwapsGas || - hexMax( - `0x${decimalToHex(usedQuote?.maxGas || 0)}`, - estimatedGasLimitWithMultiplier, - ) + (usedQuote?.gasEstimate + ? estimatedGasLimitWithMultiplier + : usedQuote?.maxGas) const usedGasPrice = getUsedSwapsGasPrice(state) usedTradeTxParams.gas = maxGasLimit diff --git a/ui/app/helpers/utils/conversions.util.js b/ui/app/helpers/utils/conversions.util.js index a61043dbc..e4cf21717 100644 --- a/ui/app/helpers/utils/conversions.util.js +++ b/ui/app/helpers/utils/conversions.util.js @@ -1,4 +1,3 @@ -import BigNumber from 'bignumber.js' import { ETH, GWEI, WEI } from '../constants/common' import { addHexPrefix } from '../../../../app/scripts/lib/util' 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) { return addCurrencies(aHexWEI, bHexWEI, { aBase: 16, diff --git a/ui/app/pages/swaps/swaps.util.js b/ui/app/pages/swaps/swaps.util.js index d3189e5b7..b031a1c12 100644 --- a/ui/app/pages/swaps/swaps.util.js +++ b/ui/app/pages/swaps/swaps.util.js @@ -112,6 +112,11 @@ const QUOTE_VALIDATORS = [ property: 'maxGas', type: 'number', }, + { + property: 'gasEstimate', + type: 'number|undefined', + validator: (gasEstimate) => gasEstimate === undefined || gasEstimate > 0, + }, ] const TOKEN_VALIDATORS = [ diff --git a/ui/app/pages/swaps/view-quote/view-quote.js b/ui/app/pages/swaps/view-quote/view-quote.js index 4db97824f..d3e54575f 100644 --- a/ui/app/pages/swaps/view-quote/view-quote.js +++ b/ui/app/pages/swaps/view-quote/view-quote.js @@ -12,6 +12,7 @@ import { useSwapsEthToken } from '../../../hooks/useSwapsEthToken' import { MetaMetricsContext } from '../../../contexts/metametrics.new' import FeeCard from '../fee-card' import { + FALLBACK_GAS_MULTIPLIER, getQuotes, getSelectedQuote, getApproveTxParams, @@ -57,7 +58,6 @@ import { } from '../../../helpers/utils/token-util' import { decimalToHex, - hexMax, hexToDecimal, getValueFromWeiHex, } from '../../../helpers/utils/conversions.util' @@ -123,18 +123,16 @@ export default function ViewQuote() { usedQuote?.gasEstimateWithRefund || `0x${decimalToHex(usedQuote?.averageGas || 0)}` - const gasLimitForMax = - usedQuote?.gasEstimate || `0x${decimalToHex(usedQuote?.averageGas || 0)}` + const gasLimitForMax = usedQuote?.gasEstimate || `0x0` const usedGasLimitWithMultiplier = new BigNumber(gasLimitForMax, 16) - .times(1.4, 10) + .times(usedQuote?.gasMultiplier || FALLBACK_GAS_MULTIPLIER, 10) .round(0) .toString(16) - const nonCustomMaxGasLimit = hexMax( - `0x${decimalToHex(usedQuote?.maxGas || 0)}`, - usedGasLimitWithMultiplier, - ) + const nonCustomMaxGasLimit = usedQuote?.gasEstimate + ? usedGasLimitWithMultiplier + : `0x${decimalToHex(usedQuote?.maxGas || 0)}` const maxGasLimit = customMaxGas || nonCustomMaxGasLimit const gasTotalInWeiHex = calcGasTotal(maxGasLimit, gasPrice)