From 9daab6aa596a15bdd01a8af627e2d9352eb0ef88 Mon Sep 17 00:00:00 2001 From: Daniel <80175477+dan437@users.noreply.github.com> Date: Tue, 3 May 2022 17:55:41 +0200 Subject: [PATCH] Disable STX if a regular tx is in progress (#14554) * Disable STX if a regular tx is in progress * disableStxIfRegularTxInProgress : early return * Fix UTs * Trigger Build --- shared/constants/transaction.js | 12 ++++++++++++ ui/ducks/swaps/swaps.js | 25 +++++++++++++++++-------- ui/ducks/swaps/swaps.test.js | 9 +++++++-- ui/pages/swaps/index.js | 6 +++--- ui/pages/swaps/swaps.util.js | 4 ++-- 5 files changed, 41 insertions(+), 15 deletions(-) diff --git a/shared/constants/transaction.js b/shared/constants/transaction.js index 1e461f491..c3a959caf 100644 --- a/shared/constants/transaction.js +++ b/shared/constants/transaction.js @@ -133,6 +133,17 @@ export const TRANSACTION_STATUSES = { PENDING: 'pending', }; +/** + * With this list we can detect if a transaction is still in progress. + */ +export const IN_PROGRESS_TRANSACTION_STATUSES = [ + TRANSACTION_STATUSES.UNAPPROVED, + TRANSACTION_STATUSES.APPROVED, + TRANSACTION_STATUSES.SIGNED, + TRANSACTION_STATUSES.SUBMITTED, + TRANSACTION_STATUSES.PENDING, +]; + /** * Transaction Group Status is a MetaMask construct to track the status of groups * of transactions. @@ -159,6 +170,7 @@ export const TRANSACTION_GROUP_STATUSES = { * @typedef {Object} SmartTransactionStatuses * @property {'cancelled'} CANCELLED - It can be cancelled for various reasons. * @property {'pending'} PENDING - Smart transaction is being processed. + * @property {'success'} SUCCESS - Smart transaction was successfully mined. */ /** diff --git a/ui/ducks/swaps/swaps.js b/ui/ducks/swaps/swaps.js index 75be0e3ef..a7d5e8ad9 100644 --- a/ui/ducks/swaps/swaps.js +++ b/ui/ducks/swaps/swaps.js @@ -84,7 +84,7 @@ import { } from '../../../shared/constants/swaps'; import { TRANSACTION_TYPES, - TRANSACTION_STATUSES, + IN_PROGRESS_TRANSACTION_STATUSES, SMART_TRANSACTION_STATUSES, } from '../../../shared/constants/transaction'; import { getGasFeeEstimates } from '../metamask/metamask'; @@ -554,6 +554,20 @@ export const fetchAndSetSwapsGasPriceInfo = () => { }; }; +const disableStxIfRegularTxInProgress = (dispatch, transactions) => { + if (transactions?.length <= 0) { + return; + } + for (const transaction of transactions) { + if (IN_PROGRESS_TRANSACTION_STATUSES.includes(transaction.status)) { + dispatch( + setCurrentSmartTransactionsError(stxErrorTypes.REGULAR_TX_IN_PROGRESS), + ); + break; + } + } +}; + export const fetchSwapsLivenessAndFeatureFlags = () => { return async (dispatch, getState) => { let swapsLivenessForNetwork = { @@ -566,17 +580,12 @@ export const fetchSwapsLivenessAndFeatureFlags = () => { await dispatch(setSwapsFeatureFlags(swapsFeatureFlags)); if (ALLOWED_SMART_TRANSACTIONS_CHAIN_IDS.includes(chainId)) { await dispatch(fetchSmartTransactionsLiveness()); - const pendingTransactions = await getTransactions({ + const transactions = await getTransactions({ searchCriteria: { - status: TRANSACTION_STATUSES.PENDING, from: state.metamask?.selectedAddress, }, }); - if (pendingTransactions?.length > 0) { - dispatch( - setCurrentSmartTransactionsError(stxErrorTypes.REGULAR_TX_PENDING), - ); - } + disableStxIfRegularTxInProgress(dispatch, transactions); } swapsLivenessForNetwork = getSwapsLivenessForNetwork( swapsFeatureFlags, diff --git a/ui/ducks/swaps/swaps.test.js b/ui/ducks/swaps/swaps.test.js index 9808b0392..39e4ea25b 100644 --- a/ui/ducks/swaps/swaps.test.js +++ b/ui/ducks/swaps/swaps.test.js @@ -15,7 +15,9 @@ jest.mock('../../store/actions.js', () => ({ setSwapsLiveness: jest.fn(), setSwapsFeatureFlags: jest.fn(), fetchSmartTransactionsLiveness: jest.fn(), - getTransactions: jest.fn(), + getTransactions: jest.fn(() => { + return []; + }), })); const providerState = { @@ -62,7 +64,10 @@ describe('Ducks - Swaps', () => { const createGetState = () => { return () => ({ - metamask: { provider: { ...providerState } }, + metamask: { + provider: { ...providerState }, + from: '0x64a845a5b02460acf8a3d84503b0d68d028b4bb4', + }, }); }; diff --git a/ui/pages/swaps/index.js b/ui/pages/swaps/index.js index c60b0bd2c..bae9e6cbd 100644 --- a/ui/pages/swaps/index.js +++ b/ui/pages/swaps/index.js @@ -358,8 +358,8 @@ export default function Swap() { const isStxNotEnoughFundsError = currentSmartTransactionsError === stxErrorTypes.NOT_ENOUGH_FUNDS; - const isStxRegularTxPendingError = - currentSmartTransactionsError === stxErrorTypes.REGULAR_TX_PENDING; + const isRegularTxInProgressError = + currentSmartTransactionsError === stxErrorTypes.REGULAR_TX_IN_PROGRESS; return (
@@ -423,7 +423,7 @@ export default function Swap() { {t('stxUnavailable')}
- {isStxRegularTxPendingError + {isRegularTxInProgressError ? t('stxFallbackPendingTx') : t('stxFallbackUnavailable')}
diff --git a/ui/pages/swaps/swaps.util.js b/ui/pages/swaps/swaps.util.js index 9c4f6b52d..a34da5a0a 100644 --- a/ui/pages/swaps/swaps.util.js +++ b/ui/pages/swaps/swaps.util.js @@ -933,13 +933,13 @@ export const showRemainingTimeInMinAndSec = (remainingTimeInSec) => { export const stxErrorTypes = { UNAVAILABLE: 'unavailable', NOT_ENOUGH_FUNDS: 'not_enough_funds', - REGULAR_TX_PENDING: 'regular_tx_pending', + REGULAR_TX_IN_PROGRESS: 'regular_tx_pending', }; export const getTranslatedStxErrorMessage = (errorType, t) => { switch (errorType) { case stxErrorTypes.UNAVAILABLE: - case stxErrorTypes.REGULAR_TX_PENDING: + case stxErrorTypes.REGULAR_TX_IN_PROGRESS: return t('stxErrorUnavailable'); case stxErrorTypes.NOT_ENOUGH_FUNDS: return t('stxErrorNotEnoughFunds');