mirror of
https://github.com/kremalicious/metamask-extension.git
synced 2024-12-23 09:52:26 +01:00
Fix SwapsController handling of insufficient balance for gas when in max ETH mode
This commit is contained in:
parent
94810ce9c9
commit
1062e2a97d
@ -3,6 +3,7 @@ import log from 'loglevel'
|
|||||||
import BigNumber from 'bignumber.js'
|
import BigNumber from 'bignumber.js'
|
||||||
import ObservableStore from 'obs-store'
|
import ObservableStore from 'obs-store'
|
||||||
import { mapValues } from 'lodash'
|
import { mapValues } from 'lodash'
|
||||||
|
import ethUtil from 'ethereumjs-util'
|
||||||
import abi from 'human-standard-token-abi'
|
import abi from 'human-standard-token-abi'
|
||||||
import { calcTokenAmount } from '../../../ui/app/helpers/utils/token-util'
|
import { calcTokenAmount } from '../../../ui/app/helpers/utils/token-util'
|
||||||
import { calcGasTotal } from '../../../ui/app/pages/send/send.utils'
|
import { calcGasTotal } from '../../../ui/app/pages/send/send.utils'
|
||||||
@ -182,7 +183,7 @@ export default class SwapsController {
|
|||||||
newQuotes = await this.getAllQuotesWithGasEstimates(newQuotes)
|
newQuotes = await this.getAllQuotesWithGasEstimates(newQuotes)
|
||||||
|
|
||||||
if (fetchParamsMetaData.maxMode && fetchParams.sourceToken === ETH_SWAPS_TOKEN_ADDRESS) {
|
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) {
|
if (Object.values(newQuotes).length === 0) {
|
||||||
@ -582,36 +583,39 @@ export default class SwapsController {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
async _modifyValuesForMaxEthMode (newQuotes, accountBalance) {
|
async _modifyAndFilterValuesForMaxEthMode (newQuotes, accountBalance) {
|
||||||
const {
|
const {
|
||||||
swapsState: { customGasPrice },
|
swapsState: { customGasPrice },
|
||||||
} = this.store.getState()
|
} = this.store.getState()
|
||||||
|
|
||||||
const usedGasPrice = customGasPrice || await this._getEthersGasPrice()
|
const usedGasPrice = customGasPrice || await this._getEthersGasPrice()
|
||||||
|
|
||||||
const mappedNewQuotes = mapValues(newQuotes, (quote) => {
|
const mappedNewQuotes = {}
|
||||||
|
Object.values(newQuotes).forEach((quote) => {
|
||||||
const oldSourceAmount = quote.sourceAmount
|
const oldSourceAmount = quote.sourceAmount
|
||||||
|
|
||||||
const gasTotalInWeiHex = calcGasTotal((new BigNumber(quote.maxGas, 10)).toString(16), usedGasPrice)
|
const gasTotalInWeiHex = calcGasTotal((new BigNumber(quote.maxGas, 10)).toString(16), usedGasPrice)
|
||||||
const newSourceAmount = (new BigNumber(accountBalance, 16)).minus(gasTotalInWeiHex, 16).toString(10)
|
const newSourceAmount = (new BigNumber(accountBalance, 16)).minus(gasTotalInWeiHex, 16).toString(10)
|
||||||
|
|
||||||
const newOldRatio = (new BigNumber(newSourceAmount, 10)).div(oldSourceAmount, 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 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 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 {
|
if ((new BigNumber(newSourceAmount, 10).gt(0))) {
|
||||||
...quote,
|
mappedNewQuotes[quote.aggregator] = {
|
||||||
trade: {
|
...quote,
|
||||||
...quote.trade,
|
trade: {
|
||||||
value: newValue,
|
...quote.trade,
|
||||||
},
|
value: ethUtil.addHexPrefix(newValue),
|
||||||
destinationAmount: newDestinationAmount,
|
},
|
||||||
sourceAmount: newSourceAmount,
|
destinationAmount: newDestinationAmount,
|
||||||
|
sourceAmount: newSourceAmount,
|
||||||
|
}
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user