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
feature/default_network_editable
Daniel 3 years ago committed by GitHub
parent 6915dd1a57
commit 9daab6aa59
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 12
      shared/constants/transaction.js
  2. 25
      ui/ducks/swaps/swaps.js
  3. 9
      ui/ducks/swaps/swaps.test.js
  4. 6
      ui/pages/swaps/index.js
  5. 4
      ui/pages/swaps/swaps.util.js

@ -133,6 +133,17 @@ export const TRANSACTION_STATUSES = {
PENDING: 'pending', 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 * Transaction Group Status is a MetaMask construct to track the status of groups
* of transactions. * of transactions.
@ -159,6 +170,7 @@ export const TRANSACTION_GROUP_STATUSES = {
* @typedef {Object} SmartTransactionStatuses * @typedef {Object} SmartTransactionStatuses
* @property {'cancelled'} CANCELLED - It can be cancelled for various reasons. * @property {'cancelled'} CANCELLED - It can be cancelled for various reasons.
* @property {'pending'} PENDING - Smart transaction is being processed. * @property {'pending'} PENDING - Smart transaction is being processed.
* @property {'success'} SUCCESS - Smart transaction was successfully mined.
*/ */
/** /**

@ -84,7 +84,7 @@ import {
} from '../../../shared/constants/swaps'; } from '../../../shared/constants/swaps';
import { import {
TRANSACTION_TYPES, TRANSACTION_TYPES,
TRANSACTION_STATUSES, IN_PROGRESS_TRANSACTION_STATUSES,
SMART_TRANSACTION_STATUSES, SMART_TRANSACTION_STATUSES,
} from '../../../shared/constants/transaction'; } from '../../../shared/constants/transaction';
import { getGasFeeEstimates } from '../metamask/metamask'; 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 = () => { export const fetchSwapsLivenessAndFeatureFlags = () => {
return async (dispatch, getState) => { return async (dispatch, getState) => {
let swapsLivenessForNetwork = { let swapsLivenessForNetwork = {
@ -566,17 +580,12 @@ export const fetchSwapsLivenessAndFeatureFlags = () => {
await dispatch(setSwapsFeatureFlags(swapsFeatureFlags)); await dispatch(setSwapsFeatureFlags(swapsFeatureFlags));
if (ALLOWED_SMART_TRANSACTIONS_CHAIN_IDS.includes(chainId)) { if (ALLOWED_SMART_TRANSACTIONS_CHAIN_IDS.includes(chainId)) {
await dispatch(fetchSmartTransactionsLiveness()); await dispatch(fetchSmartTransactionsLiveness());
const pendingTransactions = await getTransactions({ const transactions = await getTransactions({
searchCriteria: { searchCriteria: {
status: TRANSACTION_STATUSES.PENDING,
from: state.metamask?.selectedAddress, from: state.metamask?.selectedAddress,
}, },
}); });
if (pendingTransactions?.length > 0) { disableStxIfRegularTxInProgress(dispatch, transactions);
dispatch(
setCurrentSmartTransactionsError(stxErrorTypes.REGULAR_TX_PENDING),
);
}
} }
swapsLivenessForNetwork = getSwapsLivenessForNetwork( swapsLivenessForNetwork = getSwapsLivenessForNetwork(
swapsFeatureFlags, swapsFeatureFlags,

@ -15,7 +15,9 @@ jest.mock('../../store/actions.js', () => ({
setSwapsLiveness: jest.fn(), setSwapsLiveness: jest.fn(),
setSwapsFeatureFlags: jest.fn(), setSwapsFeatureFlags: jest.fn(),
fetchSmartTransactionsLiveness: jest.fn(), fetchSmartTransactionsLiveness: jest.fn(),
getTransactions: jest.fn(), getTransactions: jest.fn(() => {
return [];
}),
})); }));
const providerState = { const providerState = {
@ -62,7 +64,10 @@ describe('Ducks - Swaps', () => {
const createGetState = () => { const createGetState = () => {
return () => ({ return () => ({
metamask: { provider: { ...providerState } }, metamask: {
provider: { ...providerState },
from: '0x64a845a5b02460acf8a3d84503b0d68d028b4bb4',
},
}); });
}; };

@ -358,8 +358,8 @@ export default function Swap() {
const isStxNotEnoughFundsError = const isStxNotEnoughFundsError =
currentSmartTransactionsError === stxErrorTypes.NOT_ENOUGH_FUNDS; currentSmartTransactionsError === stxErrorTypes.NOT_ENOUGH_FUNDS;
const isStxRegularTxPendingError = const isRegularTxInProgressError =
currentSmartTransactionsError === stxErrorTypes.REGULAR_TX_PENDING; currentSmartTransactionsError === stxErrorTypes.REGULAR_TX_IN_PROGRESS;
return ( return (
<div className="swaps"> <div className="swaps">
@ -423,7 +423,7 @@ export default function Swap() {
{t('stxUnavailable')} {t('stxUnavailable')}
</div> </div>
<div> <div>
{isStxRegularTxPendingError {isRegularTxInProgressError
? t('stxFallbackPendingTx') ? t('stxFallbackPendingTx')
: t('stxFallbackUnavailable')} : t('stxFallbackUnavailable')}
</div> </div>

@ -933,13 +933,13 @@ export const showRemainingTimeInMinAndSec = (remainingTimeInSec) => {
export const stxErrorTypes = { export const stxErrorTypes = {
UNAVAILABLE: 'unavailable', UNAVAILABLE: 'unavailable',
NOT_ENOUGH_FUNDS: 'not_enough_funds', NOT_ENOUGH_FUNDS: 'not_enough_funds',
REGULAR_TX_PENDING: 'regular_tx_pending', REGULAR_TX_IN_PROGRESS: 'regular_tx_pending',
}; };
export const getTranslatedStxErrorMessage = (errorType, t) => { export const getTranslatedStxErrorMessage = (errorType, t) => {
switch (errorType) { switch (errorType) {
case stxErrorTypes.UNAVAILABLE: case stxErrorTypes.UNAVAILABLE:
case stxErrorTypes.REGULAR_TX_PENDING: case stxErrorTypes.REGULAR_TX_IN_PROGRESS:
return t('stxErrorUnavailable'); return t('stxErrorUnavailable');
case stxErrorTypes.NOT_ENOUGH_FUNDS: case stxErrorTypes.NOT_ENOUGH_FUNDS:
return t('stxErrorNotEnoughFunds'); return t('stxErrorNotEnoughFunds');

Loading…
Cancel
Save