1
0
mirror of https://github.com/kremalicious/metamask-extension.git synced 2024-12-23 01:39:44 +01:00

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
This commit is contained in:
Daniel 2022-05-03 17:55:41 +02:00 committed by GitHub
parent 6915dd1a57
commit 9daab6aa59
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 41 additions and 15 deletions

View File

@ -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.
*/
/**

View File

@ -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,

View File

@ -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',
},
});
};

View File

@ -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 (
<div className="swaps">
@ -423,7 +423,7 @@ export default function Swap() {
{t('stxUnavailable')}
</div>
<div>
{isStxRegularTxPendingError
{isRegularTxInProgressError
? t('stxFallbackPendingTx')
: t('stxFallbackUnavailable')}
</div>

View File

@ -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');