2018-07-31 07:03:20 +02:00
|
|
|
import { createSelector } from 'reselect'
|
|
|
|
import {
|
|
|
|
UNAPPROVED_STATUS,
|
|
|
|
APPROVED_STATUS,
|
|
|
|
SUBMITTED_STATUS,
|
2018-12-09 21:48:06 +01:00
|
|
|
CONFIRMED_STATUS,
|
2019-03-22 00:03:30 +01:00
|
|
|
} from '../helpers/constants/transactions'
|
2018-12-09 21:48:06 +01:00
|
|
|
import {
|
|
|
|
TRANSACTION_TYPE_CANCEL,
|
|
|
|
TRANSACTION_TYPE_RETRY,
|
|
|
|
} from '../../../app/scripts/controllers/transactions/enums'
|
2019-03-22 00:03:30 +01:00
|
|
|
import { hexToDecimal } from '../helpers/utils/conversions.util'
|
2018-08-06 07:25:58 +02:00
|
|
|
import { selectedTokenAddressSelector } from './tokens'
|
2019-11-01 02:51:28 +01:00
|
|
|
import { getFastPriceEstimateInHexWEI } from './custom-gas'
|
2020-03-06 22:34:56 +01:00
|
|
|
import {
|
|
|
|
getSelectedToken,
|
|
|
|
getSelectedAddress,
|
|
|
|
} from './selectors'
|
2018-12-09 21:48:06 +01:00
|
|
|
import txHelper from '../../lib/tx-helper'
|
2018-08-06 07:25:58 +02:00
|
|
|
|
2020-02-15 21:34:12 +01:00
|
|
|
export const incomingTxListSelector = (state) => {
|
2019-08-21 20:42:14 +02:00
|
|
|
const { showIncomingTransactions } = state.metamask.featureFlags
|
|
|
|
if (!showIncomingTransactions) {
|
|
|
|
return []
|
|
|
|
}
|
|
|
|
|
2019-08-20 20:52:00 +02:00
|
|
|
const network = state.metamask.network
|
2020-03-06 22:34:56 +01:00
|
|
|
const selectedAddress = getSelectedAddress(state)
|
2019-08-16 20:54:10 +02:00
|
|
|
return Object.values(state.metamask.incomingTransactions)
|
2019-08-20 20:52:00 +02:00
|
|
|
.filter(({ metamaskNetworkId, txParams }) => (
|
|
|
|
txParams.to === selectedAddress && metamaskNetworkId === network
|
|
|
|
))
|
2019-08-16 20:54:10 +02:00
|
|
|
}
|
2020-02-15 21:34:12 +01:00
|
|
|
export const unapprovedMsgsSelector = (state) => state.metamask.unapprovedMsgs
|
2020-03-06 22:34:56 +01:00
|
|
|
export const currentNetworkTxListSelector = (state) => state.metamask.currentNetworkTxList
|
2020-02-15 21:34:12 +01:00
|
|
|
export const unapprovedPersonalMsgsSelector = (state) => state.metamask.unapprovedPersonalMsgs
|
2020-02-19 19:24:16 +01:00
|
|
|
export const unapprovedDecryptMsgsSelector = (state) => state.metamask.unapprovedDecryptMsgs
|
|
|
|
export const unapprovedEncryptionPublicKeyMsgsSelector = (state) => state.metamask.unapprovedEncryptionPublicKeyMsgs
|
2020-02-15 21:34:12 +01:00
|
|
|
export const unapprovedTypedMessagesSelector = (state) => state.metamask.unapprovedTypedMessages
|
|
|
|
export const networkSelector = (state) => state.metamask.network
|
2018-12-09 21:48:06 +01:00
|
|
|
|
2020-03-06 22:34:56 +01:00
|
|
|
export const selectedAddressTxListSelector = createSelector(
|
|
|
|
getSelectedAddress,
|
|
|
|
currentNetworkTxListSelector,
|
|
|
|
(selectedAddress, transactions = []) => {
|
|
|
|
return transactions.filter(({ txParams }) => txParams.from === selectedAddress)
|
|
|
|
}
|
|
|
|
)
|
|
|
|
|
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,
|
|
|
|
networkSelector,
|
|
|
|
(
|
|
|
|
unapprovedMsgs = {},
|
|
|
|
unapprovedPersonalMsgs = {},
|
2020-02-19 19:24:16 +01:00
|
|
|
unapprovedDecryptMsgs = {},
|
|
|
|
unapprovedEncryptionPublicKeyMsgs = {},
|
2018-12-09 21:48:06 +01:00
|
|
|
unapprovedTypedMessages = {},
|
|
|
|
network
|
|
|
|
) => txHelper(
|
|
|
|
{},
|
|
|
|
unapprovedMsgs,
|
|
|
|
unapprovedPersonalMsgs,
|
2020-02-19 19:24:16 +01:00
|
|
|
unapprovedDecryptMsgs,
|
|
|
|
unapprovedEncryptionPublicKeyMsgs,
|
2018-12-09 21:48:06 +01:00
|
|
|
unapprovedTypedMessages,
|
|
|
|
network
|
|
|
|
) || []
|
|
|
|
)
|
2018-07-31 07:03:20 +02:00
|
|
|
|
|
|
|
const pendingStatusHash = {
|
|
|
|
[UNAPPROVED_STATUS]: true,
|
|
|
|
[APPROVED_STATUS]: true,
|
|
|
|
[SUBMITTED_STATUS]: true,
|
|
|
|
}
|
|
|
|
|
2018-12-09 21:48:06 +01:00
|
|
|
const priorityStatusHash = {
|
|
|
|
...pendingStatusHash,
|
|
|
|
[CONFIRMED_STATUS]: true,
|
|
|
|
}
|
|
|
|
|
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 = []) => {
|
|
|
|
return unapprovedMessages.concat(incomingTxList)
|
2020-03-06 22:34:56 +01:00
|
|
|
}
|
|
|
|
)
|
|
|
|
|
|
|
|
const transactionSelectorReturnHelper = (selectedTokenAddress, transactions) => {
|
|
|
|
return selectedTokenAddress
|
|
|
|
? transactions
|
|
|
|
.filter(({ txParams }) => txParams && txParams.to === selectedTokenAddress)
|
|
|
|
.sort((a, b) => b.time - a.time)
|
|
|
|
: transactions
|
|
|
|
.sort((a, b) => b.time - a.time)
|
|
|
|
}
|
|
|
|
|
|
|
|
export const transactionsSelector = createSelector(
|
|
|
|
selectedTokenAddressSelector,
|
|
|
|
transactionSubSelector,
|
2018-07-31 07:03:20 +02:00
|
|
|
selectedAddressTxListSelector,
|
2020-03-06 22:34:56 +01:00
|
|
|
(selectedTokenAddress, subSelectorTxList = [], selectedAddressTxList = []) => {
|
|
|
|
const txsToRender = selectedAddressTxList.concat(subSelectorTxList)
|
2018-07-31 07:03:20 +02:00
|
|
|
|
2020-03-06 22:34:56 +01:00
|
|
|
return transactionSelectorReturnHelper(selectedTokenAddress, txsToRender)
|
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.
|
|
|
|
* @returns {string[]}
|
|
|
|
*/
|
|
|
|
const insertOrderedNonce = (nonces, nonceToInsert) => {
|
|
|
|
let insertIndex = nonces.length
|
|
|
|
|
|
|
|
for (let i = 0; i < nonces.length; i++) {
|
|
|
|
const nonce = nonces[i]
|
|
|
|
|
2019-01-14 23:34:37 +01:00
|
|
|
if (Number(hexToDecimal(nonce)) > Number(hexToDecimal(nonceToInsert))) {
|
2018-12-09 21:48:06 +01:00
|
|
|
insertIndex = i
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
nonces.splice(insertIndex, 0, nonceToInsert)
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @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) => {
|
|
|
|
const { time } = transaction
|
|
|
|
|
|
|
|
let insertIndex = transactions.length
|
|
|
|
|
|
|
|
for (let i = 0; i < transactions.length; i++) {
|
|
|
|
const tx = transactions[i]
|
|
|
|
|
|
|
|
if (tx.time > time) {
|
|
|
|
insertIndex = i
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
transactions.splice(insertIndex, 0, transaction)
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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) => {
|
2019-01-14 23:34:37 +01:00
|
|
|
const { primaryTransaction: { time: groupToInsertTime } = {} } = transactionGroup
|
2018-12-09 21:48:06 +01:00
|
|
|
|
|
|
|
let insertIndex = transactionGroups.length
|
|
|
|
|
|
|
|
for (let i = 0; i < transactionGroups.length; i++) {
|
|
|
|
const txGroup = transactionGroups[i]
|
2019-01-14 23:34:37 +01:00
|
|
|
const { primaryTransaction: { time } = {} } = txGroup
|
2018-12-09 21:48:06 +01:00
|
|
|
|
2019-01-14 23:34:37 +01:00
|
|
|
if (time > groupToInsertTime) {
|
2018-12-09 21:48:06 +01:00
|
|
|
insertIndex = i
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
transactionGroups.splice(insertIndex, 0, transactionGroup)
|
|
|
|
}
|
|
|
|
|
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
|
|
|
*/
|
2019-08-16 20:54:10 +02:00
|
|
|
const mergeNonNonceTransactionGroups = (orderedTransactionGroups, nonNonceTransactionGroups) => {
|
2020-04-24 17:33:24 +02:00
|
|
|
nonNonceTransactionGroups.forEach((transactionGroup) => {
|
|
|
|
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 = []) => {
|
|
|
|
const unapprovedTransactionGroups = []
|
2019-08-16 20:54:10 +02:00
|
|
|
const incomingTransactionGroups = []
|
2018-12-09 21:48:06 +01:00
|
|
|
const orderedNonces = []
|
|
|
|
const nonceToTransactionsMap = {}
|
|
|
|
|
2020-02-15 21:34:12 +01:00
|
|
|
transactions.forEach((transaction) => {
|
2020-04-24 17:33:24 +02:00
|
|
|
const { txParams: { nonce } = {}, status, type, time: txTime, transactionCategory } = transaction
|
2018-12-09 21:48:06 +01:00
|
|
|
|
2019-08-16 20:54:10 +02:00
|
|
|
if (typeof nonce === 'undefined' || transactionCategory === 'incoming') {
|
2018-12-09 21:48:06 +01:00
|
|
|
const transactionGroup = {
|
|
|
|
transactions: [transaction],
|
|
|
|
initialTransaction: transaction,
|
|
|
|
primaryTransaction: transaction,
|
|
|
|
hasRetried: false,
|
|
|
|
hasCancelled: false,
|
|
|
|
}
|
|
|
|
|
2020-04-24 17:33:24 +02:00
|
|
|
if (transactionCategory === 'incoming') {
|
2019-08-16 20:54:10 +02:00
|
|
|
incomingTransactionGroups.push(transactionGroup)
|
2019-01-14 23:34:37 +01:00
|
|
|
} else {
|
|
|
|
insertTransactionGroupByTime(unapprovedTransactionGroups, transactionGroup)
|
|
|
|
}
|
2018-12-09 21:48:06 +01:00
|
|
|
} else if (nonce in nonceToTransactionsMap) {
|
|
|
|
const nonceProps = nonceToTransactionsMap[nonce]
|
|
|
|
insertTransactionByTime(nonceProps.transactions, transaction)
|
|
|
|
|
|
|
|
if (status in priorityStatusHash) {
|
|
|
|
const { primaryTransaction: { time: primaryTxTime = 0 } = {} } = nonceProps
|
|
|
|
|
|
|
|
if (status === CONFIRMED_STATUS || txTime > primaryTxTime) {
|
|
|
|
nonceProps.primaryTransaction = transaction
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
const { initialTransaction: { time: initialTxTime = 0 } = {} } = nonceProps
|
|
|
|
|
|
|
|
// 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) {
|
|
|
|
nonceProps.initialTransaction = transaction
|
|
|
|
}
|
|
|
|
|
|
|
|
if (type === TRANSACTION_TYPE_RETRY) {
|
|
|
|
nonceProps.hasRetried = true
|
|
|
|
}
|
|
|
|
|
|
|
|
if (type === TRANSACTION_TYPE_CANCEL) {
|
|
|
|
nonceProps.hasCancelled = true
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
nonceToTransactionsMap[nonce] = {
|
|
|
|
nonce,
|
|
|
|
transactions: [transaction],
|
|
|
|
initialTransaction: transaction,
|
|
|
|
primaryTransaction: transaction,
|
|
|
|
hasRetried: transaction.type === TRANSACTION_TYPE_RETRY,
|
|
|
|
hasCancelled: transaction.type === TRANSACTION_TYPE_CANCEL,
|
|
|
|
}
|
|
|
|
|
|
|
|
insertOrderedNonce(orderedNonces, nonce)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
2020-02-15 21:34:12 +01:00
|
|
|
const orderedTransactionGroups = orderedNonces.map((nonce) => nonceToTransactionsMap[nonce])
|
2019-08-16 20:54:10 +02:00
|
|
|
mergeNonNonceTransactionGroups(orderedTransactionGroups, incomingTransactionGroups)
|
2018-12-09 21:48:06 +01:00
|
|
|
return unapprovedTransactionGroups.concat(orderedTransactionGroups)
|
|
|
|
}
|
|
|
|
)
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @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,
|
2018-07-31 07:03:20 +02:00
|
|
|
(transactions = []) => (
|
2019-01-14 23:34:37 +01:00
|
|
|
transactions.filter(({ primaryTransaction }) => primaryTransaction.status in pendingStatusHash)
|
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,
|
2018-08-07 07:39:54 +02:00
|
|
|
(transactions = []) => (
|
2019-01-14 23:34:37 +01:00
|
|
|
transactions
|
|
|
|
.filter(({ primaryTransaction }) => !(primaryTransaction.status in pendingStatusHash))
|
|
|
|
.reverse()
|
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,
|
|
|
|
(transactions = []) => (
|
2020-02-15 21:34:12 +01:00
|
|
|
transactions.filter((transaction) => transaction.status === SUBMITTED_STATUS)
|
2018-07-31 07:03:20 +02:00
|
|
|
)
|
|
|
|
)
|
2019-11-01 02:51:28 +01:00
|
|
|
|
|
|
|
export const getTxParams = (state, selectedTransaction = {}) => {
|
|
|
|
const { metamask: { send } } = state
|
|
|
|
const { txParams } = selectedTransaction
|
|
|
|
return txParams || {
|
|
|
|
from: send.from,
|
|
|
|
gas: send.gasLimit || '0x5208',
|
|
|
|
gasPrice: send.gasPrice || getFastPriceEstimateInHexWEI(state, true),
|
|
|
|
to: send.to,
|
|
|
|
value: getSelectedToken(state) ? '0x0' : send.amount,
|
|
|
|
}
|
|
|
|
}
|