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,
|
2022-02-18 17:48:38 +01:00
|
|
|
getSmartTransactionsOptInStatus,
|
|
|
|
signAndSendSwapsSmartTransaction,
|
|
|
|
getSwapsRefreshStates,
|
|
|
|
getSmartTransactionsEnabled,
|
|
|
|
getCurrentSmartTransactionsError,
|
|
|
|
getCurrentSmartTransactionsErrorMessageDismissed,
|
|
|
|
getSwapsSTXLoading,
|
|
|
|
estimateSwapsSmartTransactionsGas,
|
|
|
|
getSmartTransactionEstimatedGas,
|
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,
|
2022-01-11 20:17:56 +01:00
|
|
|
getEIP1559V2Enabled,
|
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
|
|
|
|
2022-03-07 19:54:36 +01:00
|
|
|
import { toPrecisionWithoutTrailingZeros } 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';
|
2022-01-10 17:23:53 +01:00
|
|
|
import { getTransactionData } 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,
|
2022-02-18 17:48:38 +01:00
|
|
|
hexWEIToDecGWEI,
|
2021-07-30 13:35:30 +02:00
|
|
|
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,
|
2022-02-18 17:48:38 +01:00
|
|
|
getFeeForSmartTransaction,
|
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';
|
2022-02-18 17:48:38 +01:00
|
|
|
import PulseLoader from '../../../components/ui/pulse-loader'; // TODO: Replace this with a different loading component.
|
|
|
|
import Box from '../../../components/ui/box';
|
2022-03-07 19:54:36 +01:00
|
|
|
import { isEqualCaseInsensitive } from '../../../../shared/modules/string-utils';
|
2021-02-04 19:15:23 +01:00
|
|
|
import ViewQuotePriceDifference from './view-quote-price-difference';
|
2020-10-06 20:28:38 +02:00
|
|
|
|
2022-02-18 17:48:38 +01:00
|
|
|
let intervalId;
|
|
|
|
|
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);
|
2022-01-11 20:17:56 +01:00
|
|
|
const eip1559V2Enabled = useSelector(getEIP1559V2Enabled);
|
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);
|
2022-02-18 17:48:38 +01:00
|
|
|
const smartTransactionsOptInStatus = useSelector(
|
|
|
|
getSmartTransactionsOptInStatus,
|
|
|
|
);
|
|
|
|
const smartTransactionsEnabled = useSelector(getSmartTransactionsEnabled);
|
|
|
|
const swapsSTXLoading = useSelector(getSwapsSTXLoading);
|
|
|
|
const currentSmartTransactionsError = useSelector(
|
|
|
|
getCurrentSmartTransactionsError,
|
|
|
|
);
|
|
|
|
const currentSmartTransactionsErrorMessageDismissed = useSelector(
|
|
|
|
getCurrentSmartTransactionsErrorMessageDismissed,
|
|
|
|
);
|
|
|
|
const currentSmartTransactionsEnabled =
|
|
|
|
smartTransactionsEnabled &&
|
|
|
|
!(
|
|
|
|
currentSmartTransactionsError &&
|
|
|
|
(currentSmartTransactionsError !== 'not_enough_funds' ||
|
|
|
|
currentSmartTransactionsErrorMessageDismissed)
|
|
|
|
);
|
|
|
|
const smartTransactionEstimatedGas = useSelector(
|
|
|
|
getSmartTransactionEstimatedGas,
|
|
|
|
);
|
|
|
|
const swapsRefreshRates = useSelector(getSwapsRefreshStates);
|
|
|
|
const unsignedTransaction = usedQuote.trade;
|
|
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
if (currentSmartTransactionsEnabled && smartTransactionsOptInStatus) {
|
|
|
|
const unsignedTx = {
|
|
|
|
from: unsignedTransaction.from,
|
|
|
|
to: unsignedTransaction.to,
|
|
|
|
value: unsignedTransaction.value,
|
|
|
|
data: unsignedTransaction.data,
|
|
|
|
gas: unsignedTransaction.gas,
|
|
|
|
chainId,
|
|
|
|
};
|
|
|
|
intervalId = setInterval(() => {
|
|
|
|
dispatch(
|
|
|
|
estimateSwapsSmartTransactionsGas(unsignedTx, approveTxParams),
|
|
|
|
);
|
|
|
|
}, swapsRefreshRates.stxGetTransactionsRefreshTime);
|
|
|
|
dispatch(estimateSwapsSmartTransactionsGas(unsignedTx, approveTxParams));
|
|
|
|
} else if (intervalId) {
|
|
|
|
clearInterval(intervalId);
|
|
|
|
}
|
|
|
|
return () => clearInterval(intervalId);
|
|
|
|
// eslint-disable-next-line
|
|
|
|
}, [
|
|
|
|
dispatch,
|
|
|
|
currentSmartTransactionsEnabled,
|
|
|
|
smartTransactionsOptInStatus,
|
|
|
|
unsignedTransaction.data,
|
|
|
|
unsignedTransaction.from,
|
|
|
|
unsignedTransaction.value,
|
|
|
|
unsignedTransaction.gas,
|
|
|
|
unsignedTransaction.to,
|
|
|
|
chainId,
|
|
|
|
swapsRefreshRates.stxGetTransactionsRefreshTime,
|
|
|
|
]);
|
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)}`;
|
2022-02-18 17:48:38 +01:00
|
|
|
let 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;
|
|
|
|
|
2022-02-18 17:48:38 +01:00
|
|
|
// EIP-1559 gas fees.
|
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,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2022-02-18 17:48:38 +01:00
|
|
|
// Smart Transactions gas fees.
|
|
|
|
if (
|
|
|
|
currentSmartTransactionsEnabled &&
|
|
|
|
smartTransactionsOptInStatus &&
|
|
|
|
smartTransactionEstimatedGas?.txData
|
|
|
|
) {
|
|
|
|
maxGasLimit = `0x${decimalToHex(
|
|
|
|
smartTransactionEstimatedGas?.txData.gasLimit || 0,
|
|
|
|
)}`;
|
|
|
|
}
|
|
|
|
|
|
|
|
const gasTotalInWeiHex = calcGasTotal(maxGasLimit, maxFeePerGas || gasPrice);
|
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
|
|
|
|
2022-01-10 17:23:53 +01:00
|
|
|
const approveData = getTransactionData(approveTxParams?.data);
|
2021-02-04 19:15:23 +01:00
|
|
|
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,
|
2022-02-18 17:48:38 +01:00
|
|
|
smartTransactionsEnabled &&
|
|
|
|
smartTransactionsOptInStatus &&
|
|
|
|
smartTransactionEstimatedGas?.txData,
|
|
|
|
nativeCurrencySymbol,
|
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,
|
2022-02-18 17:48:38 +01:00
|
|
|
smartTransactionEstimatedGas?.txData,
|
|
|
|
nativeCurrencySymbol,
|
|
|
|
smartTransactionsEnabled,
|
|
|
|
smartTransactionsOptInStatus,
|
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
|
|
|
|
2022-02-18 17:48:38 +01:00
|
|
|
let { feeInFiat, feeInEth } = getRenderableNetworkFeesForQuote({
|
2021-03-18 11:20:06 +01:00
|
|
|
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
|
|
|
|
2022-02-18 17:48:38 +01:00
|
|
|
const renderableMaxFees = getRenderableNetworkFeesForQuote({
|
2021-03-18 11:20:06 +01:00
|
|
|
tradeGas: maxGasLimit,
|
2020-10-06 20:28:38 +02:00
|
|
|
approveGas,
|
2022-02-18 17:48:38 +01:00
|
|
|
gasPrice: 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
|
|
|
});
|
2022-02-18 17:48:38 +01:00
|
|
|
let { feeInFiat: maxFeeInFiat, feeInEth: maxFeeInEth } = renderableMaxFees;
|
|
|
|
const { nonGasFee } = renderableMaxFees;
|
|
|
|
|
|
|
|
if (
|
|
|
|
currentSmartTransactionsEnabled &&
|
|
|
|
smartTransactionsOptInStatus &&
|
|
|
|
smartTransactionEstimatedGas?.txData
|
|
|
|
) {
|
|
|
|
const stxEstimatedFeeInWeiDec =
|
|
|
|
smartTransactionEstimatedGas.txData.feeEstimate +
|
|
|
|
(smartTransactionEstimatedGas.approvalTxData?.feeEstimate || 0);
|
|
|
|
const stxMaxFeeInWeiDec = stxEstimatedFeeInWeiDec * 2;
|
|
|
|
({ feeInFiat, feeInEth } = getFeeForSmartTransaction({
|
|
|
|
chainId,
|
|
|
|
currentCurrency,
|
|
|
|
conversionRate,
|
|
|
|
nativeCurrencySymbol,
|
|
|
|
feeInWeiDec: stxEstimatedFeeInWeiDec,
|
|
|
|
}));
|
|
|
|
({
|
|
|
|
feeInFiat: maxFeeInFiat,
|
|
|
|
feeInEth: maxFeeInEth,
|
|
|
|
} = getFeeForSmartTransaction({
|
|
|
|
chainId,
|
|
|
|
currentCurrency,
|
|
|
|
conversionRate,
|
|
|
|
nativeCurrencySymbol,
|
|
|
|
feeInWeiDec: stxMaxFeeInWeiDec,
|
|
|
|
}));
|
|
|
|
}
|
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,
|
2022-02-18 17:48:38 +01:00
|
|
|
stx_enabled: currentSmartTransactionsEnabled,
|
|
|
|
current_stx_enabled: currentSmartTransactionsEnabled,
|
|
|
|
stx_user_opt_in: smartTransactionsOptInStatus,
|
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]);
|
|
|
|
|
2022-02-18 17:48:38 +01:00
|
|
|
useEffect(() => {
|
|
|
|
// if smart transaction error is turned off, reset submit clicked boolean
|
|
|
|
if (
|
|
|
|
!currentSmartTransactionsEnabled &&
|
|
|
|
currentSmartTransactionsError &&
|
|
|
|
submitClicked
|
|
|
|
) {
|
|
|
|
setSubmitClicked(false);
|
|
|
|
}
|
|
|
|
}, [
|
|
|
|
currentSmartTransactionsEnabled,
|
|
|
|
currentSmartTransactionsError,
|
|
|
|
submitClicked,
|
|
|
|
]);
|
|
|
|
|
2021-12-12 00:26:28 +01:00
|
|
|
const transaction = {
|
|
|
|
userFeeLevel: swapsUserFeeLevel || GAS_RECOMMENDATIONS.HIGH,
|
|
|
|
txParams: {
|
|
|
|
maxFeePerGas,
|
|
|
|
maxPriorityFeePerGas,
|
|
|
|
gas: maxGasLimit,
|
|
|
|
},
|
|
|
|
};
|
|
|
|
|
2022-01-11 20:17:56 +01:00
|
|
|
const supportsEIP1559V2 = eip1559V2Enabled && networkAndAccountSupports1559;
|
2021-12-12 00:26:28 +01:00
|
|
|
|
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}
|
|
|
|
>
|
2022-02-01 18:53:03 +01:00
|
|
|
<TransactionModalContextProvider>
|
2021-12-12 00:26:28 +01:00
|
|
|
<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}
|
2022-02-18 17:48:38 +01:00
|
|
|
hideEstimatedGasFee={
|
|
|
|
smartTransactionsEnabled && smartTransactionsOptInStatus
|
|
|
|
}
|
2021-12-12 00:26:28 +01:00
|
|
|
/>
|
|
|
|
)}
|
|
|
|
|
|
|
|
{!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
|
|
|
/>
|
2022-02-18 17:48:38 +01:00
|
|
|
{currentSmartTransactionsEnabled &&
|
|
|
|
smartTransactionsOptInStatus &&
|
|
|
|
!smartTransactionEstimatedGas?.txData && (
|
|
|
|
<Box marginTop={0} marginBottom={10}>
|
|
|
|
<PulseLoader />
|
|
|
|
</Box>
|
|
|
|
)}
|
|
|
|
{(!currentSmartTransactionsEnabled ||
|
|
|
|
!smartTransactionsOptInStatus ||
|
|
|
|
smartTransactionEstimatedGas?.txData) && (
|
|
|
|
<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}
|
|
|
|
networkAndAccountSupports1559={networkAndAccountSupports1559}
|
|
|
|
maxPriorityFeePerGasDecGWEI={hexWEIToDecGWEI(
|
|
|
|
maxPriorityFeePerGas,
|
|
|
|
)}
|
|
|
|
maxFeePerGasDecGWEI={hexWEIToDecGWEI(maxFeePerGas)}
|
|
|
|
smartTransactionsEnabled={currentSmartTransactionsEnabled}
|
|
|
|
smartTransactionsOptInStatus={smartTransactionsOptInStatus}
|
|
|
|
/>
|
|
|
|
</div>
|
|
|
|
)}
|
2021-12-12 00:26:28 +01:00
|
|
|
</div>
|
|
|
|
<SwapsFooter
|
|
|
|
onSubmit={() => {
|
|
|
|
setSubmitClicked(true);
|
|
|
|
if (!balanceError) {
|
2022-02-18 17:48:38 +01:00
|
|
|
if (
|
|
|
|
currentSmartTransactionsEnabled &&
|
|
|
|
smartTransactionsOptInStatus &&
|
|
|
|
smartTransactionEstimatedGas?.txData
|
|
|
|
) {
|
|
|
|
dispatch(
|
|
|
|
signAndSendSwapsSmartTransaction({
|
|
|
|
unsignedTransaction,
|
|
|
|
metaMetricsEvent,
|
|
|
|
history,
|
|
|
|
}),
|
|
|
|
);
|
|
|
|
} else {
|
|
|
|
dispatch(signAndSendTransactions(history, metaMetricsEvent));
|
|
|
|
}
|
2021-12-12 00:26:28 +01:00
|
|
|
} 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
|
|
|
}}
|
2022-02-18 17:48:38 +01:00
|
|
|
submitText={
|
|
|
|
currentSmartTransactionsEnabled &&
|
|
|
|
smartTransactionsOptInStatus &&
|
|
|
|
swapsSTXLoading
|
|
|
|
? t('preparingSwap')
|
|
|
|
: t('swap')
|
|
|
|
}
|
2021-12-12 00:26:28 +01:00
|
|
|
hideCancel
|
|
|
|
disabled={
|
|
|
|
submitClicked ||
|
|
|
|
balanceError ||
|
|
|
|
tokenBalanceUnavailable ||
|
|
|
|
disableSubmissionDueToPriceWarning ||
|
|
|
|
(networkAndAccountSupports1559 &&
|
|
|
|
baseAndPriorityFeePerGas === undefined) ||
|
|
|
|
(!networkAndAccountSupports1559 &&
|
2022-02-18 17:48:38 +01:00
|
|
|
(gasPrice === null || gasPrice === undefined)) ||
|
|
|
|
(currentSmartTransactionsEnabled && currentSmartTransactionsError)
|
2020-10-14 19:06:17 +02:00
|
|
|
}
|
2022-02-18 17:48:38 +01:00
|
|
|
className={isShowingWarning && 'view-quote__thin-swaps-footer'}
|
|
|
|
showTopBorder
|
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
|
|
|
}
|