2021-07-23 01:13:40 +02:00
|
|
|
import { useDispatch, useSelector } from 'react-redux';
|
2021-02-04 19:15:23 +01:00
|
|
|
import { getKnownMethodData } from '../selectors/selectors';
|
2021-02-08 17:06:58 +01:00
|
|
|
import {
|
|
|
|
getStatusKey,
|
2021-03-10 21:16:44 +01:00
|
|
|
getTransactionTypeTitle,
|
2021-02-08 17:06:58 +01:00
|
|
|
} from '../helpers/utils/transactions.util';
|
2021-02-04 19:15:23 +01:00
|
|
|
import { camelCaseToCapitalize } from '../helpers/utils/common.util';
|
|
|
|
import { PRIMARY, SECONDARY } from '../helpers/constants/common';
|
|
|
|
import { getTokenAddressParam } from '../helpers/utils/token-util';
|
2020-11-03 00:41:28 +01:00
|
|
|
import {
|
2021-09-10 19:37:19 +02:00
|
|
|
isEqualCaseInsensitive,
|
2020-11-03 00:41:28 +01:00
|
|
|
formatDateWithYearContext,
|
|
|
|
shortenAddress,
|
|
|
|
stripHttpSchemes,
|
2021-02-04 19:15:23 +01:00
|
|
|
} from '../helpers/utils/util';
|
2021-09-10 19:37:19 +02:00
|
|
|
|
2020-05-26 22:49:11 +02:00
|
|
|
import {
|
|
|
|
PENDING_STATUS_HASH,
|
|
|
|
TOKEN_CATEGORY_HASH,
|
2021-02-04 19:15:23 +01:00
|
|
|
} from '../helpers/constants/transactions';
|
|
|
|
import { getTokens } from '../ducks/metamask/metamask';
|
2020-11-03 23:57:51 +01:00
|
|
|
import {
|
2021-03-10 21:16:44 +01:00
|
|
|
TRANSACTION_TYPES,
|
2020-11-03 23:57:51 +01:00
|
|
|
TRANSACTION_GROUP_CATEGORIES,
|
|
|
|
TRANSACTION_STATUSES,
|
2021-04-28 21:53:59 +02:00
|
|
|
} from '../../shared/constants/transaction';
|
2021-07-23 01:13:40 +02:00
|
|
|
import { captureSingleException } from '../store/actions';
|
2021-02-04 19:15:23 +01:00
|
|
|
import { useI18nContext } from './useI18nContext';
|
|
|
|
import { useTokenFiatAmount } from './useTokenFiatAmount';
|
|
|
|
import { useUserPreferencedCurrency } from './useUserPreferencedCurrency';
|
|
|
|
import { useCurrencyDisplay } from './useCurrencyDisplay';
|
|
|
|
import { useTokenDisplayValue } from './useTokenDisplayValue';
|
|
|
|
import { useTokenData } from './useTokenData';
|
|
|
|
import { useSwappedTokenValue } from './useSwappedTokenValue';
|
|
|
|
import { useCurrentAsset } from './useCurrentAsset';
|
2020-05-26 22:49:11 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @typedef {Object} TransactionDisplayData
|
2020-06-17 18:38:15 +02:00
|
|
|
* @property {string} title - primary description of the transaction
|
|
|
|
* @property {string} subtitle - supporting text describing the transaction
|
|
|
|
* @property {bool} subtitleContainsOrigin - true if the subtitle includes the origin of the tx
|
|
|
|
* @property {string} category - the transaction category
|
|
|
|
* @property {string} primaryCurrency - the currency string to display in the primary position
|
|
|
|
* @property {string} [secondaryCurrency] - the currency string to display in the secondary position
|
|
|
|
* @property {string} status - the status of the transaction
|
|
|
|
* @property {string} senderAddress - the Ethereum address of the sender
|
|
|
|
* @property {string} recipientAddress - the Ethereum address of the recipient
|
2020-05-26 22:49:11 +02:00
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get computed values used for displaying transaction data to a user
|
|
|
|
*
|
|
|
|
* The goal of this method is to perform all of the necessary computation and
|
|
|
|
* state access required to take a transactionGroup and derive from it a shape
|
|
|
|
* of data that can power all views related to a transaction. Presently the main
|
|
|
|
* case is for shared logic between transaction-list-item and transaction-detail-view
|
2020-11-10 18:30:41 +01:00
|
|
|
* @param {Object} transactionGroup - group of transactions
|
2020-05-26 22:49:11 +02:00
|
|
|
* @return {TransactionDisplayData}
|
|
|
|
*/
|
2020-11-03 00:41:28 +01:00
|
|
|
export function useTransactionDisplayData(transactionGroup) {
|
2020-10-06 20:28:38 +02:00
|
|
|
// To determine which primary currency to display for swaps transactions we need to be aware
|
|
|
|
// of which asset, if any, we are viewing at present
|
2021-07-23 01:13:40 +02:00
|
|
|
const dispatch = useDispatch();
|
2021-02-04 19:15:23 +01:00
|
|
|
const currentAsset = useCurrentAsset();
|
|
|
|
const knownTokens = useSelector(getTokens);
|
|
|
|
const t = useI18nContext();
|
|
|
|
const { initialTransaction, primaryTransaction } = transactionGroup;
|
2020-05-26 22:49:11 +02:00
|
|
|
// initialTransaction contains the data we need to derive the primary purpose of this transaction group
|
2021-03-10 21:16:44 +01:00
|
|
|
const { type } = initialTransaction;
|
2020-05-26 22:49:11 +02:00
|
|
|
|
2021-02-04 19:15:23 +01:00
|
|
|
const { from: senderAddress, to } = initialTransaction.txParams || {};
|
2020-05-26 22:49:11 +02:00
|
|
|
|
|
|
|
// for smart contract interactions, methodData can be used to derive the name of the action being taken
|
2020-11-03 00:41:28 +01:00
|
|
|
const methodData =
|
|
|
|
useSelector((state) =>
|
|
|
|
getKnownMethodData(state, initialTransaction?.txParams?.data),
|
2021-02-04 19:15:23 +01:00
|
|
|
) || {};
|
2020-05-26 22:49:11 +02:00
|
|
|
|
2021-02-04 19:15:23 +01:00
|
|
|
const displayedStatusKey = getStatusKey(primaryTransaction);
|
|
|
|
const isPending = displayedStatusKey in PENDING_STATUS_HASH;
|
|
|
|
const isSubmitted = displayedStatusKey === TRANSACTION_STATUSES.SUBMITTED;
|
2020-05-26 22:49:11 +02:00
|
|
|
|
2021-02-04 19:15:23 +01:00
|
|
|
const primaryValue = primaryTransaction.txParams?.value;
|
|
|
|
let prefix = '-';
|
2021-08-13 17:22:12 +02:00
|
|
|
const date = formatDateWithYearContext(initialTransaction.time);
|
2021-02-04 19:15:23 +01:00
|
|
|
let subtitle;
|
|
|
|
let subtitleContainsOrigin = false;
|
|
|
|
let recipientAddress = to;
|
2020-05-26 22:49:11 +02:00
|
|
|
|
|
|
|
// This value is used to determine whether we should look inside txParams.data
|
|
|
|
// to pull out and render token related information
|
2021-03-10 21:16:44 +01:00
|
|
|
const isTokenCategory = TOKEN_CATEGORY_HASH[type];
|
2020-05-26 22:49:11 +02:00
|
|
|
|
|
|
|
// these values are always instantiated because they are either
|
|
|
|
// used by or returned from hooks. Hooks must be called at the top level,
|
|
|
|
// so as an additional safeguard against inappropriately associating token
|
|
|
|
// transfers, we pass an additional argument to these hooks that will be
|
|
|
|
// false for non-token transactions. This additional argument forces the
|
|
|
|
// hook to return null
|
2020-11-03 00:41:28 +01:00
|
|
|
const token =
|
|
|
|
isTokenCategory &&
|
2021-09-10 19:37:19 +02:00
|
|
|
knownTokens.find(({ address }) =>
|
|
|
|
isEqualCaseInsensitive(address, recipientAddress),
|
|
|
|
);
|
2020-11-03 00:41:28 +01:00
|
|
|
const tokenData = useTokenData(
|
|
|
|
initialTransaction?.txParams?.data,
|
|
|
|
isTokenCategory,
|
2021-02-04 19:15:23 +01:00
|
|
|
);
|
2020-11-03 00:41:28 +01:00
|
|
|
const tokenDisplayValue = useTokenDisplayValue(
|
|
|
|
initialTransaction?.txParams?.data,
|
|
|
|
token,
|
|
|
|
isTokenCategory,
|
2021-02-04 19:15:23 +01:00
|
|
|
);
|
2020-11-03 00:41:28 +01:00
|
|
|
const tokenFiatAmount = useTokenFiatAmount(
|
|
|
|
token?.address,
|
|
|
|
tokenDisplayValue,
|
|
|
|
token?.symbol,
|
2021-02-04 19:15:23 +01:00
|
|
|
);
|
2020-05-26 22:49:11 +02:00
|
|
|
|
2020-11-03 00:41:28 +01:00
|
|
|
const origin = stripHttpSchemes(
|
|
|
|
initialTransaction.origin || initialTransaction.msgParams?.origin || '',
|
2021-02-04 19:15:23 +01:00
|
|
|
);
|
2020-06-17 18:38:15 +02:00
|
|
|
|
2020-10-06 20:28:38 +02:00
|
|
|
// used to append to the primary display value. initialized to either token.symbol or undefined
|
|
|
|
// but can later be modified if dealing with a swap
|
2021-02-04 19:15:23 +01:00
|
|
|
let primarySuffix = isTokenCategory ? token?.symbol : undefined;
|
2020-10-06 20:28:38 +02:00
|
|
|
// used to display the primary value of tx. initialized to either tokenDisplayValue or undefined
|
|
|
|
// but can later be modified if dealing with a swap
|
2021-02-04 19:15:23 +01:00
|
|
|
let primaryDisplayValue = isTokenCategory ? tokenDisplayValue : undefined;
|
2020-10-06 20:28:38 +02:00
|
|
|
// used to display fiat amount of tx. initialized to either tokenFiatAmount or undefined
|
|
|
|
// but can later be modified if dealing with a swap
|
2021-02-04 19:15:23 +01:00
|
|
|
let secondaryDisplayValue = isTokenCategory ? tokenFiatAmount : undefined;
|
2020-10-06 20:28:38 +02:00
|
|
|
// The transaction group category that will be used for rendering the icon in the activity list
|
2021-02-04 19:15:23 +01:00
|
|
|
let category;
|
2020-10-06 20:28:38 +02:00
|
|
|
// The primary title of the Tx that will be displayed in the activity list
|
2021-02-04 19:15:23 +01:00
|
|
|
let title;
|
2020-10-06 20:28:38 +02:00
|
|
|
|
2020-11-03 00:41:28 +01:00
|
|
|
const {
|
|
|
|
swapTokenValue,
|
|
|
|
isNegative,
|
|
|
|
swapTokenFiatAmount,
|
|
|
|
isViewingReceivedTokenFromSwap,
|
2021-02-04 19:15:23 +01:00
|
|
|
} = useSwappedTokenValue(transactionGroup, currentAsset);
|
2020-10-06 20:28:38 +02:00
|
|
|
|
|
|
|
// There are seven types of transaction entries that are currently differentiated in the design
|
|
|
|
// 1. Signature request
|
2020-05-26 22:49:11 +02:00
|
|
|
// 2. Send (sendEth sendTokens)
|
|
|
|
// 3. Deposit
|
|
|
|
// 4. Site interaction
|
|
|
|
// 5. Approval
|
2020-10-06 20:28:38 +02:00
|
|
|
// 6. Swap
|
|
|
|
// 7. Swap Approval
|
|
|
|
|
2021-05-11 19:57:06 +02:00
|
|
|
const signatureTypes = [
|
|
|
|
null,
|
|
|
|
undefined,
|
|
|
|
TRANSACTION_TYPES.SIGN,
|
|
|
|
TRANSACTION_TYPES.PERSONAL_SIGN,
|
|
|
|
TRANSACTION_TYPES.SIGN_TYPED_DATA,
|
|
|
|
TRANSACTION_TYPES.ETH_DECRYPT,
|
|
|
|
TRANSACTION_TYPES.ETH_GET_ENCRYPTION_PUBLIC_KEY,
|
|
|
|
];
|
|
|
|
|
|
|
|
if (signatureTypes.includes(type)) {
|
2021-02-04 19:15:23 +01:00
|
|
|
category = TRANSACTION_GROUP_CATEGORIES.SIGNATURE_REQUEST;
|
|
|
|
title = t('signatureRequest');
|
|
|
|
subtitle = origin;
|
|
|
|
subtitleContainsOrigin = true;
|
2021-03-10 21:16:44 +01:00
|
|
|
} else if (type === TRANSACTION_TYPES.SWAP) {
|
2021-02-04 19:15:23 +01:00
|
|
|
category = TRANSACTION_GROUP_CATEGORIES.SWAP;
|
2020-10-06 20:28:38 +02:00
|
|
|
title = t('swapTokenToToken', [
|
2020-10-10 21:50:20 +02:00
|
|
|
initialTransaction.sourceTokenSymbol,
|
|
|
|
initialTransaction.destinationTokenSymbol,
|
2021-02-04 19:15:23 +01:00
|
|
|
]);
|
|
|
|
subtitle = origin;
|
|
|
|
subtitleContainsOrigin = true;
|
2020-10-06 20:28:38 +02:00
|
|
|
primarySuffix = isViewingReceivedTokenFromSwap
|
|
|
|
? currentAsset.symbol
|
2021-02-04 19:15:23 +01:00
|
|
|
: initialTransaction.sourceTokenSymbol;
|
|
|
|
primaryDisplayValue = swapTokenValue;
|
|
|
|
secondaryDisplayValue = swapTokenFiatAmount;
|
2020-10-28 08:08:02 +01:00
|
|
|
if (isNegative) {
|
2021-02-04 19:15:23 +01:00
|
|
|
prefix = '';
|
2020-10-28 08:08:02 +01:00
|
|
|
} else if (isViewingReceivedTokenFromSwap) {
|
2021-02-04 19:15:23 +01:00
|
|
|
prefix = '+';
|
2020-10-28 08:08:02 +01:00
|
|
|
} else {
|
2021-02-04 19:15:23 +01:00
|
|
|
prefix = '-';
|
2020-10-28 08:08:02 +01:00
|
|
|
}
|
2021-03-10 21:16:44 +01:00
|
|
|
} else if (type === TRANSACTION_TYPES.SWAP_APPROVAL) {
|
2021-02-04 19:15:23 +01:00
|
|
|
category = TRANSACTION_GROUP_CATEGORIES.APPROVAL;
|
|
|
|
title = t('swapApproval', [primaryTransaction.sourceTokenSymbol]);
|
|
|
|
subtitle = origin;
|
|
|
|
subtitleContainsOrigin = true;
|
|
|
|
primarySuffix = primaryTransaction.sourceTokenSymbol;
|
2021-03-10 21:16:44 +01:00
|
|
|
} else if (type === TRANSACTION_TYPES.TOKEN_METHOD_APPROVE) {
|
2021-02-04 19:15:23 +01:00
|
|
|
category = TRANSACTION_GROUP_CATEGORIES.APPROVAL;
|
|
|
|
prefix = '';
|
|
|
|
title = t('approveSpendLimit', [token?.symbol || t('token')]);
|
|
|
|
subtitle = origin;
|
|
|
|
subtitleContainsOrigin = true;
|
2020-11-03 00:41:28 +01:00
|
|
|
} else if (
|
2021-03-10 21:16:44 +01:00
|
|
|
type === TRANSACTION_TYPES.DEPLOY_CONTRACT ||
|
|
|
|
type === TRANSACTION_TYPES.CONTRACT_INTERACTION
|
2020-11-03 00:41:28 +01:00
|
|
|
) {
|
2021-02-04 19:15:23 +01:00
|
|
|
category = TRANSACTION_GROUP_CATEGORIES.INTERACTION;
|
2021-03-10 21:16:44 +01:00
|
|
|
const transactionTypeTitle = getTransactionTypeTitle(t, type);
|
2020-11-03 00:41:28 +01:00
|
|
|
title =
|
|
|
|
(methodData?.name && camelCaseToCapitalize(methodData.name)) ||
|
2021-03-10 21:16:44 +01:00
|
|
|
transactionTypeTitle;
|
2021-02-04 19:15:23 +01:00
|
|
|
subtitle = origin;
|
|
|
|
subtitleContainsOrigin = true;
|
2021-03-10 21:16:44 +01:00
|
|
|
} else if (type === TRANSACTION_TYPES.INCOMING) {
|
2021-02-04 19:15:23 +01:00
|
|
|
category = TRANSACTION_GROUP_CATEGORIES.RECEIVE;
|
|
|
|
title = t('receive');
|
|
|
|
prefix = '';
|
|
|
|
subtitle = t('fromAddress', [shortenAddress(senderAddress)]);
|
2020-11-03 00:41:28 +01:00
|
|
|
} else if (
|
2021-03-10 21:16:44 +01:00
|
|
|
type === TRANSACTION_TYPES.TOKEN_METHOD_TRANSFER_FROM ||
|
|
|
|
type === TRANSACTION_TYPES.TOKEN_METHOD_TRANSFER
|
2020-11-03 00:41:28 +01:00
|
|
|
) {
|
2021-02-04 19:15:23 +01:00
|
|
|
category = TRANSACTION_GROUP_CATEGORIES.SEND;
|
|
|
|
title = t('sendSpecifiedTokens', [token?.symbol || t('token')]);
|
|
|
|
recipientAddress = getTokenAddressParam(tokenData);
|
|
|
|
subtitle = t('toAddress', [shortenAddress(recipientAddress)]);
|
2021-09-15 23:54:51 +02:00
|
|
|
} else if (type === TRANSACTION_TYPES.SIMPLE_SEND) {
|
2021-02-04 19:15:23 +01:00
|
|
|
category = TRANSACTION_GROUP_CATEGORIES.SEND;
|
2021-03-19 18:12:11 +01:00
|
|
|
title = t('send');
|
2021-02-04 19:15:23 +01:00
|
|
|
subtitle = t('toAddress', [shortenAddress(recipientAddress)]);
|
2021-05-14 23:03:14 +02:00
|
|
|
} else {
|
2021-07-23 01:13:40 +02:00
|
|
|
dispatch(
|
|
|
|
captureSingleException(
|
2021-05-21 14:01:45 +02:00
|
|
|
`useTransactionDisplayData does not recognize transaction type. Type received is: ${type}`,
|
|
|
|
),
|
2021-05-14 23:03:14 +02:00
|
|
|
);
|
2020-05-26 22:49:11 +02:00
|
|
|
}
|
|
|
|
|
2021-02-04 19:15:23 +01:00
|
|
|
const primaryCurrencyPreferences = useUserPreferencedCurrency(PRIMARY);
|
|
|
|
const secondaryCurrencyPreferences = useUserPreferencedCurrency(SECONDARY);
|
2020-05-26 22:49:11 +02:00
|
|
|
|
|
|
|
const [primaryCurrency] = useCurrencyDisplay(primaryValue, {
|
|
|
|
prefix,
|
2020-10-06 20:28:38 +02:00
|
|
|
displayValue: primaryDisplayValue,
|
|
|
|
suffix: primarySuffix,
|
2020-05-26 22:49:11 +02:00
|
|
|
...primaryCurrencyPreferences,
|
2021-02-04 19:15:23 +01:00
|
|
|
});
|
2020-05-26 22:49:11 +02:00
|
|
|
|
|
|
|
const [secondaryCurrency] = useCurrencyDisplay(primaryValue, {
|
|
|
|
prefix,
|
2020-10-06 20:28:38 +02:00
|
|
|
displayValue: secondaryDisplayValue,
|
|
|
|
hideLabel: isTokenCategory || Boolean(swapTokenValue),
|
2020-05-26 22:49:11 +02:00
|
|
|
...secondaryCurrencyPreferences,
|
2021-02-04 19:15:23 +01:00
|
|
|
});
|
2020-05-26 22:49:11 +02:00
|
|
|
|
|
|
|
return {
|
|
|
|
title,
|
|
|
|
category,
|
|
|
|
date,
|
|
|
|
subtitle,
|
2020-06-17 18:38:15 +02:00
|
|
|
subtitleContainsOrigin,
|
2020-11-03 00:41:28 +01:00
|
|
|
primaryCurrency:
|
2021-03-10 21:16:44 +01:00
|
|
|
type === TRANSACTION_TYPES.SWAP && isPending ? '' : primaryCurrency,
|
2020-05-26 22:49:11 +02:00
|
|
|
senderAddress,
|
|
|
|
recipientAddress,
|
2020-11-03 00:41:28 +01:00
|
|
|
secondaryCurrency:
|
2020-10-06 20:28:38 +02:00
|
|
|
(isTokenCategory && !tokenFiatAmount) ||
|
2021-03-10 21:16:44 +01:00
|
|
|
(type === TRANSACTION_TYPES.SWAP && !swapTokenFiatAmount)
|
2020-11-03 00:41:28 +01:00
|
|
|
? undefined
|
|
|
|
: secondaryCurrency,
|
2020-10-07 23:59:38 +02:00
|
|
|
displayedStatusKey,
|
2020-10-06 20:28:38 +02:00
|
|
|
isPending,
|
2020-10-19 16:45:21 +02:00
|
|
|
isSubmitted,
|
2021-02-04 19:15:23 +01:00
|
|
|
};
|
2020-05-26 22:49:11 +02:00
|
|
|
}
|