1
0
mirror of https://github.com/kremalicious/metamask-extension.git synced 2024-11-25 11:28:51 +01:00
metamask-extension/ui/hooks/useTransactionFunctionType.js

47 lines
1.4 KiB
JavaScript
Raw Normal View History

import { useSelector } from 'react-redux';
import { ORIGIN_METAMASK } from '../../shared/constants/app';
import { TransactionType } from '../../shared/constants/transaction';
import { getKnownMethodData } from '../selectors';
import { getNativeCurrency } from '../ducks/metamask/metamask';
import { getTransactionTypeTitle } from '../helpers/utils/transactions.util';
import { getMethodName } from '../helpers/utils/metrics';
import { useI18nContext } from './useI18nContext';
export const useTransactionFunctionType = (txData = {}) => {
const t = useI18nContext();
const nativeCurrency = useSelector(getNativeCurrency);
const { txParams } = txData;
const methodData = useSelector(
(state) => getKnownMethodData(state, txParams?.data) || {},
);
if (!txParams) {
return {};
}
const isTokenApproval =
txData.type === TransactionType.tokenMethodSetApprovalForAll ||
txData.type === TransactionType.tokenMethodApprove;
const isContractInteraction =
txData.type === TransactionType.contractInteraction;
const isTransactionFromDapp =
(isTokenApproval || isContractInteraction) &&
txData.origin !== ORIGIN_METAMASK;
let functionType = isTransactionFromDapp
? getMethodName(methodData?.name)
: undefined;
if (!functionType) {
functionType = txData.type
? getTransactionTypeTitle(t, txData.type, nativeCurrency)
: t('contractInteraction');
}
return { functionType };
};