2020-10-06 20:28:38 +02:00
|
|
|
import React, { useState, useContext, useMemo, useEffect, useRef } from 'react'
|
|
|
|
import { useSelector, useDispatch } from 'react-redux'
|
|
|
|
import { useHistory } from 'react-router-dom'
|
|
|
|
import BigNumber from 'bignumber.js'
|
|
|
|
import { isEqual } from 'lodash'
|
|
|
|
import classnames from 'classnames'
|
|
|
|
import { I18nContext } from '../../../contexts/i18n'
|
|
|
|
import SelectQuotePopover from '../select-quote-popover'
|
|
|
|
import { useEqualityCheck } from '../../../hooks/useEqualityCheck'
|
|
|
|
import { useNewMetricEvent } from '../../../hooks/useMetricEvent'
|
2020-10-09 21:17:56 +02:00
|
|
|
import { useSwapsEthToken } from '../../../hooks/useSwapsEthToken'
|
2020-10-06 20:28:38 +02:00
|
|
|
import { MetaMetricsContext } from '../../../contexts/metametrics.new'
|
|
|
|
import FeeCard from '../fee-card'
|
|
|
|
import {
|
|
|
|
getQuotes,
|
|
|
|
getSelectedQuote,
|
|
|
|
getApproveTxParams,
|
|
|
|
getFetchParams,
|
|
|
|
setBalanceError,
|
|
|
|
getQuotesLastFetched,
|
|
|
|
getBalanceError,
|
|
|
|
getCustomSwapsGas,
|
|
|
|
getDestinationTokenInfo,
|
2020-11-04 17:14:08 +01:00
|
|
|
getUsedSwapsGasPrice,
|
2020-10-06 20:28:38 +02:00
|
|
|
getTopQuote,
|
|
|
|
navigateBackToBuildQuote,
|
|
|
|
signAndSendTransactions,
|
|
|
|
getBackgroundSwapRouteState,
|
2020-11-12 20:12:04 +01:00
|
|
|
swapsQuoteSelected,
|
2020-10-06 20:28:38 +02:00
|
|
|
} from '../../../ducks/swaps/swaps'
|
|
|
|
import {
|
|
|
|
conversionRateSelector,
|
|
|
|
getSelectedAccount,
|
|
|
|
getCurrentCurrency,
|
|
|
|
getTokenExchangeRates,
|
|
|
|
} from '../../../selectors'
|
|
|
|
import { toPrecisionWithoutTrailingZeros } from '../../../helpers/utils/util'
|
|
|
|
import { getTokens } from '../../../ducks/metamask/metamask'
|
|
|
|
import {
|
|
|
|
safeRefetchQuotes,
|
|
|
|
setCustomApproveTxData,
|
|
|
|
setSwapsErrorKey,
|
|
|
|
showModal,
|
|
|
|
} from '../../../store/actions'
|
|
|
|
import {
|
|
|
|
ASSET_ROUTE,
|
|
|
|
BUILD_QUOTE_ROUTE,
|
|
|
|
DEFAULT_ROUTE,
|
|
|
|
SWAPS_ERROR_ROUTE,
|
|
|
|
AWAITING_SWAP_ROUTE,
|
|
|
|
} from '../../../helpers/constants/routes'
|
|
|
|
import { getTokenData } from '../../../helpers/utils/transactions.util'
|
|
|
|
import {
|
|
|
|
calcTokenAmount,
|
|
|
|
calcTokenValue,
|
|
|
|
getTokenValueParam,
|
|
|
|
} from '../../../helpers/utils/token-util'
|
|
|
|
import {
|
|
|
|
decimalToHex,
|
|
|
|
hexMax,
|
|
|
|
hexToDecimal,
|
|
|
|
getValueFromWeiHex,
|
|
|
|
} from '../../../helpers/utils/conversions.util'
|
|
|
|
import MainQuoteSummary from '../main-quote-summary'
|
|
|
|
import { calcGasTotal } from '../../send/send.utils'
|
|
|
|
import { getCustomTxParamsData } from '../../confirm-approve/confirm-approve.util'
|
|
|
|
import ActionableMessage from '../actionable-message'
|
2020-11-03 00:41:28 +01:00
|
|
|
import {
|
|
|
|
quotesToRenderableData,
|
|
|
|
getRenderableNetworkFeesForQuote,
|
|
|
|
} from '../swaps.util'
|
2020-10-06 20:28:38 +02:00
|
|
|
import { useTokenTracker } from '../../../hooks/useTokenTracker'
|
2020-10-09 21:17:56 +02:00
|
|
|
import { QUOTES_EXPIRED_ERROR } from '../../../helpers/constants/swaps'
|
2020-10-06 20:28:38 +02:00
|
|
|
import CountdownTimer from '../countdown-timer'
|
|
|
|
import SwapsFooter from '../swaps-footer'
|
2020-10-19 21:45:32 +02:00
|
|
|
import InfoTooltip from '../../../components/ui/info-tooltip'
|
2020-10-06 20:28:38 +02:00
|
|
|
|
2020-11-03 00:41:28 +01:00
|
|
|
export default function ViewQuote() {
|
2020-10-06 20:28:38 +02:00
|
|
|
const history = useHistory()
|
|
|
|
const dispatch = useDispatch()
|
|
|
|
const t = useContext(I18nContext)
|
|
|
|
const metaMetricsEvent = useContext(MetaMetricsContext)
|
|
|
|
|
|
|
|
const [dispatchedSafeRefetch, setDispatchedSafeRefetch] = useState(false)
|
|
|
|
const [selectQuotePopoverShown, setSelectQuotePopoverShown] = useState(false)
|
|
|
|
const [warningHidden, setWarningHidden] = useState(false)
|
|
|
|
const [originalApproveAmount, setOriginalApproveAmount] = useState(null)
|
|
|
|
|
|
|
|
const routeState = useSelector(getBackgroundSwapRouteState)
|
|
|
|
const quotes = useSelector(getQuotes, isEqual)
|
|
|
|
useEffect(() => {
|
|
|
|
if (!Object.values(quotes).length) {
|
|
|
|
history.push(BUILD_QUOTE_ROUTE)
|
|
|
|
} else if (routeState === 'awaiting') {
|
|
|
|
history.push(AWAITING_SWAP_ROUTE)
|
|
|
|
}
|
|
|
|
}, [history, quotes, routeState])
|
|
|
|
|
|
|
|
const quotesLastFetched = useSelector(getQuotesLastFetched)
|
|
|
|
|
|
|
|
// Select necessary data
|
2020-11-04 17:14:08 +01:00
|
|
|
const gasPrice = useSelector(getUsedSwapsGasPrice)
|
2020-10-06 20:28:38 +02:00
|
|
|
const customMaxGas = useSelector(getCustomSwapsGas)
|
|
|
|
const tokenConversionRates = useSelector(getTokenExchangeRates)
|
|
|
|
const memoizedTokenConversionRates = useEqualityCheck(tokenConversionRates)
|
|
|
|
const { balance: ethBalance } = useSelector(getSelectedAccount)
|
|
|
|
const conversionRate = useSelector(conversionRateSelector)
|
|
|
|
const currentCurrency = useSelector(getCurrentCurrency)
|
|
|
|
const swapsTokens = useSelector(getTokens)
|
|
|
|
const balanceError = useSelector(getBalanceError)
|
|
|
|
const fetchParams = useSelector(getFetchParams)
|
|
|
|
const approveTxParams = useSelector(getApproveTxParams)
|
|
|
|
const selectedQuote = useSelector(getSelectedQuote)
|
|
|
|
const topQuote = useSelector(getTopQuote)
|
|
|
|
const usedQuote = selectedQuote || topQuote
|
2020-11-10 00:36:00 +01:00
|
|
|
const tradeValue = usedQuote?.trade?.value ?? '0x0'
|
2020-10-06 20:28:38 +02:00
|
|
|
|
|
|
|
const fetchParamsSourceToken = fetchParams?.sourceToken
|
|
|
|
|
2020-11-03 00:41:28 +01:00
|
|
|
const usedGasLimit =
|
2020-10-06 20:28:38 +02:00
|
|
|
usedQuote?.gasEstimateWithRefund ||
|
2020-11-03 00:41:28 +01:00
|
|
|
`0x${decimalToHex(usedQuote?.averageGas || 0)}`
|
2020-10-06 20:28:38 +02:00
|
|
|
|
2020-11-03 00:41:28 +01:00
|
|
|
const gasLimitForMax =
|
|
|
|
usedQuote?.gasEstimate || `0x${decimalToHex(usedQuote?.averageGas || 0)}`
|
2020-10-06 20:28:38 +02:00
|
|
|
|
2020-11-03 00:41:28 +01:00
|
|
|
const usedGasLimitWithMultiplier = new BigNumber(gasLimitForMax, 16)
|
|
|
|
.times(1.4, 10)
|
2020-10-06 20:28:38 +02:00
|
|
|
.round(0)
|
|
|
|
.toString(16)
|
|
|
|
|
2020-10-19 15:11:23 +02:00
|
|
|
const nonCustomMaxGasLimit = hexMax(
|
2020-11-03 00:41:28 +01:00
|
|
|
`0x${decimalToHex(usedQuote?.maxGas || 0)}`,
|
2020-10-19 15:11:23 +02:00
|
|
|
usedGasLimitWithMultiplier,
|
2020-10-06 20:28:38 +02:00
|
|
|
)
|
2020-10-19 15:11:23 +02:00
|
|
|
const maxGasLimit = customMaxGas || nonCustomMaxGasLimit
|
2020-10-06 20:28:38 +02:00
|
|
|
|
2020-10-07 18:55:38 +02:00
|
|
|
const gasTotalInWeiHex = calcGasTotal(maxGasLimit, gasPrice)
|
2020-10-06 20:28:38 +02:00
|
|
|
|
|
|
|
const { tokensWithBalances } = useTokenTracker(swapsTokens)
|
2020-10-09 21:17:56 +02:00
|
|
|
const swapsEthToken = useSwapsEthToken()
|
2020-11-03 00:41:28 +01:00
|
|
|
const balanceToken =
|
|
|
|
fetchParamsSourceToken === swapsEthToken.address
|
|
|
|
? swapsEthToken
|
|
|
|
: tokensWithBalances.find(
|
|
|
|
({ address }) => address === fetchParamsSourceToken,
|
|
|
|
)
|
2020-10-06 20:28:38 +02:00
|
|
|
|
|
|
|
const selectedFromToken = balanceToken || usedQuote.sourceTokenInfo
|
2020-11-03 00:41:28 +01:00
|
|
|
const tokenBalance =
|
2020-10-06 20:28:38 +02:00
|
|
|
tokensWithBalances?.length &&
|
|
|
|
calcTokenAmount(
|
|
|
|
selectedFromToken.balance || '0x0',
|
|
|
|
selectedFromToken.decimals,
|
|
|
|
).toFixed(9)
|
|
|
|
|
|
|
|
const approveData = getTokenData(approveTxParams?.data)
|
|
|
|
const approveValue = approveData && getTokenValueParam(approveData)
|
2020-11-03 00:41:28 +01:00
|
|
|
const approveAmount =
|
|
|
|
approveValue &&
|
|
|
|
selectedFromToken?.decimals !== undefined &&
|
2020-10-06 20:28:38 +02:00
|
|
|
calcTokenAmount(approveValue, selectedFromToken.decimals).toFixed(9)
|
|
|
|
const approveGas = approveTxParams?.gas
|
|
|
|
|
|
|
|
const renderablePopoverData = useMemo(() => {
|
|
|
|
return quotesToRenderableData(
|
|
|
|
quotes,
|
|
|
|
gasPrice,
|
|
|
|
conversionRate,
|
|
|
|
currentCurrency,
|
|
|
|
approveGas,
|
|
|
|
memoizedTokenConversionRates,
|
|
|
|
)
|
|
|
|
}, [
|
|
|
|
quotes,
|
|
|
|
gasPrice,
|
|
|
|
conversionRate,
|
|
|
|
currentCurrency,
|
|
|
|
approveGas,
|
|
|
|
memoizedTokenConversionRates,
|
|
|
|
])
|
|
|
|
|
|
|
|
const renderableDataForUsedQuote = renderablePopoverData.find(
|
2020-11-03 00:41:28 +01:00
|
|
|
(renderablePopoverDatum) =>
|
|
|
|
renderablePopoverDatum.aggId === usedQuote.aggregator,
|
2020-10-06 20:28:38 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
const {
|
|
|
|
destinationTokenDecimals,
|
|
|
|
destinationTokenSymbol,
|
|
|
|
destinationTokenValue,
|
2020-10-25 14:23:34 +01:00
|
|
|
destinationIconUrl,
|
2020-10-06 20:28:38 +02:00
|
|
|
sourceTokenDecimals,
|
|
|
|
sourceTokenSymbol,
|
|
|
|
sourceTokenValue,
|
2020-10-25 14:23:34 +01:00
|
|
|
sourceTokenIconUrl,
|
2020-10-06 20:28:38 +02:00
|
|
|
} = renderableDataForUsedQuote
|
|
|
|
|
2020-10-27 20:02:21 +01:00
|
|
|
const { feeInFiat, feeInEth } = getRenderableNetworkFeesForQuote(
|
2020-10-06 20:28:38 +02:00
|
|
|
usedGasLimit,
|
|
|
|
approveGas,
|
|
|
|
gasPrice,
|
|
|
|
currentCurrency,
|
|
|
|
conversionRate,
|
2020-10-27 20:02:21 +01:00
|
|
|
tradeValue,
|
|
|
|
sourceTokenSymbol,
|
|
|
|
usedQuote.sourceAmount,
|
2020-10-06 20:28:38 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
const {
|
|
|
|
feeInFiat: maxFeeInFiat,
|
|
|
|
feeInEth: maxFeeInEth,
|
2020-10-27 20:02:21 +01:00
|
|
|
nonGasFee,
|
|
|
|
} = getRenderableNetworkFeesForQuote(
|
2020-10-06 20:28:38 +02:00
|
|
|
maxGasLimit,
|
|
|
|
approveGas,
|
|
|
|
gasPrice,
|
|
|
|
currentCurrency,
|
|
|
|
conversionRate,
|
2020-10-27 20:02:21 +01:00
|
|
|
tradeValue,
|
|
|
|
sourceTokenSymbol,
|
|
|
|
usedQuote.sourceAmount,
|
2020-10-06 20:28:38 +02:00
|
|
|
)
|
|
|
|
|
2020-11-03 00:41:28 +01:00
|
|
|
const tokenCost = new BigNumber(usedQuote.sourceAmount)
|
|
|
|
const ethCost = new BigNumber(usedQuote.trade.value || 0, 10).plus(
|
|
|
|
new BigNumber(gasTotalInWeiHex, 16),
|
|
|
|
)
|
2020-10-06 20:28:38 +02:00
|
|
|
|
2020-11-03 00:41:28 +01:00
|
|
|
const insufficientTokens =
|
2020-10-06 20:28:38 +02:00
|
|
|
(tokensWithBalances?.length || balanceError) &&
|
2020-11-03 00:41:28 +01:00
|
|
|
tokenCost.gt(new BigNumber(selectedFromToken.balance || '0x0'))
|
2020-10-06 20:28:38 +02:00
|
|
|
|
2020-11-03 00:41:28 +01:00
|
|
|
const insufficientEth = ethCost.gt(new BigNumber(ethBalance || '0x0'))
|
2020-10-06 20:28:38 +02:00
|
|
|
|
|
|
|
const tokenBalanceNeeded = insufficientTokens
|
|
|
|
? toPrecisionWithoutTrailingZeros(
|
2020-11-03 00:41:28 +01:00
|
|
|
calcTokenAmount(tokenCost, selectedFromToken.decimals)
|
|
|
|
.minus(tokenBalance)
|
|
|
|
.toString(10),
|
|
|
|
6,
|
|
|
|
)
|
2020-10-06 20:28:38 +02:00
|
|
|
: null
|
|
|
|
|
|
|
|
const ethBalanceNeeded = insufficientEth
|
|
|
|
? toPrecisionWithoutTrailingZeros(
|
2020-11-03 00:41:28 +01:00
|
|
|
ethCost
|
|
|
|
.minus(ethBalance, 16)
|
|
|
|
.div('1000000000000000000', 10)
|
|
|
|
.toString(10),
|
|
|
|
6,
|
|
|
|
)
|
2020-10-06 20:28:38 +02:00
|
|
|
: null
|
|
|
|
|
|
|
|
const destinationToken = useSelector(getDestinationTokenInfo)
|
|
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
if (insufficientTokens || insufficientEth) {
|
|
|
|
dispatch(setBalanceError(true))
|
|
|
|
} else if (balanceError && !insufficientTokens && !insufficientEth) {
|
|
|
|
dispatch(setBalanceError(false))
|
|
|
|
}
|
|
|
|
}, [insufficientTokens, insufficientEth, balanceError, dispatch])
|
|
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
const currentTime = Date.now()
|
|
|
|
const timeSinceLastFetched = currentTime - quotesLastFetched
|
|
|
|
if (timeSinceLastFetched > 60000 && !dispatchedSafeRefetch) {
|
|
|
|
setDispatchedSafeRefetch(true)
|
|
|
|
dispatch(safeRefetchQuotes())
|
|
|
|
} else if (timeSinceLastFetched > 60000) {
|
|
|
|
dispatch(setSwapsErrorKey(QUOTES_EXPIRED_ERROR))
|
|
|
|
history.push(SWAPS_ERROR_ROUTE)
|
|
|
|
}
|
|
|
|
}, [quotesLastFetched, dispatchedSafeRefetch, dispatch, history])
|
|
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
if (!originalApproveAmount && approveAmount) {
|
|
|
|
setOriginalApproveAmount(approveAmount)
|
|
|
|
}
|
|
|
|
}, [originalApproveAmount, approveAmount])
|
|
|
|
|
2020-11-03 00:41:28 +01:00
|
|
|
const showWarning =
|
|
|
|
(balanceError || tokenBalanceNeeded || ethBalanceNeeded) && !warningHidden
|
2020-10-06 20:28:38 +02:00
|
|
|
|
|
|
|
const numberOfQuotes = Object.values(quotes).length
|
|
|
|
const bestQuoteReviewedEventSent = useRef()
|
|
|
|
const eventObjectBase = {
|
|
|
|
token_from: sourceTokenSymbol,
|
|
|
|
token_from_amount: sourceTokenValue,
|
|
|
|
token_to: destinationTokenSymbol,
|
|
|
|
token_to_amount: destinationTokenValue,
|
|
|
|
request_type: fetchParams?.balanceError,
|
|
|
|
slippage: fetchParams?.slippage,
|
|
|
|
custom_slippage: fetchParams?.slippage !== 2,
|
|
|
|
response_time: fetchParams?.responseTime,
|
|
|
|
best_quote_source: topQuote?.aggregator,
|
|
|
|
available_quotes: numberOfQuotes,
|
|
|
|
}
|
|
|
|
|
2020-11-10 16:49:01 +01:00
|
|
|
const allAvailableQuotesOpened = useNewMetricEvent({
|
2020-10-06 20:28:38 +02:00
|
|
|
event: 'All Available Quotes Opened',
|
2020-11-10 16:49:01 +01:00
|
|
|
category: 'swaps',
|
|
|
|
sensitiveProperties: {
|
2020-10-06 20:28:38 +02:00
|
|
|
...eventObjectBase,
|
|
|
|
other_quote_selected: usedQuote?.aggregator !== topQuote?.aggregator,
|
2020-11-03 00:41:28 +01:00
|
|
|
other_quote_selected_source:
|
|
|
|
usedQuote?.aggregator === topQuote?.aggregator
|
|
|
|
? null
|
|
|
|
: usedQuote?.aggregator,
|
2020-10-06 20:28:38 +02:00
|
|
|
},
|
2020-11-03 00:41:28 +01:00
|
|
|
})
|
2020-11-10 16:49:01 +01:00
|
|
|
const quoteDetailsOpened = useNewMetricEvent({
|
2020-10-06 20:28:38 +02:00
|
|
|
event: 'Quote Details Opened',
|
2020-11-10 16:49:01 +01:00
|
|
|
category: 'swaps',
|
|
|
|
sensitiveProperties: {
|
2020-10-06 20:28:38 +02:00
|
|
|
...eventObjectBase,
|
|
|
|
other_quote_selected: usedQuote?.aggregator !== topQuote?.aggregator,
|
2020-11-03 00:41:28 +01:00
|
|
|
other_quote_selected_source:
|
|
|
|
usedQuote?.aggregator === topQuote?.aggregator
|
|
|
|
? null
|
|
|
|
: usedQuote?.aggregator,
|
2020-10-06 20:28:38 +02:00
|
|
|
},
|
|
|
|
})
|
2020-11-10 16:49:01 +01:00
|
|
|
const editSpendLimitOpened = useNewMetricEvent({
|
2020-10-06 20:28:38 +02:00
|
|
|
event: 'Edit Spend Limit Opened',
|
2020-11-10 16:49:01 +01:00
|
|
|
category: 'swaps',
|
|
|
|
sensitiveProperties: {
|
2020-10-06 20:28:38 +02:00
|
|
|
...eventObjectBase,
|
|
|
|
custom_spend_limit_set: originalApproveAmount === approveAmount,
|
2020-11-03 00:41:28 +01:00
|
|
|
custom_spend_limit_amount:
|
|
|
|
originalApproveAmount === approveAmount ? null : approveAmount,
|
2020-10-06 20:28:38 +02:00
|
|
|
},
|
2020-11-03 00:41:28 +01:00
|
|
|
})
|
2020-10-06 20:28:38 +02:00
|
|
|
|
2020-11-03 00:41:28 +01:00
|
|
|
const bestQuoteReviewedEvent = useNewMetricEvent({
|
|
|
|
event: 'Best Quote Reviewed',
|
|
|
|
category: 'swaps',
|
2020-11-10 16:49:01 +01:00
|
|
|
sensitiveProperties: { ...eventObjectBase, network_fees: feeInFiat },
|
2020-11-03 00:41:28 +01:00
|
|
|
})
|
2020-10-06 20:28:38 +02:00
|
|
|
useEffect(() => {
|
2020-11-03 00:41:28 +01:00
|
|
|
if (
|
|
|
|
!bestQuoteReviewedEventSent.current &&
|
|
|
|
[
|
|
|
|
sourceTokenSymbol,
|
|
|
|
sourceTokenValue,
|
|
|
|
destinationTokenSymbol,
|
|
|
|
destinationTokenValue,
|
|
|
|
fetchParams,
|
|
|
|
topQuote,
|
|
|
|
numberOfQuotes,
|
|
|
|
feeInFiat,
|
|
|
|
].every((dep) => dep !== null && dep !== undefined)
|
|
|
|
) {
|
2020-10-06 20:28:38 +02:00
|
|
|
bestQuoteReviewedEventSent.current = true
|
|
|
|
bestQuoteReviewedEvent()
|
|
|
|
}
|
2020-11-03 00:41:28 +01:00
|
|
|
}, [
|
|
|
|
sourceTokenSymbol,
|
|
|
|
sourceTokenValue,
|
|
|
|
destinationTokenSymbol,
|
|
|
|
destinationTokenValue,
|
|
|
|
fetchParams,
|
|
|
|
topQuote,
|
|
|
|
numberOfQuotes,
|
|
|
|
feeInFiat,
|
|
|
|
bestQuoteReviewedEvent,
|
|
|
|
])
|
2020-10-06 20:28:38 +02:00
|
|
|
|
2020-10-22 16:58:19 +02:00
|
|
|
const metaMaskFee = usedQuote.fee
|
2020-10-20 01:28:48 +02:00
|
|
|
|
2020-10-14 19:06:17 +02:00
|
|
|
const onFeeCardTokenApprovalClick = () => {
|
2020-10-06 20:28:38 +02:00
|
|
|
editSpendLimitOpened()
|
2020-11-03 00:41:28 +01:00
|
|
|
dispatch(
|
|
|
|
showModal({
|
|
|
|
name: 'EDIT_APPROVAL_PERMISSION',
|
|
|
|
decimals: selectedFromToken.decimals,
|
|
|
|
origin: 'MetaMask',
|
|
|
|
setCustomAmount: (newCustomPermissionAmount) => {
|
|
|
|
const customPermissionAmount =
|
|
|
|
newCustomPermissionAmount === ''
|
|
|
|
? originalApproveAmount
|
|
|
|
: newCustomPermissionAmount
|
|
|
|
const newData = getCustomTxParamsData(approveTxParams.data, {
|
|
|
|
customPermissionAmount,
|
|
|
|
decimals: selectedFromToken.decimals,
|
|
|
|
})
|
|
|
|
|
|
|
|
if (
|
|
|
|
customPermissionAmount?.length &&
|
|
|
|
approveTxParams.data !== newData
|
|
|
|
) {
|
|
|
|
dispatch(setCustomApproveTxData(newData))
|
|
|
|
}
|
|
|
|
},
|
|
|
|
tokenAmount: originalApproveAmount,
|
|
|
|
customTokenAmount:
|
|
|
|
originalApproveAmount === approveAmount ? null : approveAmount,
|
|
|
|
tokenBalance,
|
|
|
|
tokenSymbol: selectedFromToken.symbol,
|
|
|
|
requiredMinimum: calcTokenAmount(
|
|
|
|
usedQuote.sourceAmount,
|
|
|
|
selectedFromToken.decimals,
|
|
|
|
),
|
|
|
|
}),
|
|
|
|
)
|
2020-10-06 20:28:38 +02:00
|
|
|
}
|
|
|
|
|
2020-11-03 00:41:28 +01:00
|
|
|
const nonGasFeeIsPositive = new BigNumber(nonGasFee, 16).gt(0)
|
2020-10-27 20:02:21 +01:00
|
|
|
const approveGasTotal = calcGasTotal(approveGas || '0x0', gasPrice)
|
2020-11-03 00:41:28 +01:00
|
|
|
const extraNetworkFeeTotalInHexWEI = new BigNumber(nonGasFee, 16)
|
2020-10-27 20:02:21 +01:00
|
|
|
.plus(approveGasTotal, 16)
|
|
|
|
.toString(16)
|
|
|
|
const extraNetworkFeeTotalInEth = getValueFromWeiHex({
|
|
|
|
value: extraNetworkFeeTotalInHexWEI,
|
|
|
|
toDenomination: 'ETH',
|
|
|
|
numberOfDecimals: 4,
|
|
|
|
})
|
|
|
|
|
|
|
|
let extraInfoRowLabel = ''
|
|
|
|
if (approveGas && nonGasFeeIsPositive) {
|
|
|
|
extraInfoRowLabel = t('approvalAndAggregatorTxFeeCost')
|
|
|
|
} else if (approveGas) {
|
|
|
|
extraInfoRowLabel = t('approvalTxGasCost')
|
|
|
|
} else if (nonGasFeeIsPositive) {
|
|
|
|
extraInfoRowLabel = t('aggregatorFeeCost')
|
|
|
|
}
|
|
|
|
|
2020-11-03 00:41:28 +01:00
|
|
|
const onFeeCardMaxRowClick = () =>
|
|
|
|
dispatch(
|
|
|
|
showModal({
|
2020-11-04 17:14:08 +01:00
|
|
|
name: 'CUSTOMIZE_METASWAP_GAS',
|
2020-11-10 00:36:00 +01:00
|
|
|
value: tradeValue,
|
2020-11-03 00:41:28 +01:00
|
|
|
customGasLimitMessage: approveGas
|
|
|
|
? t('extraApprovalGas', [hexToDecimal(approveGas)])
|
|
|
|
: '',
|
|
|
|
customTotalSupplement: approveGasTotal,
|
|
|
|
extraInfoRow: extraInfoRowLabel
|
|
|
|
? {
|
|
|
|
label: extraInfoRowLabel,
|
|
|
|
value: t('amountInEth', [extraNetworkFeeTotalInEth]),
|
|
|
|
}
|
|
|
|
: null,
|
2020-11-04 17:14:08 +01:00
|
|
|
initialGasPrice: gasPrice,
|
|
|
|
initialGasLimit: maxGasLimit,
|
2020-11-12 20:12:04 +01:00
|
|
|
minimumGasLimit: new BigNumber(nonCustomMaxGasLimit, 16).toNumber(),
|
2020-11-03 00:41:28 +01:00
|
|
|
}),
|
|
|
|
)
|
2020-10-06 20:28:38 +02:00
|
|
|
|
2020-10-14 19:06:17 +02:00
|
|
|
const tokenApprovalTextComponent = (
|
2020-11-03 00:41:28 +01:00
|
|
|
<span key="swaps-view-quote-approve-symbol-1" className="view-quote__bold">
|
2020-10-06 20:28:38 +02:00
|
|
|
{sourceTokenSymbol}
|
|
|
|
</span>
|
|
|
|
)
|
|
|
|
|
|
|
|
const actionableMessage = t('swapApproveNeedMoreTokens', [
|
2020-11-03 00:41:28 +01:00
|
|
|
<span key="swapApproveNeedMoreTokens-1" className="view-quote__bold">
|
2020-10-06 20:28:38 +02:00
|
|
|
{tokenBalanceNeeded || ethBalanceNeeded}
|
|
|
|
</span>,
|
|
|
|
tokenBalanceNeeded && !(sourceTokenSymbol === 'ETH')
|
|
|
|
? sourceTokenSymbol
|
|
|
|
: 'ETH',
|
|
|
|
])
|
|
|
|
|
|
|
|
return (
|
|
|
|
<div className="view-quote">
|
|
|
|
<div className="view-quote__content">
|
|
|
|
{selectQuotePopoverShown && (
|
|
|
|
<SelectQuotePopover
|
|
|
|
quoteDataRows={renderablePopoverData}
|
|
|
|
onClose={() => setSelectQuotePopoverShown(false)}
|
2020-11-12 20:12:04 +01:00
|
|
|
onSubmit={(aggId) => dispatch(swapsQuoteSelected(aggId))}
|
2020-10-06 20:28:38 +02:00
|
|
|
swapToSymbol={destinationTokenSymbol}
|
|
|
|
initialAggId={usedQuote.aggregator}
|
2020-11-10 16:49:01 +01:00
|
|
|
onQuoteDetailsIsOpened={quoteDetailsOpened}
|
2020-10-06 20:28:38 +02:00
|
|
|
/>
|
|
|
|
)}
|
|
|
|
<div className="view-quote__insufficient-eth-warning-wrapper">
|
|
|
|
{showWarning && (
|
|
|
|
<ActionableMessage
|
|
|
|
message={actionableMessage}
|
|
|
|
onClose={() => setWarningHidden(true)}
|
|
|
|
/>
|
|
|
|
)}
|
|
|
|
</div>
|
|
|
|
<div
|
|
|
|
className={classnames('view-quote__countdown-timer-container', {
|
|
|
|
'view-quote__countdown-timer-container--thin': showWarning,
|
|
|
|
})}
|
|
|
|
>
|
|
|
|
<CountdownTimer
|
|
|
|
timeStarted={quotesLastFetched}
|
|
|
|
warningTime="0:30"
|
|
|
|
infoTooltipLabelKey="swapQuotesAreRefreshed"
|
|
|
|
labelKey="swapNewQuoteIn"
|
|
|
|
/>
|
|
|
|
</div>
|
2020-10-25 14:23:34 +01:00
|
|
|
<MainQuoteSummary
|
|
|
|
sourceValue={calcTokenValue(sourceTokenValue, sourceTokenDecimals)}
|
|
|
|
sourceDecimals={sourceTokenDecimals}
|
|
|
|
sourceSymbol={sourceTokenSymbol}
|
|
|
|
destinationValue={calcTokenValue(
|
|
|
|
destinationTokenValue,
|
|
|
|
destinationTokenDecimals,
|
|
|
|
)}
|
|
|
|
destinationDecimals={destinationTokenDecimals}
|
|
|
|
destinationSymbol={destinationTokenSymbol}
|
|
|
|
sourceIconUrl={sourceTokenIconUrl}
|
|
|
|
destinationIconUrl={destinationIconUrl}
|
|
|
|
/>
|
2020-11-03 00:41:28 +01:00
|
|
|
<div className="view-quote__view-other-button-container">
|
2020-10-06 20:28:38 +02:00
|
|
|
<div className="view-quote__view-other-button">
|
|
|
|
{t('swapNQuotesAvailable', [Object.values(quotes).length])}
|
|
|
|
<i className="fa fa-arrow-right" />
|
|
|
|
</div>
|
|
|
|
<div
|
|
|
|
className="view-quote__view-other-button-fade"
|
|
|
|
onClick={() => {
|
|
|
|
allAvailableQuotesOpened()
|
|
|
|
setSelectQuotePopoverShown(true)
|
|
|
|
}}
|
|
|
|
>
|
|
|
|
{t('swapNQuotesAvailable', [Object.values(quotes).length])}
|
|
|
|
<i className="fa fa-arrow-right" />
|
|
|
|
</div>
|
|
|
|
</div>
|
2020-10-19 21:45:32 +02:00
|
|
|
<div className="view-quote__metamask-rate">
|
2020-11-03 00:41:28 +01:00
|
|
|
<p className="view-quote__metamask-rate-text">
|
|
|
|
{t('swapQuoteIncludesRate', [metaMaskFee])}
|
|
|
|
</p>
|
2020-10-19 21:45:32 +02:00
|
|
|
<InfoTooltip
|
|
|
|
position="top"
|
|
|
|
contentText={t('swapMetaMaskFeeDescription', [metaMaskFee])}
|
|
|
|
wrapperClassName="view-quote__metamask-rate-info-icon"
|
|
|
|
/>
|
|
|
|
</div>
|
2020-10-06 20:28:38 +02:00
|
|
|
<div
|
|
|
|
className={classnames('view-quote__fee-card-container', {
|
|
|
|
'view-quote__fee-card-container--thin': showWarning,
|
2020-11-03 00:41:28 +01:00
|
|
|
'view-quote__fee-card-container--three-rows':
|
|
|
|
approveTxParams && (!balanceError || warningHidden),
|
2020-10-06 20:28:38 +02:00
|
|
|
})}
|
|
|
|
>
|
|
|
|
<FeeCard
|
2020-11-03 00:41:28 +01:00
|
|
|
primaryFee={{
|
2020-10-06 20:28:38 +02:00
|
|
|
fee: feeInEth,
|
|
|
|
maxFee: maxFeeInEth,
|
2020-11-03 00:41:28 +01:00
|
|
|
}}
|
|
|
|
secondaryFee={{
|
2020-10-06 20:28:38 +02:00
|
|
|
fee: feeInFiat,
|
|
|
|
maxFee: maxFeeInFiat,
|
2020-11-03 00:41:28 +01:00
|
|
|
}}
|
2020-10-14 19:06:17 +02:00
|
|
|
onFeeCardMaxRowClick={onFeeCardMaxRowClick}
|
|
|
|
hideTokenApprovalRow={
|
|
|
|
!approveTxParams || (balanceError && !warningHidden)
|
|
|
|
}
|
|
|
|
tokenApprovalTextComponent={tokenApprovalTextComponent}
|
2020-10-14 19:12:35 +02:00
|
|
|
tokenApprovalSourceTokenSymbol={sourceTokenSymbol}
|
|
|
|
onTokenApprovalClick={onFeeCardTokenApprovalClick}
|
2020-10-06 20:28:38 +02:00
|
|
|
/>
|
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
<SwapsFooter
|
|
|
|
onSubmit={() => {
|
|
|
|
if (!balanceError) {
|
|
|
|
dispatch(signAndSendTransactions(history, metaMetricsEvent))
|
|
|
|
} else if (destinationToken.symbol === 'ETH') {
|
|
|
|
history.push(DEFAULT_ROUTE)
|
|
|
|
} else {
|
|
|
|
history.push(`${ASSET_ROUTE}/${destinationToken.address}`)
|
|
|
|
}
|
|
|
|
}}
|
|
|
|
submitText={t('swap')}
|
|
|
|
onCancel={async () => await dispatch(navigateBackToBuildQuote(history))}
|
2020-11-04 17:14:08 +01:00
|
|
|
disabled={balanceError || gasPrice === null || gasPrice === undefined}
|
2020-10-06 20:28:38 +02:00
|
|
|
showTermsOfService
|
|
|
|
showTopBorder
|
|
|
|
/>
|
|
|
|
</div>
|
|
|
|
)
|
|
|
|
}
|