2021-02-04 19:15:23 +01:00
|
|
|
import React, { useState, useContext, useMemo, useEffect, useRef } from 'react';
|
2021-12-07 18:33:13 +01:00
|
|
|
import { shallowEqual, useSelector, useDispatch } from 'react-redux';
|
2021-02-04 19:15:23 +01:00
|
|
|
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 { useEthFiatAmount } from '../../../hooks/useEthFiatAmount';
|
|
|
|
import { useEqualityCheck } from '../../../hooks/useEqualityCheck';
|
|
|
|
import { useNewMetricEvent } from '../../../hooks/useMetricEvent';
|
2021-02-05 21:40:40 +01:00
|
|
|
import { usePrevious } from '../../../hooks/usePrevious';
|
2021-10-22 19:14:28 +02:00
|
|
|
import { useGasFeeInputs } from '../../../hooks/gasFeeInput/useGasFeeInputs';
|
2021-02-04 19:15:23 +01:00
|
|
|
import { MetaMetricsContext } from '../../../contexts/metametrics.new';
|
|
|
|
import FeeCard from '../fee-card';
|
2021-07-30 13:35:30 +02:00
|
|
|
import EditGasPopover from '../../../components/app/edit-gas-popover/edit-gas-popover.component';
|
2020-10-06 20:28:38 +02:00
|
|
|
import {
|
2020-12-15 18:16:51 +01:00
|
|
|
FALLBACK_GAS_MULTIPLIER,
|
2020-10-06 20:28:38 +02:00
|
|
|
getQuotes,
|
|
|
|
getSelectedQuote,
|
|
|
|
getApproveTxParams,
|
|
|
|
getFetchParams,
|
|
|
|
setBalanceError,
|
|
|
|
getQuotesLastFetched,
|
|
|
|
getBalanceError,
|
2021-07-30 13:35:30 +02:00
|
|
|
getCustomSwapsGas, // Gas limit.
|
|
|
|
getCustomMaxFeePerGas,
|
|
|
|
getCustomMaxPriorityFeePerGas,
|
2021-08-06 21:31:30 +02:00
|
|
|
getSwapsUserFeeLevel,
|
2020-10-06 20:28:38 +02:00
|
|
|
getDestinationTokenInfo,
|
2020-11-04 17:14:08 +01:00
|
|
|
getUsedSwapsGasPrice,
|
2020-10-06 20:28:38 +02:00
|
|
|
getTopQuote,
|
|
|
|
signAndSendTransactions,
|
|
|
|
getBackgroundSwapRouteState,
|
2020-11-12 20:12:04 +01:00
|
|
|
swapsQuoteSelected,
|
2020-12-15 21:24:22 +01:00
|
|
|
getSwapsQuoteRefreshTime,
|
2021-09-15 15:13:18 +02:00
|
|
|
getReviewSwapClickedTimestamp,
|
2021-02-04 19:15:23 +01:00
|
|
|
} from '../../../ducks/swaps/swaps';
|
2020-10-06 20:28:38 +02:00
|
|
|
import {
|
|
|
|
conversionRateSelector,
|
|
|
|
getSelectedAccount,
|
|
|
|
getCurrentCurrency,
|
|
|
|
getTokenExchangeRates,
|
2021-03-18 11:20:06 +01:00
|
|
|
getSwapsDefaultToken,
|
|
|
|
getCurrentChainId,
|
2021-05-13 21:26:08 +02:00
|
|
|
isHardwareWallet,
|
|
|
|
getHardwareWalletType,
|
2021-08-03 00:52:18 +02:00
|
|
|
checkNetworkAndAccountSupports1559,
|
2021-02-04 19:15:23 +01:00
|
|
|
} from '../../../selectors';
|
2021-08-03 00:52:18 +02:00
|
|
|
import { getNativeCurrency, getTokens } from '../../../ducks/metamask/metamask';
|
2021-06-08 18:03:59 +02:00
|
|
|
|
2021-10-08 23:27:45 +02:00
|
|
|
import {
|
|
|
|
toPrecisionWithoutTrailingZeros,
|
|
|
|
isEqualCaseInsensitive,
|
|
|
|
} from '../../../helpers/utils/util';
|
2021-06-08 18:03:59 +02:00
|
|
|
|
2020-10-06 20:28:38 +02:00
|
|
|
import {
|
|
|
|
safeRefetchQuotes,
|
|
|
|
setCustomApproveTxData,
|
|
|
|
setSwapsErrorKey,
|
|
|
|
showModal,
|
2021-09-15 15:13:18 +02:00
|
|
|
setSwapsQuotesPollingLimitEnabled,
|
2021-02-04 19:15:23 +01:00
|
|
|
} from '../../../store/actions';
|
2020-10-06 20:28:38 +02:00
|
|
|
import {
|
|
|
|
ASSET_ROUTE,
|
|
|
|
BUILD_QUOTE_ROUTE,
|
|
|
|
DEFAULT_ROUTE,
|
|
|
|
SWAPS_ERROR_ROUTE,
|
|
|
|
AWAITING_SWAP_ROUTE,
|
2021-02-04 19:15:23 +01:00
|
|
|
} from '../../../helpers/constants/routes';
|
|
|
|
import { getTokenData } from '../../../helpers/utils/transactions.util';
|
2020-10-06 20:28:38 +02:00
|
|
|
import {
|
|
|
|
calcTokenAmount,
|
|
|
|
calcTokenValue,
|
|
|
|
getTokenValueParam,
|
2021-02-04 19:15:23 +01:00
|
|
|
} from '../../../helpers/utils/token-util';
|
2020-10-06 20:28:38 +02:00
|
|
|
import {
|
|
|
|
decimalToHex,
|
|
|
|
hexToDecimal,
|
|
|
|
getValueFromWeiHex,
|
2021-07-30 13:35:30 +02:00
|
|
|
decGWEIToHexWEI,
|
|
|
|
addHexes,
|
2021-02-04 19:15:23 +01:00
|
|
|
} from '../../../helpers/utils/conversions.util';
|
2021-12-12 00:26:28 +01:00
|
|
|
import { GasFeeContextProvider } from '../../../contexts/gasFee';
|
|
|
|
import { TransactionModalContextProvider } from '../../../contexts/transaction-modal';
|
|
|
|
import AdvancedGasFeePopover from '../../../components/app/advanced-gas-fee-popover';
|
|
|
|
import EditGasFeePopover from '../../../components/app/edit-gas-fee-popover';
|
2021-02-04 19:15:23 +01:00
|
|
|
import MainQuoteSummary from '../main-quote-summary';
|
|
|
|
import { calcGasTotal } from '../../send/send.utils';
|
|
|
|
import { getCustomTxParamsData } from '../../confirm-approve/confirm-approve.util';
|
2021-07-02 21:07:56 +02:00
|
|
|
import ActionableMessage from '../../../components/ui/actionable-message/actionable-message';
|
2020-11-03 00:41:28 +01:00
|
|
|
import {
|
|
|
|
quotesToRenderableData,
|
|
|
|
getRenderableNetworkFeesForQuote,
|
2021-02-04 19:15:23 +01:00
|
|
|
} from '../swaps.util';
|
|
|
|
import { useTokenTracker } from '../../../hooks/useTokenTracker';
|
2021-04-28 21:53:59 +02:00
|
|
|
import { QUOTES_EXPIRED_ERROR } from '../../../../shared/constants/swaps';
|
2021-11-06 01:59:23 +01:00
|
|
|
import {
|
|
|
|
EDIT_GAS_MODES,
|
|
|
|
GAS_RECOMMENDATIONS,
|
|
|
|
} from '../../../../shared/constants/gas';
|
2021-02-04 19:15:23 +01:00
|
|
|
import CountdownTimer from '../countdown-timer';
|
|
|
|
import SwapsFooter from '../swaps-footer';
|
|
|
|
import ViewQuotePriceDifference from './view-quote-price-difference';
|
2020-10-06 20:28:38 +02:00
|
|
|
|
2021-12-12 00:26:28 +01:00
|
|
|
// eslint-disable-next-line prefer-destructuring
|
|
|
|
const EIP_1559_V2_ENABLED =
|
|
|
|
process.env.EIP_1559_V2 === true || process.env.EIP_1559_V2 === 'true';
|
|
|
|
|
2020-11-03 00:41:28 +01:00
|
|
|
export default function ViewQuote() {
|
2021-02-04 19:15:23 +01:00
|
|
|
const history = useHistory();
|
|
|
|
const dispatch = useDispatch();
|
|
|
|
const t = useContext(I18nContext);
|
|
|
|
const metaMetricsEvent = useContext(MetaMetricsContext);
|
2020-10-06 20:28:38 +02:00
|
|
|
|
2021-02-04 19:15:23 +01:00
|
|
|
const [dispatchedSafeRefetch, setDispatchedSafeRefetch] = useState(false);
|
|
|
|
const [submitClicked, setSubmitClicked] = useState(false);
|
|
|
|
const [selectQuotePopoverShown, setSelectQuotePopoverShown] = useState(false);
|
|
|
|
const [warningHidden, setWarningHidden] = useState(false);
|
|
|
|
const [originalApproveAmount, setOriginalApproveAmount] = useState(null);
|
2021-07-30 13:35:30 +02:00
|
|
|
const [showEditGasPopover, setShowEditGasPopover] = useState(false);
|
2021-09-15 15:13:18 +02:00
|
|
|
// We need to have currentTimestamp in state, otherwise it would change with each rerender.
|
|
|
|
const [currentTimestamp] = useState(Date.now());
|
2020-10-06 20:28:38 +02:00
|
|
|
|
2021-02-04 16:58:46 +01:00
|
|
|
const [
|
|
|
|
acknowledgedPriceDifference,
|
|
|
|
setAcknowledgedPriceDifference,
|
2021-02-04 19:15:23 +01:00
|
|
|
] = useState(false);
|
2021-11-06 01:59:23 +01:00
|
|
|
const priceDifferenceRiskyBuckets = [
|
|
|
|
GAS_RECOMMENDATIONS.HIGH,
|
|
|
|
GAS_RECOMMENDATIONS.MEDIUM,
|
|
|
|
];
|
2021-02-04 16:58:46 +01:00
|
|
|
|
2021-02-04 19:15:23 +01:00
|
|
|
const routeState = useSelector(getBackgroundSwapRouteState);
|
|
|
|
const quotes = useSelector(getQuotes, isEqual);
|
2020-10-06 20:28:38 +02:00
|
|
|
useEffect(() => {
|
|
|
|
if (!Object.values(quotes).length) {
|
2021-02-04 19:15:23 +01:00
|
|
|
history.push(BUILD_QUOTE_ROUTE);
|
2020-10-06 20:28:38 +02:00
|
|
|
} else if (routeState === 'awaiting') {
|
2021-02-04 19:15:23 +01:00
|
|
|
history.push(AWAITING_SWAP_ROUTE);
|
2020-10-06 20:28:38 +02:00
|
|
|
}
|
2021-02-04 19:15:23 +01:00
|
|
|
}, [history, quotes, routeState]);
|
2020-10-06 20:28:38 +02:00
|
|
|
|
2021-02-04 19:15:23 +01:00
|
|
|
const quotesLastFetched = useSelector(getQuotesLastFetched);
|
2020-10-06 20:28:38 +02:00
|
|
|
|
|
|
|
// Select necessary data
|
2021-02-04 19:15:23 +01:00
|
|
|
const gasPrice = useSelector(getUsedSwapsGasPrice);
|
|
|
|
const customMaxGas = useSelector(getCustomSwapsGas);
|
2021-07-30 13:35:30 +02:00
|
|
|
const customMaxFeePerGas = useSelector(getCustomMaxFeePerGas);
|
|
|
|
const customMaxPriorityFeePerGas = useSelector(getCustomMaxPriorityFeePerGas);
|
2021-08-06 21:31:30 +02:00
|
|
|
const swapsUserFeeLevel = useSelector(getSwapsUserFeeLevel);
|
2021-12-07 18:33:13 +01:00
|
|
|
const tokenConversionRates = useSelector(getTokenExchangeRates, isEqual);
|
2021-02-04 19:15:23 +01:00
|
|
|
const memoizedTokenConversionRates = useEqualityCheck(tokenConversionRates);
|
2021-12-07 18:33:13 +01:00
|
|
|
const { balance: ethBalance } = useSelector(getSelectedAccount, shallowEqual);
|
2021-02-04 19:15:23 +01:00
|
|
|
const conversionRate = useSelector(conversionRateSelector);
|
|
|
|
const currentCurrency = useSelector(getCurrentCurrency);
|
2021-12-07 18:33:13 +01:00
|
|
|
const swapsTokens = useSelector(getTokens, isEqual);
|
2021-08-03 00:52:18 +02:00
|
|
|
const networkAndAccountSupports1559 = useSelector(
|
|
|
|
checkNetworkAndAccountSupports1559,
|
|
|
|
);
|
2021-02-04 19:15:23 +01:00
|
|
|
const balanceError = useSelector(getBalanceError);
|
2021-12-07 18:33:13 +01:00
|
|
|
const fetchParams = useSelector(getFetchParams, isEqual);
|
|
|
|
const approveTxParams = useSelector(getApproveTxParams, shallowEqual);
|
|
|
|
const selectedQuote = useSelector(getSelectedQuote, isEqual);
|
|
|
|
const topQuote = useSelector(getTopQuote, isEqual);
|
2021-02-04 19:15:23 +01:00
|
|
|
const usedQuote = selectedQuote || topQuote;
|
|
|
|
const tradeValue = usedQuote?.trade?.value ?? '0x0';
|
|
|
|
const swapsQuoteRefreshTime = useSelector(getSwapsQuoteRefreshTime);
|
2021-12-07 18:33:13 +01:00
|
|
|
const defaultSwapsToken = useSelector(getSwapsDefaultToken, isEqual);
|
2021-03-18 11:20:06 +01:00
|
|
|
const chainId = useSelector(getCurrentChainId);
|
2021-03-29 21:30:06 +02:00
|
|
|
const nativeCurrencySymbol = useSelector(getNativeCurrency);
|
2021-09-15 15:13:18 +02:00
|
|
|
const reviewSwapClickedTimestamp = useSelector(getReviewSwapClickedTimestamp);
|
2021-02-04 19:15:23 +01:00
|
|
|
|
2021-07-30 13:35:30 +02:00
|
|
|
let gasFeeInputs;
|
2021-08-03 00:52:18 +02:00
|
|
|
if (networkAndAccountSupports1559) {
|
2021-07-30 13:35:30 +02:00
|
|
|
// For Swaps we want to get 'high' estimations by default.
|
|
|
|
// eslint-disable-next-line react-hooks/rules-of-hooks
|
2021-11-06 01:59:23 +01:00
|
|
|
gasFeeInputs = useGasFeeInputs(GAS_RECOMMENDATIONS.HIGH, {
|
|
|
|
userFeeLevel: swapsUserFeeLevel || GAS_RECOMMENDATIONS.HIGH,
|
2021-08-06 21:31:30 +02:00
|
|
|
});
|
2021-07-30 13:35:30 +02:00
|
|
|
}
|
|
|
|
|
2021-02-04 19:15:23 +01:00
|
|
|
const { isBestQuote } = usedQuote;
|
|
|
|
|
|
|
|
const fetchParamsSourceToken = fetchParams?.sourceToken;
|
2020-10-06 20:28:38 +02:00
|
|
|
|
2020-11-03 00:41:28 +01:00
|
|
|
const usedGasLimit =
|
2020-10-06 20:28:38 +02:00
|
|
|
usedQuote?.gasEstimateWithRefund ||
|
2021-02-04 19:15:23 +01:00
|
|
|
`0x${decimalToHex(usedQuote?.averageGas || 0)}`;
|
2020-10-06 20:28:38 +02:00
|
|
|
|
2021-02-04 19:15:23 +01:00
|
|
|
const gasLimitForMax = usedQuote?.gasEstimate || `0x0`;
|
2020-10-06 20:28:38 +02:00
|
|
|
|
2020-11-03 00:41:28 +01:00
|
|
|
const usedGasLimitWithMultiplier = new BigNumber(gasLimitForMax, 16)
|
2020-12-15 18:16:51 +01:00
|
|
|
.times(usedQuote?.gasMultiplier || FALLBACK_GAS_MULTIPLIER, 10)
|
2020-10-06 20:28:38 +02:00
|
|
|
.round(0)
|
2021-02-04 19:15:23 +01:00
|
|
|
.toString(16);
|
2020-10-06 20:28:38 +02:00
|
|
|
|
2020-12-15 18:16:51 +01:00
|
|
|
const nonCustomMaxGasLimit = usedQuote?.gasEstimate
|
|
|
|
? usedGasLimitWithMultiplier
|
2021-02-04 19:15:23 +01:00
|
|
|
: `0x${decimalToHex(usedQuote?.maxGas || 0)}`;
|
|
|
|
const maxGasLimit = customMaxGas || nonCustomMaxGasLimit;
|
2020-10-06 20:28:38 +02:00
|
|
|
|
2021-07-30 13:35:30 +02:00
|
|
|
let maxFeePerGas;
|
|
|
|
let maxPriorityFeePerGas;
|
|
|
|
let baseAndPriorityFeePerGas;
|
|
|
|
|
2021-08-03 00:52:18 +02:00
|
|
|
if (networkAndAccountSupports1559) {
|
2021-07-30 13:35:30 +02:00
|
|
|
const {
|
|
|
|
maxFeePerGas: suggestedMaxFeePerGas,
|
|
|
|
maxPriorityFeePerGas: suggestedMaxPriorityFeePerGas,
|
|
|
|
gasFeeEstimates: { estimatedBaseFee = '0' },
|
|
|
|
} = gasFeeInputs;
|
|
|
|
maxFeePerGas = customMaxFeePerGas || decGWEIToHexWEI(suggestedMaxFeePerGas);
|
|
|
|
maxPriorityFeePerGas =
|
|
|
|
customMaxPriorityFeePerGas ||
|
|
|
|
decGWEIToHexWEI(suggestedMaxPriorityFeePerGas);
|
|
|
|
baseAndPriorityFeePerGas = addHexes(
|
|
|
|
decGWEIToHexWEI(estimatedBaseFee),
|
|
|
|
maxPriorityFeePerGas,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
const gasTotalInWeiHex = calcGasTotal(
|
|
|
|
maxGasLimit,
|
2021-08-03 00:52:18 +02:00
|
|
|
networkAndAccountSupports1559 ? maxFeePerGas : gasPrice,
|
2021-07-30 13:35:30 +02:00
|
|
|
);
|
2020-10-06 20:28:38 +02:00
|
|
|
|
2021-02-17 15:49:24 +01:00
|
|
|
const { tokensWithBalances } = useTokenTracker(swapsTokens, true);
|
2020-11-03 00:41:28 +01:00
|
|
|
const balanceToken =
|
2021-03-18 11:20:06 +01:00
|
|
|
fetchParamsSourceToken === defaultSwapsToken.address
|
|
|
|
? defaultSwapsToken
|
2021-10-08 23:27:45 +02:00
|
|
|
: tokensWithBalances.find(({ address }) =>
|
|
|
|
isEqualCaseInsensitive(address, fetchParamsSourceToken),
|
2021-02-04 19:15:23 +01:00
|
|
|
);
|
2020-10-06 20:28:38 +02:00
|
|
|
|
2021-02-04 19:15:23 +01: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,
|
2021-02-04 19:15:23 +01:00
|
|
|
).toFixed(9);
|
2021-02-17 15:49:24 +01:00
|
|
|
const tokenBalanceUnavailable =
|
|
|
|
tokensWithBalances && balanceToken === undefined;
|
2020-10-06 20:28:38 +02:00
|
|
|
|
2021-02-04 19:15:23 +01:00
|
|
|
const approveData = getTokenData(approveTxParams?.data);
|
|
|
|
const approveValue = approveData && getTokenValueParam(approveData);
|
2020-11-03 00:41:28 +01:00
|
|
|
const approveAmount =
|
|
|
|
approveValue &&
|
|
|
|
selectedFromToken?.decimals !== undefined &&
|
2021-02-04 19:15:23 +01:00
|
|
|
calcTokenAmount(approveValue, selectedFromToken.decimals).toFixed(9);
|
|
|
|
const approveGas = approveTxParams?.gas;
|
2020-10-06 20:28:38 +02:00
|
|
|
|
|
|
|
const renderablePopoverData = useMemo(() => {
|
|
|
|
return quotesToRenderableData(
|
|
|
|
quotes,
|
2021-08-03 00:52:18 +02:00
|
|
|
networkAndAccountSupports1559 ? baseAndPriorityFeePerGas : gasPrice,
|
2020-10-06 20:28:38 +02:00
|
|
|
conversionRate,
|
|
|
|
currentCurrency,
|
|
|
|
approveGas,
|
|
|
|
memoizedTokenConversionRates,
|
2021-03-18 11:20:06 +01:00
|
|
|
chainId,
|
2021-02-04 19:15:23 +01:00
|
|
|
);
|
2020-10-06 20:28:38 +02:00
|
|
|
}, [
|
|
|
|
quotes,
|
|
|
|
gasPrice,
|
2021-07-30 13:35:30 +02:00
|
|
|
baseAndPriorityFeePerGas,
|
2021-08-03 00:52:18 +02:00
|
|
|
networkAndAccountSupports1559,
|
2020-10-06 20:28:38 +02:00
|
|
|
conversionRate,
|
|
|
|
currentCurrency,
|
|
|
|
approveGas,
|
|
|
|
memoizedTokenConversionRates,
|
2021-03-18 11:20:06 +01:00
|
|
|
chainId,
|
2021-02-04 19:15:23 +01:00
|
|
|
]);
|
2020-10-06 20:28:38 +02:00
|
|
|
|
|
|
|
const renderableDataForUsedQuote = renderablePopoverData.find(
|
2020-11-03 00:41:28 +01:00
|
|
|
(renderablePopoverDatum) =>
|
|
|
|
renderablePopoverDatum.aggId === usedQuote.aggregator,
|
2021-02-04 19:15:23 +01:00
|
|
|
);
|
2020-10-06 20:28:38 +02:00
|
|
|
|
|
|
|
const {
|
|
|
|
destinationTokenDecimals,
|
|
|
|
destinationTokenSymbol,
|
|
|
|
destinationTokenValue,
|
2020-12-08 17:47:53 +01:00
|
|
|
destinationIconUrl,
|
2020-10-06 20:28:38 +02:00
|
|
|
sourceTokenDecimals,
|
|
|
|
sourceTokenSymbol,
|
|
|
|
sourceTokenValue,
|
2020-12-08 17:47:53 +01:00
|
|
|
sourceTokenIconUrl,
|
2021-02-04 19:15:23 +01:00
|
|
|
} = renderableDataForUsedQuote;
|
2020-10-06 20:28:38 +02:00
|
|
|
|
2021-03-18 11:20:06 +01:00
|
|
|
const { feeInFiat, feeInEth } = getRenderableNetworkFeesForQuote({
|
|
|
|
tradeGas: usedGasLimit,
|
2020-10-06 20:28:38 +02:00
|
|
|
approveGas,
|
2021-08-03 00:52:18 +02:00
|
|
|
gasPrice: networkAndAccountSupports1559
|
|
|
|
? baseAndPriorityFeePerGas
|
|
|
|
: gasPrice,
|
2020-10-06 20:28:38 +02:00
|
|
|
currentCurrency,
|
|
|
|
conversionRate,
|
2020-10-27 20:02:21 +01:00
|
|
|
tradeValue,
|
2021-03-18 11:20:06 +01:00
|
|
|
sourceSymbol: sourceTokenSymbol,
|
|
|
|
sourceAmount: usedQuote.sourceAmount,
|
|
|
|
chainId,
|
2021-03-29 21:30:06 +02:00
|
|
|
nativeCurrencySymbol,
|
2021-03-18 11:20:06 +01:00
|
|
|
});
|
2020-10-06 20:28:38 +02:00
|
|
|
|
|
|
|
const {
|
|
|
|
feeInFiat: maxFeeInFiat,
|
|
|
|
feeInEth: maxFeeInEth,
|
2020-10-27 20:02:21 +01:00
|
|
|
nonGasFee,
|
2021-03-18 11:20:06 +01:00
|
|
|
} = getRenderableNetworkFeesForQuote({
|
|
|
|
tradeGas: maxGasLimit,
|
2020-10-06 20:28:38 +02:00
|
|
|
approveGas,
|
2021-08-03 00:52:18 +02:00
|
|
|
gasPrice: networkAndAccountSupports1559 ? maxFeePerGas : gasPrice,
|
2020-10-06 20:28:38 +02:00
|
|
|
currentCurrency,
|
|
|
|
conversionRate,
|
2020-10-27 20:02:21 +01:00
|
|
|
tradeValue,
|
2021-03-18 11:20:06 +01:00
|
|
|
sourceSymbol: sourceTokenSymbol,
|
|
|
|
sourceAmount: usedQuote.sourceAmount,
|
|
|
|
chainId,
|
2021-03-29 21:30:06 +02:00
|
|
|
nativeCurrencySymbol,
|
2021-03-18 11:20:06 +01:00
|
|
|
});
|
2020-10-06 20:28:38 +02:00
|
|
|
|
2021-02-04 19:15:23 +01:00
|
|
|
const tokenCost = new BigNumber(usedQuote.sourceAmount);
|
2020-11-03 00:41:28 +01:00
|
|
|
const ethCost = new BigNumber(usedQuote.trade.value || 0, 10).plus(
|
|
|
|
new BigNumber(gasTotalInWeiHex, 16),
|
2021-02-04 19:15:23 +01:00
|
|
|
);
|
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) &&
|
2021-02-04 19:15:23 +01:00
|
|
|
tokenCost.gt(new BigNumber(selectedFromToken.balance || '0x0'));
|
2020-10-06 20:28:38 +02:00
|
|
|
|
2021-02-04 19:15:23 +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,
|
|
|
|
)
|
2021-02-04 19:15:23 +01:00
|
|
|
: null;
|
2020-10-06 20:28:38 +02:00
|
|
|
|
|
|
|
const ethBalanceNeeded = insufficientEth
|
|
|
|
? toPrecisionWithoutTrailingZeros(
|
2020-11-03 00:41:28 +01:00
|
|
|
ethCost
|
|
|
|
.minus(ethBalance, 16)
|
|
|
|
.div('1000000000000000000', 10)
|
|
|
|
.toString(10),
|
|
|
|
6,
|
|
|
|
)
|
2021-02-04 19:15:23 +01:00
|
|
|
: null;
|
2020-10-06 20:28:38 +02:00
|
|
|
|
2021-12-07 18:33:13 +01:00
|
|
|
const destinationToken = useSelector(getDestinationTokenInfo, isEqual);
|
2020-10-06 20:28:38 +02:00
|
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
if (insufficientTokens || insufficientEth) {
|
2021-02-04 19:15:23 +01:00
|
|
|
dispatch(setBalanceError(true));
|
2020-10-06 20:28:38 +02:00
|
|
|
} else if (balanceError && !insufficientTokens && !insufficientEth) {
|
2021-02-04 19:15:23 +01:00
|
|
|
dispatch(setBalanceError(false));
|
2020-10-06 20:28:38 +02:00
|
|
|
}
|
2021-02-04 19:15:23 +01:00
|
|
|
}, [insufficientTokens, insufficientEth, balanceError, dispatch]);
|
2020-10-06 20:28:38 +02:00
|
|
|
|
|
|
|
useEffect(() => {
|
2021-02-04 19:15:23 +01:00
|
|
|
const currentTime = Date.now();
|
|
|
|
const timeSinceLastFetched = currentTime - quotesLastFetched;
|
2020-12-15 21:24:22 +01:00
|
|
|
if (
|
|
|
|
timeSinceLastFetched > swapsQuoteRefreshTime &&
|
|
|
|
!dispatchedSafeRefetch
|
|
|
|
) {
|
2021-02-04 19:15:23 +01:00
|
|
|
setDispatchedSafeRefetch(true);
|
|
|
|
dispatch(safeRefetchQuotes());
|
2020-12-15 21:24:22 +01:00
|
|
|
} else if (timeSinceLastFetched > swapsQuoteRefreshTime) {
|
2021-02-04 19:15:23 +01:00
|
|
|
dispatch(setSwapsErrorKey(QUOTES_EXPIRED_ERROR));
|
|
|
|
history.push(SWAPS_ERROR_ROUTE);
|
2020-10-06 20:28:38 +02:00
|
|
|
}
|
2020-12-15 21:24:22 +01:00
|
|
|
}, [
|
|
|
|
quotesLastFetched,
|
|
|
|
dispatchedSafeRefetch,
|
|
|
|
dispatch,
|
|
|
|
history,
|
|
|
|
swapsQuoteRefreshTime,
|
2021-02-04 19:15:23 +01:00
|
|
|
]);
|
2020-10-06 20:28:38 +02:00
|
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
if (!originalApproveAmount && approveAmount) {
|
2021-02-04 19:15:23 +01:00
|
|
|
setOriginalApproveAmount(approveAmount);
|
2020-10-06 20:28:38 +02:00
|
|
|
}
|
2021-02-04 19:15:23 +01:00
|
|
|
}, [originalApproveAmount, approveAmount]);
|
2020-10-06 20:28:38 +02:00
|
|
|
|
2020-12-02 22:27:45 +01:00
|
|
|
const showInsufficientWarning =
|
2021-02-04 19:15:23 +01:00
|
|
|
(balanceError || tokenBalanceNeeded || ethBalanceNeeded) && !warningHidden;
|
2020-10-06 20:28:38 +02:00
|
|
|
|
2021-09-15 15:13:18 +02:00
|
|
|
const hardwareWalletUsed = useSelector(isHardwareWallet);
|
|
|
|
const hardwareWalletType = useSelector(getHardwareWalletType);
|
|
|
|
|
2021-02-04 19:15:23 +01:00
|
|
|
const numberOfQuotes = Object.values(quotes).length;
|
|
|
|
const bestQuoteReviewedEventSent = useRef();
|
2020-10-06 20:28:38 +02:00
|
|
|
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,
|
2021-09-15 15:13:18 +02:00
|
|
|
is_hardware_wallet: hardwareWalletUsed,
|
|
|
|
hardware_wallet_type: hardwareWalletType,
|
2021-02-04 19:15:23 +01:00
|
|
|
};
|
2020-10-06 20:28:38 +02:00
|
|
|
|
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
|
|
|
},
|
2021-02-04 19:15:23 +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
|
|
|
},
|
2021-02-04 19:15:23 +01: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
|
|
|
},
|
2021-02-04 19:15:23 +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',
|
2021-05-13 21:26:08 +02:00
|
|
|
sensitiveProperties: {
|
|
|
|
...eventObjectBase,
|
|
|
|
network_fees: feeInFiat,
|
|
|
|
},
|
2021-02-04 19:15:23 +01:00
|
|
|
});
|
2021-09-15 15:13:18 +02:00
|
|
|
|
|
|
|
const viewQuotePageLoadedEvent = useNewMetricEvent({
|
|
|
|
event: 'View Quote Page Loaded',
|
|
|
|
category: 'swaps',
|
|
|
|
sensitiveProperties: {
|
|
|
|
...eventObjectBase,
|
|
|
|
response_time: currentTimestamp - reviewSwapClickedTimestamp,
|
|
|
|
},
|
|
|
|
});
|
|
|
|
|
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)
|
|
|
|
) {
|
2021-02-04 19:15:23 +01:00
|
|
|
bestQuoteReviewedEventSent.current = true;
|
|
|
|
bestQuoteReviewedEvent();
|
2020-10-06 20:28:38 +02:00
|
|
|
}
|
2020-11-03 00:41:28 +01:00
|
|
|
}, [
|
|
|
|
sourceTokenSymbol,
|
|
|
|
sourceTokenValue,
|
|
|
|
destinationTokenSymbol,
|
|
|
|
destinationTokenValue,
|
|
|
|
fetchParams,
|
|
|
|
topQuote,
|
|
|
|
numberOfQuotes,
|
|
|
|
feeInFiat,
|
|
|
|
bestQuoteReviewedEvent,
|
2021-02-04 19:15:23 +01:00
|
|
|
]);
|
2020-10-06 20:28:38 +02:00
|
|
|
|
2021-02-04 19:15:23 +01:00
|
|
|
const metaMaskFee = usedQuote.fee;
|
2020-10-20 01:28:48 +02:00
|
|
|
|
2020-10-14 19:06:17 +02:00
|
|
|
const onFeeCardTokenApprovalClick = () => {
|
2021-02-04 19:15:23 +01: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
|
2021-02-04 19:15:23 +01:00
|
|
|
: newCustomPermissionAmount;
|
2020-11-03 00:41:28 +01:00
|
|
|
const newData = getCustomTxParamsData(approveTxParams.data, {
|
|
|
|
customPermissionAmount,
|
|
|
|
decimals: selectedFromToken.decimals,
|
2021-02-04 19:15:23 +01:00
|
|
|
});
|
2020-11-03 00:41:28 +01:00
|
|
|
|
|
|
|
if (
|
|
|
|
customPermissionAmount?.length &&
|
|
|
|
approveTxParams.data !== newData
|
|
|
|
) {
|
2021-02-04 19:15:23 +01:00
|
|
|
dispatch(setCustomApproveTxData(newData));
|
2020-11-03 00:41:28 +01:00
|
|
|
}
|
|
|
|
},
|
|
|
|
tokenAmount: originalApproveAmount,
|
|
|
|
customTokenAmount:
|
|
|
|
originalApproveAmount === approveAmount ? null : approveAmount,
|
|
|
|
tokenBalance,
|
|
|
|
tokenSymbol: selectedFromToken.symbol,
|
|
|
|
requiredMinimum: calcTokenAmount(
|
|
|
|
usedQuote.sourceAmount,
|
|
|
|
selectedFromToken.decimals,
|
|
|
|
),
|
|
|
|
}),
|
2021-02-04 19:15:23 +01:00
|
|
|
);
|
|
|
|
};
|
2020-10-06 20:28:38 +02:00
|
|
|
|
2021-02-04 19:15:23 +01:00
|
|
|
const nonGasFeeIsPositive = new BigNumber(nonGasFee, 16).gt(0);
|
2021-07-30 13:35:30 +02:00
|
|
|
const approveGasTotal = calcGasTotal(
|
|
|
|
approveGas || '0x0',
|
2021-08-03 00:52:18 +02:00
|
|
|
networkAndAccountSupports1559 ? baseAndPriorityFeePerGas : gasPrice,
|
2021-07-30 13:35:30 +02:00
|
|
|
);
|
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)
|
2021-02-04 19:15:23 +01:00
|
|
|
.toString(16);
|
2020-10-27 20:02:21 +01:00
|
|
|
const extraNetworkFeeTotalInEth = getValueFromWeiHex({
|
|
|
|
value: extraNetworkFeeTotalInHexWEI,
|
|
|
|
toDenomination: 'ETH',
|
|
|
|
numberOfDecimals: 4,
|
2021-02-04 19:15:23 +01:00
|
|
|
});
|
2020-10-27 20:02:21 +01:00
|
|
|
|
2021-02-04 19:15:23 +01:00
|
|
|
let extraInfoRowLabel = '';
|
2020-10-27 20:02:21 +01:00
|
|
|
if (approveGas && nonGasFeeIsPositive) {
|
2021-02-04 19:15:23 +01:00
|
|
|
extraInfoRowLabel = t('approvalAndAggregatorTxFeeCost');
|
2020-10-27 20:02:21 +01:00
|
|
|
} else if (approveGas) {
|
2021-02-04 19:15:23 +01:00
|
|
|
extraInfoRowLabel = t('approvalTxGasCost');
|
2020-10-27 20:02:21 +01:00
|
|
|
} else if (nonGasFeeIsPositive) {
|
2021-02-04 19:15:23 +01:00
|
|
|
extraInfoRowLabel = t('aggregatorFeeCost');
|
2020-10-27 20:02:21 +01:00
|
|
|
}
|
|
|
|
|
2021-07-30 13:35:30 +02:00
|
|
|
const onFeeCardMaxRowClick = () => {
|
2021-08-03 00:52:18 +02:00
|
|
|
networkAndAccountSupports1559
|
2021-07-30 13:35:30 +02:00
|
|
|
? setShowEditGasPopover(true)
|
|
|
|
: dispatch(
|
|
|
|
showModal({
|
|
|
|
name: 'CUSTOMIZE_METASWAP_GAS',
|
|
|
|
value: tradeValue,
|
|
|
|
customGasLimitMessage: approveGas
|
|
|
|
? t('extraApprovalGas', [hexToDecimal(approveGas)])
|
|
|
|
: '',
|
|
|
|
customTotalSupplement: approveGasTotal,
|
|
|
|
extraInfoRow: extraInfoRowLabel
|
|
|
|
? {
|
|
|
|
label: extraInfoRowLabel,
|
|
|
|
value: `${extraNetworkFeeTotalInEth} ${nativeCurrencySymbol}`,
|
|
|
|
}
|
|
|
|
: null,
|
|
|
|
initialGasPrice: gasPrice,
|
|
|
|
initialGasLimit: maxGasLimit,
|
|
|
|
minimumGasLimit: new BigNumber(nonCustomMaxGasLimit, 16).toNumber(),
|
|
|
|
}),
|
|
|
|
);
|
|
|
|
};
|
2020-10-06 20:28:38 +02:00
|
|
|
|
2021-02-17 15:49:24 +01:00
|
|
|
const actionableBalanceErrorMessage = tokenBalanceUnavailable
|
|
|
|
? t('swapTokenBalanceUnavailable', [sourceTokenSymbol])
|
|
|
|
: t('swapApproveNeedMoreTokens', [
|
|
|
|
<span key="swapApproveNeedMoreTokens-1" className="view-quote__bold">
|
|
|
|
{tokenBalanceNeeded || ethBalanceNeeded}
|
|
|
|
</span>,
|
2021-03-18 11:20:06 +01:00
|
|
|
tokenBalanceNeeded && !(sourceTokenSymbol === defaultSwapsToken.symbol)
|
2021-02-17 15:49:24 +01:00
|
|
|
? sourceTokenSymbol
|
2021-03-18 11:20:06 +01:00
|
|
|
: defaultSwapsToken.symbol,
|
2021-02-17 15:49:24 +01:00
|
|
|
]);
|
2020-10-06 20:28:38 +02:00
|
|
|
|
2021-02-04 16:58:46 +01:00
|
|
|
// Price difference warning
|
2021-02-05 21:40:40 +01:00
|
|
|
const priceSlippageBucket = usedQuote?.priceSlippage?.bucket;
|
|
|
|
const lastPriceDifferenceBucket = usePrevious(priceSlippageBucket);
|
|
|
|
|
|
|
|
// If the user agreed to a different bucket of risk, make them agree again
|
|
|
|
useEffect(() => {
|
|
|
|
if (
|
|
|
|
acknowledgedPriceDifference &&
|
2021-11-06 01:59:23 +01:00
|
|
|
lastPriceDifferenceBucket === GAS_RECOMMENDATIONS.MEDIUM &&
|
|
|
|
priceSlippageBucket === GAS_RECOMMENDATIONS.HIGH
|
2021-02-05 21:40:40 +01:00
|
|
|
) {
|
|
|
|
setAcknowledgedPriceDifference(false);
|
|
|
|
}
|
|
|
|
}, [
|
|
|
|
priceSlippageBucket,
|
|
|
|
acknowledgedPriceDifference,
|
|
|
|
lastPriceDifferenceBucket,
|
|
|
|
]);
|
|
|
|
|
2021-02-04 19:15:23 +01:00
|
|
|
let viewQuotePriceDifferenceComponent = null;
|
2021-02-04 16:58:46 +01:00
|
|
|
const priceSlippageFromSource = useEthFiatAmount(
|
|
|
|
usedQuote?.priceSlippage?.sourceAmountInETH || 0,
|
2021-03-30 17:25:29 +02:00
|
|
|
{ showFiat: true },
|
2021-02-04 19:15:23 +01:00
|
|
|
);
|
2021-02-04 16:58:46 +01:00
|
|
|
const priceSlippageFromDestination = useEthFiatAmount(
|
2021-02-23 17:52:04 +01:00
|
|
|
usedQuote?.priceSlippage?.destinationAmountInETH || 0,
|
2021-03-30 17:25:29 +02:00
|
|
|
{ showFiat: true },
|
2021-02-04 19:15:23 +01:00
|
|
|
);
|
2020-12-02 22:27:45 +01:00
|
|
|
|
2021-02-04 16:58:46 +01:00
|
|
|
// We cannot present fiat value if there is a calculation error or no slippage
|
|
|
|
// from source or destination
|
|
|
|
const priceSlippageUnknownFiatValue =
|
|
|
|
!priceSlippageFromSource ||
|
|
|
|
!priceSlippageFromDestination ||
|
2021-02-04 19:15:23 +01:00
|
|
|
usedQuote?.priceSlippage?.calculationError;
|
2021-02-04 16:58:46 +01:00
|
|
|
|
2021-02-04 19:15:23 +01:00
|
|
|
let priceDifferencePercentage = 0;
|
2021-02-04 16:58:46 +01:00
|
|
|
if (usedQuote?.priceSlippage?.ratio) {
|
|
|
|
priceDifferencePercentage = parseFloat(
|
|
|
|
new BigNumber(usedQuote.priceSlippage.ratio, 10)
|
|
|
|
.minus(1, 10)
|
|
|
|
.times(100, 10)
|
|
|
|
.toFixed(2),
|
|
|
|
10,
|
2021-02-04 19:15:23 +01:00
|
|
|
);
|
2021-02-04 16:58:46 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
const shouldShowPriceDifferenceWarning =
|
2021-02-17 15:49:24 +01:00
|
|
|
!tokenBalanceUnavailable &&
|
2021-02-04 16:58:46 +01:00
|
|
|
!showInsufficientWarning &&
|
|
|
|
usedQuote &&
|
2021-02-05 21:40:40 +01:00
|
|
|
(priceDifferenceRiskyBuckets.includes(priceSlippageBucket) ||
|
2021-02-04 19:15:23 +01:00
|
|
|
priceSlippageUnknownFiatValue);
|
2021-02-04 16:58:46 +01:00
|
|
|
|
|
|
|
if (shouldShowPriceDifferenceWarning) {
|
|
|
|
viewQuotePriceDifferenceComponent = (
|
|
|
|
<ViewQuotePriceDifference
|
|
|
|
usedQuote={usedQuote}
|
|
|
|
sourceTokenValue={sourceTokenValue}
|
|
|
|
destinationTokenValue={destinationTokenValue}
|
|
|
|
priceSlippageFromSource={priceSlippageFromSource}
|
|
|
|
priceSlippageFromDestination={priceSlippageFromDestination}
|
|
|
|
priceDifferencePercentage={priceDifferencePercentage}
|
|
|
|
priceSlippageUnknownFiatValue={priceSlippageUnknownFiatValue}
|
|
|
|
onAcknowledgementClick={() => {
|
2021-02-04 19:15:23 +01:00
|
|
|
setAcknowledgedPriceDifference(true);
|
2021-02-04 16:58:46 +01:00
|
|
|
}}
|
|
|
|
acknowledged={acknowledgedPriceDifference}
|
|
|
|
/>
|
2021-02-04 19:15:23 +01:00
|
|
|
);
|
2021-02-04 16:58:46 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
const disableSubmissionDueToPriceWarning =
|
2021-02-04 19:15:23 +01:00
|
|
|
shouldShowPriceDifferenceWarning && !acknowledgedPriceDifference;
|
2021-02-04 16:58:46 +01:00
|
|
|
|
2020-12-02 22:27:45 +01:00
|
|
|
const isShowingWarning =
|
2021-02-04 19:15:23 +01:00
|
|
|
showInsufficientWarning || shouldShowPriceDifferenceWarning;
|
2020-12-02 22:27:45 +01:00
|
|
|
|
2021-07-30 13:35:30 +02:00
|
|
|
const onCloseEditGasPopover = () => {
|
|
|
|
setShowEditGasPopover(false);
|
|
|
|
};
|
|
|
|
|
2021-09-15 15:13:18 +02:00
|
|
|
useEffect(() => {
|
|
|
|
// Thanks to the next line we will only do quotes polling 3 times before showing a Quote Timeout modal.
|
|
|
|
dispatch(setSwapsQuotesPollingLimitEnabled(true));
|
|
|
|
if (reviewSwapClickedTimestamp) {
|
|
|
|
viewQuotePageLoadedEvent();
|
|
|
|
}
|
|
|
|
}, [dispatch, viewQuotePageLoadedEvent, reviewSwapClickedTimestamp]);
|
|
|
|
|
2021-12-12 00:26:28 +01:00
|
|
|
const transaction = {
|
|
|
|
userFeeLevel: swapsUserFeeLevel || GAS_RECOMMENDATIONS.HIGH,
|
|
|
|
txParams: {
|
|
|
|
maxFeePerGas,
|
|
|
|
maxPriorityFeePerGas,
|
|
|
|
gas: maxGasLimit,
|
|
|
|
},
|
|
|
|
};
|
|
|
|
|
|
|
|
const supportsEIP1559V2 =
|
|
|
|
EIP_1559_V2_ENABLED && networkAndAccountSupports1559;
|
|
|
|
|
2020-10-06 20:28:38 +02:00
|
|
|
return (
|
2021-12-12 00:26:28 +01:00
|
|
|
<GasFeeContextProvider
|
|
|
|
editGasMode={EDIT_GAS_MODES.SWAPS}
|
|
|
|
minimumGasLimit={usedGasLimit}
|
|
|
|
transaction={transaction}
|
|
|
|
>
|
|
|
|
<TransactionModalContextProvider captureEventEnabled={false}>
|
|
|
|
<div className="view-quote">
|
|
|
|
<div
|
|
|
|
className={classnames('view-quote__content', {
|
|
|
|
'view-quote__content_modal': disableSubmissionDueToPriceWarning,
|
|
|
|
})}
|
|
|
|
>
|
|
|
|
{selectQuotePopoverShown && (
|
|
|
|
<SelectQuotePopover
|
|
|
|
quoteDataRows={renderablePopoverData}
|
|
|
|
onClose={() => setSelectQuotePopoverShown(false)}
|
|
|
|
onSubmit={(aggId) => dispatch(swapsQuoteSelected(aggId))}
|
|
|
|
swapToSymbol={destinationTokenSymbol}
|
|
|
|
initialAggId={usedQuote.aggregator}
|
|
|
|
onQuoteDetailsIsOpened={quoteDetailsOpened}
|
|
|
|
/>
|
|
|
|
)}
|
|
|
|
|
|
|
|
{!supportsEIP1559V2 &&
|
|
|
|
showEditGasPopover &&
|
|
|
|
networkAndAccountSupports1559 && (
|
|
|
|
<EditGasPopover
|
|
|
|
transaction={transaction}
|
|
|
|
minimumGasLimit={usedGasLimit}
|
|
|
|
defaultEstimateToUse={GAS_RECOMMENDATIONS.HIGH}
|
|
|
|
mode={EDIT_GAS_MODES.SWAPS}
|
|
|
|
confirmButtonText={t('submit')}
|
|
|
|
onClose={onCloseEditGasPopover}
|
|
|
|
/>
|
|
|
|
)}
|
|
|
|
{supportsEIP1559V2 && (
|
|
|
|
<>
|
|
|
|
<EditGasFeePopover />
|
|
|
|
<AdvancedGasFeePopover />
|
|
|
|
</>
|
|
|
|
)}
|
|
|
|
|
|
|
|
<div
|
|
|
|
className={classnames('view-quote__warning-wrapper', {
|
|
|
|
'view-quote__warning-wrapper--thin': !isShowingWarning,
|
|
|
|
})}
|
|
|
|
>
|
|
|
|
{viewQuotePriceDifferenceComponent}
|
|
|
|
{(showInsufficientWarning || tokenBalanceUnavailable) && (
|
|
|
|
<ActionableMessage
|
|
|
|
message={actionableBalanceErrorMessage}
|
|
|
|
onClose={() => setWarningHidden(true)}
|
|
|
|
/>
|
|
|
|
)}
|
|
|
|
</div>
|
|
|
|
<div className="view-quote__countdown-timer-container">
|
|
|
|
<CountdownTimer
|
|
|
|
timeStarted={quotesLastFetched}
|
|
|
|
warningTime="0:30"
|
|
|
|
labelKey="swapNewQuoteIn"
|
|
|
|
/>
|
|
|
|
</div>
|
|
|
|
<MainQuoteSummary
|
|
|
|
sourceValue={calcTokenValue(
|
|
|
|
sourceTokenValue,
|
|
|
|
sourceTokenDecimals,
|
|
|
|
)}
|
|
|
|
sourceDecimals={sourceTokenDecimals}
|
|
|
|
sourceSymbol={sourceTokenSymbol}
|
|
|
|
destinationValue={calcTokenValue(
|
|
|
|
destinationTokenValue,
|
|
|
|
destinationTokenDecimals,
|
|
|
|
)}
|
|
|
|
destinationDecimals={destinationTokenDecimals}
|
|
|
|
destinationSymbol={destinationTokenSymbol}
|
|
|
|
sourceIconUrl={sourceTokenIconUrl}
|
|
|
|
destinationIconUrl={destinationIconUrl}
|
2020-10-06 20:28:38 +02:00
|
|
|
/>
|
2021-12-12 00:26:28 +01:00
|
|
|
<div
|
|
|
|
className={classnames('view-quote__fee-card-container', {
|
|
|
|
'view-quote__fee-card-container--three-rows':
|
|
|
|
approveTxParams && (!balanceError || warningHidden),
|
|
|
|
})}
|
|
|
|
>
|
|
|
|
<FeeCard
|
|
|
|
primaryFee={{
|
|
|
|
fee: feeInEth,
|
|
|
|
maxFee: maxFeeInEth,
|
|
|
|
}}
|
|
|
|
secondaryFee={{
|
|
|
|
fee: feeInFiat,
|
|
|
|
maxFee: maxFeeInFiat,
|
|
|
|
}}
|
|
|
|
onFeeCardMaxRowClick={onFeeCardMaxRowClick}
|
|
|
|
hideTokenApprovalRow={
|
|
|
|
!approveTxParams || (balanceError && !warningHidden)
|
|
|
|
}
|
|
|
|
tokenApprovalSourceTokenSymbol={sourceTokenSymbol}
|
|
|
|
onTokenApprovalClick={onFeeCardTokenApprovalClick}
|
|
|
|
metaMaskFee={String(metaMaskFee)}
|
|
|
|
numberOfQuotes={Object.values(quotes).length}
|
|
|
|
onQuotesClick={() => {
|
|
|
|
allAvailableQuotesOpened();
|
|
|
|
setSelectQuotePopoverShown(true);
|
|
|
|
}}
|
|
|
|
chainId={chainId}
|
|
|
|
isBestQuote={isBestQuote}
|
|
|
|
supportsEIP1559V2={supportsEIP1559V2}
|
|
|
|
/>
|
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
<SwapsFooter
|
|
|
|
onSubmit={() => {
|
|
|
|
setSubmitClicked(true);
|
|
|
|
if (!balanceError) {
|
|
|
|
dispatch(signAndSendTransactions(history, metaMetricsEvent));
|
|
|
|
} else if (destinationToken.symbol === defaultSwapsToken.symbol) {
|
|
|
|
history.push(DEFAULT_ROUTE);
|
|
|
|
} else {
|
|
|
|
history.push(`${ASSET_ROUTE}/${destinationToken.address}`);
|
|
|
|
}
|
2020-11-03 00:41:28 +01:00
|
|
|
}}
|
2021-12-12 00:26:28 +01:00
|
|
|
submitText={t('swap')}
|
|
|
|
hideCancel
|
|
|
|
disabled={
|
|
|
|
submitClicked ||
|
|
|
|
balanceError ||
|
|
|
|
tokenBalanceUnavailable ||
|
|
|
|
disableSubmissionDueToPriceWarning ||
|
|
|
|
(networkAndAccountSupports1559 &&
|
|
|
|
baseAndPriorityFeePerGas === undefined) ||
|
|
|
|
(!networkAndAccountSupports1559 &&
|
|
|
|
(gasPrice === null || gasPrice === undefined))
|
2020-10-14 19:06:17 +02:00
|
|
|
}
|
2020-10-14 19:12:35 +02:00
|
|
|
tokenApprovalSourceTokenSymbol={sourceTokenSymbol}
|
|
|
|
onTokenApprovalClick={onFeeCardTokenApprovalClick}
|
2020-12-11 19:03:51 +01:00
|
|
|
metaMaskFee={String(metaMaskFee)}
|
2020-12-08 17:47:53 +01:00
|
|
|
numberOfQuotes={Object.values(quotes).length}
|
|
|
|
onQuotesClick={() => {
|
2021-02-04 19:15:23 +01:00
|
|
|
allAvailableQuotesOpened();
|
|
|
|
setSelectQuotePopoverShown(true);
|
2020-12-08 17:47:53 +01:00
|
|
|
}}
|
2021-04-16 23:52:32 +02:00
|
|
|
chainId={chainId}
|
2021-12-07 04:51:26 +01:00
|
|
|
isBestQuote={isBestQuote}
|
2020-10-06 20:28:38 +02:00
|
|
|
/>
|
|
|
|
</div>
|
2021-12-12 00:26:28 +01:00
|
|
|
</TransactionModalContextProvider>
|
|
|
|
</GasFeeContextProvider>
|
2021-02-04 19:15:23 +01:00
|
|
|
);
|
2020-10-06 20:28:38 +02:00
|
|
|
}
|