2021-02-04 19:15:23 +01:00
|
|
|
import EventEmitter from 'safe-event-emitter';
|
|
|
|
import { ObservableStore } from '@metamask/obs-store';
|
|
|
|
import log from 'loglevel';
|
2021-03-30 16:54:05 +02:00
|
|
|
import { keyBy, mapValues, omitBy, pickBy, sortBy } from 'lodash';
|
2021-03-18 19:23:46 +01:00
|
|
|
import createId from '../../../../shared/modules/random-id';
|
2021-02-04 19:15:23 +01:00
|
|
|
import { TRANSACTION_STATUSES } from '../../../../shared/constants/transaction';
|
2021-02-09 00:22:30 +01:00
|
|
|
import { METAMASK_CONTROLLER_EVENTS } from '../../metamask-controller';
|
2021-03-01 16:15:42 +01:00
|
|
|
import { transactionMatchesNetwork } from '../../../../shared/modules/transaction.utils';
|
2022-04-26 19:07:39 +02:00
|
|
|
import { ORIGIN_METAMASK } from '../../../../shared/constants/app';
|
2020-11-03 00:41:28 +01:00
|
|
|
import {
|
|
|
|
generateHistoryEntry,
|
|
|
|
replayHistory,
|
|
|
|
snapshotFromTxMeta,
|
2021-02-04 19:15:23 +01:00
|
|
|
} from './lib/tx-state-history-helpers';
|
2022-02-18 17:48:38 +01:00
|
|
|
import {
|
|
|
|
getFinalStates,
|
|
|
|
normalizeAndValidateTxParams,
|
|
|
|
validateConfirmedExternalTransaction,
|
|
|
|
} from './lib/util';
|
2020-08-19 18:27:05 +02:00
|
|
|
|
2018-04-19 20:29:26 +02:00
|
|
|
/**
|
2020-11-07 08:38:12 +01:00
|
|
|
* TransactionStatuses reimported from the shared transaction constants file
|
2022-01-07 16:57:33 +01:00
|
|
|
*
|
2021-03-30 16:54:05 +02:00
|
|
|
* @typedef {import(
|
|
|
|
* '../../../../shared/constants/transaction'
|
|
|
|
* ).TransactionStatusString} TransactionStatusString
|
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @typedef {import('../../../../shared/constants/transaction').TxParams} TxParams
|
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @typedef {import(
|
|
|
|
* '../../../../shared/constants/transaction'
|
|
|
|
* ).TransactionMeta} TransactionMeta
|
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @typedef {Object} TransactionState
|
|
|
|
* @property {Record<string, TransactionMeta>} transactions - TransactionMeta
|
|
|
|
* keyed by the transaction's id.
|
2020-11-07 08:38:12 +01:00
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* TransactionStateManager is responsible for the state of a transaction and
|
|
|
|
* storing the transaction. It also has some convenience methods for finding
|
|
|
|
* subsets of transactions.
|
2022-01-07 16:57:33 +01:00
|
|
|
*
|
2020-11-07 08:38:12 +01:00
|
|
|
* @param {Object} opts
|
2021-03-30 16:54:05 +02:00
|
|
|
* @param {TransactionState} [opts.initState={ transactions: {} }] - initial
|
|
|
|
* transactions list keyed by id
|
2020-11-10 18:30:41 +01:00
|
|
|
* @param {number} [opts.txHistoryLimit] - limit for how many finished
|
2020-11-07 08:38:12 +01:00
|
|
|
* transactions can hang around in state
|
2020-11-10 18:30:41 +01:00
|
|
|
* @param {Function} opts.getNetwork - return network number
|
2020-11-07 08:38:12 +01:00
|
|
|
*/
|
2020-04-27 16:45:00 +02:00
|
|
|
export default class TransactionStateManager extends EventEmitter {
|
2021-03-01 16:15:42 +01:00
|
|
|
constructor({ initState, txHistoryLimit, getNetwork, getCurrentChainId }) {
|
2021-02-04 19:15:23 +01:00
|
|
|
super();
|
2017-09-12 18:59:59 +02:00
|
|
|
|
2021-03-30 16:54:05 +02:00
|
|
|
this.store = new ObservableStore({
|
|
|
|
transactions: {},
|
|
|
|
...initState,
|
|
|
|
});
|
2021-02-04 19:15:23 +01:00
|
|
|
this.txHistoryLimit = txHistoryLimit;
|
|
|
|
this.getNetwork = getNetwork;
|
2021-03-01 16:15:42 +01:00
|
|
|
this.getCurrentChainId = getCurrentChainId;
|
2017-08-11 23:19:35 +02:00
|
|
|
}
|
2017-09-27 01:52:08 +02:00
|
|
|
|
2018-04-19 20:29:26 +02:00
|
|
|
/**
|
2021-03-30 16:54:05 +02:00
|
|
|
* Generates a TransactionMeta object consisting of the fields required for
|
|
|
|
* use throughout the extension. The argument here will override everything
|
|
|
|
* in the resulting transaction meta.
|
|
|
|
*
|
|
|
|
* TODO: Don't overwrite everything?
|
|
|
|
*
|
|
|
|
* @param {Partial<TransactionMeta>} opts - the object to use when
|
|
|
|
* overwriting default keys of the TransactionMeta
|
|
|
|
* @returns {TransactionMeta} the default txMeta object
|
2020-11-07 08:38:12 +01:00
|
|
|
*/
|
2021-06-29 22:08:16 +02:00
|
|
|
generateTxMeta(opts = {}) {
|
2021-02-04 19:15:23 +01:00
|
|
|
const netId = this.getNetwork();
|
2021-03-01 16:15:42 +01:00
|
|
|
const chainId = this.getCurrentChainId();
|
2019-11-20 01:03:20 +01:00
|
|
|
if (netId === 'loading') {
|
2021-02-04 19:15:23 +01:00
|
|
|
throw new Error('MetaMask is having trouble connecting to the network');
|
2019-11-20 01:03:20 +01:00
|
|
|
}
|
2021-06-29 22:08:16 +02:00
|
|
|
|
|
|
|
let dappSuggestedGasFees = null;
|
|
|
|
|
|
|
|
// If we are dealing with a transaction suggested by a dapp and not
|
|
|
|
// an internally created metamask transaction, we need to keep record of
|
|
|
|
// the originally submitted gasParams.
|
|
|
|
if (
|
|
|
|
opts.txParams &&
|
|
|
|
typeof opts.origin === 'string' &&
|
2022-04-26 19:07:39 +02:00
|
|
|
opts.origin !== ORIGIN_METAMASK
|
2021-06-29 22:08:16 +02:00
|
|
|
) {
|
|
|
|
if (typeof opts.txParams.gasPrice !== 'undefined') {
|
|
|
|
dappSuggestedGasFees = {
|
|
|
|
gasPrice: opts.txParams.gasPrice,
|
|
|
|
};
|
|
|
|
} else if (
|
|
|
|
typeof opts.txParams.maxFeePerGas !== 'undefined' ||
|
|
|
|
typeof opts.txParams.maxPriorityFeePerGas !== 'undefined'
|
|
|
|
) {
|
|
|
|
dappSuggestedGasFees = {
|
|
|
|
maxPriorityFeePerGas: opts.txParams.maxPriorityFeePerGas,
|
|
|
|
maxFeePerGas: opts.txParams.maxFeePerGas,
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
if (typeof opts.txParams.gas !== 'undefined') {
|
|
|
|
dappSuggestedGasFees = {
|
|
|
|
...dappSuggestedGasFees,
|
|
|
|
gas: opts.txParams.gas,
|
|
|
|
};
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-08-19 18:27:05 +02:00
|
|
|
return {
|
2018-02-28 00:14:18 +01:00
|
|
|
id: createId(),
|
2020-11-03 00:41:28 +01:00
|
|
|
time: new Date().getTime(),
|
2020-11-07 08:38:12 +01:00
|
|
|
status: TRANSACTION_STATUSES.UNAPPROVED,
|
2018-12-13 20:14:46 +01:00
|
|
|
metamaskNetworkId: netId,
|
2022-01-21 16:58:59 +01:00
|
|
|
originalGasEstimate: opts.txParams?.gas,
|
|
|
|
userEditedGasLimit: false,
|
2021-03-01 16:15:42 +01:00
|
|
|
chainId,
|
2020-11-03 00:41:28 +01:00
|
|
|
loadingDefaults: true,
|
2021-06-29 22:08:16 +02:00
|
|
|
dappSuggestedGasFees,
|
2020-11-03 00:41:28 +01:00
|
|
|
...opts,
|
2021-02-04 19:15:23 +01:00
|
|
|
};
|
2018-02-28 00:14:18 +01:00
|
|
|
}
|
|
|
|
|
2018-04-19 20:29:26 +02:00
|
|
|
/**
|
2021-03-30 16:54:05 +02:00
|
|
|
* Get an object containing all unapproved transactions for the current
|
|
|
|
* network. This is the only transaction fetching method that returns an
|
|
|
|
* object, so it doesn't use getTransactions like everything else.
|
2020-07-16 22:22:41 +02:00
|
|
|
*
|
2021-03-30 16:54:05 +02:00
|
|
|
* @returns {Record<string, TransactionMeta>} Unapproved transactions keyed
|
|
|
|
* by id
|
2020-11-07 08:38:12 +01:00
|
|
|
*/
|
2020-11-03 00:41:28 +01:00
|
|
|
getUnapprovedTxList() {
|
2021-03-30 16:54:05 +02:00
|
|
|
const chainId = this.getCurrentChainId();
|
|
|
|
const network = this.getNetwork();
|
|
|
|
return pickBy(
|
|
|
|
this.store.getState().transactions,
|
|
|
|
(transaction) =>
|
|
|
|
transaction.status === TRANSACTION_STATUSES.UNAPPROVED &&
|
|
|
|
transactionMatchesNetwork(transaction, chainId, network),
|
2021-02-04 19:15:23 +01:00
|
|
|
);
|
2017-08-18 21:23:35 +02:00
|
|
|
}
|
|
|
|
|
2018-11-14 22:34:07 +01:00
|
|
|
/**
|
2021-03-30 16:54:05 +02:00
|
|
|
* Get all approved transactions for the current network. If an address is
|
|
|
|
* provided, the list will be further refined to only those transactions
|
|
|
|
* originating from the supplied address.
|
|
|
|
*
|
|
|
|
* @param {string} [address] - hex prefixed address to find transactions for.
|
|
|
|
* @returns {TransactionMeta[]} the filtered list of transactions
|
2020-11-07 08:38:12 +01:00
|
|
|
*/
|
2020-11-03 00:41:28 +01:00
|
|
|
getApprovedTransactions(address) {
|
2021-03-30 16:54:05 +02:00
|
|
|
const searchCriteria = { status: TRANSACTION_STATUSES.APPROVED };
|
2019-11-20 01:03:20 +01:00
|
|
|
if (address) {
|
2021-03-30 16:54:05 +02:00
|
|
|
searchCriteria.from = address;
|
2019-11-20 01:03:20 +01:00
|
|
|
}
|
2021-03-30 16:54:05 +02:00
|
|
|
return this.getTransactions({ searchCriteria });
|
2018-11-14 22:34:07 +01:00
|
|
|
}
|
|
|
|
|
2018-04-19 20:29:26 +02:00
|
|
|
/**
|
2021-03-30 16:54:05 +02:00
|
|
|
* Get all pending transactions for the current network. If an address is
|
|
|
|
* provided, the list will be further refined to only those transactions
|
|
|
|
* originating from the supplied address.
|
|
|
|
*
|
|
|
|
* @param {string} [address] - hex prefixed address to find transactions for.
|
|
|
|
* @returns {TransactionMeta[]} the filtered list of transactions
|
2020-11-07 08:38:12 +01:00
|
|
|
*/
|
2020-11-03 00:41:28 +01:00
|
|
|
getPendingTransactions(address) {
|
2021-03-30 16:54:05 +02:00
|
|
|
const searchCriteria = { status: TRANSACTION_STATUSES.SUBMITTED };
|
2019-11-20 01:03:20 +01:00
|
|
|
if (address) {
|
2021-03-30 16:54:05 +02:00
|
|
|
searchCriteria.from = address;
|
2019-11-20 01:03:20 +01:00
|
|
|
}
|
2021-03-30 16:54:05 +02:00
|
|
|
return this.getTransactions({ searchCriteria });
|
2017-09-08 23:24:40 +02:00
|
|
|
}
|
|
|
|
|
2018-04-19 20:29:26 +02:00
|
|
|
/**
|
2021-03-30 16:54:05 +02:00
|
|
|
* Get all confirmed transactions for the current network. If an address is
|
|
|
|
* provided, the list will be further refined to only those transactions
|
|
|
|
* originating from the supplied address.
|
|
|
|
*
|
|
|
|
* @param {string} [address] - hex prefixed address to find transactions for.
|
|
|
|
* @returns {TransactionMeta[]} the filtered list of transactions
|
|
|
|
*/
|
2020-11-03 00:41:28 +01:00
|
|
|
getConfirmedTransactions(address) {
|
2021-03-30 16:54:05 +02:00
|
|
|
const searchCriteria = { status: TRANSACTION_STATUSES.CONFIRMED };
|
2019-11-20 01:03:20 +01:00
|
|
|
if (address) {
|
2021-03-30 16:54:05 +02:00
|
|
|
searchCriteria.from = address;
|
2019-11-20 01:03:20 +01:00
|
|
|
}
|
2021-03-30 16:54:05 +02:00
|
|
|
return this.getTransactions({ searchCriteria });
|
2017-10-06 21:50:33 +02:00
|
|
|
}
|
|
|
|
|
2018-04-19 20:29:26 +02:00
|
|
|
/**
|
2020-11-07 08:38:12 +01:00
|
|
|
* Adds the txMeta to the list of transactions in the store.
|
|
|
|
* if the list is over txHistoryLimit it will remove a transaction that
|
|
|
|
* is in its final state.
|
|
|
|
* it will also add the key `history` to the txMeta with the snap shot of
|
|
|
|
* the original object
|
2022-01-07 16:57:33 +01:00
|
|
|
*
|
2021-03-30 16:54:05 +02:00
|
|
|
* @param {TransactionMeta} txMeta - The TransactionMeta object to add.
|
|
|
|
* @returns {TransactionMeta} The same TransactionMeta, but with validated
|
|
|
|
* txParams and transaction history.
|
2020-11-07 08:38:12 +01:00
|
|
|
*/
|
2021-03-30 16:54:05 +02:00
|
|
|
addTransaction(txMeta) {
|
2019-06-29 00:51:51 +02:00
|
|
|
// normalize and validate txParams if present
|
|
|
|
if (txMeta.txParams) {
|
2021-03-30 16:54:05 +02:00
|
|
|
txMeta.txParams = normalizeAndValidateTxParams(txMeta.txParams, false);
|
2019-06-29 00:51:51 +02:00
|
|
|
}
|
|
|
|
|
2020-08-13 01:49:33 +02:00
|
|
|
this.once(`${txMeta.id}:signed`, () => {
|
2021-02-04 19:15:23 +01:00
|
|
|
this.removeAllListeners(`${txMeta.id}:rejected`);
|
|
|
|
});
|
2020-08-13 01:49:33 +02:00
|
|
|
this.once(`${txMeta.id}:rejected`, () => {
|
2021-02-04 19:15:23 +01:00
|
|
|
this.removeAllListeners(`${txMeta.id}:signed`);
|
|
|
|
});
|
2017-08-21 20:35:22 +02:00
|
|
|
// initialize history
|
2021-02-04 19:15:23 +01:00
|
|
|
txMeta.history = [];
|
2017-08-21 20:35:22 +02:00
|
|
|
// capture initial snapshot of txMeta for history
|
2021-02-04 19:15:23 +01:00
|
|
|
const snapshot = snapshotFromTxMeta(txMeta);
|
|
|
|
txMeta.history.push(snapshot);
|
2017-08-18 21:23:35 +02:00
|
|
|
|
2021-03-30 16:54:05 +02:00
|
|
|
const transactions = this.getTransactions({
|
|
|
|
filterToCurrentNetwork: false,
|
|
|
|
});
|
2021-02-04 19:15:23 +01:00
|
|
|
const { txHistoryLimit } = this;
|
2017-08-11 23:19:35 +02:00
|
|
|
|
2021-03-30 16:54:05 +02:00
|
|
|
// checks if the length of the tx history is longer then desired persistence
|
|
|
|
// limit and then if it is removes the oldest confirmed or rejected tx.
|
|
|
|
// Pending or unapproved transactions will not be removed by this
|
2021-05-17 19:34:44 +02:00
|
|
|
// operation. For safety of presenting a fully functional transaction UI
|
|
|
|
// representation, this function will not break apart transactions with the
|
|
|
|
// same nonce, per network. Not accounting for transactions of the same
|
|
|
|
// nonce and network combo can result in confusing or broken experiences
|
|
|
|
// in the UI.
|
2021-03-30 16:54:05 +02:00
|
|
|
//
|
|
|
|
// TODO: we are already limiting what we send to the UI, and in the future
|
|
|
|
// we will send UI only collected groups of transactions *per page* so at
|
|
|
|
// some point in the future, this persistence limit can be adjusted. When
|
|
|
|
// we do that I think we should figure out a better storage solution for
|
|
|
|
// transaction history entries.
|
2021-05-17 19:34:44 +02:00
|
|
|
const nonceNetworkSet = new Set();
|
|
|
|
const txsToDelete = transactions
|
|
|
|
.reverse()
|
|
|
|
.filter((tx) => {
|
2022-02-22 18:59:13 +01:00
|
|
|
const { nonce, from } = tx.txParams;
|
2021-05-17 19:34:44 +02:00
|
|
|
const { chainId, metamaskNetworkId, status } = tx;
|
2022-02-22 18:59:13 +01:00
|
|
|
const key = `${nonce}-${chainId ?? metamaskNetworkId}-${from}`;
|
2021-05-17 19:34:44 +02:00
|
|
|
if (nonceNetworkSet.has(key)) {
|
|
|
|
return false;
|
|
|
|
} else if (
|
|
|
|
nonceNetworkSet.size < txHistoryLimit - 1 ||
|
|
|
|
getFinalStates().includes(status) === false
|
|
|
|
) {
|
|
|
|
nonceNetworkSet.add(key);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
})
|
|
|
|
.map((tx) => tx.id);
|
2019-11-27 13:58:03 +01:00
|
|
|
|
2021-05-17 19:34:44 +02:00
|
|
|
this._deleteTransactions(txsToDelete);
|
2021-03-30 16:54:05 +02:00
|
|
|
this._addTransactionsToState([txMeta]);
|
2021-02-04 19:15:23 +01:00
|
|
|
return txMeta;
|
2017-08-11 23:19:35 +02:00
|
|
|
}
|
2020-08-19 18:27:05 +02:00
|
|
|
|
2022-02-18 17:48:38 +01:00
|
|
|
addExternalTransaction(txMeta) {
|
|
|
|
const fromAddress = txMeta?.txParams?.from;
|
|
|
|
const confirmedTransactions = this.getConfirmedTransactions(fromAddress);
|
|
|
|
const pendingTransactions = this.getPendingTransactions(fromAddress);
|
|
|
|
validateConfirmedExternalTransaction({
|
|
|
|
txMeta,
|
|
|
|
pendingTransactions,
|
|
|
|
confirmedTransactions,
|
|
|
|
});
|
|
|
|
this._addTransactionsToState([txMeta]);
|
|
|
|
return txMeta;
|
|
|
|
}
|
|
|
|
|
2018-04-19 20:29:26 +02:00
|
|
|
/**
|
2020-11-07 08:38:12 +01:00
|
|
|
* @param {number} txId
|
2021-03-30 16:54:05 +02:00
|
|
|
* @returns {TransactionMeta} the txMeta who matches the given id if none found
|
2020-11-07 08:38:12 +01:00
|
|
|
* for the network returns undefined
|
|
|
|
*/
|
2021-03-30 16:54:05 +02:00
|
|
|
getTransaction(txId) {
|
|
|
|
const { transactions } = this.store.getState();
|
|
|
|
return transactions[txId];
|
2017-08-11 23:19:35 +02:00
|
|
|
}
|
|
|
|
|
2018-04-19 20:29:26 +02:00
|
|
|
/**
|
2020-11-07 08:38:12 +01:00
|
|
|
* updates the txMeta in the list and adds a history entry
|
2022-01-07 16:57:33 +01:00
|
|
|
*
|
2020-11-07 08:38:12 +01:00
|
|
|
* @param {Object} txMeta - the txMeta to update
|
|
|
|
* @param {string} [note] - a note about the update for history
|
|
|
|
*/
|
2021-03-30 16:54:05 +02:00
|
|
|
updateTransaction(txMeta, note) {
|
2019-06-29 00:51:51 +02:00
|
|
|
// normalize and validate txParams if present
|
2017-09-13 23:27:27 +02:00
|
|
|
if (txMeta.txParams) {
|
2021-03-30 16:54:05 +02:00
|
|
|
txMeta.txParams = normalizeAndValidateTxParams(txMeta.txParams, false);
|
2017-09-13 23:27:27 +02:00
|
|
|
}
|
|
|
|
|
2017-08-11 23:19:35 +02:00
|
|
|
// create txMeta snapshot for history
|
2021-02-04 19:15:23 +01:00
|
|
|
const currentState = snapshotFromTxMeta(txMeta);
|
2017-08-21 20:35:22 +02:00
|
|
|
// recover previous tx state obj
|
2021-02-04 19:15:23 +01:00
|
|
|
const previousState = replayHistory(txMeta.history);
|
2017-08-21 20:35:22 +02:00
|
|
|
// generate history entry and add to history
|
2021-02-04 19:15:23 +01:00
|
|
|
const entry = generateHistoryEntry(previousState, currentState, note);
|
2020-04-22 05:09:16 +02:00
|
|
|
if (entry.length) {
|
2021-02-04 19:15:23 +01:00
|
|
|
txMeta.history.push(entry);
|
2020-04-22 05:09:16 +02:00
|
|
|
}
|
2017-08-21 20:35:22 +02:00
|
|
|
|
|
|
|
// commit txMeta to state
|
2021-02-04 19:15:23 +01:00
|
|
|
const txId = txMeta.id;
|
2021-03-30 16:54:05 +02:00
|
|
|
this.store.updateState({
|
|
|
|
transactions: {
|
|
|
|
...this.store.getState().transactions,
|
|
|
|
[txId]: txMeta,
|
|
|
|
},
|
|
|
|
});
|
2017-08-11 23:19:35 +02:00
|
|
|
}
|
|
|
|
|
2018-04-19 20:29:26 +02:00
|
|
|
/**
|
2021-03-30 16:54:05 +02:00
|
|
|
* SearchCriteria can search in any key in TxParams or the base
|
|
|
|
* TransactionMeta. This type represents any key on either of those two
|
|
|
|
* types.
|
2022-01-07 16:57:33 +01:00
|
|
|
*
|
2021-03-30 16:54:05 +02:00
|
|
|
* @typedef {TxParams[keyof TxParams] | TransactionMeta[keyof TransactionMeta]} SearchableKeys
|
2020-11-07 08:38:12 +01:00
|
|
|
*/
|
2017-08-11 23:19:35 +02:00
|
|
|
|
2019-06-29 00:51:51 +02:00
|
|
|
/**
|
2021-03-30 16:54:05 +02:00
|
|
|
* Predicates can either be strict values, which is shorthand for using
|
|
|
|
* strict equality, or a method that receives he value of the specified key
|
|
|
|
* and returns a boolean.
|
2022-01-07 16:57:33 +01:00
|
|
|
*
|
2021-03-30 16:54:05 +02:00
|
|
|
* @typedef {(v: unknown) => boolean | unknown} FilterPredicate
|
2019-06-29 00:51:51 +02:00
|
|
|
*/
|
|
|
|
|
2018-04-19 20:29:26 +02:00
|
|
|
/**
|
2021-03-30 16:54:05 +02:00
|
|
|
* Retrieve a list of transactions from state. By default this will return
|
|
|
|
* the full list of Transactions for the currently selected chain/network.
|
|
|
|
* Additional options can be provided to change what is included in the final
|
|
|
|
* list.
|
|
|
|
*
|
|
|
|
* @param opts - options to change filter behavior
|
|
|
|
* @param {Record<SearchableKeys, FilterPredicate>} [opts.searchCriteria] -
|
|
|
|
* an object with keys that match keys in TransactionMeta or TxParams, and
|
|
|
|
* values that are predicates. Predicates can either be strict values,
|
|
|
|
* which is shorthand for using strict equality, or a method that receives
|
|
|
|
* the value of the specified key and returns a boolean. The transaction
|
|
|
|
* list will be filtered to only those items that the predicate returns
|
|
|
|
* truthy for. **HINT**: `err: undefined` is like looking for a tx with no
|
|
|
|
* err. so you can also search txs that don't have something as well by
|
|
|
|
* setting the value as undefined.
|
|
|
|
* @param {TransactionMeta[]} [opts.initialList] - If provided the filtering
|
|
|
|
* will occur on the provided list. By default this will be the full list
|
|
|
|
* from state sorted by time ASC.
|
2022-01-07 16:57:33 +01:00
|
|
|
* @param {boolean} [opts.filterToCurrentNetwork] - Filter transaction
|
2021-03-30 16:54:05 +02:00
|
|
|
* list to only those that occurred on the current chain or network.
|
|
|
|
* Defaults to true.
|
|
|
|
* @param {number} [opts.limit] - limit the number of transactions returned
|
|
|
|
* to N unique nonces.
|
|
|
|
* @returns {TransactionMeta[]} The TransactionMeta objects that all provided
|
|
|
|
* predicates return truthy for.
|
2020-11-07 08:38:12 +01:00
|
|
|
*/
|
2021-03-30 16:54:05 +02:00
|
|
|
getTransactions({
|
|
|
|
searchCriteria = {},
|
|
|
|
initialList,
|
|
|
|
filterToCurrentNetwork = true,
|
|
|
|
limit,
|
|
|
|
} = {}) {
|
|
|
|
const chainId = this.getCurrentChainId();
|
|
|
|
const network = this.getNetwork();
|
|
|
|
// searchCriteria is an object that might have values that aren't predicate
|
|
|
|
// methods. When providing any other value type (string, number, etc), we
|
|
|
|
// consider this shorthand for "check the value at key for strict equality
|
|
|
|
// with the provided value". To conform this object to be only methods, we
|
|
|
|
// mapValues (lodash) such that every value on the object is a method that
|
|
|
|
// returns a boolean.
|
|
|
|
const predicateMethods = mapValues(searchCriteria, (predicate) => {
|
|
|
|
return typeof predicate === 'function'
|
|
|
|
? predicate
|
|
|
|
: (v) => v === predicate;
|
2021-02-04 19:15:23 +01:00
|
|
|
});
|
2020-08-19 18:27:05 +02:00
|
|
|
|
2021-03-30 16:54:05 +02:00
|
|
|
// If an initial list is provided we need to change it back into an object
|
|
|
|
// first, so that it matches the shape of our state. This is done by the
|
|
|
|
// lodash keyBy method. This is the edge case for this method, typically
|
|
|
|
// initialList will be undefined.
|
|
|
|
const transactionsToFilter = initialList
|
|
|
|
? keyBy(initialList, 'id')
|
|
|
|
: this.store.getState().transactions;
|
|
|
|
|
|
|
|
// Combine sortBy and pickBy to transform our state object into an array of
|
|
|
|
// matching transactions that are sorted by time.
|
|
|
|
const filteredTransactions = sortBy(
|
|
|
|
pickBy(transactionsToFilter, (transaction) => {
|
|
|
|
// default matchesCriteria to the value of transactionMatchesNetwork
|
|
|
|
// when filterToCurrentNetwork is true.
|
|
|
|
if (
|
|
|
|
filterToCurrentNetwork &&
|
|
|
|
transactionMatchesNetwork(transaction, chainId, network) === false
|
|
|
|
) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
// iterate over the predicateMethods keys to check if the transaction
|
|
|
|
// matches the searchCriteria
|
|
|
|
for (const [key, predicate] of Object.entries(predicateMethods)) {
|
|
|
|
// We return false early as soon as we know that one of the specified
|
|
|
|
// search criteria do not match the transaction. This prevents
|
|
|
|
// needlessly checking all criteria when we already know the criteria
|
|
|
|
// are not fully satisfied. We check both txParams and the base
|
|
|
|
// object as predicate keys can be either.
|
|
|
|
if (key in transaction.txParams) {
|
|
|
|
if (predicate(transaction.txParams[key]) === false) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
} else if (predicate(transaction[key]) === false) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
2019-08-29 14:57:28 +02:00
|
|
|
|
2021-03-30 16:54:05 +02:00
|
|
|
return true;
|
|
|
|
}),
|
|
|
|
'time',
|
|
|
|
);
|
|
|
|
if (limit !== undefined) {
|
|
|
|
// We need to have all transactions of a given nonce in order to display
|
|
|
|
// necessary details in the UI. We use the size of this set to determine
|
|
|
|
// whether we have reached the limit provided, thus ensuring that all
|
|
|
|
// transactions of nonces we include will be sent to the UI.
|
|
|
|
const nonces = new Set();
|
|
|
|
const txs = [];
|
|
|
|
// By default, the transaction list we filter from is sorted by time ASC.
|
|
|
|
// To ensure that filtered results prefers the newest transactions we
|
|
|
|
// iterate from right to left, inserting transactions into front of a new
|
|
|
|
// array. The original order is preserved, but we ensure that newest txs
|
|
|
|
// are preferred.
|
|
|
|
for (let i = filteredTransactions.length - 1; i > -1; i--) {
|
|
|
|
const txMeta = filteredTransactions[i];
|
|
|
|
const { nonce } = txMeta.txParams;
|
|
|
|
if (!nonces.has(nonce)) {
|
|
|
|
if (nonces.size < limit) {
|
|
|
|
nonces.add(nonce);
|
|
|
|
} else {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// Push transaction into the beginning of our array to ensure the
|
|
|
|
// original order is preserved.
|
|
|
|
txs.unshift(txMeta);
|
2017-08-11 23:19:35 +02:00
|
|
|
}
|
2021-03-30 16:54:05 +02:00
|
|
|
return txs;
|
|
|
|
}
|
|
|
|
return filteredTransactions;
|
2017-08-11 23:19:35 +02:00
|
|
|
}
|
|
|
|
|
2018-04-19 20:29:26 +02:00
|
|
|
/**
|
2021-03-30 16:54:05 +02:00
|
|
|
* Update status of the TransactionMeta with provided id to 'rejected'.
|
|
|
|
* After setting the status, the TransactionMeta is deleted from state.
|
|
|
|
*
|
|
|
|
* TODO: Should we show historically rejected transactions somewhere in the
|
|
|
|
* UI? Seems like it could be valuable for information purposes. Of course
|
|
|
|
* only after limit issues are reduced.
|
|
|
|
*
|
|
|
|
* @param {number} txId - the target TransactionMeta's Id
|
2020-11-07 08:38:12 +01:00
|
|
|
*/
|
2020-11-03 00:41:28 +01:00
|
|
|
setTxStatusRejected(txId) {
|
2021-10-25 15:01:30 +02:00
|
|
|
this._setTransactionStatus(txId, TRANSACTION_STATUSES.REJECTED);
|
2021-03-30 16:54:05 +02:00
|
|
|
this._deleteTransaction(txId);
|
2017-08-11 23:19:35 +02:00
|
|
|
}
|
|
|
|
|
2018-04-19 20:29:26 +02:00
|
|
|
/**
|
2021-03-30 16:54:05 +02:00
|
|
|
* Update status of the TransactionMeta with provided id to 'unapproved'
|
|
|
|
*
|
|
|
|
* @param {number} txId - the target TransactionMeta's Id
|
2020-11-07 08:38:12 +01:00
|
|
|
*/
|
2020-11-03 00:41:28 +01:00
|
|
|
setTxStatusUnapproved(txId) {
|
2021-03-30 16:54:05 +02:00
|
|
|
this._setTransactionStatus(txId, TRANSACTION_STATUSES.UNAPPROVED);
|
2017-12-07 04:20:11 +01:00
|
|
|
}
|
2020-08-19 18:27:05 +02:00
|
|
|
|
2018-04-19 20:29:26 +02:00
|
|
|
/**
|
2021-03-30 16:54:05 +02:00
|
|
|
* Update status of the TransactionMeta with provided id to 'approved'
|
|
|
|
*
|
|
|
|
* @param {number} txId - the target TransactionMeta's Id
|
2020-11-07 08:38:12 +01:00
|
|
|
*/
|
2020-11-03 00:41:28 +01:00
|
|
|
setTxStatusApproved(txId) {
|
2021-03-30 16:54:05 +02:00
|
|
|
this._setTransactionStatus(txId, TRANSACTION_STATUSES.APPROVED);
|
2017-08-11 23:19:35 +02:00
|
|
|
}
|
|
|
|
|
2018-04-19 20:29:26 +02:00
|
|
|
/**
|
2021-03-30 16:54:05 +02:00
|
|
|
* Update status of the TransactionMeta with provided id to 'signed'
|
|
|
|
*
|
|
|
|
* @param {number} txId - the target TransactionMeta's Id
|
2020-11-07 08:38:12 +01:00
|
|
|
*/
|
2020-11-03 00:41:28 +01:00
|
|
|
setTxStatusSigned(txId) {
|
2021-03-30 16:54:05 +02:00
|
|
|
this._setTransactionStatus(txId, TRANSACTION_STATUSES.SIGNED);
|
2017-08-11 23:19:35 +02:00
|
|
|
}
|
|
|
|
|
2018-04-19 20:29:26 +02:00
|
|
|
/**
|
2021-03-30 16:54:05 +02:00
|
|
|
* Update status of the TransactionMeta with provided id to 'submitted'
|
|
|
|
* and sets the 'submittedTime' property with the current Unix epoch time.
|
|
|
|
*
|
|
|
|
* @param {number} txId - the target TransactionMeta's Id
|
2020-11-07 08:38:12 +01:00
|
|
|
*/
|
2020-11-03 00:41:28 +01:00
|
|
|
setTxStatusSubmitted(txId) {
|
2021-03-30 16:54:05 +02:00
|
|
|
const txMeta = this.getTransaction(txId);
|
2021-02-04 19:15:23 +01:00
|
|
|
txMeta.submittedTime = new Date().getTime();
|
2021-03-30 16:54:05 +02:00
|
|
|
this.updateTransaction(txMeta, 'txStateManager - add submitted time stamp');
|
|
|
|
this._setTransactionStatus(txId, TRANSACTION_STATUSES.SUBMITTED);
|
2017-08-11 23:19:35 +02:00
|
|
|
}
|
|
|
|
|
2018-04-19 20:29:26 +02:00
|
|
|
/**
|
2021-03-30 16:54:05 +02:00
|
|
|
* Update status of the TransactionMeta with provided id to 'confirmed'
|
|
|
|
*
|
|
|
|
* @param {number} txId - the target TransactionMeta's Id
|
2020-11-07 08:38:12 +01:00
|
|
|
*/
|
2020-11-03 00:41:28 +01:00
|
|
|
setTxStatusConfirmed(txId) {
|
2021-03-30 16:54:05 +02:00
|
|
|
this._setTransactionStatus(txId, TRANSACTION_STATUSES.CONFIRMED);
|
2017-08-11 23:19:35 +02:00
|
|
|
}
|
|
|
|
|
2018-04-19 20:29:26 +02:00
|
|
|
/**
|
2021-03-30 16:54:05 +02:00
|
|
|
* Update status of the TransactionMeta with provided id to 'dropped'
|
|
|
|
*
|
|
|
|
* @param {number} txId - the target TransactionMeta's Id
|
2020-11-07 08:38:12 +01:00
|
|
|
*/
|
2020-11-03 00:41:28 +01:00
|
|
|
setTxStatusDropped(txId) {
|
2021-03-30 16:54:05 +02:00
|
|
|
this._setTransactionStatus(txId, TRANSACTION_STATUSES.DROPPED);
|
2018-02-28 00:14:18 +01:00
|
|
|
}
|
|
|
|
|
2018-04-19 20:29:26 +02:00
|
|
|
/**
|
2021-03-30 16:54:05 +02:00
|
|
|
* Update status of the TransactionMeta with provided id to 'failed' and put
|
|
|
|
* the error on the TransactionMeta object.
|
|
|
|
*
|
|
|
|
* @param {number} txId - the target TransactionMeta's Id
|
|
|
|
* @param {Error} err - error object
|
2020-11-07 08:38:12 +01:00
|
|
|
*/
|
2020-11-03 00:41:28 +01:00
|
|
|
setTxStatusFailed(txId, err) {
|
2021-02-04 19:15:23 +01:00
|
|
|
const error = err || new Error('Internal metamask failure');
|
2018-11-26 20:00:58 +01:00
|
|
|
|
2021-03-30 16:54:05 +02:00
|
|
|
const txMeta = this.getTransaction(txId);
|
2017-09-06 23:01:07 +02:00
|
|
|
txMeta.err = {
|
2021-09-22 19:41:23 +02:00
|
|
|
message: error.message?.toString() || error.toString(),
|
2018-11-26 20:00:58 +01:00
|
|
|
rpc: error.value,
|
|
|
|
stack: error.stack,
|
2021-02-04 19:15:23 +01:00
|
|
|
};
|
2021-03-30 16:54:05 +02:00
|
|
|
this.updateTransaction(
|
|
|
|
txMeta,
|
|
|
|
'transactions:tx-state-manager#fail - add error',
|
|
|
|
);
|
|
|
|
this._setTransactionStatus(txId, TRANSACTION_STATUSES.FAILED);
|
2017-08-11 23:19:35 +02:00
|
|
|
}
|
|
|
|
|
2018-04-19 20:29:26 +02:00
|
|
|
/**
|
2021-03-30 16:54:05 +02:00
|
|
|
* Removes all transactions for the given address on the current network,
|
|
|
|
* preferring chainId for comparison over networkId.
|
|
|
|
*
|
2020-11-07 08:38:12 +01:00
|
|
|
* @param {string} address - hex string of the from address on the txParams
|
|
|
|
* to remove
|
|
|
|
*/
|
2020-11-03 00:41:28 +01:00
|
|
|
wipeTransactions(address) {
|
2018-01-31 10:25:32 +01:00
|
|
|
// network only tx
|
2021-03-30 16:54:05 +02:00
|
|
|
const { transactions } = this.store.getState();
|
2021-02-04 19:15:23 +01:00
|
|
|
const network = this.getNetwork();
|
2021-03-01 16:15:42 +01:00
|
|
|
const chainId = this.getCurrentChainId();
|
2018-01-31 10:25:32 +01:00
|
|
|
|
|
|
|
// Update state
|
2021-03-30 16:54:05 +02:00
|
|
|
this.store.updateState({
|
|
|
|
transactions: omitBy(
|
|
|
|
transactions,
|
|
|
|
(transaction) =>
|
|
|
|
transaction.txParams.from === address &&
|
|
|
|
transactionMatchesNetwork(transaction, chainId, network),
|
|
|
|
),
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Filters out the unapproved transactions from state
|
|
|
|
*/
|
|
|
|
clearUnapprovedTxs() {
|
|
|
|
this.store.updateState({
|
|
|
|
transactions: omitBy(
|
|
|
|
this.store.getState().transactions,
|
|
|
|
(transaction) => transaction.status === TRANSACTION_STATUSES.UNAPPROVED,
|
|
|
|
),
|
|
|
|
});
|
2018-01-31 09:33:15 +01:00
|
|
|
}
|
2020-11-07 08:38:12 +01:00
|
|
|
|
2019-07-31 22:17:11 +02:00
|
|
|
//
|
|
|
|
// PRIVATE METHODS
|
|
|
|
//
|
2017-08-11 23:19:35 +02:00
|
|
|
|
2018-04-19 20:29:26 +02:00
|
|
|
/**
|
2021-03-30 16:54:05 +02:00
|
|
|
* Updates a transaction's status in state, and then emits events that are
|
|
|
|
* subscribed to elsewhere. See below for best guesses on where and how these
|
|
|
|
* events are received.
|
2022-01-07 16:57:33 +01:00
|
|
|
*
|
2021-03-30 16:54:05 +02:00
|
|
|
* @param {number} txId - the TransactionMeta Id
|
|
|
|
* @param {TransactionStatusString} status - the status to set on the
|
|
|
|
* TransactionMeta
|
2022-01-07 16:57:33 +01:00
|
|
|
* @fires txMeta.id:txMeta.status - every time a transaction's status changes
|
2021-03-30 16:54:05 +02:00
|
|
|
* we emit the change passing along the id. This does not appear to be used
|
|
|
|
* outside of this file, which only listens to this to unsubscribe listeners
|
|
|
|
* of :rejected and :signed statuses when the inverse status changes. Likely
|
|
|
|
* safe to drop.
|
2022-01-07 16:57:33 +01:00
|
|
|
* @fires tx:status-update - every time a transaction's status changes we
|
2021-03-30 16:54:05 +02:00
|
|
|
* emit this event and pass txId and status. This event is subscribed to in
|
|
|
|
* the TransactionController and re-broadcast by the TransactionController.
|
|
|
|
* It is used internally within the TransactionController to try and update
|
|
|
|
* pending transactions on each new block (from blockTracker). It's also
|
|
|
|
* subscribed to in metamask-controller to display a browser notification on
|
|
|
|
* confirmed or failed transactions.
|
2022-01-07 16:57:33 +01:00
|
|
|
* @fires txMeta.id:finished - When a transaction moves to a finished state
|
2021-03-30 16:54:05 +02:00
|
|
|
* this event is emitted, which is used in the TransactionController to pass
|
|
|
|
* along details of the transaction to the dapp that suggested them. This
|
|
|
|
* pattern is replicated across all of the message managers and can likely
|
|
|
|
* be supplemented or replaced by the ApprovalController.
|
2022-01-07 16:57:33 +01:00
|
|
|
* @fires updateBadge - When the number of transactions changes in state,
|
2021-03-30 16:54:05 +02:00
|
|
|
* the badge in the browser extension bar should be updated to reflect the
|
|
|
|
* number of pending transactions. This particular emit doesn't appear to
|
|
|
|
* bubble up anywhere that is actually used. TransactionController emits
|
|
|
|
* this *anytime the state changes*, so this is probably superfluous.
|
2020-11-07 08:38:12 +01:00
|
|
|
*/
|
2021-03-30 16:54:05 +02:00
|
|
|
_setTransactionStatus(txId, status) {
|
|
|
|
const txMeta = this.getTransaction(txId);
|
2018-10-16 00:00:47 +02:00
|
|
|
|
|
|
|
if (!txMeta) {
|
2021-02-04 19:15:23 +01:00
|
|
|
return;
|
2018-10-16 00:00:47 +02:00
|
|
|
}
|
|
|
|
|
2021-02-04 19:15:23 +01:00
|
|
|
txMeta.status = status;
|
2020-05-09 15:46:58 +02:00
|
|
|
try {
|
2021-03-30 16:54:05 +02:00
|
|
|
this.updateTransaction(
|
|
|
|
txMeta,
|
|
|
|
`txStateManager: setting status to ${status}`,
|
|
|
|
);
|
2021-02-04 19:15:23 +01:00
|
|
|
this.emit(`${txMeta.id}:${status}`, txId);
|
|
|
|
this.emit(`tx:status-update`, txId, status);
|
2020-11-07 08:38:12 +01:00
|
|
|
if (
|
|
|
|
[
|
|
|
|
TRANSACTION_STATUSES.SUBMITTED,
|
|
|
|
TRANSACTION_STATUSES.REJECTED,
|
|
|
|
TRANSACTION_STATUSES.FAILED,
|
|
|
|
].includes(status)
|
|
|
|
) {
|
2021-02-04 19:15:23 +01:00
|
|
|
this.emit(`${txMeta.id}:finished`, txMeta);
|
2018-04-30 01:00:13 +02:00
|
|
|
}
|
2021-02-09 00:22:30 +01:00
|
|
|
this.emit(METAMASK_CONTROLLER_EVENTS.UPDATE_BADGE);
|
2020-05-09 15:46:58 +02:00
|
|
|
} catch (error) {
|
2021-02-04 19:15:23 +01:00
|
|
|
log.error(error);
|
2020-05-09 15:46:58 +02:00
|
|
|
}
|
2017-08-11 23:19:35 +02:00
|
|
|
}
|
|
|
|
|
2018-04-19 20:29:26 +02:00
|
|
|
/**
|
2021-03-30 16:54:05 +02:00
|
|
|
* Adds one or more transactions into state. This is not intended for
|
|
|
|
* external use.
|
|
|
|
*
|
|
|
|
* @private
|
|
|
|
* @param {TransactionMeta[]} transactions - the list of transactions to save
|
2020-11-07 08:38:12 +01:00
|
|
|
*/
|
2021-03-30 16:54:05 +02:00
|
|
|
_addTransactionsToState(transactions) {
|
|
|
|
this.store.updateState({
|
|
|
|
transactions: transactions.reduce((result, newTx) => {
|
|
|
|
result[newTx.id] = newTx;
|
|
|
|
return result;
|
|
|
|
}, this.store.getState().transactions),
|
|
|
|
});
|
2018-06-26 00:07:54 +02:00
|
|
|
}
|
2020-07-17 03:37:56 +02:00
|
|
|
|
|
|
|
/**
|
2021-03-30 16:54:05 +02:00
|
|
|
* removes one transaction from state. This is not intended for external use.
|
|
|
|
*
|
|
|
|
* @private
|
|
|
|
* @param {number} targetTransactionId - the transaction to delete
|
2020-07-17 03:37:56 +02:00
|
|
|
*/
|
2021-03-30 16:54:05 +02:00
|
|
|
_deleteTransaction(targetTransactionId) {
|
|
|
|
const { transactions } = this.store.getState();
|
|
|
|
delete transactions[targetTransactionId];
|
|
|
|
this.store.updateState({
|
|
|
|
transactions,
|
|
|
|
});
|
2020-07-17 03:37:56 +02:00
|
|
|
}
|
2021-05-17 19:34:44 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* removes multiple transaction from state. This is not intended for external use.
|
|
|
|
*
|
|
|
|
* @private
|
|
|
|
* @param {number[]} targetTransactionIds - the transactions to delete
|
|
|
|
*/
|
|
|
|
_deleteTransactions(targetTransactionIds) {
|
|
|
|
const { transactions } = this.store.getState();
|
|
|
|
targetTransactionIds.forEach((transactionId) => {
|
|
|
|
delete transactions[transactionId];
|
|
|
|
});
|
|
|
|
this.store.updateState({
|
|
|
|
transactions,
|
|
|
|
});
|
|
|
|
}
|
2017-10-06 21:50:33 +02:00
|
|
|
}
|