2021-02-04 19:15:23 +01:00
|
|
|
import { createSelector } from 'reselect';
|
2023-05-11 07:56:17 +02:00
|
|
|
import { ApprovalType } from '@metamask/controller-utils';
|
2018-07-31 07:03:20 +02:00
|
|
|
import {
|
2020-05-26 22:49:11 +02:00
|
|
|
PRIORITY_STATUS_HASH,
|
|
|
|
PENDING_STATUS_HASH,
|
2021-02-04 19:15:23 +01:00
|
|
|
} from '../helpers/constants/transactions';
|
2021-04-28 21:53:59 +02:00
|
|
|
import txHelper from '../helpers/utils/tx-helper';
|
2020-11-03 23:57:51 +01:00
|
|
|
import {
|
2023-01-18 15:47:29 +01:00
|
|
|
TransactionStatus,
|
|
|
|
TransactionType,
|
|
|
|
SmartTransactionStatus,
|
2021-04-28 21:53:59 +02:00
|
|
|
} from '../../shared/constants/transaction';
|
|
|
|
import { transactionMatchesNetwork } from '../../shared/modules/transaction.utils';
|
2023-01-20 18:04:37 +01:00
|
|
|
import { hexToDecimal } from '../../shared/modules/conversion.utils';
|
2023-05-02 14:36:24 +02:00
|
|
|
import { getProviderConfig } from '../ducks/metamask/metamask';
|
2022-01-07 20:18:02 +01:00
|
|
|
import {
|
|
|
|
getCurrentChainId,
|
|
|
|
deprecatedGetCurrentNetworkId,
|
|
|
|
getSelectedAddress,
|
|
|
|
} from './selectors';
|
2023-05-11 07:56:17 +02:00
|
|
|
import { hasPendingApprovals, getApprovalRequestsByType } from './approvals';
|
2018-08-06 07:25:58 +02:00
|
|
|
|
2022-04-25 21:39:34 +02:00
|
|
|
const INVALID_INITIAL_TRANSACTION_TYPES = [
|
2023-01-18 15:47:29 +01:00
|
|
|
TransactionType.cancel,
|
|
|
|
TransactionType.retry,
|
2022-04-25 21:39:34 +02:00
|
|
|
];
|
|
|
|
|
2020-02-15 21:34:12 +01:00
|
|
|
export const incomingTxListSelector = (state) => {
|
2021-02-04 19:15:23 +01:00
|
|
|
const { showIncomingTransactions } = state.metamask.featureFlags;
|
2019-08-21 20:42:14 +02:00
|
|
|
if (!showIncomingTransactions) {
|
2021-02-04 19:15:23 +01:00
|
|
|
return [];
|
2019-08-21 20:42:14 +02:00
|
|
|
}
|
|
|
|
|
2023-05-02 14:36:24 +02:00
|
|
|
const { networkId } = state.metamask;
|
|
|
|
const { chainId } = getProviderConfig(state);
|
2021-02-04 19:15:23 +01:00
|
|
|
const selectedAddress = getSelectedAddress(state);
|
2020-11-03 00:41:28 +01:00
|
|
|
return Object.values(state.metamask.incomingTransactions).filter(
|
2021-03-01 16:15:42 +01:00
|
|
|
(tx) =>
|
|
|
|
tx.txParams.to === selectedAddress &&
|
NetworkController: Split `network` into `networkId` and `networkStatus` (#17556)
The `network` store of the network controller crams two types of data
into one place. It roughly tracks whether we have enough information to
make requests to the network and whether the network is capable of
receiving requests, but it also stores the ID of the network (as
obtained via `net_version`).
Generally we shouldn't be using the network ID for anything, as it has
been completely replaced by chain ID, which all custom RPC endpoints
have been required to support for over a year now. However, as the
network ID is used in various places within the extension codebase,
removing it entirely would be a non-trivial effort. So, minimally, this
commit splits `network` into two stores: `networkId` and
`networkStatus`. But it also expands the concept of network status.
Previously, the network was in one of two states: "loading" and
"not-loading". But now it can be in one of four states:
- `available`: The network is able to receive and respond to requests.
- `unavailable`: The network is not able to receive and respond to
requests for unknown reasons.
- `blocked`: The network is actively blocking requests based on the
user's geolocation. (This is specific to Infura.)
- `unknown`: We don't know whether the network can receive and respond
to requests, either because we haven't checked or we tried to check
and were unsuccessful.
This commit also changes how the network status is determined —
specifically, how many requests are used to determine that status, when
they occur, and whether they are awaited. Previously, the network
controller would make 2 to 3 requests during the course of running
`lookupNetwork`.
* First, if it was an Infura network, it would make a request for
`eth_blockNumber` to determine whether Infura was blocking requests or
not, then emit an appropriate event. This operation was not awaited.
* Then, regardless of the network, it would fetch the network ID via
`net_version`. This operation was awaited.
* Finally, regardless of the network, it would fetch the latest block
via `eth_getBlockByNumber`, then use the result to determine whether
the network supported EIP-1559. This operation was awaited.
Now:
* One fewer request is made, specifically `eth_blockNumber`, as we don't
need to make an extra request to determine whether Infura is blocking
requests; we can reuse `eth_getBlockByNumber`;
* All requests are awaited, which makes `lookupNetwork` run fully
in-band instead of partially out-of-band; and
* Both requests for `net_version` and `eth_getBlockByNumber` are
performed in parallel to make `lookupNetwork` run slightly faster.
2023-03-31 00:49:12 +02:00
|
|
|
transactionMatchesNetwork(tx, chainId, networkId),
|
2021-02-04 19:15:23 +01:00
|
|
|
);
|
|
|
|
};
|
|
|
|
export const unapprovedMsgsSelector = (state) => state.metamask.unapprovedMsgs;
|
2020-11-03 00:41:28 +01:00
|
|
|
export const currentNetworkTxListSelector = (state) =>
|
2021-02-04 19:15:23 +01:00
|
|
|
state.metamask.currentNetworkTxList;
|
2020-11-03 00:41:28 +01:00
|
|
|
export const unapprovedPersonalMsgsSelector = (state) =>
|
2021-02-04 19:15:23 +01:00
|
|
|
state.metamask.unapprovedPersonalMsgs;
|
2020-11-03 00:41:28 +01:00
|
|
|
export const unapprovedDecryptMsgsSelector = (state) =>
|
2021-02-04 19:15:23 +01:00
|
|
|
state.metamask.unapprovedDecryptMsgs;
|
2020-11-03 00:41:28 +01:00
|
|
|
export const unapprovedEncryptionPublicKeyMsgsSelector = (state) =>
|
2021-02-04 19:15:23 +01:00
|
|
|
state.metamask.unapprovedEncryptionPublicKeyMsgs;
|
2020-11-03 00:41:28 +01:00
|
|
|
export const unapprovedTypedMessagesSelector = (state) =>
|
2021-02-04 19:15:23 +01:00
|
|
|
state.metamask.unapprovedTypedMessages;
|
2018-12-09 21:48:06 +01:00
|
|
|
|
2022-02-18 17:48:38 +01:00
|
|
|
export const smartTransactionsListSelector = (state) =>
|
|
|
|
state.metamask.smartTransactionsState?.smartTransactions?.[
|
|
|
|
getCurrentChainId(state)
|
|
|
|
]
|
|
|
|
?.filter((stx) => !stx.confirmed)
|
|
|
|
.map((stx) => ({
|
|
|
|
...stx,
|
2023-01-18 15:47:29 +01:00
|
|
|
transactionType: TransactionType.smart,
|
2022-02-18 17:48:38 +01:00
|
|
|
status: stx.status?.startsWith('cancelled')
|
2023-01-18 15:47:29 +01:00
|
|
|
? SmartTransactionStatus.cancelled
|
2022-02-18 17:48:38 +01:00
|
|
|
: stx.status,
|
|
|
|
}));
|
|
|
|
|
2020-03-06 22:34:56 +01:00
|
|
|
export const selectedAddressTxListSelector = createSelector(
|
|
|
|
getSelectedAddress,
|
|
|
|
currentNetworkTxListSelector,
|
2022-02-18 17:48:38 +01:00
|
|
|
smartTransactionsListSelector,
|
|
|
|
(selectedAddress, transactions = [], smTransactions = []) => {
|
|
|
|
return transactions
|
|
|
|
.filter(({ txParams }) => txParams.from === selectedAddress)
|
|
|
|
.concat(smTransactions);
|
2020-07-14 17:20:41 +02:00
|
|
|
},
|
2021-02-04 19:15:23 +01:00
|
|
|
);
|
2020-03-06 22:34:56 +01:00
|
|
|
|
2018-12-09 21:48:06 +01:00
|
|
|
export const unapprovedMessagesSelector = createSelector(
|
|
|
|
unapprovedMsgsSelector,
|
|
|
|
unapprovedPersonalMsgsSelector,
|
2020-02-19 19:24:16 +01:00
|
|
|
unapprovedDecryptMsgsSelector,
|
|
|
|
unapprovedEncryptionPublicKeyMsgsSelector,
|
2018-12-09 21:48:06 +01:00
|
|
|
unapprovedTypedMessagesSelector,
|
2021-03-12 23:23:26 +01:00
|
|
|
deprecatedGetCurrentNetworkId,
|
2021-03-01 16:15:42 +01:00
|
|
|
getCurrentChainId,
|
2018-12-09 21:48:06 +01:00
|
|
|
(
|
|
|
|
unapprovedMsgs = {},
|
|
|
|
unapprovedPersonalMsgs = {},
|
2020-02-19 19:24:16 +01:00
|
|
|
unapprovedDecryptMsgs = {},
|
|
|
|
unapprovedEncryptionPublicKeyMsgs = {},
|
2018-12-09 21:48:06 +01:00
|
|
|
unapprovedTypedMessages = {},
|
2020-07-14 17:20:41 +02:00
|
|
|
network,
|
2021-03-01 16:15:42 +01:00
|
|
|
chainId,
|
2020-11-03 00:41:28 +01:00
|
|
|
) =>
|
|
|
|
txHelper(
|
|
|
|
{},
|
|
|
|
unapprovedMsgs,
|
|
|
|
unapprovedPersonalMsgs,
|
|
|
|
unapprovedDecryptMsgs,
|
|
|
|
unapprovedEncryptionPublicKeyMsgs,
|
|
|
|
unapprovedTypedMessages,
|
|
|
|
network,
|
2021-03-01 16:15:42 +01:00
|
|
|
chainId,
|
2020-11-03 00:41:28 +01:00
|
|
|
) || [],
|
2021-02-04 19:15:23 +01:00
|
|
|
);
|
2018-07-31 07:03:20 +02:00
|
|
|
|
2020-03-06 22:34:56 +01:00
|
|
|
export const transactionSubSelector = createSelector(
|
2018-12-09 21:48:06 +01:00
|
|
|
unapprovedMessagesSelector,
|
2019-08-16 20:54:10 +02:00
|
|
|
incomingTxListSelector,
|
2020-04-24 17:33:24 +02:00
|
|
|
(unapprovedMessages = [], incomingTxList = []) => {
|
2021-02-04 19:15:23 +01:00
|
|
|
return unapprovedMessages.concat(incomingTxList);
|
2020-07-14 17:20:41 +02:00
|
|
|
},
|
2021-02-04 19:15:23 +01:00
|
|
|
);
|
2020-03-06 22:34:56 +01:00
|
|
|
|
|
|
|
export const transactionsSelector = createSelector(
|
|
|
|
transactionSubSelector,
|
2018-07-31 07:03:20 +02:00
|
|
|
selectedAddressTxListSelector,
|
2020-05-28 05:11:15 +02:00
|
|
|
(subSelectorTxList = [], selectedAddressTxList = []) => {
|
2021-02-04 19:15:23 +01:00
|
|
|
const txsToRender = selectedAddressTxList.concat(subSelectorTxList);
|
2018-07-31 07:03:20 +02:00
|
|
|
|
2021-02-04 19:15:23 +01:00
|
|
|
return txsToRender.sort((a, b) => b.time - a.time);
|
2020-07-14 17:20:41 +02:00
|
|
|
},
|
2021-02-04 19:15:23 +01:00
|
|
|
);
|
2018-07-31 07:03:20 +02:00
|
|
|
|
2018-12-09 21:48:06 +01:00
|
|
|
/**
|
|
|
|
* @name insertOrderedNonce
|
|
|
|
* @private
|
|
|
|
* @description Inserts (mutates) a nonce into an array of ordered nonces, sorted in ascending
|
|
|
|
* order.
|
|
|
|
* @param {string[]} nonces - Array of nonce strings in hex
|
|
|
|
* @param {string} nonceToInsert - Nonce string in hex to be inserted into the array of nonces.
|
|
|
|
*/
|
|
|
|
const insertOrderedNonce = (nonces, nonceToInsert) => {
|
2021-02-04 19:15:23 +01:00
|
|
|
let insertIndex = nonces.length;
|
2018-12-09 21:48:06 +01:00
|
|
|
|
|
|
|
for (let i = 0; i < nonces.length; i++) {
|
2021-02-04 19:15:23 +01:00
|
|
|
const nonce = nonces[i];
|
2018-12-09 21:48:06 +01:00
|
|
|
|
2019-01-14 23:34:37 +01:00
|
|
|
if (Number(hexToDecimal(nonce)) > Number(hexToDecimal(nonceToInsert))) {
|
2021-02-04 19:15:23 +01:00
|
|
|
insertIndex = i;
|
|
|
|
break;
|
2018-12-09 21:48:06 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-02-04 19:15:23 +01:00
|
|
|
nonces.splice(insertIndex, 0, nonceToInsert);
|
|
|
|
};
|
2018-12-09 21:48:06 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @name insertTransactionByTime
|
|
|
|
* @private
|
|
|
|
* @description Inserts (mutates) a transaction object into an array of ordered transactions, sorted
|
|
|
|
* in ascending order by time.
|
2022-07-27 15:28:05 +02:00
|
|
|
* @param {object[]} transactions - Array of transaction objects.
|
|
|
|
* @param {object} transaction - Transaction object to be inserted into the array of transactions.
|
2018-12-09 21:48:06 +01:00
|
|
|
*/
|
|
|
|
const insertTransactionByTime = (transactions, transaction) => {
|
2021-02-04 19:15:23 +01:00
|
|
|
const { time } = transaction;
|
2018-12-09 21:48:06 +01:00
|
|
|
|
2021-02-04 19:15:23 +01:00
|
|
|
let insertIndex = transactions.length;
|
2018-12-09 21:48:06 +01:00
|
|
|
|
|
|
|
for (let i = 0; i < transactions.length; i++) {
|
2021-02-04 19:15:23 +01:00
|
|
|
const tx = transactions[i];
|
2018-12-09 21:48:06 +01:00
|
|
|
|
|
|
|
if (tx.time > time) {
|
2021-02-04 19:15:23 +01:00
|
|
|
insertIndex = i;
|
|
|
|
break;
|
2018-12-09 21:48:06 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-02-04 19:15:23 +01:00
|
|
|
transactions.splice(insertIndex, 0, transaction);
|
|
|
|
};
|
2018-12-09 21:48:06 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Contains transactions and properties associated with those transactions of the same nonce.
|
2022-01-07 16:57:33 +01:00
|
|
|
*
|
2022-07-27 15:28:05 +02:00
|
|
|
* @typedef {object} transactionGroup
|
2018-12-09 21:48:06 +01:00
|
|
|
* @property {string} nonce - The nonce that the transactions within this transactionGroup share.
|
2022-07-27 15:28:05 +02:00
|
|
|
* @property {object[]} transactions - An array of transaction (txMeta) objects.
|
|
|
|
* @property {object} initialTransaction - The transaction (txMeta) with the lowest "time".
|
|
|
|
* @property {object} primaryTransaction - Either the latest transaction or the confirmed
|
2018-12-09 21:48:06 +01:00
|
|
|
* transaction.
|
|
|
|
* @property {boolean} hasRetried - True if a transaction in the group was a retry transaction.
|
|
|
|
* @property {boolean} hasCancelled - True if a transaction in the group was a cancel transaction.
|
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @name insertTransactionGroupByTime
|
|
|
|
* @private
|
|
|
|
* @description Inserts (mutates) a transactionGroup object into an array of ordered
|
|
|
|
* transactionGroups, sorted in ascending order by nonce.
|
|
|
|
* @param {transactionGroup[]} transactionGroups - Array of transactionGroup objects.
|
|
|
|
* @param {transactionGroup} transactionGroup - transactionGroup object to be inserted into the
|
|
|
|
* array of transactionGroups.
|
|
|
|
*/
|
|
|
|
const insertTransactionGroupByTime = (transactionGroups, transactionGroup) => {
|
2022-07-31 20:26:40 +02:00
|
|
|
const { primaryTransaction: { time: groupToInsertTime } = {} } =
|
|
|
|
transactionGroup;
|
2018-12-09 21:48:06 +01:00
|
|
|
|
2021-02-04 19:15:23 +01:00
|
|
|
let insertIndex = transactionGroups.length;
|
2018-12-09 21:48:06 +01:00
|
|
|
|
|
|
|
for (let i = 0; i < transactionGroups.length; i++) {
|
2021-02-04 19:15:23 +01:00
|
|
|
const txGroup = transactionGroups[i];
|
|
|
|
const { primaryTransaction: { time } = {} } = txGroup;
|
2018-12-09 21:48:06 +01:00
|
|
|
|
2019-01-14 23:34:37 +01:00
|
|
|
if (time > groupToInsertTime) {
|
2021-02-04 19:15:23 +01:00
|
|
|
insertIndex = i;
|
|
|
|
break;
|
2018-12-09 21:48:06 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-02-04 19:15:23 +01:00
|
|
|
transactionGroups.splice(insertIndex, 0, transactionGroup);
|
|
|
|
};
|
2018-12-09 21:48:06 +01:00
|
|
|
|
2019-01-14 23:34:37 +01:00
|
|
|
/**
|
2019-08-16 20:54:10 +02:00
|
|
|
* @name mergeNonNonceTransactionGroups
|
2019-01-14 23:34:37 +01:00
|
|
|
* @private
|
2019-08-16 20:54:10 +02:00
|
|
|
* @description Inserts (mutates) transactionGroups that are not to be ordered by nonce into an array
|
2020-04-24 17:33:24 +02:00
|
|
|
* of nonce-ordered transactionGroups by time.
|
2019-01-14 23:34:37 +01:00
|
|
|
* @param {transactionGroup[]} orderedTransactionGroups - Array of transactionGroups ordered by
|
|
|
|
* nonce.
|
2019-08-16 20:54:10 +02:00
|
|
|
* @param {transactionGroup[]} nonNonceTransactionGroups - Array of transactionGroups not intended to be ordered by nonce,
|
|
|
|
* but intended to be ordered by timestamp
|
2019-01-14 23:34:37 +01:00
|
|
|
*/
|
2020-11-03 00:41:28 +01:00
|
|
|
const mergeNonNonceTransactionGroups = (
|
|
|
|
orderedTransactionGroups,
|
|
|
|
nonNonceTransactionGroups,
|
|
|
|
) => {
|
2020-04-24 17:33:24 +02:00
|
|
|
nonNonceTransactionGroups.forEach((transactionGroup) => {
|
2021-02-04 19:15:23 +01:00
|
|
|
insertTransactionGroupByTime(orderedTransactionGroups, transactionGroup);
|
|
|
|
});
|
|
|
|
};
|
2019-01-14 23:34:37 +01:00
|
|
|
|
2018-12-09 21:48:06 +01:00
|
|
|
/**
|
|
|
|
* @name nonceSortedTransactionsSelector
|
|
|
|
* @description Returns an array of transactionGroups sorted by nonce in ascending order.
|
|
|
|
* @returns {transactionGroup[]}
|
|
|
|
*/
|
|
|
|
export const nonceSortedTransactionsSelector = createSelector(
|
2018-07-31 07:03:20 +02:00
|
|
|
transactionsSelector,
|
2018-12-09 21:48:06 +01:00
|
|
|
(transactions = []) => {
|
2021-02-04 19:15:23 +01:00
|
|
|
const unapprovedTransactionGroups = [];
|
|
|
|
const incomingTransactionGroups = [];
|
|
|
|
const orderedNonces = [];
|
|
|
|
const nonceToTransactionsMap = {};
|
2018-12-09 21:48:06 +01:00
|
|
|
|
2020-02-15 21:34:12 +01:00
|
|
|
transactions.forEach((transaction) => {
|
2020-11-03 00:41:28 +01:00
|
|
|
const {
|
|
|
|
txParams: { nonce } = {},
|
|
|
|
status,
|
|
|
|
type,
|
|
|
|
time: txTime,
|
2022-04-25 21:39:34 +02:00
|
|
|
txReceipt,
|
2021-02-04 19:15:23 +01:00
|
|
|
} = transaction;
|
2018-12-09 21:48:06 +01:00
|
|
|
|
2023-05-03 17:00:38 +02:00
|
|
|
// Don't group transactions by nonce if:
|
|
|
|
// 1. Tx nonce is undefined
|
|
|
|
// 2. Tx is incoming (deposit)
|
|
|
|
// 3. Tx is custodial (mmi specific)
|
|
|
|
let shouldNotBeGrouped =
|
|
|
|
typeof nonce === 'undefined' || type === TransactionType.incoming;
|
|
|
|
|
|
|
|
///: BEGIN:ONLY_INCLUDE_IN(build-mmi)
|
|
|
|
shouldNotBeGrouped = shouldNotBeGrouped || Boolean(transaction.custodyId);
|
|
|
|
///: END:ONLY_INCLUDE_IN
|
|
|
|
|
|
|
|
if (shouldNotBeGrouped) {
|
2018-12-09 21:48:06 +01:00
|
|
|
const transactionGroup = {
|
|
|
|
transactions: [transaction],
|
|
|
|
initialTransaction: transaction,
|
|
|
|
primaryTransaction: transaction,
|
|
|
|
hasRetried: false,
|
|
|
|
hasCancelled: false,
|
2022-04-21 16:45:31 +02:00
|
|
|
nonce,
|
2021-02-04 19:15:23 +01:00
|
|
|
};
|
2018-12-09 21:48:06 +01:00
|
|
|
|
2023-01-18 15:47:29 +01:00
|
|
|
if (type === TransactionType.incoming) {
|
2021-02-04 19:15:23 +01:00
|
|
|
incomingTransactionGroups.push(transactionGroup);
|
2019-01-14 23:34:37 +01:00
|
|
|
} else {
|
2020-11-03 00:41:28 +01:00
|
|
|
insertTransactionGroupByTime(
|
|
|
|
unapprovedTransactionGroups,
|
|
|
|
transactionGroup,
|
2021-02-04 19:15:23 +01:00
|
|
|
);
|
2019-01-14 23:34:37 +01:00
|
|
|
}
|
2018-12-09 21:48:06 +01:00
|
|
|
} else if (nonce in nonceToTransactionsMap) {
|
2021-02-04 19:15:23 +01:00
|
|
|
const nonceProps = nonceToTransactionsMap[nonce];
|
|
|
|
insertTransactionByTime(nonceProps.transactions, transaction);
|
2018-12-09 21:48:06 +01:00
|
|
|
|
2020-11-03 00:41:28 +01:00
|
|
|
const {
|
|
|
|
primaryTransaction: { time: primaryTxTime = 0 } = {},
|
2022-04-25 21:39:34 +02:00
|
|
|
initialTransaction: { time: initialTxTime = 0 } = {},
|
2021-02-04 19:15:23 +01:00
|
|
|
} = nonceProps;
|
2020-11-03 00:41:28 +01:00
|
|
|
|
2022-04-25 21:39:34 +02:00
|
|
|
// Current Transaction Logic Cases
|
|
|
|
// --------------------------------------------------------------------
|
|
|
|
// Current transaction: The transaction we are examining in this loop.
|
|
|
|
// Each iteration should be in time order, but that is not guaranteed.
|
|
|
|
// --------------------------------------------------------------------
|
|
|
|
const currentTransaction = {
|
|
|
|
// A on chain failure means the current transaction was submitted and
|
|
|
|
// considered for inclusion in a block but something prevented it
|
|
|
|
// from being included, such as slippage on gas prices and conversion
|
|
|
|
// when doing a swap. These transactions will have a '0x0' value in
|
|
|
|
// the txReceipt.status field.
|
|
|
|
isOnChainFailure: txReceipt?.status === '0x0',
|
|
|
|
// Another type of failure is a "off chain" or "network" failure,
|
|
|
|
// where the error occurs on the JSON RPC call to the network client
|
|
|
|
// (Like Infura). These transactions are never broadcast for
|
|
|
|
// inclusion and the nonce associated with them is not consumed. When
|
|
|
|
// this occurs the next transaction will have the same nonce as the
|
|
|
|
// current, failed transaction. A failed on chain transaction will
|
|
|
|
// not have the FAILED status although it should (future TODO: add a
|
|
|
|
// new FAILED_ON_CHAIN) status. I use the word "Ephemeral" here
|
|
|
|
// because a failed transaction that does not get broadcast is not
|
|
|
|
// known outside of the user's local MetaMask and the nonce
|
|
|
|
// associated will be applied to the next.
|
|
|
|
isEphemeral:
|
2023-01-18 15:47:29 +01:00
|
|
|
status === TransactionStatus.failed && txReceipt?.status !== '0x0',
|
2022-04-25 21:39:34 +02:00
|
|
|
// We never want to use a speed up (retry) or cancel as the initial
|
|
|
|
// transaction in a group, regardless of time order. This is because
|
|
|
|
// useTransactionDisplayData cannot parse a retry or cancel because
|
|
|
|
// it lacks information on whether its a simple send, token transfer,
|
|
|
|
// etc.
|
|
|
|
isRetryOrCancel: INVALID_INITIAL_TRANSACTION_TYPES.includes(type),
|
|
|
|
// Primary transactions usually are the latest transaction by time,
|
|
|
|
// but not always. This value shows whether this transaction occurred
|
|
|
|
// after the current primary.
|
|
|
|
occurredAfterPrimary: txTime > primaryTxTime,
|
|
|
|
// Priority Statuses are those that are ones either already confirmed
|
|
|
|
// on chain, submitted to the network, or waiting for user approval.
|
|
|
|
// These statuses typically indicate a transaction that needs to have
|
|
|
|
// its status reflected in the UI.
|
|
|
|
hasPriorityStatus: status in PRIORITY_STATUS_HASH,
|
|
|
|
// A confirmed transaction is the most valid transaction status to
|
|
|
|
// display because no other transaction of the same nonce can have a
|
|
|
|
// more valid status.
|
2023-01-18 15:47:29 +01:00
|
|
|
isConfirmed: status === TransactionStatus.confirmed,
|
2022-04-25 21:39:34 +02:00
|
|
|
// Initial transactions usually are the earliest transaction by time,
|
|
|
|
// but not always. THis value shows whether this transaction occurred
|
|
|
|
// before the current initial.
|
|
|
|
occurredBeforeInitial: txTime < initialTxTime,
|
|
|
|
// We only allow users to retry the transaction in certain scenarios
|
|
|
|
// to help shield from expensive operations and other unwanted side
|
|
|
|
// effects. This value is used to determine if the entire transaction
|
|
|
|
// group should be marked as having had a retry.
|
|
|
|
isValidRetry:
|
2023-01-18 15:47:29 +01:00
|
|
|
type === TransactionType.retry &&
|
2022-04-25 21:39:34 +02:00
|
|
|
(status in PRIORITY_STATUS_HASH ||
|
2023-01-18 15:47:29 +01:00
|
|
|
status === TransactionStatus.dropped),
|
2022-04-25 21:39:34 +02:00
|
|
|
// We only allow users to cancel the transaction in certain scenarios
|
|
|
|
// to help shield from expensive operations and other unwanted side
|
|
|
|
// effects. This value is used to determine if the entire transaction
|
|
|
|
// group should be marked as having had a cancel.
|
|
|
|
isValidCancel:
|
2023-01-18 15:47:29 +01:00
|
|
|
type === TransactionType.cancel &&
|
2022-04-25 21:39:34 +02:00
|
|
|
(status in PRIORITY_STATUS_HASH ||
|
2023-01-18 15:47:29 +01:00
|
|
|
status === TransactionStatus.dropped),
|
2022-04-25 21:39:34 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
// We should never assign a retry or cancel transaction as the initial,
|
|
|
|
// likewise an ephemeral transaction should not be initial.
|
|
|
|
currentTransaction.eligibleForInitial =
|
|
|
|
!currentTransaction.isRetryOrCancel &&
|
|
|
|
!currentTransaction.isEphemeral;
|
|
|
|
|
|
|
|
// If a transaction failed on chain or was confirmed then it should
|
|
|
|
// always be the primary because no other transaction is more valid.
|
|
|
|
currentTransaction.shouldBePrimary =
|
|
|
|
currentTransaction.isConfirmed || currentTransaction.isOnChainFailure;
|
|
|
|
|
|
|
|
// Primary Transaction Logic Cases
|
|
|
|
// --------------------------------------------------------------------
|
|
|
|
// Primary transaction: The transaction for any given nonce which has
|
|
|
|
// the most valid status on the network.
|
|
|
|
// Example:
|
|
|
|
// 1. Submit transaction A
|
|
|
|
// 2. Speed up Transaction A.
|
|
|
|
// 3. This creates a new Transaction (B) with higher gas params.
|
|
|
|
// 4. Transaction A and Transaction B are both submitted.
|
|
|
|
// 5. We expect Transaction B to be the most valid transaction to use
|
|
|
|
// for the status of the transaction group because it has higher
|
|
|
|
// gas params and should be included first.
|
|
|
|
// The following logic variables are used for edge cases that protect
|
|
|
|
// against UI bugs when this breaks down.
|
|
|
|
const previousPrimaryTransaction = {
|
|
|
|
// As we loop through the transactions in state we may temporarily
|
|
|
|
// assign a primaryTransaction that is an "Ephemeral" transaction,
|
|
|
|
// which is one that failed before being broadcast for inclusion in a
|
|
|
|
// block. When this happens, and we have another transaction to
|
|
|
|
// consider in a nonce group, we should use the new transaction.
|
|
|
|
isEphemeral:
|
2023-01-18 15:47:29 +01:00
|
|
|
nonceProps.primaryTransaction.status === TransactionStatus.failed &&
|
2022-04-25 21:39:34 +02:00
|
|
|
nonceProps.primaryTransaction?.txReceipt?.status !== '0x0',
|
|
|
|
};
|
2020-11-03 00:41:28 +01:00
|
|
|
|
2022-04-25 21:39:34 +02:00
|
|
|
// Initial Transaction Logic Cases
|
|
|
|
// --------------------------------------------------------------------
|
|
|
|
// Initial Transaction: The transaciton that most likely represents the
|
|
|
|
// user's intent when creating/approving the transaction. In most cases
|
|
|
|
// this is the first transaction of a nonce group, by time, but this
|
|
|
|
// breaks down in the case of users with the advanced setting enabled
|
|
|
|
// to set their own nonces manually. In that case a user may submit two
|
|
|
|
// completely different transactions of the same nonce and they will be
|
|
|
|
// bundled together by this selector as the same activity entry.
|
|
|
|
const previousInitialTransaction = {
|
|
|
|
// As we loop through the transactions in state we may temporarily
|
|
|
|
// assign a initialTransaction that is an "Ephemeral" transaction,
|
|
|
|
// which is one that failed before being broadcast for inclusion in a
|
|
|
|
// block. When this happens, and we have another transaction to
|
|
|
|
// consider in a nonce group, we should use the new transaction.
|
|
|
|
isEphemeral:
|
2023-01-18 15:47:29 +01:00
|
|
|
nonceProps.initialTransaction.status === TransactionStatus.failed &&
|
2022-04-25 21:39:34 +02:00
|
|
|
nonceProps.initialTransaction.txReceipt?.status !== '0x0',
|
|
|
|
};
|
|
|
|
|
|
|
|
// Check the above logic cases and assign a new primaryTransaction if
|
|
|
|
// appropriate
|
2020-11-03 00:41:28 +01:00
|
|
|
if (
|
2022-04-25 21:39:34 +02:00
|
|
|
currentTransaction.shouldBePrimary ||
|
|
|
|
previousPrimaryTransaction.isEphemeral ||
|
|
|
|
(currentTransaction.occurredAfterPrimary &&
|
|
|
|
currentTransaction.hasPriorityStatus)
|
2020-11-03 00:41:28 +01:00
|
|
|
) {
|
2021-02-04 19:15:23 +01:00
|
|
|
nonceProps.primaryTransaction = transaction;
|
2018-12-09 21:48:06 +01:00
|
|
|
}
|
|
|
|
|
2022-04-25 21:39:34 +02:00
|
|
|
// Check the above logic cases and assign a new initialTransaction if
|
|
|
|
// appropriate
|
|
|
|
if (
|
|
|
|
(currentTransaction.occurredBeforeInitial &&
|
|
|
|
currentTransaction.eligibleForInitial) ||
|
|
|
|
(previousInitialTransaction.isEphemeral &&
|
|
|
|
currentTransaction.eligibleForInitial)
|
|
|
|
) {
|
2021-02-04 19:15:23 +01:00
|
|
|
nonceProps.initialTransaction = transaction;
|
2018-12-09 21:48:06 +01:00
|
|
|
}
|
|
|
|
|
2022-04-25 21:39:34 +02:00
|
|
|
if (currentTransaction.isValidRetry) {
|
2021-02-04 19:15:23 +01:00
|
|
|
nonceProps.hasRetried = true;
|
2018-12-09 21:48:06 +01:00
|
|
|
}
|
|
|
|
|
2022-04-25 21:39:34 +02:00
|
|
|
if (currentTransaction.isValidCancel) {
|
2021-02-04 19:15:23 +01:00
|
|
|
nonceProps.hasCancelled = true;
|
2018-12-09 21:48:06 +01:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
nonceToTransactionsMap[nonce] = {
|
|
|
|
nonce,
|
|
|
|
transactions: [transaction],
|
|
|
|
initialTransaction: transaction,
|
|
|
|
primaryTransaction: transaction,
|
2021-09-15 23:42:06 +02:00
|
|
|
hasRetried:
|
2023-01-18 15:47:29 +01:00
|
|
|
transaction.type === TransactionType.retry &&
|
2022-04-21 16:45:31 +02:00
|
|
|
(transaction.status in PRIORITY_STATUS_HASH ||
|
2023-01-18 15:47:29 +01:00
|
|
|
transaction.status === TransactionStatus.dropped),
|
2021-09-15 23:42:06 +02:00
|
|
|
hasCancelled:
|
2023-01-18 15:47:29 +01:00
|
|
|
transaction.type === TransactionType.cancel &&
|
2022-04-21 16:45:31 +02:00
|
|
|
(transaction.status in PRIORITY_STATUS_HASH ||
|
2023-01-18 15:47:29 +01:00
|
|
|
transaction.status === TransactionStatus.dropped),
|
2021-02-04 19:15:23 +01:00
|
|
|
};
|
2018-12-09 21:48:06 +01:00
|
|
|
|
2021-02-04 19:15:23 +01:00
|
|
|
insertOrderedNonce(orderedNonces, nonce);
|
2018-12-09 21:48:06 +01:00
|
|
|
}
|
2021-02-04 19:15:23 +01:00
|
|
|
});
|
2018-12-09 21:48:06 +01:00
|
|
|
|
2020-11-03 00:41:28 +01:00
|
|
|
const orderedTransactionGroups = orderedNonces.map(
|
|
|
|
(nonce) => nonceToTransactionsMap[nonce],
|
2021-02-04 19:15:23 +01:00
|
|
|
);
|
2020-11-03 00:41:28 +01:00
|
|
|
mergeNonNonceTransactionGroups(
|
|
|
|
orderedTransactionGroups,
|
|
|
|
incomingTransactionGroups,
|
2021-02-04 19:15:23 +01:00
|
|
|
);
|
2022-04-25 21:39:34 +02:00
|
|
|
return unapprovedTransactionGroups
|
|
|
|
.concat(orderedTransactionGroups)
|
|
|
|
.map((txGroup) => {
|
|
|
|
// In the case that we have a cancel or retry as initial transaction
|
|
|
|
// and there is a valid transaction in the group, we should reassign
|
|
|
|
// the other valid transaction as initial. In this case validity of the
|
|
|
|
// transaction is expanded to include off-chain failures because it is
|
|
|
|
// valid to retry those with higher gas prices.
|
|
|
|
if (
|
|
|
|
INVALID_INITIAL_TRANSACTION_TYPES.includes(
|
|
|
|
txGroup.initialTransaction?.type,
|
|
|
|
)
|
|
|
|
) {
|
|
|
|
const nonRetryOrCancel = txGroup.transactions.find(
|
|
|
|
(tx) => !INVALID_INITIAL_TRANSACTION_TYPES.includes(tx.type),
|
|
|
|
);
|
|
|
|
if (nonRetryOrCancel) {
|
|
|
|
return {
|
|
|
|
...txGroup,
|
|
|
|
initialTransaction: nonRetryOrCancel,
|
|
|
|
};
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return txGroup;
|
|
|
|
});
|
2020-07-14 17:20:41 +02:00
|
|
|
},
|
2021-02-04 19:15:23 +01:00
|
|
|
);
|
2018-12-09 21:48:06 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @name nonceSortedPendingTransactionsSelector
|
|
|
|
* @description Returns an array of transactionGroups where transactions are still pending sorted by
|
|
|
|
* nonce in descending order.
|
|
|
|
* @returns {transactionGroup[]}
|
|
|
|
*/
|
|
|
|
export const nonceSortedPendingTransactionsSelector = createSelector(
|
|
|
|
nonceSortedTransactionsSelector,
|
2020-11-03 00:41:28 +01:00
|
|
|
(transactions = []) =>
|
|
|
|
transactions.filter(
|
|
|
|
({ primaryTransaction }) =>
|
|
|
|
primaryTransaction.status in PENDING_STATUS_HASH,
|
|
|
|
),
|
2021-02-04 19:15:23 +01:00
|
|
|
);
|
2018-07-31 07:03:20 +02:00
|
|
|
|
2018-12-09 21:48:06 +01:00
|
|
|
/**
|
|
|
|
* @name nonceSortedCompletedTransactionsSelector
|
|
|
|
* @description Returns an array of transactionGroups where transactions are confirmed sorted by
|
|
|
|
* nonce in descending order.
|
|
|
|
* @returns {transactionGroup[]}
|
|
|
|
*/
|
|
|
|
export const nonceSortedCompletedTransactionsSelector = createSelector(
|
|
|
|
nonceSortedTransactionsSelector,
|
2020-11-03 00:41:28 +01:00
|
|
|
(transactions = []) =>
|
2019-01-14 23:34:37 +01:00
|
|
|
transactions
|
2020-11-03 00:41:28 +01:00
|
|
|
.filter(
|
|
|
|
({ primaryTransaction }) =>
|
|
|
|
!(primaryTransaction.status in PENDING_STATUS_HASH),
|
|
|
|
)
|
|
|
|
.reverse(),
|
2021-02-04 19:15:23 +01:00
|
|
|
);
|
2018-08-07 07:39:54 +02:00
|
|
|
|
2018-12-09 21:48:06 +01:00
|
|
|
export const submittedPendingTransactionsSelector = createSelector(
|
2018-07-31 07:03:20 +02:00
|
|
|
transactionsSelector,
|
2020-11-03 00:41:28 +01:00
|
|
|
(transactions = []) =>
|
|
|
|
transactions.filter(
|
2023-01-18 15:47:29 +01:00
|
|
|
(transaction) => transaction.status === TransactionStatus.submitted,
|
2020-11-03 00:41:28 +01:00
|
|
|
),
|
2021-02-04 19:15:23 +01:00
|
|
|
);
|
2023-05-11 07:56:17 +02:00
|
|
|
|
|
|
|
const hasUnapprovedTransactionsInCurrentNetwork = (state) => {
|
|
|
|
const { unapprovedTxs } = state.metamask;
|
|
|
|
const unapprovedTxRequests = getApprovalRequestsByType(
|
|
|
|
state,
|
|
|
|
ApprovalType.Transaction,
|
|
|
|
);
|
|
|
|
|
|
|
|
const chainId = getCurrentChainId(state);
|
|
|
|
|
|
|
|
const filteredUnapprovedTxInCurrentNetwork = unapprovedTxRequests.filter(
|
|
|
|
({ id }) => transactionMatchesNetwork(unapprovedTxs[id], chainId),
|
|
|
|
);
|
|
|
|
|
|
|
|
return filteredUnapprovedTxInCurrentNetwork.length > 0;
|
|
|
|
};
|
|
|
|
|
|
|
|
const TRANSACTION_APPROVAL_TYPES = [
|
|
|
|
ApprovalType.EthDecrypt,
|
|
|
|
ApprovalType.EthGetEncryptionPublicKey,
|
|
|
|
ApprovalType.EthSign,
|
|
|
|
ApprovalType.EthSignTypedData,
|
|
|
|
ApprovalType.PersonalSign,
|
|
|
|
];
|
|
|
|
|
|
|
|
export function hasTransactionPendingApprovals(state) {
|
|
|
|
return (
|
|
|
|
hasUnapprovedTransactionsInCurrentNetwork(state) ||
|
|
|
|
TRANSACTION_APPROVAL_TYPES.some((type) => hasPendingApprovals(state, type))
|
|
|
|
);
|
|
|
|
}
|