|
|
|
@ -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, |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
}) |
|
|
|
|
|
|
|
|
|