From 1062e2a97d4c04e4e2a159e55f7d61fea4112f3c Mon Sep 17 00:00:00 2001 From: Dan J Miller Date: Tue, 6 Oct 2020 17:47:58 -0230 Subject: [PATCH] Fix SwapsController handling of insufficient balance for gas when in max ETH mode --- app/scripts/controllers/swaps.js | 32 ++++++++++++++++++-------------- 1 file changed, 18 insertions(+), 14 deletions(-) diff --git a/app/scripts/controllers/swaps.js b/app/scripts/controllers/swaps.js index 31e669c8b..2114341fc 100644 --- a/app/scripts/controllers/swaps.js +++ b/app/scripts/controllers/swaps.js @@ -3,6 +3,7 @@ import log from 'loglevel' import BigNumber from 'bignumber.js' import ObservableStore from 'obs-store' import { mapValues } from 'lodash' +import ethUtil from 'ethereumjs-util' import abi from 'human-standard-token-abi' import { calcTokenAmount } from '../../../ui/app/helpers/utils/token-util' import { calcGasTotal } from '../../../ui/app/pages/send/send.utils' @@ -182,7 +183,7 @@ export default class SwapsController { newQuotes = await this.getAllQuotesWithGasEstimates(newQuotes) if (fetchParamsMetaData.maxMode && fetchParams.sourceToken === ETH_SWAPS_TOKEN_ADDRESS) { - newQuotes = await this._modifyValuesForMaxEthMode(newQuotes, fetchParamsMetaData.accountBalance) + newQuotes = await this._modifyAndFilterValuesForMaxEthMode(newQuotes, fetchParamsMetaData.accountBalance) } if (Object.values(newQuotes).length === 0) { @@ -582,36 +583,39 @@ export default class SwapsController { } } - async _modifyValuesForMaxEthMode (newQuotes, accountBalance) { + async _modifyAndFilterValuesForMaxEthMode (newQuotes, accountBalance) { const { swapsState: { customGasPrice }, } = this.store.getState() const usedGasPrice = customGasPrice || await this._getEthersGasPrice() - const mappedNewQuotes = mapValues(newQuotes, (quote) => { + const mappedNewQuotes = {} + Object.values(newQuotes).forEach((quote) => { const oldSourceAmount = quote.sourceAmount const gasTotalInWeiHex = calcGasTotal((new BigNumber(quote.maxGas, 10)).toString(16), usedGasPrice) const newSourceAmount = (new BigNumber(accountBalance, 16)).minus(gasTotalInWeiHex, 16).toString(10) const newOldRatio = (new BigNumber(newSourceAmount, 10)).div(oldSourceAmount, 10) - const oldNewDifference = (new BigNumber(oldSourceAmount, 10)).minus(newSourceAmount, 10) + const oldNewDifference = (new BigNumber(oldSourceAmount, 10)).minus(newSourceAmount, 10).toString(10) const oldDestinationAmount = quote.destinationAmount - const newDestinationAmount = (new BigNumber(oldDestinationAmount, 10)).times(newOldRatio) + const newDestinationAmount = (new BigNumber(oldDestinationAmount, 10)).times(newOldRatio).toString(10) const oldValue = quote.trade.value - const newValue = (new BigNumber(oldValue, 16)).minus(oldNewDifference, 10) + const newValue = (new BigNumber(oldValue, 16)).minus(oldNewDifference, 10).toString(16) - return { - ...quote, - trade: { - ...quote.trade, - value: newValue, - }, - destinationAmount: newDestinationAmount, - sourceAmount: newSourceAmount, + if ((new BigNumber(newSourceAmount, 10).gt(0))) { + mappedNewQuotes[quote.aggregator] = { + ...quote, + trade: { + ...quote.trade, + value: ethUtil.addHexPrefix(newValue), + }, + destinationAmount: newDestinationAmount, + sourceAmount: newSourceAmount, + } } })