2020-01-09 04:34:58 +01:00
|
|
|
import EventEmitter from 'safe-event-emitter'
|
2020-12-16 22:14:49 +01:00
|
|
|
import { ObservableStore } from '@metamask/obs-store'
|
2020-01-09 04:34:58 +01:00
|
|
|
import log from 'loglevel'
|
|
|
|
import createId from '../../lib/random-id'
|
2020-11-07 08:38:12 +01:00
|
|
|
import { TRANSACTION_STATUSES } from '../../../../shared/constants/transaction'
|
2020-11-03 00:41:28 +01:00
|
|
|
import {
|
|
|
|
generateHistoryEntry,
|
|
|
|
replayHistory,
|
|
|
|
snapshotFromTxMeta,
|
|
|
|
} from './lib/tx-state-history-helpers'
|
2020-01-09 04:34:58 +01:00
|
|
|
import { getFinalStates, normalizeTxParams } 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
|
|
|
|
* @typedef {import('../../../../shared/constants/transaction').TransactionStatuses} TransactionStatuses
|
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* TransactionStateManager is responsible for the state of a transaction and
|
|
|
|
* storing the transaction. It also has some convenience methods for finding
|
|
|
|
* subsets of transactions.
|
|
|
|
* @param {Object} opts
|
2020-11-10 18:30:41 +01:00
|
|
|
* @param {Object} [opts.initState={ transactions: [] }] - initial transactions list with the key transaction {Array}
|
|
|
|
* @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
|
|
|
* @class
|
|
|
|
*/
|
2020-04-27 16:45:00 +02:00
|
|
|
export default class TransactionStateManager extends EventEmitter {
|
2020-11-03 00:41:28 +01:00
|
|
|
constructor({ initState, txHistoryLimit, getNetwork }) {
|
2017-09-12 18:59:59 +02:00
|
|
|
super()
|
|
|
|
|
2020-11-03 00:41:28 +01:00
|
|
|
this.store = new ObservableStore({ transactions: [], ...initState })
|
2017-08-11 23:19:35 +02:00
|
|
|
this.txHistoryLimit = txHistoryLimit
|
|
|
|
this.getNetwork = getNetwork
|
|
|
|
}
|
2017-09-27 01:52:08 +02:00
|
|
|
|
2018-04-19 20:29:26 +02:00
|
|
|
/**
|
2020-11-07 08:38:12 +01:00
|
|
|
* @param {Object} opts - the object to use when overwriting defaults
|
2020-11-10 18:30:41 +01:00
|
|
|
* @returns {txMeta} the default txMeta object
|
2020-11-07 08:38:12 +01:00
|
|
|
*/
|
2020-11-03 00:41:28 +01:00
|
|
|
generateTxMeta(opts) {
|
2018-12-13 20:14:46 +01:00
|
|
|
const netId = this.getNetwork()
|
2019-11-20 01:03:20 +01:00
|
|
|
if (netId === 'loading') {
|
|
|
|
throw new Error('MetaMask is having trouble connecting to the network')
|
|
|
|
}
|
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,
|
2020-11-03 00:41:28 +01:00
|
|
|
loadingDefaults: true,
|
|
|
|
...opts,
|
2020-08-19 18:27:05 +02:00
|
|
|
}
|
2018-02-28 00:14:18 +01:00
|
|
|
}
|
|
|
|
|
2018-04-19 20:29:26 +02:00
|
|
|
/**
|
2020-07-16 22:22:41 +02:00
|
|
|
* Returns the full tx list for the current network
|
|
|
|
*
|
|
|
|
* The list is iterated backwards as new transactions are pushed onto it.
|
|
|
|
*
|
2020-11-10 18:30:41 +01:00
|
|
|
* @param {number} [limit] - a limit for the number of transactions to return
|
2020-07-16 22:22:41 +02:00
|
|
|
* @returns {Object[]} The {@code txMeta}s, filtered to the current network
|
|
|
|
*/
|
2020-11-03 00:41:28 +01:00
|
|
|
getTxList(limit) {
|
2017-08-11 23:19:35 +02:00
|
|
|
const network = this.getNetwork()
|
|
|
|
const fullTxList = this.getFullTxList()
|
2020-07-16 22:22:41 +02:00
|
|
|
|
|
|
|
const nonces = new Set()
|
|
|
|
const txs = []
|
|
|
|
for (let i = fullTxList.length - 1; i > -1; i--) {
|
|
|
|
const txMeta = fullTxList[i]
|
|
|
|
if (txMeta.metamaskNetworkId !== network) {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
if (limit !== undefined) {
|
|
|
|
const { nonce } = txMeta.txParams
|
|
|
|
if (!nonces.has(nonce)) {
|
|
|
|
if (nonces.size < limit) {
|
|
|
|
nonces.add(nonce)
|
|
|
|
} else {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
txs.unshift(txMeta)
|
|
|
|
}
|
|
|
|
return txs
|
2017-08-11 23:19:35 +02:00
|
|
|
}
|
|
|
|
|
2018-04-19 20:29:26 +02:00
|
|
|
/**
|
2020-11-10 18:30:41 +01:00
|
|
|
* @returns {Array} of all the txMetas in store
|
2020-11-07 08:38:12 +01:00
|
|
|
*/
|
2020-11-03 00:41:28 +01:00
|
|
|
getFullTxList() {
|
2017-09-12 18:59:59 +02:00
|
|
|
return this.store.getState().transactions
|
2017-08-11 23:19:35 +02:00
|
|
|
}
|
|
|
|
|
2018-04-19 20:29:26 +02:00
|
|
|
/**
|
2020-11-10 18:30:41 +01:00
|
|
|
* @returns {Array} the tx list with unapproved status
|
2020-11-07 08:38:12 +01:00
|
|
|
*/
|
2020-11-03 00:41:28 +01:00
|
|
|
getUnapprovedTxList() {
|
2020-11-07 08:38:12 +01:00
|
|
|
const txList = this.getTxsByMetaData(
|
|
|
|
'status',
|
|
|
|
TRANSACTION_STATUSES.UNAPPROVED,
|
|
|
|
)
|
2017-08-18 21:23:35 +02:00
|
|
|
return txList.reduce((result, tx) => {
|
|
|
|
result[tx.id] = tx
|
|
|
|
return result
|
|
|
|
}, {})
|
|
|
|
}
|
|
|
|
|
2018-11-14 22:34:07 +01:00
|
|
|
/**
|
2020-11-10 18:30:41 +01:00
|
|
|
* @param {string} [address] - hex prefixed address to sort the txMetas for [optional]
|
|
|
|
* @returns {Array} the tx list with approved status if no address is provide
|
2020-11-07 08:38:12 +01:00
|
|
|
* returns all txMetas with approved statuses for the current network
|
|
|
|
*/
|
2020-11-03 00:41:28 +01:00
|
|
|
getApprovedTransactions(address) {
|
2020-11-07 08:38:12 +01:00
|
|
|
const opts = { status: TRANSACTION_STATUSES.APPROVED }
|
2019-11-20 01:03:20 +01:00
|
|
|
if (address) {
|
|
|
|
opts.from = address
|
|
|
|
}
|
2018-11-14 22:34:07 +01:00
|
|
|
return this.getFilteredTxList(opts)
|
|
|
|
}
|
|
|
|
|
2018-04-19 20:29:26 +02:00
|
|
|
/**
|
2020-11-10 18:30:41 +01:00
|
|
|
* @param {string} [address] - hex prefixed address to sort the txMetas for [optional]
|
|
|
|
* @returns {Array} the tx list submitted status if no address is provide
|
2020-11-07 08:38:12 +01:00
|
|
|
* returns all txMetas with submitted statuses for the current network
|
|
|
|
*/
|
2020-11-03 00:41:28 +01:00
|
|
|
getPendingTransactions(address) {
|
2020-11-07 08:38:12 +01:00
|
|
|
const opts = { status: TRANSACTION_STATUSES.SUBMITTED }
|
2019-11-20 01:03:20 +01:00
|
|
|
if (address) {
|
|
|
|
opts.from = address
|
|
|
|
}
|
2017-09-09 00:02:36 +02:00
|
|
|
return this.getFilteredTxList(opts)
|
2017-09-08 23:24:40 +02:00
|
|
|
}
|
|
|
|
|
2018-04-19 20:29:26 +02:00
|
|
|
/**
|
2020-11-10 18:30:41 +01:00
|
|
|
@param {string} [address] - hex prefixed address to sort the txMetas for [optional]
|
|
|
|
@returns {Array} the tx list whose status is confirmed if no address is provide
|
2018-04-25 20:13:51 +02:00
|
|
|
returns all txMetas who's status is confirmed for the current network
|
2018-04-19 20:29:26 +02:00
|
|
|
*/
|
2020-11-03 00:41:28 +01:00
|
|
|
getConfirmedTransactions(address) {
|
2020-11-07 08:38:12 +01:00
|
|
|
const opts = { status: TRANSACTION_STATUSES.CONFIRMED }
|
2019-11-20 01:03:20 +01:00
|
|
|
if (address) {
|
|
|
|
opts.from = address
|
|
|
|
}
|
2017-10-06 21:50:33 +02:00
|
|
|
return this.getFilteredTxList(opts)
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
* @param {Object} txMeta
|
2020-11-10 18:30:41 +01:00
|
|
|
* @returns {Object} the txMeta
|
2020-11-07 08:38:12 +01:00
|
|
|
*/
|
2020-11-03 00:41:28 +01:00
|
|
|
addTx(txMeta) {
|
2019-06-29 00:51:51 +02:00
|
|
|
// normalize and validate txParams if present
|
|
|
|
if (txMeta.txParams) {
|
|
|
|
txMeta.txParams = this.normalizeAndValidateTxParams(txMeta.txParams)
|
|
|
|
}
|
|
|
|
|
2020-08-13 01:49:33 +02:00
|
|
|
this.once(`${txMeta.id}:signed`, () => {
|
2017-08-18 21:23:35 +02:00
|
|
|
this.removeAllListeners(`${txMeta.id}:rejected`)
|
|
|
|
})
|
2020-08-13 01:49:33 +02:00
|
|
|
this.once(`${txMeta.id}:rejected`, () => {
|
2017-08-18 21:23:35 +02:00
|
|
|
this.removeAllListeners(`${txMeta.id}:signed`)
|
|
|
|
})
|
2017-08-21 20:35:22 +02:00
|
|
|
// initialize history
|
|
|
|
txMeta.history = []
|
|
|
|
// capture initial snapshot of txMeta for history
|
2020-04-20 17:30:51 +02:00
|
|
|
const snapshot = snapshotFromTxMeta(txMeta)
|
2017-08-21 20:35:22 +02:00
|
|
|
txMeta.history.push(snapshot)
|
2017-08-18 21:23:35 +02:00
|
|
|
|
2017-08-11 23:19:35 +02:00
|
|
|
const transactions = this.getFullTxList()
|
2018-03-27 00:58:36 +02:00
|
|
|
const txCount = transactions.length
|
2020-08-18 22:06:58 +02:00
|
|
|
const { txHistoryLimit } = this
|
2017-08-11 23:19:35 +02:00
|
|
|
|
|
|
|
// checks if the length of the tx history is
|
|
|
|
// longer then desired persistence limit
|
|
|
|
// and then if it is removes only confirmed
|
|
|
|
// or rejected tx's.
|
|
|
|
// not tx's that are pending or unapproved
|
|
|
|
if (txCount > txHistoryLimit - 1) {
|
2018-04-10 23:53:40 +02:00
|
|
|
const index = transactions.findIndex((metaTx) => {
|
2018-04-19 20:29:26 +02:00
|
|
|
return getFinalStates().includes(metaTx.status)
|
2018-04-06 20:07:20 +02:00
|
|
|
})
|
2018-04-06 19:18:00 +02:00
|
|
|
if (index !== -1) {
|
|
|
|
transactions.splice(index, 1)
|
|
|
|
}
|
2017-08-11 23:19:35 +02:00
|
|
|
}
|
2020-11-03 00:41:28 +01:00
|
|
|
const newTxIndex = transactions.findIndex(
|
|
|
|
(currentTxMeta) => currentTxMeta.time > txMeta.time,
|
|
|
|
)
|
2019-11-27 13:58:03 +01:00
|
|
|
|
|
|
|
newTxIndex === -1
|
|
|
|
? transactions.push(txMeta)
|
|
|
|
: transactions.splice(newTxIndex, 0, txMeta)
|
2017-08-11 23:19:35 +02:00
|
|
|
this._saveTxList(transactions)
|
2017-08-21 20:35:22 +02:00
|
|
|
return txMeta
|
2017-08-11 23:19:35 +02:00
|
|
|
}
|
2020-08-19 18:27:05 +02:00
|
|
|
|
2018-04-19 20:29:26 +02:00
|
|
|
/**
|
2020-11-07 08:38:12 +01:00
|
|
|
* @param {number} txId
|
2020-11-10 18:30:41 +01:00
|
|
|
* @returns {Object} the txMeta who matches the given id if none found
|
2020-11-07 08:38:12 +01:00
|
|
|
* for the network returns undefined
|
|
|
|
*/
|
2020-11-03 00:41:28 +01:00
|
|
|
getTx(txId) {
|
2017-08-11 23:19:35 +02:00
|
|
|
const txMeta = this.getTxsByMetaData('id', txId)[0]
|
|
|
|
return txMeta
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
* @param {Object} txMeta - the txMeta to update
|
|
|
|
* @param {string} [note] - a note about the update for history
|
|
|
|
*/
|
2020-11-03 00:41:28 +01:00
|
|
|
updateTx(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) {
|
2019-06-29 00:51:51 +02:00
|
|
|
txMeta.txParams = this.normalizeAndValidateTxParams(txMeta.txParams)
|
2017-09-13 23:27:27 +02:00
|
|
|
}
|
|
|
|
|
2017-08-11 23:19:35 +02:00
|
|
|
// create txMeta snapshot for history
|
2020-04-20 17:30:51 +02:00
|
|
|
const currentState = snapshotFromTxMeta(txMeta)
|
2017-08-21 20:35:22 +02:00
|
|
|
// recover previous tx state obj
|
2020-04-20 17:30:51 +02:00
|
|
|
const previousState = replayHistory(txMeta.history)
|
2017-08-21 20:35:22 +02:00
|
|
|
// generate history entry and add to history
|
2020-04-20 17:30:51 +02:00
|
|
|
const entry = generateHistoryEntry(previousState, currentState, note)
|
2020-04-22 05:09:16 +02:00
|
|
|
if (entry.length) {
|
|
|
|
txMeta.history.push(entry)
|
|
|
|
}
|
2017-08-21 20:35:22 +02:00
|
|
|
|
|
|
|
// commit txMeta to state
|
2017-08-11 23:19:35 +02:00
|
|
|
const txId = txMeta.id
|
|
|
|
const txList = this.getFullTxList()
|
2020-02-15 21:34:12 +01:00
|
|
|
const index = txList.findIndex((txData) => txData.id === txId)
|
2017-08-11 23:19:35 +02:00
|
|
|
txList[index] = txMeta
|
|
|
|
this._saveTxList(txList)
|
|
|
|
}
|
|
|
|
|
2018-04-19 20:29:26 +02:00
|
|
|
/**
|
2020-11-07 08:38:12 +01:00
|
|
|
* merges txParams obj onto txMeta.txParams use extend to ensure
|
|
|
|
* that all fields are filled
|
|
|
|
* @param {number} txId - the id of the txMeta
|
|
|
|
* @param {Object} txParams - the updated txParams
|
|
|
|
*/
|
2020-11-03 00:41:28 +01:00
|
|
|
updateTxParams(txId, txParams) {
|
2017-08-11 23:19:35 +02:00
|
|
|
const txMeta = this.getTx(txId)
|
2020-01-13 19:59:36 +01:00
|
|
|
txMeta.txParams = { ...txMeta.txParams, ...txParams }
|
2017-10-02 22:41:29 +02:00
|
|
|
this.updateTx(txMeta, `txStateManager#updateTxParams`)
|
2017-08-11 23:19:35 +02:00
|
|
|
}
|
|
|
|
|
2019-06-29 00:51:51 +02:00
|
|
|
/**
|
|
|
|
* normalize and validate txParams members
|
2020-01-13 19:36:36 +01:00
|
|
|
* @param {Object} txParams - txParams
|
2019-06-29 00:51:51 +02:00
|
|
|
*/
|
2020-11-03 00:41:28 +01:00
|
|
|
normalizeAndValidateTxParams(txParams) {
|
2019-06-29 00:51:51 +02:00
|
|
|
if (typeof txParams.data === 'undefined') {
|
|
|
|
delete txParams.data
|
|
|
|
}
|
2020-08-15 13:58:11 +02:00
|
|
|
// eslint-disable-next-line no-param-reassign
|
2019-06-29 00:51:51 +02:00
|
|
|
txParams = normalizeTxParams(txParams, false)
|
|
|
|
this.validateTxParams(txParams)
|
|
|
|
return txParams
|
|
|
|
}
|
|
|
|
|
2018-04-19 20:29:26 +02:00
|
|
|
/**
|
2020-11-07 08:38:12 +01:00
|
|
|
* validates txParams members by type
|
|
|
|
* @param {Object} txParams - txParams to validate
|
|
|
|
*/
|
2020-11-03 00:41:28 +01:00
|
|
|
validateTxParams(txParams) {
|
2018-03-28 08:55:18 +02:00
|
|
|
Object.keys(txParams).forEach((key) => {
|
|
|
|
const value = txParams[key]
|
2018-04-03 22:55:20 +02:00
|
|
|
// validate types
|
|
|
|
switch (key) {
|
|
|
|
case 'chainId':
|
2019-11-20 01:03:20 +01:00
|
|
|
if (typeof value !== 'number' && typeof value !== 'string') {
|
2020-11-03 00:41:28 +01:00
|
|
|
throw new Error(
|
|
|
|
`${key} in txParams is not a Number or hex string. got: (${value})`,
|
|
|
|
)
|
2019-11-20 01:03:20 +01:00
|
|
|
}
|
2018-04-03 22:55:20 +02:00
|
|
|
break
|
|
|
|
default:
|
2019-11-20 01:03:20 +01:00
|
|
|
if (typeof value !== 'string') {
|
2020-11-03 00:41:28 +01:00
|
|
|
throw new Error(
|
|
|
|
`${key} in txParams is not a string. got: (${value})`,
|
|
|
|
)
|
2019-11-20 01:03:20 +01:00
|
|
|
}
|
2018-04-03 22:55:20 +02:00
|
|
|
break
|
|
|
|
}
|
2018-03-28 08:55:18 +02:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2019-07-31 22:17:11 +02:00
|
|
|
/**
|
2020-01-13 19:36:36 +01:00
|
|
|
@param {Object} opts - an object of fields to search for eg:<br>
|
2018-04-19 20:29:26 +02:00
|
|
|
let <code>thingsToLookFor = {<br>
|
|
|
|
to: '0x0..',<br>
|
|
|
|
from: '0x0..',<br>
|
2019-08-29 14:57:28 +02:00
|
|
|
status: 'signed', \\ (status) => status !== 'rejected' give me all txs who's status is not rejected<br>
|
2018-04-19 20:29:26 +02:00
|
|
|
err: undefined,<br>
|
|
|
|
}<br></code>
|
2019-08-29 14:57:28 +02:00
|
|
|
optionally the values of the keys can be functions for situations like where
|
|
|
|
you want all but one status.
|
2020-11-10 18:30:41 +01:00
|
|
|
@param {Array} [initialList=this.getTxList()]
|
|
|
|
@returns {Array} array of txMeta with all
|
2017-08-11 23:19:35 +02:00
|
|
|
options matching
|
2018-04-19 20:29:26 +02:00
|
|
|
*/
|
|
|
|
/*
|
2017-08-11 23:19:35 +02:00
|
|
|
****************HINT****************
|
|
|
|
| `err: undefined` is like looking |
|
|
|
|
| for a tx with no err |
|
|
|
|
| so you can also search txs that |
|
|
|
|
| dont have something as well by |
|
|
|
|
| setting the value as undefined |
|
|
|
|
************************************
|
|
|
|
|
|
|
|
this is for things like filtering a the tx list
|
|
|
|
for only tx's from 1 account
|
2019-08-29 14:57:28 +02:00
|
|
|
or for filtering for all txs from one account
|
2017-08-11 23:19:35 +02:00
|
|
|
and that have been 'confirmed'
|
|
|
|
*/
|
2020-11-03 00:41:28 +01:00
|
|
|
getFilteredTxList(opts, initialList) {
|
2017-08-11 23:19:35 +02:00
|
|
|
let filteredTxList = initialList
|
|
|
|
Object.keys(opts).forEach((key) => {
|
|
|
|
filteredTxList = this.getTxsByMetaData(key, opts[key], filteredTxList)
|
|
|
|
})
|
|
|
|
return filteredTxList
|
|
|
|
}
|
2020-08-19 18:27:05 +02:00
|
|
|
|
2018-04-19 20:29:26 +02:00
|
|
|
/**
|
2020-11-07 08:38:12 +01:00
|
|
|
* @param {string} key - the key to check
|
2020-11-10 18:30:41 +01:00
|
|
|
* @param {any} value - the value your looking for can also be a function that returns a bool
|
|
|
|
* @param {Array} [txList=this.getTxList()] - the list to search. default is the txList
|
2020-11-07 08:38:12 +01:00
|
|
|
* from txStateManager#getTxList
|
2020-11-10 18:30:41 +01:00
|
|
|
* @returns {Array} a list of txMetas who matches the search params
|
2020-11-07 08:38:12 +01:00
|
|
|
*/
|
2020-11-03 00:41:28 +01:00
|
|
|
getTxsByMetaData(key, value, txList = this.getTxList()) {
|
2019-08-29 14:57:28 +02:00
|
|
|
const filter = typeof value === 'function' ? value : (v) => v === value
|
|
|
|
|
2017-08-11 23:19:35 +02:00
|
|
|
return txList.filter((txMeta) => {
|
2018-05-01 22:57:14 +02:00
|
|
|
if (key in txMeta.txParams) {
|
2019-08-29 14:57:28 +02:00
|
|
|
return filter(txMeta.txParams[key])
|
2017-08-11 23:19:35 +02:00
|
|
|
}
|
2020-08-19 18:27:05 +02:00
|
|
|
return filter(txMeta[key])
|
2017-08-11 23:19:35 +02:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
// get::set status
|
|
|
|
|
2018-04-19 20:29:26 +02:00
|
|
|
/**
|
2020-11-07 08:38:12 +01:00
|
|
|
* @param {number} txId - the txMeta Id
|
2020-11-10 18:30:41 +01:00
|
|
|
* @returns {string} the status of the tx.
|
2020-11-07 08:38:12 +01:00
|
|
|
*/
|
2020-11-03 00:41:28 +01:00
|
|
|
getTxStatus(txId) {
|
2017-08-11 23:19:35 +02:00
|
|
|
const txMeta = this.getTx(txId)
|
|
|
|
return txMeta.status
|
|
|
|
}
|
|
|
|
|
2018-04-19 20:29:26 +02:00
|
|
|
/**
|
2020-11-07 08:38:12 +01:00
|
|
|
* Update the status of the tx to 'rejected'.
|
|
|
|
* @param {number} txId - the txMeta Id
|
|
|
|
*/
|
2020-11-03 00:41:28 +01:00
|
|
|
setTxStatusRejected(txId) {
|
2017-08-11 23:19:35 +02:00
|
|
|
this._setTxStatus(txId, 'rejected')
|
2018-06-26 00:07:54 +02:00
|
|
|
this._removeTx(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
|
|
|
* Update the status of the tx to 'unapproved'.
|
|
|
|
* @param {number} txId - the txMeta Id
|
|
|
|
*/
|
2020-11-03 00:41:28 +01:00
|
|
|
setTxStatusUnapproved(txId) {
|
2020-11-07 08:38:12 +01:00
|
|
|
this._setTxStatus(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
|
|
|
/**
|
2020-11-07 08:38:12 +01:00
|
|
|
* Update the status of the tx to 'approved'.
|
|
|
|
* @param {number} txId - the txMeta Id
|
|
|
|
*/
|
2020-11-03 00:41:28 +01:00
|
|
|
setTxStatusApproved(txId) {
|
2020-11-07 08:38:12 +01:00
|
|
|
this._setTxStatus(txId, TRANSACTION_STATUSES.APPROVED)
|
2017-08-11 23:19:35 +02:00
|
|
|
}
|
|
|
|
|
2018-04-19 20:29:26 +02:00
|
|
|
/**
|
2020-11-07 08:38:12 +01:00
|
|
|
* Update the status of the tx to 'signed'.
|
|
|
|
* @param {number} txId - the txMeta Id
|
|
|
|
*/
|
2020-11-03 00:41:28 +01:00
|
|
|
setTxStatusSigned(txId) {
|
2020-11-07 08:38:12 +01:00
|
|
|
this._setTxStatus(txId, TRANSACTION_STATUSES.SIGNED)
|
2017-08-11 23:19:35 +02:00
|
|
|
}
|
|
|
|
|
2018-04-19 20:29:26 +02:00
|
|
|
/**
|
2020-11-07 08:38:12 +01:00
|
|
|
* Update the status of the tx to 'submitted' and add a time stamp
|
|
|
|
* for when it was called
|
|
|
|
* @param {number} txId - the txMeta Id
|
|
|
|
*/
|
2020-11-03 00:41:28 +01:00
|
|
|
setTxStatusSubmitted(txId) {
|
2018-03-13 17:51:37 +01:00
|
|
|
const txMeta = this.getTx(txId)
|
2020-11-03 00:41:28 +01:00
|
|
|
txMeta.submittedTime = new Date().getTime()
|
2018-03-13 17:51:37 +01:00
|
|
|
this.updateTx(txMeta, 'txStateManager - add submitted time stamp')
|
2020-11-07 08:38:12 +01:00
|
|
|
this._setTxStatus(txId, TRANSACTION_STATUSES.SUBMITTED)
|
2017-08-11 23:19:35 +02:00
|
|
|
}
|
|
|
|
|
2018-04-19 20:29:26 +02:00
|
|
|
/**
|
2020-11-07 08:38:12 +01:00
|
|
|
* Update the status of the tx to 'confirmed'.
|
|
|
|
* @param {number} txId - the txMeta Id
|
|
|
|
*/
|
2020-11-03 00:41:28 +01:00
|
|
|
setTxStatusConfirmed(txId) {
|
2020-11-07 08:38:12 +01:00
|
|
|
this._setTxStatus(txId, TRANSACTION_STATUSES.CONFIRMED)
|
2017-08-11 23:19:35 +02:00
|
|
|
}
|
|
|
|
|
2018-04-19 20:29:26 +02:00
|
|
|
/**
|
2020-11-07 08:38:12 +01:00
|
|
|
* Update the status of the tx to 'dropped'.
|
|
|
|
* @param {number} txId - the txMeta Id
|
|
|
|
*/
|
2020-11-03 00:41:28 +01:00
|
|
|
setTxStatusDropped(txId) {
|
2020-11-07 08:38:12 +01:00
|
|
|
this._setTxStatus(txId, TRANSACTION_STATUSES.DROPPED)
|
2018-02-28 00:14:18 +01:00
|
|
|
}
|
|
|
|
|
2018-04-19 20:29:26 +02:00
|
|
|
/**
|
2020-11-07 08:38:12 +01:00
|
|
|
* Updates the status of the tx to 'failed' and put the error on the txMeta
|
|
|
|
* @param {number} txId - the txMeta Id
|
|
|
|
* @param {erroObject} err - error object
|
|
|
|
*/
|
2020-11-03 00:41:28 +01:00
|
|
|
setTxStatusFailed(txId, err) {
|
2020-08-14 13:47:43 +02:00
|
|
|
const error = err || new Error('Internal metamask failure')
|
2018-11-26 20:00:58 +01:00
|
|
|
|
2017-08-11 23:19:35 +02:00
|
|
|
const txMeta = this.getTx(txId)
|
2017-09-06 23:01:07 +02:00
|
|
|
txMeta.err = {
|
2018-11-26 20:00:58 +01:00
|
|
|
message: error.toString(),
|
|
|
|
rpc: error.value,
|
|
|
|
stack: error.stack,
|
2017-09-06 23:01:07 +02:00
|
|
|
}
|
2018-11-26 20:00:58 +01:00
|
|
|
this.updateTx(txMeta, 'transactions:tx-state-manager#fail - add error')
|
2020-11-07 08:38:12 +01:00
|
|
|
this._setTxStatus(txId, TRANSACTION_STATUSES.FAILED)
|
2017-08-11 23:19:35 +02:00
|
|
|
}
|
|
|
|
|
2018-04-19 20:29:26 +02:00
|
|
|
/**
|
2020-11-07 08:38:12 +01:00
|
|
|
* Removes transaction from the given address for the current network
|
|
|
|
* from the txList
|
|
|
|
* @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
|
2018-01-31 19:29:02 +01:00
|
|
|
const txs = this.getFullTxList()
|
|
|
|
const network = this.getNetwork()
|
2018-01-31 10:25:32 +01:00
|
|
|
|
2018-01-31 19:29:02 +01:00
|
|
|
// Filter out the ones from the current account and network
|
2020-11-03 00:41:28 +01:00
|
|
|
const otherAccountTxs = txs.filter(
|
|
|
|
(txMeta) =>
|
|
|
|
!(
|
|
|
|
txMeta.txParams.from === address &&
|
|
|
|
txMeta.metamaskNetworkId === network
|
|
|
|
),
|
|
|
|
)
|
2018-01-31 10:25:32 +01:00
|
|
|
|
|
|
|
// Update state
|
|
|
|
this._saveTxList(otherAccountTxs)
|
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
|
|
|
/**
|
2020-11-07 08:38:12 +01:00
|
|
|
* @param {number} txId - the txMeta Id
|
|
|
|
* @param {TransactionStatuses[keyof TransactionStatuses]} status - the status to set on the txMeta
|
|
|
|
* @emits tx:status-update - passes txId and status
|
|
|
|
* @emits ${txMeta.id}:finished - if it is a finished state. Passes the txMeta
|
|
|
|
* @emits update:badge
|
|
|
|
*/
|
2020-11-03 00:41:28 +01:00
|
|
|
_setTxStatus(txId, status) {
|
2017-08-11 23:19:35 +02:00
|
|
|
const txMeta = this.getTx(txId)
|
2018-10-16 00:00:47 +02:00
|
|
|
|
|
|
|
if (!txMeta) {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2017-08-11 23:19:35 +02:00
|
|
|
txMeta.status = status
|
2020-05-09 15:46:58 +02:00
|
|
|
try {
|
|
|
|
this.updateTx(txMeta, `txStateManager: setting status to ${status}`)
|
|
|
|
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)
|
|
|
|
) {
|
2020-05-09 15:46:58 +02:00
|
|
|
this.emit(`${txMeta.id}:finished`, txMeta)
|
2018-04-30 01:00:13 +02:00
|
|
|
}
|
2020-05-09 15:46:58 +02:00
|
|
|
this.emit('update:badge')
|
|
|
|
} catch (error) {
|
|
|
|
log.error(error)
|
|
|
|
}
|
2017-08-11 23:19:35 +02:00
|
|
|
}
|
|
|
|
|
2018-04-19 20:29:26 +02:00
|
|
|
/**
|
2020-11-07 08:38:12 +01:00
|
|
|
* Saves the new/updated txList. Intended only for internal use
|
2020-11-10 18:30:41 +01:00
|
|
|
* @param {Array} transactions - the list of transactions to save
|
2020-11-07 08:38:12 +01:00
|
|
|
*/
|
2020-11-03 00:41:28 +01:00
|
|
|
_saveTxList(transactions) {
|
2017-09-12 18:59:59 +02:00
|
|
|
this.store.updateState({ transactions })
|
2017-08-11 23:19:35 +02:00
|
|
|
}
|
2018-06-26 00:07:54 +02:00
|
|
|
|
2020-11-03 00:41:28 +01:00
|
|
|
_removeTx(txId) {
|
2018-06-26 00:07:54 +02:00
|
|
|
const transactionList = this.getFullTxList()
|
|
|
|
this._saveTxList(transactionList.filter((txMeta) => txMeta.id !== txId))
|
|
|
|
}
|
2020-07-17 03:37:56 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Filters out the unapproved transactions
|
|
|
|
*/
|
2020-11-03 00:41:28 +01:00
|
|
|
clearUnapprovedTxs() {
|
2020-07-17 03:37:56 +02:00
|
|
|
const transactions = this.getFullTxList()
|
2020-11-03 00:41:28 +01:00
|
|
|
const nonUnapprovedTxs = transactions.filter(
|
2020-11-07 08:38:12 +01:00
|
|
|
(tx) => tx.status !== TRANSACTION_STATUSES.UNAPPROVED,
|
2020-11-03 00:41:28 +01:00
|
|
|
)
|
2020-07-17 03:37:56 +02:00
|
|
|
this._saveTxList(nonUnapprovedTxs)
|
|
|
|
}
|
2017-10-06 21:50:33 +02:00
|
|
|
}
|