1
0
mirror of https://github.com/kremalicious/metamask-extension.git synced 2024-11-23 02:10:12 +01:00
metamask-extension/ui/pages/swaps/index.js

543 lines
18 KiB
JavaScript
Raw Normal View History

import React, {
useEffect,
useRef,
useContext,
useState,
useCallback,
} from 'react';
import { shallowEqual, useDispatch, useSelector } from 'react-redux';
2020-11-03 00:41:28 +01:00
import {
Switch,
Route,
useLocation,
useHistory,
Redirect,
} from 'react-router-dom';
import { shuffle, isEqual } from 'lodash';
import { I18nContext } from '../../contexts/i18n';
2020-11-03 00:41:28 +01:00
import {
getSelectedAccount,
getCurrentChainId,
getIsSwapsChain,
isHardwareWallet,
getHardwareWalletType,
getTokenList,
} from '../../selectors/selectors';
2020-10-06 20:28:38 +02:00
import {
getQuotes,
clearSwapsState,
getTradeTxId,
getApproveTxId,
getFetchingQuotes,
setTopAssets,
getFetchParams,
setAggregatorMetadata,
getAggregatorMetadata,
getBackgroundSwapRouteState,
getSwapsErrorKey,
getSwapsFeatureIsLive,
2020-10-06 20:28:38 +02:00
prepareToLeaveSwaps,
fetchAndSetSwapsGasPriceInfo,
fetchSwapsLivenessAndFeatureFlags,
2021-09-15 15:13:18 +02:00
getReviewSwapClickedTimestamp,
getPendingSmartTransactions,
getSmartTransactionsOptInStatus,
getSmartTransactionsEnabled,
getCurrentSmartTransactionsEnabled,
getCurrentSmartTransactionsError,
navigateBackToBuildQuote,
} from '../../ducks/swaps/swaps';
import {
checkNetworkAndAccountSupports1559,
currentNetworkTxListSelector,
} from '../../selectors';
2020-10-06 20:28:38 +02:00
import {
AWAITING_SIGNATURES_ROUTE,
2020-10-06 20:28:38 +02:00
AWAITING_SWAP_ROUTE,
SMART_TRANSACTION_STATUS_ROUTE,
2020-10-06 20:28:38 +02:00
BUILD_QUOTE_ROUTE,
VIEW_QUOTE_ROUTE,
LOADING_QUOTES_ROUTE,
SWAPS_ERROR_ROUTE,
DEFAULT_ROUTE,
SWAPS_MAINTENANCE_ROUTE,
} from '../../helpers/constants/routes';
2020-10-06 20:28:38 +02:00
import {
ERROR_FETCHING_QUOTES,
QUOTES_NOT_AVAILABLE_ERROR,
SWAP_FAILED_ERROR,
CONTRACT_DATA_DISABLED_ERROR,
2020-10-06 20:28:38 +02:00
OFFLINE_FOR_MAINTENANCE,
} from '../../../shared/constants/swaps';
2020-10-06 20:28:38 +02:00
2020-11-03 00:41:28 +01:00
import {
resetBackgroundSwapsState,
setSwapsTokens,
ignoreTokens,
2020-11-03 00:41:28 +01:00
setBackgroundSwapRouteState,
setSwapsErrorKey,
} from '../../store/actions';
import { useGasFeeEstimates } from '../../hooks/useGasFeeEstimates';
import FeatureToggledRoute from '../../helpers/higher-order-components/feature-toggled-route';
import { MetaMetricsEventCategory } from '../../../shared/constants/metametrics';
import { TransactionStatus } from '../../../shared/constants/transaction';
import { MetaMetricsContext } from '../../contexts/metametrics';
import { getSwapsTokensReceivedFromTxMeta } from '../../../shared/lib/transactions-controller-utils';
2020-11-03 00:41:28 +01:00
import {
fetchTokens,
fetchTopAssets,
fetchAggregatorMetadata,
} from './swaps.util';
import AwaitingSignatures from './awaiting-signatures';
import SmartTransactionStatus from './smart-transaction-status';
import AwaitingSwap from './awaiting-swap';
import LoadingQuote from './loading-swaps-quotes';
import BuildQuote from './build-quote';
import ViewQuote from './view-quote';
2020-10-06 20:28:38 +02:00
2020-11-03 00:41:28 +01:00
export default function Swap() {
const t = useContext(I18nContext);
const history = useHistory();
const dispatch = useDispatch();
const trackEvent = useContext(MetaMetricsContext);
2020-10-06 20:28:38 +02:00
const { pathname } = useLocation();
const isAwaitingSwapRoute = pathname === AWAITING_SWAP_ROUTE;
const isAwaitingSignaturesRoute = pathname === AWAITING_SIGNATURES_ROUTE;
const isSwapsErrorRoute = pathname === SWAPS_ERROR_ROUTE;
const isLoadingQuotesRoute = pathname === LOADING_QUOTES_ROUTE;
const isSmartTransactionStatusRoute =
pathname === SMART_TRANSACTION_STATUS_ROUTE;
const isViewQuoteRoute = pathname === VIEW_QUOTE_ROUTE;
2020-10-06 20:28:38 +02:00
const [currentStxErrorTracked, setCurrentStxErrorTracked] = useState(false);
const fetchParams = useSelector(getFetchParams, isEqual);
const { destinationTokenInfo = {} } = fetchParams?.metaData || {};
2020-10-06 20:28:38 +02:00
const routeState = useSelector(getBackgroundSwapRouteState);
const selectedAccount = useSelector(getSelectedAccount, shallowEqual);
const quotes = useSelector(getQuotes, isEqual);
const txList = useSelector(currentNetworkTxListSelector, shallowEqual);
const tradeTxId = useSelector(getTradeTxId);
const approveTxId = useSelector(getApproveTxId);
const aggregatorMetadata = useSelector(getAggregatorMetadata, shallowEqual);
const fetchingQuotes = useSelector(getFetchingQuotes);
let swapsErrorKey = useSelector(getSwapsErrorKey);
const swapsEnabled = useSelector(getSwapsFeatureIsLive);
const chainId = useSelector(getCurrentChainId);
const isSwapsChain = useSelector(getIsSwapsChain);
const networkAndAccountSupports1559 = useSelector(
checkNetworkAndAccountSupports1559,
);
const tokenList = useSelector(getTokenList, isEqual);
const shuffledTokensList = shuffle(Object.values(tokenList));
2021-09-15 15:13:18 +02:00
const reviewSwapClickedTimestamp = useSelector(getReviewSwapClickedTimestamp);
const pendingSmartTransactions = useSelector(getPendingSmartTransactions);
2021-09-15 15:13:18 +02:00
const reviewSwapClicked = Boolean(reviewSwapClickedTimestamp);
const smartTransactionsOptInStatus = useSelector(
getSmartTransactionsOptInStatus,
);
const smartTransactionsEnabled = useSelector(getSmartTransactionsEnabled);
const currentSmartTransactionsEnabled = useSelector(
getCurrentSmartTransactionsEnabled,
);
const currentSmartTransactionsError = useSelector(
getCurrentSmartTransactionsError,
);
2020-10-06 20:28:38 +02:00
useEffect(() => {
const leaveSwaps = async () => {
await dispatch(prepareToLeaveSwaps());
// We need to wait until "prepareToLeaveSwaps" is done, because otherwise
// a user would be redirected from DEFAULT_ROUTE back to Swaps.
history.push(DEFAULT_ROUTE);
};
if (!isSwapsChain) {
leaveSwaps();
}
}, [isSwapsChain, dispatch, history]);
// This will pre-load gas fees before going to the View Quote page.
useGasFeeEstimates();
2022-07-31 20:26:40 +02:00
const { balance: ethBalance, address: selectedAccountAddress } =
selectedAccount;
const { destinationTokenAddedForSwap } = fetchParams || {};
2020-10-06 20:28:38 +02:00
2020-11-03 00:41:28 +01:00
const approveTxData =
approveTxId && txList.find(({ id }) => approveTxId === id);
const tradeTxData = tradeTxId && txList.find(({ id }) => tradeTxId === id);
2020-11-03 00:41:28 +01:00
const tokensReceived =
tradeTxData?.txReceipt &&
getSwapsTokensReceivedFromTxMeta(
destinationTokenInfo?.symbol,
tradeTxData,
destinationTokenInfo?.address,
selectedAccountAddress,
destinationTokenInfo?.decimals,
approveTxData,
chainId,
);
const tradeConfirmed = tradeTxData?.status === TransactionStatus.confirmed;
2020-11-03 00:41:28 +01:00
const approveError =
approveTxData?.status === TransactionStatus.failed ||
approveTxData?.txReceipt?.status === '0x0';
2020-11-03 00:41:28 +01:00
const tradeError =
tradeTxData?.status === TransactionStatus.failed ||
tradeTxData?.txReceipt?.status === '0x0';
const conversionError = approveError || tradeError;
2020-10-06 20:28:38 +02:00
if (conversionError && swapsErrorKey !== CONTRACT_DATA_DISABLED_ERROR) {
swapsErrorKey = SWAP_FAILED_ERROR;
2020-10-06 20:28:38 +02:00
}
const clearTemporaryTokenRef = useRef();
2020-11-03 00:41:28 +01:00
useEffect(() => {
clearTemporaryTokenRef.current = () => {
if (
destinationTokenAddedForSwap &&
(!isAwaitingSwapRoute || conversionError)
) {
dispatch(
ignoreTokens({
tokensToIgnore: destinationTokenInfo?.address,
dontShowLoadingIndicator: true,
}),
);
2020-10-06 20:28:38 +02:00
}
};
2020-11-03 00:41:28 +01:00
}, [
conversionError,
dispatch,
destinationTokenAddedForSwap,
destinationTokenInfo,
fetchParams,
isAwaitingSwapRoute,
]);
2020-10-06 20:28:38 +02:00
useEffect(() => {
return () => {
clearTemporaryTokenRef.current();
};
}, []);
2020-10-06 20:28:38 +02:00
// eslint-disable-next-line
2020-10-06 20:28:38 +02:00
useEffect(() => {
if (!isSwapsChain) {
return undefined;
}
fetchTokens(chainId)
.then((tokens) => {
dispatch(setSwapsTokens(tokens));
})
.catch((error) => console.error(error));
fetchTopAssets(chainId).then((topAssets) => {
dispatch(setTopAssets(topAssets));
});
fetchAggregatorMetadata(chainId).then((newAggregatorMetadata) => {
dispatch(setAggregatorMetadata(newAggregatorMetadata));
});
if (!networkAndAccountSupports1559) {
dispatch(fetchAndSetSwapsGasPriceInfo(chainId));
}
return () => {
dispatch(prepareToLeaveSwaps());
};
}, [dispatch, chainId, networkAndAccountSupports1559, isSwapsChain]);
2020-10-06 20:28:38 +02:00
const hardwareWalletUsed = useSelector(isHardwareWallet);
const hardwareWalletType = useSelector(getHardwareWalletType);
const trackExitedSwapsEvent = () => {
trackEvent({
event: 'Exited Swaps',
category: MetaMetricsEventCategory.Swaps,
sensitiveProperties: {
token_from: fetchParams?.sourceTokenInfo?.symbol,
token_from_amount: fetchParams?.value,
request_type: fetchParams?.balanceError,
token_to: fetchParams?.destinationTokenInfo?.symbol,
slippage: fetchParams?.slippage,
custom_slippage: fetchParams?.slippage !== 2,
current_screen: pathname.match(/\/swaps\/(.+)/u)[1],
is_hardware_wallet: hardwareWalletUsed,
hardware_wallet_type: hardwareWalletType,
stx_enabled: smartTransactionsEnabled,
current_stx_enabled: currentSmartTransactionsEnabled,
stx_user_opt_in: smartTransactionsOptInStatus,
},
});
};
const exitEventRef = useRef();
2020-10-06 20:28:38 +02:00
useEffect(() => {
exitEventRef.current = () => {
trackExitedSwapsEvent();
};
});
2020-10-06 20:28:38 +02:00
useEffect(() => {
const fetchSwapsLivenessAndFeatureFlagsWrapper = async () => {
await dispatch(fetchSwapsLivenessAndFeatureFlags());
};
fetchSwapsLivenessAndFeatureFlagsWrapper();
2020-10-06 20:28:38 +02:00
return () => {
exitEventRef.current();
};
}, [dispatch]);
2020-10-06 20:28:38 +02:00
useEffect(() => {
2021-09-15 15:13:18 +02:00
// If there is a swapsErrorKey and reviewSwapClicked is false, there was an error in silent quotes prefetching
// and we don't want to show the error page in that case, because another API call for quotes can be successful.
if (swapsErrorKey && !isSwapsErrorRoute && reviewSwapClicked) {
history.push(SWAPS_ERROR_ROUTE);
2020-10-06 20:28:38 +02:00
}
2021-09-15 15:13:18 +02:00
}, [history, swapsErrorKey, isSwapsErrorRoute, reviewSwapClicked]);
2020-10-06 20:28:38 +02:00
const beforeUnloadEventAddedRef = useRef();
2020-10-06 20:28:38 +02:00
useEffect(() => {
const fn = () => {
clearTemporaryTokenRef.current();
2020-10-06 20:28:38 +02:00
if (isLoadingQuotesRoute) {
dispatch(prepareToLeaveSwaps());
2020-10-06 20:28:38 +02:00
}
return null;
};
2020-10-06 20:28:38 +02:00
if (isLoadingQuotesRoute && !beforeUnloadEventAddedRef.current) {
beforeUnloadEventAddedRef.current = true;
window.addEventListener('beforeunload', fn);
2020-10-06 20:28:38 +02:00
}
return () => window.removeEventListener('beforeunload', fn);
}, [dispatch, isLoadingQuotesRoute]);
2020-10-06 20:28:38 +02:00
const trackErrorStxEvent = useCallback(() => {
trackEvent({
event: 'Error Smart Transactions',
category: MetaMetricsEventCategory.Swaps,
sensitiveProperties: {
token_from: fetchParams?.sourceTokenInfo?.symbol,
token_from_amount: fetchParams?.value,
request_type: fetchParams?.balanceError,
token_to: fetchParams?.destinationTokenInfo?.symbol,
slippage: fetchParams?.slippage,
custom_slippage: fetchParams?.slippage !== 2,
current_screen: pathname.match(/\/swaps\/(.+)/u)[1],
is_hardware_wallet: hardwareWalletUsed,
hardware_wallet_type: hardwareWalletType,
stx_enabled: smartTransactionsEnabled,
current_stx_enabled: currentSmartTransactionsEnabled,
stx_user_opt_in: smartTransactionsOptInStatus,
stx_error: currentSmartTransactionsError,
},
});
}, [
currentSmartTransactionsError,
currentSmartTransactionsEnabled,
trackEvent,
fetchParams?.balanceError,
fetchParams?.destinationTokenInfo?.symbol,
fetchParams?.slippage,
fetchParams?.sourceTokenInfo?.symbol,
fetchParams?.value,
hardwareWalletType,
hardwareWalletUsed,
pathname,
smartTransactionsEnabled,
smartTransactionsOptInStatus,
]);
useEffect(() => {
if (currentSmartTransactionsError && !currentStxErrorTracked) {
setCurrentStxErrorTracked(true);
trackErrorStxEvent();
}
}, [
currentSmartTransactionsError,
trackErrorStxEvent,
currentStxErrorTracked,
]);
if (!isSwapsChain) {
// A user is being redirected outside of Swaps via the async "leaveSwaps" function above. In the meantime
// we have to prevent the code below this condition, which wouldn't work on an unsupported chain.
return <></>;
}
2020-10-06 20:28:38 +02:00
return (
<div className="swaps">
<div className="swaps__container">
<div className="swaps__header">
<div
className="swaps__header-edit"
onClick={async () => {
await dispatch(navigateBackToBuildQuote(history));
}}
>
{isViewQuoteRoute && t('edit')}
</div>
2020-11-03 00:41:28 +01:00
<div className="swaps__title">{t('swap')}</div>
<div
className="swaps__header-cancel"
onClick={async () => {
clearTemporaryTokenRef.current();
dispatch(clearSwapsState());
await dispatch(resetBackgroundSwapsState());
history.push(DEFAULT_ROUTE);
}}
>
{!isAwaitingSwapRoute &&
!isAwaitingSignaturesRoute &&
!isSmartTransactionStatusRoute &&
t('cancel')}
</div>
2020-10-06 20:28:38 +02:00
</div>
<div className="swaps__content">
<Switch>
<FeatureToggledRoute
redirectRoute={SWAPS_MAINTENANCE_ROUTE}
flag={swapsEnabled}
path={BUILD_QUOTE_ROUTE}
exact
render={() => {
if (tradeTxData && !conversionError) {
return <Redirect to={{ pathname: AWAITING_SWAP_ROUTE }} />;
} else if (tradeTxData && routeState) {
return <Redirect to={{ pathname: SWAPS_ERROR_ROUTE }} />;
2020-10-06 20:28:38 +02:00
} else if (routeState === 'loading' && aggregatorMetadata) {
return <Redirect to={{ pathname: LOADING_QUOTES_ROUTE }} />;
2020-10-06 20:28:38 +02:00
}
return (
<BuildQuote
ethBalance={ethBalance}
selectedAccountAddress={selectedAccountAddress}
shuffledTokensList={shuffledTokensList}
2020-10-06 20:28:38 +02:00
/>
);
2020-10-06 20:28:38 +02:00
}}
/>
<FeatureToggledRoute
redirectRoute={SWAPS_MAINTENANCE_ROUTE}
flag={swapsEnabled}
path={VIEW_QUOTE_ROUTE}
exact
render={() => {
if (
pendingSmartTransactions.length > 0 &&
routeState === 'smartTransactionStatus'
) {
return (
<Redirect
to={{ pathname: SMART_TRANSACTION_STATUS_ROUTE }}
/>
);
}
2020-10-06 20:28:38 +02:00
if (Object.values(quotes).length) {
return (
2020-11-03 00:41:28 +01:00
<ViewQuote numberOfQuotes={Object.values(quotes).length} />
);
2020-10-06 20:28:38 +02:00
} else if (fetchParams) {
return <Redirect to={{ pathname: SWAPS_ERROR_ROUTE }} />;
2020-10-06 20:28:38 +02:00
}
return <Redirect to={{ pathname: BUILD_QUOTE_ROUTE }} />;
2020-10-06 20:28:38 +02:00
}}
/>
<Route
path={SWAPS_ERROR_ROUTE}
exact
render={() => {
if (swapsErrorKey) {
return (
<AwaitingSwap
swapComplete={false}
errorKey={swapsErrorKey}
txHash={tradeTxData?.hash}
txId={tradeTxData?.id}
2020-10-06 20:28:38 +02:00
submittedTime={tradeTxData?.submittedTime}
/>
);
2020-10-06 20:28:38 +02:00
}
return <Redirect to={{ pathname: BUILD_QUOTE_ROUTE }} />;
2020-10-06 20:28:38 +02:00
}}
/>
<FeatureToggledRoute
redirectRoute={SWAPS_MAINTENANCE_ROUTE}
flag={swapsEnabled}
path={LOADING_QUOTES_ROUTE}
exact
render={() => {
2020-11-03 00:41:28 +01:00
return aggregatorMetadata ? (
<LoadingQuote
loadingComplete={
!fetchingQuotes && Boolean(Object.values(quotes).length)
}
onDone={async () => {
await dispatch(setBackgroundSwapRouteState(''));
2020-11-03 00:41:28 +01:00
if (
swapsErrorKey === ERROR_FETCHING_QUOTES ||
swapsErrorKey === QUOTES_NOT_AVAILABLE_ERROR
) {
dispatch(setSwapsErrorKey(QUOTES_NOT_AVAILABLE_ERROR));
history.push(SWAPS_ERROR_ROUTE);
2020-11-03 00:41:28 +01:00
} else {
history.push(VIEW_QUOTE_ROUTE);
2020-11-03 00:41:28 +01:00
}
}}
aggregatorMetadata={aggregatorMetadata}
/>
) : (
<Redirect to={{ pathname: BUILD_QUOTE_ROUTE }} />
);
2020-10-06 20:28:38 +02:00
}}
/>
<Route
path={SWAPS_MAINTENANCE_ROUTE}
exact
render={() => {
return swapsEnabled === false ? (
<AwaitingSwap errorKey={OFFLINE_FOR_MAINTENANCE} />
2020-11-03 00:41:28 +01:00
) : (
<Redirect to={{ pathname: BUILD_QUOTE_ROUTE }} />
);
2020-10-06 20:28:38 +02:00
}}
/>
<Route
path={AWAITING_SIGNATURES_ROUTE}
exact
render={() => {
return <AwaitingSignatures />;
}}
/>
<Route
path={SMART_TRANSACTION_STATUS_ROUTE}
exact
render={() => {
return <SmartTransactionStatus txId={tradeTxData?.id} />;
}}
/>
2020-10-06 20:28:38 +02:00
<Route
path={AWAITING_SWAP_ROUTE}
exact
render={() => {
2020-11-03 00:41:28 +01:00
return routeState === 'awaiting' || tradeTxData ? (
<AwaitingSwap
swapComplete={tradeConfirmed}
txHash={tradeTxData?.hash}
tokensReceived={tokensReceived}
txId={tradeTxData?.id}
2020-11-03 00:41:28 +01:00
submittingSwap={
routeState === 'awaiting' && !(approveTxId || tradeTxId)
}
/>
) : (
<Redirect to={{ pathname: DEFAULT_ROUTE }} />
);
2020-10-06 20:28:38 +02:00
}}
/>
</Switch>
</div>
</div>
</div>
);
2020-10-06 20:28:38 +02:00
}