You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
355 lines
11 KiB
355 lines
11 KiB
4 years ago
|
import { createSelector } from 'reselect';
|
||
6 years ago
|
import {
|
||
5 years ago
|
PRIORITY_STATUS_HASH,
|
||
|
PENDING_STATUS_HASH,
|
||
4 years ago
|
} from '../helpers/constants/transactions';
|
||
|
import { hexToDecimal } from '../helpers/utils/conversions.util';
|
||
4 years ago
|
import txHelper from '../helpers/utils/tx-helper';
|
||
4 years ago
|
import {
|
||
|
TRANSACTION_STATUSES,
|
||
|
TRANSACTION_TYPES,
|
||
4 years ago
|
} from '../../shared/constants/transaction';
|
||
|
import { transactionMatchesNetwork } from '../../shared/modules/transaction.utils';
|
||
4 years ago
|
import { getCurrentChainId, deprecatedGetCurrentNetworkId } from './selectors';
|
||
4 years ago
|
import { getSelectedAddress } from '.';
|
||
6 years ago
|
|
||
5 years ago
|
export const incomingTxListSelector = (state) => {
|
||
4 years ago
|
const { showIncomingTransactions } = state.metamask.featureFlags;
|
||
5 years ago
|
if (!showIncomingTransactions) {
|
||
4 years ago
|
return [];
|
||
5 years ago
|
}
|
||
|
|
||
4 years ago
|
const {
|
||
|
network,
|
||
|
provider: { chainId },
|
||
|
} = state.metamask;
|
||
4 years ago
|
const selectedAddress = getSelectedAddress(state);
|
||
4 years ago
|
return Object.values(state.metamask.incomingTransactions).filter(
|
||
4 years ago
|
(tx) =>
|
||
|
tx.txParams.to === selectedAddress &&
|
||
|
transactionMatchesNetwork(tx, chainId, network),
|
||
4 years ago
|
);
|
||
|
};
|
||
|
export const unapprovedMsgsSelector = (state) => state.metamask.unapprovedMsgs;
|
||
4 years ago
|
export const currentNetworkTxListSelector = (state) =>
|
||
4 years ago
|
state.metamask.currentNetworkTxList;
|
||
4 years ago
|
export const unapprovedPersonalMsgsSelector = (state) =>
|
||
4 years ago
|
state.metamask.unapprovedPersonalMsgs;
|
||
4 years ago
|
export const unapprovedDecryptMsgsSelector = (state) =>
|
||
4 years ago
|
state.metamask.unapprovedDecryptMsgs;
|
||
4 years ago
|
export const unapprovedEncryptionPublicKeyMsgsSelector = (state) =>
|
||
4 years ago
|
state.metamask.unapprovedEncryptionPublicKeyMsgs;
|
||
4 years ago
|
export const unapprovedTypedMessagesSelector = (state) =>
|
||
4 years ago
|
state.metamask.unapprovedTypedMessages;
|
||
6 years ago
|
|
||
5 years ago
|
export const selectedAddressTxListSelector = createSelector(
|
||
|
getSelectedAddress,
|
||
|
currentNetworkTxListSelector,
|
||
|
(selectedAddress, transactions = []) => {
|
||
4 years ago
|
return transactions.filter(
|
||
|
({ txParams }) => txParams.from === selectedAddress,
|
||
4 years ago
|
);
|
||
4 years ago
|
},
|
||
4 years ago
|
);
|
||
5 years ago
|
|
||
6 years ago
|
export const unapprovedMessagesSelector = createSelector(
|
||
|
unapprovedMsgsSelector,
|
||
|
unapprovedPersonalMsgsSelector,
|
||
5 years ago
|
unapprovedDecryptMsgsSelector,
|
||
|
unapprovedEncryptionPublicKeyMsgsSelector,
|
||
6 years ago
|
unapprovedTypedMessagesSelector,
|
||
4 years ago
|
deprecatedGetCurrentNetworkId,
|
||
4 years ago
|
getCurrentChainId,
|
||
6 years ago
|
(
|
||
|
unapprovedMsgs = {},
|
||
|
unapprovedPersonalMsgs = {},
|
||
5 years ago
|
unapprovedDecryptMsgs = {},
|
||
|
unapprovedEncryptionPublicKeyMsgs = {},
|
||
6 years ago
|
unapprovedTypedMessages = {},
|
||
4 years ago
|
network,
|
||
4 years ago
|
chainId,
|
||
4 years ago
|
) =>
|
||
|
txHelper(
|
||
|
{},
|
||
|
unapprovedMsgs,
|
||
|
unapprovedPersonalMsgs,
|
||
|
unapprovedDecryptMsgs,
|
||
|
unapprovedEncryptionPublicKeyMsgs,
|
||
|
unapprovedTypedMessages,
|
||
|
network,
|
||
4 years ago
|
chainId,
|
||
4 years ago
|
) || [],
|
||
4 years ago
|
);
|
||
6 years ago
|
|
||
5 years ago
|
export const transactionSubSelector = createSelector(
|
||
6 years ago
|
unapprovedMessagesSelector,
|
||
5 years ago
|
incomingTxListSelector,
|
||
5 years ago
|
(unapprovedMessages = [], incomingTxList = []) => {
|
||
4 years ago
|
return unapprovedMessages.concat(incomingTxList);
|
||
4 years ago
|
},
|
||
4 years ago
|
);
|
||
5 years ago
|
|
||
|
export const transactionsSelector = createSelector(
|
||
|
transactionSubSelector,
|
||
6 years ago
|
selectedAddressTxListSelector,
|
||
4 years ago
|
(subSelectorTxList = [], selectedAddressTxList = []) => {
|
||
4 years ago
|
const txsToRender = selectedAddressTxList.concat(subSelectorTxList);
|
||
6 years ago
|
|
||
4 years ago
|
return txsToRender.sort((a, b) => b.time - a.time);
|
||
4 years ago
|
},
|
||
4 years ago
|
);
|
||
6 years ago
|
|
||
6 years ago
|
/**
|
||
|
* @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.
|
||
|
* @returns {string[]}
|
||
|
*/
|
||
|
const insertOrderedNonce = (nonces, nonceToInsert) => {
|
||
4 years ago
|
let insertIndex = nonces.length;
|
||
6 years ago
|
|
||
|
for (let i = 0; i < nonces.length; i++) {
|
||
4 years ago
|
const nonce = nonces[i];
|
||
6 years ago
|
|
||
6 years ago
|
if (Number(hexToDecimal(nonce)) > Number(hexToDecimal(nonceToInsert))) {
|
||
4 years ago
|
insertIndex = i;
|
||
|
break;
|
||
6 years ago
|
}
|
||
|
}
|
||
|
|
||
4 years ago
|
nonces.splice(insertIndex, 0, nonceToInsert);
|
||
|
};
|
||
6 years ago
|
|
||
|
/**
|
||
|
* @name insertTransactionByTime
|
||
|
* @private
|
||
|
* @description Inserts (mutates) a transaction object into an array of ordered transactions, sorted
|
||
|
* in ascending order by time.
|
||
|
* @param {Object[]} transactions - Array of transaction objects.
|
||
|
* @param {Object} transaction - Transaction object to be inserted into the array of transactions.
|
||
|
* @returns {Object[]}
|
||
|
*/
|
||
|
const insertTransactionByTime = (transactions, transaction) => {
|
||
4 years ago
|
const { time } = transaction;
|
||
6 years ago
|
|
||
4 years ago
|
let insertIndex = transactions.length;
|
||
6 years ago
|
|
||
|
for (let i = 0; i < transactions.length; i++) {
|
||
4 years ago
|
const tx = transactions[i];
|
||
6 years ago
|
|
||
|
if (tx.time > time) {
|
||
4 years ago
|
insertIndex = i;
|
||
|
break;
|
||
6 years ago
|
}
|
||
|
}
|
||
|
|
||
4 years ago
|
transactions.splice(insertIndex, 0, transaction);
|
||
|
};
|
||
6 years ago
|
|
||
|
/**
|
||
|
* Contains transactions and properties associated with those transactions of the same nonce.
|
||
|
* @typedef {Object} transactionGroup
|
||
|
* @property {string} nonce - The nonce that the transactions within this transactionGroup share.
|
||
|
* @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
|
||
|
* 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) => {
|
||
4 years ago
|
const {
|
||
|
primaryTransaction: { time: groupToInsertTime } = {},
|
||
4 years ago
|
} = transactionGroup;
|
||
6 years ago
|
|
||
4 years ago
|
let insertIndex = transactionGroups.length;
|
||
6 years ago
|
|
||
|
for (let i = 0; i < transactionGroups.length; i++) {
|
||
4 years ago
|
const txGroup = transactionGroups[i];
|
||
|
const { primaryTransaction: { time } = {} } = txGroup;
|
||
6 years ago
|
|
||
6 years ago
|
if (time > groupToInsertTime) {
|
||
4 years ago
|
insertIndex = i;
|
||
|
break;
|
||
6 years ago
|
}
|
||
|
}
|
||
|
|
||
4 years ago
|
transactionGroups.splice(insertIndex, 0, transactionGroup);
|
||
|
};
|
||
6 years ago
|
|
||
6 years ago
|
/**
|
||
5 years ago
|
* @name mergeNonNonceTransactionGroups
|
||
6 years ago
|
* @private
|
||
5 years ago
|
* @description Inserts (mutates) transactionGroups that are not to be ordered by nonce into an array
|
||
5 years ago
|
* of nonce-ordered transactionGroups by time.
|
||
6 years ago
|
* @param {transactionGroup[]} orderedTransactionGroups - Array of transactionGroups ordered by
|
||
|
* nonce.
|
||
5 years ago
|
* @param {transactionGroup[]} nonNonceTransactionGroups - Array of transactionGroups not intended to be ordered by nonce,
|
||
|
* but intended to be ordered by timestamp
|
||
6 years ago
|
*/
|
||
4 years ago
|
const mergeNonNonceTransactionGroups = (
|
||
|
orderedTransactionGroups,
|
||
|
nonNonceTransactionGroups,
|
||
|
) => {
|
||
5 years ago
|
nonNonceTransactionGroups.forEach((transactionGroup) => {
|
||
4 years ago
|
insertTransactionGroupByTime(orderedTransactionGroups, transactionGroup);
|
||
|
});
|
||
|
};
|
||
6 years ago
|
|
||
6 years ago
|
/**
|
||
|
* @name nonceSortedTransactionsSelector
|
||
|
* @description Returns an array of transactionGroups sorted by nonce in ascending order.
|
||
|
* @returns {transactionGroup[]}
|
||
|
*/
|
||
|
export const nonceSortedTransactionsSelector = createSelector(
|
||
6 years ago
|
transactionsSelector,
|
||
6 years ago
|
(transactions = []) => {
|
||
4 years ago
|
const unapprovedTransactionGroups = [];
|
||
|
const incomingTransactionGroups = [];
|
||
|
const orderedNonces = [];
|
||
|
const nonceToTransactionsMap = {};
|
||
6 years ago
|
|
||
5 years ago
|
transactions.forEach((transaction) => {
|
||
4 years ago
|
const {
|
||
|
txParams: { nonce } = {},
|
||
|
status,
|
||
|
type,
|
||
|
time: txTime,
|
||
4 years ago
|
} = transaction;
|
||
6 years ago
|
|
||
4 years ago
|
if (typeof nonce === 'undefined' || type === TRANSACTION_TYPES.INCOMING) {
|
||
6 years ago
|
const transactionGroup = {
|
||
|
transactions: [transaction],
|
||
|
initialTransaction: transaction,
|
||
|
primaryTransaction: transaction,
|
||
|
hasRetried: false,
|
||
|
hasCancelled: false,
|
||
4 years ago
|
};
|
||
6 years ago
|
|
||
4 years ago
|
if (type === TRANSACTION_TYPES.INCOMING) {
|
||
4 years ago
|
incomingTransactionGroups.push(transactionGroup);
|
||
6 years ago
|
} else {
|
||
4 years ago
|
insertTransactionGroupByTime(
|
||
|
unapprovedTransactionGroups,
|
||
|
transactionGroup,
|
||
4 years ago
|
);
|
||
6 years ago
|
}
|
||
6 years ago
|
} else if (nonce in nonceToTransactionsMap) {
|
||
4 years ago
|
const nonceProps = nonceToTransactionsMap[nonce];
|
||
|
insertTransactionByTime(nonceProps.transactions, transaction);
|
||
6 years ago
|
|
||
4 years ago
|
const {
|
||
|
primaryTransaction: { time: primaryTxTime = 0 } = {},
|
||
4 years ago
|
} = nonceProps;
|
||
4 years ago
|
|
||
|
const previousPrimaryIsNetworkFailure =
|
||
4 years ago
|
nonceProps.primaryTransaction.status ===
|
||
|
TRANSACTION_STATUSES.FAILED &&
|
||
4 years ago
|
nonceProps.primaryTransaction?.txReceipt?.status !== '0x0';
|
||
4 years ago
|
const currentTransactionIsOnChainFailure =
|
||
4 years ago
|
transaction?.txReceipt?.status === '0x0';
|
||
4 years ago
|
|
||
|
if (
|
||
4 years ago
|
status === TRANSACTION_STATUSES.CONFIRMED ||
|
||
4 years ago
|
currentTransactionIsOnChainFailure ||
|
||
|
previousPrimaryIsNetworkFailure ||
|
||
|
(txTime > primaryTxTime && status in PRIORITY_STATUS_HASH)
|
||
|
) {
|
||
4 years ago
|
nonceProps.primaryTransaction = transaction;
|
||
6 years ago
|
}
|
||
|
|
||
4 years ago
|
const {
|
||
|
initialTransaction: { time: initialTxTime = 0 } = {},
|
||
4 years ago
|
} = nonceProps;
|
||
6 years ago
|
|
||
|
// Used to display the transaction action, since we don't want to overwrite the action if
|
||
|
// it was replaced with a cancel attempt transaction.
|
||
|
if (txTime < initialTxTime) {
|
||
4 years ago
|
nonceProps.initialTransaction = transaction;
|
||
6 years ago
|
}
|
||
|
|
||
4 years ago
|
if (type === TRANSACTION_TYPES.RETRY) {
|
||
4 years ago
|
nonceProps.hasRetried = true;
|
||
6 years ago
|
}
|
||
|
|
||
4 years ago
|
if (type === TRANSACTION_TYPES.CANCEL) {
|
||
4 years ago
|
nonceProps.hasCancelled = true;
|
||
6 years ago
|
}
|
||
|
} else {
|
||
|
nonceToTransactionsMap[nonce] = {
|
||
|
nonce,
|
||
|
transactions: [transaction],
|
||
|
initialTransaction: transaction,
|
||
|
primaryTransaction: transaction,
|
||
4 years ago
|
hasRetried: transaction.type === TRANSACTION_TYPES.RETRY,
|
||
|
hasCancelled: transaction.type === TRANSACTION_TYPES.CANCEL,
|
||
4 years ago
|
};
|
||
6 years ago
|
|
||
4 years ago
|
insertOrderedNonce(orderedNonces, nonce);
|
||
6 years ago
|
}
|
||
4 years ago
|
});
|
||
6 years ago
|
|
||
4 years ago
|
const orderedTransactionGroups = orderedNonces.map(
|
||
|
(nonce) => nonceToTransactionsMap[nonce],
|
||
4 years ago
|
);
|
||
4 years ago
|
mergeNonNonceTransactionGroups(
|
||
|
orderedTransactionGroups,
|
||
|
incomingTransactionGroups,
|
||
4 years ago
|
);
|
||
|
return unapprovedTransactionGroups.concat(orderedTransactionGroups);
|
||
4 years ago
|
},
|
||
4 years ago
|
);
|
||
6 years ago
|
|
||
|
/**
|
||
|
* @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,
|
||
4 years ago
|
(transactions = []) =>
|
||
|
transactions.filter(
|
||
|
({ primaryTransaction }) =>
|
||
|
primaryTransaction.status in PENDING_STATUS_HASH,
|
||
|
),
|
||
4 years ago
|
);
|
||
6 years ago
|
|
||
6 years ago
|
/**
|
||
|
* @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,
|
||
4 years ago
|
(transactions = []) =>
|
||
6 years ago
|
transactions
|
||
4 years ago
|
.filter(
|
||
|
({ primaryTransaction }) =>
|
||
|
!(primaryTransaction.status in PENDING_STATUS_HASH),
|
||
|
)
|
||
|
.reverse(),
|
||
4 years ago
|
);
|
||
6 years ago
|
|
||
6 years ago
|
export const submittedPendingTransactionsSelector = createSelector(
|
||
6 years ago
|
transactionsSelector,
|
||
4 years ago
|
(transactions = []) =>
|
||
|
transactions.filter(
|
||
4 years ago
|
(transaction) => transaction.status === TRANSACTION_STATUSES.SUBMITTED,
|
||
4 years ago
|
),
|
||
4 years ago
|
);
|