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.
260 lines
8.2 KiB
260 lines
8.2 KiB
4 years ago
|
import { TRANSACTION_TYPES } from '../../../../shared/constants/transaction';
|
||
4 years ago
|
import { getHexGasTotal } from '../../../helpers/utils/confirm-tx.util';
|
||
6 years ago
|
|
||
|
import {
|
||
|
// event constants
|
||
|
TRANSACTION_CREATED_EVENT,
|
||
|
TRANSACTION_SUBMITTED_EVENT,
|
||
|
TRANSACTION_RESUBMITTED_EVENT,
|
||
|
TRANSACTION_CONFIRMED_EVENT,
|
||
|
TRANSACTION_DROPPED_EVENT,
|
||
|
TRANSACTION_UPDATED_EVENT,
|
||
|
TRANSACTION_ERRORED_EVENT,
|
||
|
TRANSACTION_CANCEL_ATTEMPTED_EVENT,
|
||
|
TRANSACTION_CANCEL_SUCCESS_EVENT,
|
||
|
// status constants
|
||
|
SUBMITTED_STATUS,
|
||
|
CONFIRMED_STATUS,
|
||
|
DROPPED_STATUS,
|
||
4 years ago
|
} from './transaction-activity-log.constants';
|
||
6 years ago
|
|
||
4 years ago
|
// path constants
|
||
4 years ago
|
const STATUS_PATH = '/status';
|
||
|
const GAS_PRICE_PATH = '/txParams/gasPrice';
|
||
|
const GAS_LIMIT_PATH = '/txParams/gas';
|
||
4 years ago
|
|
||
|
// op constants
|
||
4 years ago
|
const REPLACE_OP = 'replace';
|
||
6 years ago
|
|
||
6 years ago
|
const eventPathsHash = {
|
||
|
[STATUS_PATH]: true,
|
||
|
[GAS_PRICE_PATH]: true,
|
||
6 years ago
|
[GAS_LIMIT_PATH]: true,
|
||
4 years ago
|
};
|
||
6 years ago
|
|
||
|
const statusHash = {
|
||
6 years ago
|
[SUBMITTED_STATUS]: TRANSACTION_SUBMITTED_EVENT,
|
||
|
[CONFIRMED_STATUS]: TRANSACTION_CONFIRMED_EVENT,
|
||
|
[DROPPED_STATUS]: TRANSACTION_DROPPED_EVENT,
|
||
4 years ago
|
};
|
||
6 years ago
|
|
||
6 years ago
|
/**
|
||
|
* @name getActivities
|
||
|
* @param {Object} transaction - txMeta object
|
||
|
* @param {boolean} isFirstTransaction - True if the transaction is the first created transaction
|
||
|
* in the list of transactions with the same nonce. If so, we use this transaction to create the
|
||
|
* transactionCreated activity.
|
||
|
* @returns {Array}
|
||
|
*/
|
||
4 years ago
|
export function getActivities(transaction, isFirstTransaction = false) {
|
||
6 years ago
|
const {
|
||
|
id,
|
||
4 years ago
|
chainId,
|
||
|
metamaskNetworkId,
|
||
6 years ago
|
hash,
|
||
|
history = [],
|
||
5 years ago
|
txParams: { gas: paramsGasLimit, gasPrice: paramsGasPrice },
|
||
6 years ago
|
xReceipt: { status } = {},
|
||
|
type,
|
||
4 years ago
|
} = transaction;
|
||
6 years ago
|
|
||
4 years ago
|
let cachedGasLimit = '0x0';
|
||
|
let cachedGasPrice = '0x0';
|
||
6 years ago
|
|
||
|
const historyActivities = history.reduce((acc, base, index) => {
|
||
6 years ago
|
// First history item should be transaction creation
|
||
6 years ago
|
if (index === 0 && !Array.isArray(base) && base.txParams) {
|
||
4 years ago
|
const {
|
||
|
time: timestamp,
|
||
|
txParams: { value, gas = '0x0', gasPrice = '0x0' } = {},
|
||
4 years ago
|
} = base;
|
||
6 years ago
|
// The cached gas limit and gas price are used to display the gas fee in the activity log. We
|
||
|
// need to cache these values because the status update history events don't provide us with
|
||
|
// the latest gas limit and gas price.
|
||
4 years ago
|
cachedGasLimit = gas;
|
||
|
cachedGasPrice = gasPrice;
|
||
6 years ago
|
|
||
|
if (isFirstTransaction) {
|
||
|
return acc.concat({
|
||
|
id,
|
||
|
hash,
|
||
4 years ago
|
chainId,
|
||
|
metamaskNetworkId,
|
||
6 years ago
|
eventKey: TRANSACTION_CREATED_EVENT,
|
||
|
timestamp,
|
||
|
value,
|
||
4 years ago
|
});
|
||
6 years ago
|
}
|
||
6 years ago
|
// An entry in the history may be an array of more sub-entries.
|
||
6 years ago
|
} else if (Array.isArray(base)) {
|
||
4 years ago
|
const events = [];
|
||
6 years ago
|
|
||
5 years ago
|
base.forEach((entry) => {
|
||
4 years ago
|
const { op, path, value, timestamp: entryTimestamp } = entry;
|
||
6 years ago
|
// Not all sub-entries in a history entry have a timestamp. If the sub-entry does not have a
|
||
|
// timestamp, the first sub-entry in a history entry should.
|
||
4 years ago
|
const timestamp = entryTimestamp || (base[0] && base[0].timestamp);
|
||
6 years ago
|
|
||
|
if (path in eventPathsHash && op === REPLACE_OP) {
|
||
|
switch (path) {
|
||
|
case STATUS_PATH: {
|
||
4 years ago
|
const gasFee =
|
||
|
cachedGasLimit === '0x0' && cachedGasPrice === '0x0'
|
||
|
? getHexGasTotal({
|
||
|
gasLimit: paramsGasLimit,
|
||
|
gasPrice: paramsGasPrice,
|
||
|
})
|
||
|
: getHexGasTotal({
|
||
|
gasLimit: cachedGasLimit,
|
||
|
gasPrice: cachedGasPrice,
|
||
4 years ago
|
});
|
||
6 years ago
|
|
||
6 years ago
|
if (value in statusHash) {
|
||
4 years ago
|
let eventKey = statusHash[value];
|
||
6 years ago
|
|
||
|
// If the status is 'submitted', we need to determine whether the event is a
|
||
|
// transaction retry or a cancellation attempt.
|
||
|
if (value === SUBMITTED_STATUS) {
|
||
4 years ago
|
if (type === TRANSACTION_TYPES.RETRY) {
|
||
4 years ago
|
eventKey = TRANSACTION_RESUBMITTED_EVENT;
|
||
4 years ago
|
} else if (type === TRANSACTION_TYPES.CANCEL) {
|
||
4 years ago
|
eventKey = TRANSACTION_CANCEL_ATTEMPTED_EVENT;
|
||
6 years ago
|
}
|
||
|
} else if (value === CONFIRMED_STATUS) {
|
||
4 years ago
|
if (type === TRANSACTION_TYPES.CANCEL) {
|
||
4 years ago
|
eventKey = TRANSACTION_CANCEL_SUCCESS_EVENT;
|
||
6 years ago
|
}
|
||
|
}
|
||
|
|
||
|
events.push({
|
||
|
id,
|
||
|
hash,
|
||
|
eventKey,
|
||
|
timestamp,
|
||
4 years ago
|
chainId,
|
||
|
metamaskNetworkId,
|
||
6 years ago
|
value: gasFee,
|
||
4 years ago
|
});
|
||
6 years ago
|
}
|
||
|
|
||
4 years ago
|
break;
|
||
6 years ago
|
}
|
||
|
|
||
6 years ago
|
// If the gas price or gas limit has been changed, we update the gasFee of the
|
||
|
// previously submitted event. These events happen when the gas limit and gas price is
|
||
|
// changed at the confirm screen.
|
||
|
case GAS_PRICE_PATH:
|
||
|
case GAS_LIMIT_PATH: {
|
||
4 years ago
|
const lastEvent = events[events.length - 1] || {};
|
||
|
const { lastEventKey } = lastEvent;
|
||
6 years ago
|
|
||
|
if (path === GAS_LIMIT_PATH) {
|
||
4 years ago
|
cachedGasLimit = value;
|
||
6 years ago
|
} else if (path === GAS_PRICE_PATH) {
|
||
4 years ago
|
cachedGasPrice = value;
|
||
6 years ago
|
}
|
||
|
|
||
4 years ago
|
if (
|
||
|
lastEventKey === TRANSACTION_SUBMITTED_EVENT ||
|
||
|
lastEventKey === TRANSACTION_RESUBMITTED_EVENT
|
||
|
) {
|
||
6 years ago
|
lastEvent.value = getHexGasTotal({
|
||
|
gasLimit: cachedGasLimit,
|
||
|
gasPrice: cachedGasPrice,
|
||
4 years ago
|
});
|
||
6 years ago
|
}
|
||
|
|
||
4 years ago
|
break;
|
||
6 years ago
|
}
|
||
|
|
||
|
default: {
|
||
6 years ago
|
events.push({
|
||
|
id,
|
||
|
hash,
|
||
4 years ago
|
chainId,
|
||
|
metamaskNetworkId,
|
||
6 years ago
|
eventKey: TRANSACTION_UPDATED_EVENT,
|
||
|
timestamp,
|
||
4 years ago
|
});
|
||
6 years ago
|
}
|
||
|
}
|
||
|
}
|
||
4 years ago
|
});
|
||
6 years ago
|
|
||
4 years ago
|
return acc.concat(events);
|
||
6 years ago
|
}
|
||
|
|
||
4 years ago
|
return acc;
|
||
|
}, []);
|
||
6 years ago
|
|
||
4 years ago
|
// If txReceipt.status is '0x0', that means that an on-chain error occurred for the transaction,
|
||
6 years ago
|
// so we add an error entry to the Activity Log.
|
||
|
return status === '0x0'
|
||
4 years ago
|
? historyActivities.concat({
|
||
|
id,
|
||
|
hash,
|
||
4 years ago
|
chainId,
|
||
|
metamaskNetworkId,
|
||
4 years ago
|
eventKey: TRANSACTION_ERRORED_EVENT,
|
||
|
})
|
||
4 years ago
|
: historyActivities;
|
||
6 years ago
|
}
|
||
6 years ago
|
|
||
|
/**
|
||
|
* @description Removes "Transaction dropped" activities from a list of sorted activities if one of
|
||
|
* the transactions has been confirmed. Typically, if multiple transactions have the same nonce,
|
||
|
* once one transaction is confirmed, the rest are dropped. In this case, we don't want to show
|
||
|
* multiple "Transaction dropped" activities, and instead want to show a single "Transaction
|
||
|
* confirmed".
|
||
|
* @param {Array} activities - List of sorted activities generated from the getActivities function.
|
||
|
* @returns {Array}
|
||
|
*/
|
||
4 years ago
|
function filterSortedActivities(activities) {
|
||
4 years ago
|
const filteredActivities = [];
|
||
4 years ago
|
const hasConfirmedActivity = Boolean(
|
||
|
activities.find(
|
||
|
({ eventKey }) =>
|
||
|
eventKey === TRANSACTION_CONFIRMED_EVENT ||
|
||
|
eventKey === TRANSACTION_CANCEL_SUCCESS_EVENT,
|
||
|
),
|
||
4 years ago
|
);
|
||
|
let addedDroppedActivity = false;
|
||
6 years ago
|
|
||
5 years ago
|
activities.forEach((activity) => {
|
||
6 years ago
|
if (activity.eventKey === TRANSACTION_DROPPED_EVENT) {
|
||
|
if (!hasConfirmedActivity && !addedDroppedActivity) {
|
||
4 years ago
|
filteredActivities.push(activity);
|
||
|
addedDroppedActivity = true;
|
||
6 years ago
|
}
|
||
|
} else {
|
||
4 years ago
|
filteredActivities.push(activity);
|
||
6 years ago
|
}
|
||
4 years ago
|
});
|
||
6 years ago
|
|
||
4 years ago
|
return filteredActivities;
|
||
6 years ago
|
}
|
||
|
|
||
|
/**
|
||
|
* Combines the histories of an array of transactions into a single array.
|
||
|
* @param {Array} transactions - Array of txMeta transaction objects.
|
||
|
* @returns {Array}
|
||
|
*/
|
||
4 years ago
|
export function combineTransactionHistories(transactions = []) {
|
||
6 years ago
|
if (!transactions.length) {
|
||
4 years ago
|
return [];
|
||
6 years ago
|
}
|
||
|
|
||
4 years ago
|
const activities = [];
|
||
6 years ago
|
|
||
|
transactions.forEach((transaction, index) => {
|
||
|
// The first transaction should be the transaction with the earliest submittedTime. We show the
|
||
|
// 'created' and 'submitted' activities here. All subsequent transactions will use 'resubmitted'
|
||
|
// instead.
|
||
4 years ago
|
const transactionActivities = getActivities(transaction, index === 0);
|
||
|
activities.push(...transactionActivities);
|
||
|
});
|
||
6 years ago
|
|
||
4 years ago
|
const sortedActivities = activities.sort((a, b) => a.timestamp - b.timestamp);
|
||
|
return filterSortedActivities(sortedActivities);
|
||
6 years ago
|
}
|