2023-02-24 18:07:26 +01:00
|
|
|
import React, { useCallback, useEffect, useState } from 'react';
|
2023-02-08 12:00:25 +01:00
|
|
|
import { useDispatch, useSelector } from 'react-redux';
|
|
|
|
import { Switch, Route, useHistory, useParams } from 'react-router-dom';
|
2023-02-11 16:17:12 +01:00
|
|
|
|
2021-02-04 19:15:23 +01:00
|
|
|
import Loading from '../../components/ui/loading-screen';
|
2022-09-21 18:22:36 +02:00
|
|
|
import ConfirmContractInteraction from '../confirm-contract-interaction';
|
2021-02-04 19:15:23 +01:00
|
|
|
import ConfirmDeployContract from '../confirm-deploy-contract';
|
|
|
|
import ConfirmDecryptMessage from '../confirm-decrypt-message';
|
|
|
|
import ConfirmEncryptionPublicKey from '../confirm-encryption-public-key';
|
2023-02-11 16:17:12 +01:00
|
|
|
import ConfirmSendEther from '../confirm-send-ether';
|
|
|
|
import ConfirmTransactionSwitch from '../confirm-transaction-switch';
|
2020-02-19 19:24:16 +01:00
|
|
|
|
2023-02-08 12:00:25 +01:00
|
|
|
import { ORIGIN_METAMASK } from '../../../shared/constants/app';
|
|
|
|
|
|
|
|
import {
|
|
|
|
clearConfirmTransaction,
|
|
|
|
setTransactionToConfirm,
|
|
|
|
} from '../../ducks/confirm-transaction/confirm-transaction.duck';
|
|
|
|
import { getMostRecentOverviewPage } from '../../ducks/history/history';
|
|
|
|
import { getSendTo } from '../../ducks/send';
|
2018-06-23 08:52:45 +02:00
|
|
|
import {
|
2018-06-25 21:06:57 +02:00
|
|
|
CONFIRM_DEPLOY_CONTRACT_PATH,
|
|
|
|
CONFIRM_SEND_ETHER_PATH,
|
|
|
|
CONFIRM_TOKEN_METHOD_PATH,
|
2023-02-11 16:17:12 +01:00
|
|
|
CONFIRM_TRANSACTION_ROUTE,
|
2020-02-19 19:24:16 +01:00
|
|
|
DECRYPT_MESSAGE_REQUEST_PATH,
|
2021-05-03 19:51:09 +02:00
|
|
|
DEFAULT_ROUTE,
|
2023-02-11 16:17:12 +01:00
|
|
|
ENCRYPTION_PUBLIC_KEY_REQUEST_PATH,
|
|
|
|
SIGNATURE_REQUEST_PATH,
|
2021-02-04 19:15:23 +01:00
|
|
|
} from '../../helpers/constants/routes';
|
2023-02-08 12:00:25 +01:00
|
|
|
import { isTokenMethodAction } from '../../helpers/utils/transactions.util';
|
|
|
|
import { usePrevious } from '../../hooks/usePrevious';
|
|
|
|
import {
|
|
|
|
getUnapprovedTransactions,
|
|
|
|
unconfirmedTransactionsListSelector,
|
|
|
|
unconfirmedTransactionsHashSelector,
|
|
|
|
} from '../../selectors';
|
2021-07-16 18:06:32 +02:00
|
|
|
import {
|
|
|
|
disconnectGasFeeEstimatePoller,
|
2023-02-08 12:00:25 +01:00
|
|
|
getContractMethodData,
|
2021-07-16 18:06:32 +02:00
|
|
|
getGasFeeEstimatesAndStartPolling,
|
2021-08-04 23:53:13 +02:00
|
|
|
addPollingTokenToAppState,
|
|
|
|
removePollingTokenFromAppState,
|
2023-02-08 12:00:25 +01:00
|
|
|
setDefaultHomeActiveTabName,
|
2021-07-16 18:06:32 +02:00
|
|
|
} from '../../store/actions';
|
2023-02-02 15:52:11 +01:00
|
|
|
import ConfirmSignatureRequest from '../confirm-signature-request';
|
2022-03-09 15:38:12 +01:00
|
|
|
import ConfirmTokenTransactionSwitch from './confirm-token-transaction-switch';
|
2018-06-23 08:52:45 +02:00
|
|
|
|
2023-02-08 12:00:25 +01:00
|
|
|
const ConfirmTransaction = () => {
|
|
|
|
const dispatch = useDispatch();
|
|
|
|
const history = useHistory();
|
|
|
|
const { id: paramsTransactionId } = useParams();
|
|
|
|
|
|
|
|
const [isMounted, setIsMounted] = useState(false);
|
|
|
|
const [pollingToken, setPollingToken] = useState();
|
|
|
|
|
|
|
|
const mostRecentOverviewPage = useSelector(getMostRecentOverviewPage);
|
|
|
|
const sendTo = useSelector(getSendTo);
|
|
|
|
const unapprovedTxs = useSelector(getUnapprovedTransactions);
|
2023-02-11 16:17:12 +01:00
|
|
|
const unconfirmedTxs = useSelector(unconfirmedTransactionsListSelector);
|
2023-02-08 12:00:25 +01:00
|
|
|
const unconfirmedMessages = useSelector(unconfirmedTransactionsHashSelector);
|
|
|
|
|
2023-02-11 16:17:12 +01:00
|
|
|
const totalUnapproved = unconfirmedTxs.length || 0;
|
2023-02-24 18:07:26 +01:00
|
|
|
const getTransaction = useCallback(() => {
|
2023-02-11 16:17:12 +01:00
|
|
|
return totalUnapproved
|
2023-02-08 12:00:25 +01:00
|
|
|
? unapprovedTxs[paramsTransactionId] ||
|
|
|
|
unconfirmedMessages[paramsTransactionId] ||
|
2023-02-11 16:17:12 +01:00
|
|
|
unconfirmedTxs[0]
|
2023-02-08 12:00:25 +01:00
|
|
|
: {};
|
|
|
|
}, [
|
|
|
|
paramsTransactionId,
|
2023-02-11 16:17:12 +01:00
|
|
|
totalUnapproved,
|
2023-02-08 12:00:25 +01:00
|
|
|
unapprovedTxs,
|
|
|
|
unconfirmedMessages,
|
2023-02-11 16:17:12 +01:00
|
|
|
unconfirmedTxs,
|
2023-02-08 12:00:25 +01:00
|
|
|
]);
|
2023-02-24 18:07:26 +01:00
|
|
|
const [transaction, setTransaction] = useState(getTransaction);
|
|
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
const tx = getTransaction();
|
|
|
|
setTransaction(tx);
|
|
|
|
if (tx?.id) {
|
|
|
|
dispatch(setTransactionToConfirm(tx.id));
|
|
|
|
}
|
|
|
|
}, [
|
|
|
|
dispatch,
|
|
|
|
getTransaction,
|
|
|
|
paramsTransactionId,
|
|
|
|
totalUnapproved,
|
|
|
|
unapprovedTxs,
|
|
|
|
unconfirmedMessages,
|
|
|
|
unconfirmedTxs,
|
|
|
|
]);
|
2023-02-08 12:00:25 +01:00
|
|
|
|
|
|
|
const { id, type } = transaction;
|
|
|
|
const transactionId = id && String(id);
|
2023-02-24 18:07:26 +01:00
|
|
|
const isValidTokenMethod = isTokenMethodAction(type);
|
2023-02-11 16:17:12 +01:00
|
|
|
const isValidTransactionId =
|
|
|
|
transactionId &&
|
|
|
|
(!paramsTransactionId || paramsTransactionId === transactionId);
|
2023-02-08 12:00:25 +01:00
|
|
|
|
|
|
|
const prevParamsTransactionId = usePrevious(paramsTransactionId);
|
|
|
|
const prevTransactionId = usePrevious(transactionId);
|
|
|
|
|
|
|
|
const _beforeUnload = useCallback(() => {
|
|
|
|
setIsMounted(false);
|
|
|
|
|
|
|
|
if (pollingToken) {
|
|
|
|
disconnectGasFeeEstimatePoller(pollingToken);
|
|
|
|
removePollingTokenFromAppState(pollingToken);
|
2021-08-04 23:53:13 +02:00
|
|
|
}
|
2023-02-08 12:00:25 +01:00
|
|
|
}, [pollingToken]);
|
|
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
setIsMounted(true);
|
|
|
|
|
|
|
|
getGasFeeEstimatesAndStartPolling().then((_pollingToken) => {
|
|
|
|
if (isMounted) {
|
|
|
|
setPollingToken(_pollingToken);
|
|
|
|
addPollingTokenToAppState(_pollingToken);
|
2021-07-16 18:06:32 +02:00
|
|
|
} else {
|
2023-02-08 12:00:25 +01:00
|
|
|
disconnectGasFeeEstimatePoller(_pollingToken);
|
|
|
|
removePollingTokenFromAppState(_pollingToken);
|
2021-07-16 18:06:32 +02:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2023-02-08 12:00:25 +01:00
|
|
|
window.addEventListener('beforeunload', _beforeUnload);
|
2021-08-04 23:53:13 +02:00
|
|
|
|
2023-02-11 16:17:12 +01:00
|
|
|
if (!totalUnapproved && !sendTo) {
|
2021-02-04 19:15:23 +01:00
|
|
|
history.replace(mostRecentOverviewPage);
|
2023-02-08 12:00:25 +01:00
|
|
|
} else {
|
2023-02-11 16:17:12 +01:00
|
|
|
const { txParams: { data } = {}, origin } = transaction;
|
|
|
|
|
2023-02-08 12:00:25 +01:00
|
|
|
if (origin !== ORIGIN_METAMASK) {
|
|
|
|
dispatch(getContractMethodData(data));
|
|
|
|
}
|
2022-03-09 15:38:12 +01:00
|
|
|
|
2023-02-08 12:00:25 +01:00
|
|
|
const txId = transactionId || paramsTransactionId;
|
|
|
|
if (txId) {
|
|
|
|
dispatch(setTransactionToConfirm(txId));
|
|
|
|
}
|
2019-11-20 01:03:20 +01:00
|
|
|
}
|
2018-06-25 21:06:57 +02:00
|
|
|
|
2023-02-08 12:00:25 +01:00
|
|
|
return () => {
|
|
|
|
_beforeUnload();
|
|
|
|
window.removeEventListener('beforeunload', _beforeUnload);
|
|
|
|
};
|
|
|
|
// eslint-disable-next-line react-hooks/exhaustive-deps
|
|
|
|
}, []);
|
2021-07-16 18:06:32 +02:00
|
|
|
|
2023-02-08 12:00:25 +01:00
|
|
|
useEffect(() => {
|
2020-11-03 00:41:28 +01:00
|
|
|
if (
|
|
|
|
paramsTransactionId &&
|
|
|
|
transactionId &&
|
2023-02-08 12:00:25 +01:00
|
|
|
prevParamsTransactionId !== paramsTransactionId
|
2020-11-03 00:41:28 +01:00
|
|
|
) {
|
2023-02-11 16:17:12 +01:00
|
|
|
const { txData: { txParams: { data } = {}, origin } = {} } = transaction;
|
|
|
|
|
2023-02-08 12:00:25 +01:00
|
|
|
dispatch(clearConfirmTransaction());
|
|
|
|
dispatch(setTransactionToConfirm(paramsTransactionId));
|
|
|
|
if (origin !== ORIGIN_METAMASK) {
|
|
|
|
dispatch(getContractMethodData(data));
|
2022-08-04 21:50:32 +02:00
|
|
|
}
|
2023-02-11 16:17:12 +01:00
|
|
|
} else if (prevTransactionId && !transactionId && !totalUnapproved) {
|
2023-02-08 12:00:25 +01:00
|
|
|
dispatch(setDefaultHomeActiveTabName('activity')).then(() => {
|
2021-05-03 19:51:09 +02:00
|
|
|
history.replace(DEFAULT_ROUTE);
|
|
|
|
});
|
2020-11-03 00:41:28 +01:00
|
|
|
} else if (
|
2023-02-08 12:00:25 +01:00
|
|
|
prevTransactionId &&
|
2020-11-03 00:41:28 +01:00
|
|
|
transactionId &&
|
2023-02-24 18:07:26 +01:00
|
|
|
prevTransactionId !== transactionId &&
|
|
|
|
paramsTransactionId !== transactionId
|
2020-11-03 00:41:28 +01:00
|
|
|
) {
|
2021-02-04 19:15:23 +01:00
|
|
|
history.replace(mostRecentOverviewPage);
|
2018-06-23 08:52:45 +02:00
|
|
|
}
|
2023-02-08 12:00:25 +01:00
|
|
|
}, [
|
|
|
|
dispatch,
|
|
|
|
history,
|
|
|
|
mostRecentOverviewPage,
|
2023-02-11 16:17:12 +01:00
|
|
|
paramsTransactionId,
|
2023-02-08 12:00:25 +01:00
|
|
|
prevParamsTransactionId,
|
|
|
|
prevTransactionId,
|
2023-02-11 16:17:12 +01:00
|
|
|
totalUnapproved,
|
|
|
|
transaction,
|
|
|
|
transactionId,
|
2023-02-08 12:00:25 +01:00
|
|
|
]);
|
|
|
|
|
2023-02-24 18:07:26 +01:00
|
|
|
if (isValidTokenMethod && isValidTransactionId) {
|
2023-02-08 12:00:25 +01:00
|
|
|
return <ConfirmTokenTransactionSwitch transaction={transaction} />;
|
2018-06-23 08:52:45 +02:00
|
|
|
}
|
2023-02-08 12:00:25 +01:00
|
|
|
// Show routes when state.confirmTransaction has been set and when either the ID in the params
|
|
|
|
// isn't specified or is specified and matches the ID in state.confirmTransaction in order to
|
|
|
|
// support URLs of /confirm-transaction or /confirm-transaction/<transactionId>
|
2023-02-11 16:17:12 +01:00
|
|
|
return isValidTransactionId ? (
|
2023-02-08 12:00:25 +01:00
|
|
|
<Switch>
|
|
|
|
<Route
|
|
|
|
exact
|
|
|
|
path={`${CONFIRM_TRANSACTION_ROUTE}/:id?${CONFIRM_DEPLOY_CONTRACT_PATH}`}
|
|
|
|
component={ConfirmDeployContract}
|
|
|
|
/>
|
|
|
|
<Route
|
|
|
|
exact
|
|
|
|
path={`${CONFIRM_TRANSACTION_ROUTE}/:id?${CONFIRM_SEND_ETHER_PATH}`}
|
|
|
|
component={ConfirmSendEther}
|
|
|
|
/>
|
|
|
|
<Route
|
|
|
|
exact
|
|
|
|
path={`${CONFIRM_TRANSACTION_ROUTE}/:id?${CONFIRM_TOKEN_METHOD_PATH}`}
|
|
|
|
component={ConfirmContractInteraction}
|
|
|
|
/>
|
|
|
|
<Route
|
|
|
|
exact
|
|
|
|
path={`${CONFIRM_TRANSACTION_ROUTE}/:id?${SIGNATURE_REQUEST_PATH}`}
|
|
|
|
component={ConfirmSignatureRequest}
|
|
|
|
/>
|
|
|
|
<Route
|
|
|
|
exact
|
|
|
|
path={`${CONFIRM_TRANSACTION_ROUTE}/:id?${DECRYPT_MESSAGE_REQUEST_PATH}`}
|
|
|
|
component={ConfirmDecryptMessage}
|
|
|
|
/>
|
|
|
|
<Route
|
|
|
|
exact
|
|
|
|
path={`${CONFIRM_TRANSACTION_ROUTE}/:id?${ENCRYPTION_PUBLIC_KEY_REQUEST_PATH}`}
|
|
|
|
component={ConfirmEncryptionPublicKey}
|
|
|
|
/>
|
|
|
|
<Route path="*" component={ConfirmTransactionSwitch} />
|
|
|
|
</Switch>
|
|
|
|
) : (
|
|
|
|
<Loading />
|
|
|
|
);
|
|
|
|
};
|
|
|
|
|
|
|
|
export default ConfirmTransaction;
|