1
0
mirror of https://github.com/kremalicious/metamask-extension.git synced 2024-10-22 19:26:13 +02:00
metamask-extension/ui/selectors/confirm-transaction.js

390 lines
12 KiB
JavaScript
Raw Normal View History

import { createSelector } from 'reselect';
import txHelper from '../helpers/utils/tx-helper';
import {
roundExponential,
getTransactionFee,
addFiat,
addEth,
} from '../helpers/utils/confirm-tx.util';
import { transactionMatchesNetwork } from '../../shared/modules/transaction.utils';
import {
getGasEstimateType,
getGasFeeEstimates,
getNativeCurrency,
} from '../ducks/metamask/metamask';
import { TransactionEnvelopeType } from '../../shared/constants/transaction';
import {
GasEstimateTypes,
CUSTOM_GAS_ESTIMATE,
} from '../../shared/constants/gas';
import {
getMaximumGasTotalInHexWei,
getMinimumGasTotalInHexWei,
} from '../../shared/modules/gas.utils';
import { isEqualCaseInsensitive } from '../../shared/modules/string-utils';
import { calcTokenAmount } from '../../shared/lib/transactions-controller-utils';
import {
decGWEIToHexWEI,
getValueFromWeiHex,
sumHexes,
} from '../../shared/modules/conversion.utils';
import { getAveragePriceEstimateInHexWEI } from './custom-gas';
import { getCurrentChainId, deprecatedGetCurrentNetworkId } from './selectors';
import { checkNetworkAndAccountSupports1559 } from '.';
const unapprovedTxsSelector = (state) => state.metamask.unapprovedTxs;
const unapprovedMsgsSelector = (state) => state.metamask.unapprovedMsgs;
2020-11-03 00:41:28 +01:00
const unapprovedPersonalMsgsSelector = (state) =>
state.metamask.unapprovedPersonalMsgs;
2020-11-03 00:41:28 +01:00
const unapprovedDecryptMsgsSelector = (state) =>
state.metamask.unapprovedDecryptMsgs;
2020-11-03 00:41:28 +01:00
const unapprovedEncryptionPublicKeyMsgsSelector = (state) =>
state.metamask.unapprovedEncryptionPublicKeyMsgs;
2020-11-03 00:41:28 +01:00
const unapprovedTypedMessagesSelector = (state) =>
state.metamask.unapprovedTypedMessages;
export const unconfirmedTransactionsListSelector = createSelector(
unapprovedTxsSelector,
unapprovedMsgsSelector,
unapprovedPersonalMsgsSelector,
unapprovedDecryptMsgsSelector,
unapprovedEncryptionPublicKeyMsgsSelector,
unapprovedTypedMessagesSelector,
deprecatedGetCurrentNetworkId,
getCurrentChainId,
(
unapprovedTxs = {},
unapprovedMsgs = {},
unapprovedPersonalMsgs = {},
unapprovedDecryptMsgs = {},
unapprovedEncryptionPublicKeyMsgs = {},
unapprovedTypedMessages = {},
network,
chainId,
2020-11-03 00:41:28 +01:00
) =>
txHelper(
unapprovedTxs,
unapprovedMsgs,
unapprovedPersonalMsgs,
unapprovedDecryptMsgs,
unapprovedEncryptionPublicKeyMsgs,
unapprovedTypedMessages,
network,
chainId,
2020-11-03 00:41:28 +01:00
) || [],
);
export const unconfirmedTransactionsHashSelector = createSelector(
unapprovedTxsSelector,
unapprovedMsgsSelector,
unapprovedPersonalMsgsSelector,
unapprovedDecryptMsgsSelector,
unapprovedEncryptionPublicKeyMsgsSelector,
unapprovedTypedMessagesSelector,
deprecatedGetCurrentNetworkId,
getCurrentChainId,
(
unapprovedTxs = {},
unapprovedMsgs = {},
unapprovedPersonalMsgs = {},
unapprovedDecryptMsgs = {},
unapprovedEncryptionPublicKeyMsgs = {},
unapprovedTypedMessages = {},
network,
chainId,
) => {
2020-11-03 00:41:28 +01:00
const filteredUnapprovedTxs = Object.keys(unapprovedTxs).reduce(
(acc, address) => {
const transactions = { ...acc };
if (
transactionMatchesNetwork(unapprovedTxs[address], chainId, network)
) {
transactions[address] = unapprovedTxs[address];
2020-11-03 00:41:28 +01:00
}
return transactions;
2020-11-03 00:41:28 +01:00
},
{},
);
return {
...filteredUnapprovedTxs,
...unapprovedMsgs,
...unapprovedPersonalMsgs,
...unapprovedDecryptMsgs,
...unapprovedEncryptionPublicKeyMsgs,
...unapprovedTypedMessages,
};
},
);
export const unconfirmedMessagesHashSelector = createSelector(
unapprovedMsgsSelector,
unapprovedPersonalMsgsSelector,
unapprovedDecryptMsgsSelector,
unapprovedEncryptionPublicKeyMsgsSelector,
unapprovedTypedMessagesSelector,
(
unapprovedMsgs = {},
unapprovedPersonalMsgs = {},
unapprovedDecryptMsgs = {},
unapprovedEncryptionPublicKeyMsgs = {},
unapprovedTypedMessages = {},
) => {
return {
...unapprovedMsgs,
...unapprovedPersonalMsgs,
...unapprovedDecryptMsgs,
...unapprovedEncryptionPublicKeyMsgs,
...unapprovedTypedMessages,
};
},
);
const unapprovedMsgCountSelector = (state) => state.metamask.unapprovedMsgCount;
2020-11-03 00:41:28 +01:00
const unapprovedPersonalMsgCountSelector = (state) =>
state.metamask.unapprovedPersonalMsgCount;
2020-11-03 00:41:28 +01:00
const unapprovedDecryptMsgCountSelector = (state) =>
state.metamask.unapprovedDecryptMsgCount;
2020-11-03 00:41:28 +01:00
const unapprovedEncryptionPublicKeyMsgCountSelector = (state) =>
state.metamask.unapprovedEncryptionPublicKeyMsgCount;
2020-11-03 00:41:28 +01:00
const unapprovedTypedMessagesCountSelector = (state) =>
state.metamask.unapprovedTypedMessagesCount;
export const unconfirmedTransactionsCountSelector = createSelector(
unapprovedTxsSelector,
unapprovedMsgCountSelector,
unapprovedPersonalMsgCountSelector,
unapprovedDecryptMsgCountSelector,
unapprovedEncryptionPublicKeyMsgCountSelector,
unapprovedTypedMessagesCountSelector,
deprecatedGetCurrentNetworkId,
getCurrentChainId,
(
unapprovedTxs = {},
unapprovedMsgCount = 0,
unapprovedPersonalMsgCount = 0,
unapprovedDecryptMsgCount = 0,
unapprovedEncryptionPublicKeyMsgCount = 0,
unapprovedTypedMessagesCount = 0,
network,
chainId,
) => {
const filteredUnapprovedTxIds = Object.keys(unapprovedTxs).filter((txId) =>
transactionMatchesNetwork(unapprovedTxs[txId], chainId, network),
);
2020-11-03 00:41:28 +01:00
return (
filteredUnapprovedTxIds.length +
unapprovedTypedMessagesCount +
unapprovedMsgCount +
unapprovedPersonalMsgCount +
unapprovedDecryptMsgCount +
unapprovedEncryptionPublicKeyMsgCount
);
},
);
export const currentCurrencySelector = (state) =>
state.metamask.currentCurrency;
export const conversionRateSelector = (state) => state.metamask.conversionRate;
export const txDataSelector = (state) => state.confirmTransaction.txData;
const tokenDataSelector = (state) => state.confirmTransaction.tokenData;
const tokenPropsSelector = (state) => state.confirmTransaction.tokenProps;
2020-11-03 00:41:28 +01:00
const contractExchangeRatesSelector = (state) =>
state.metamask.contractExchangeRates;
const tokenDecimalsSelector = createSelector(
tokenPropsSelector,
(tokenProps) => tokenProps && tokenProps.decimals,
);
const tokenDataArgsSelector = createSelector(
tokenDataSelector,
(tokenData) => (tokenData && tokenData.args) || [],
);
const txParamsSelector = createSelector(
txDataSelector,
(txData) => (txData && txData.txParams) || {},
);
export const tokenAddressSelector = createSelector(
txParamsSelector,
(txParams) => txParams && txParams.to,
);
const TOKEN_PARAM_TO = '_to';
const TOKEN_PARAM_VALUE = '_value';
export const sendTokenTokenAmountAndToAddressSelector = createSelector(
tokenDataArgsSelector,
tokenDecimalsSelector,
(args, tokenDecimals) => {
let toAddress = '';
let tokenAmount = '0';
// Token params here are ethers BigNumbers, which have a different
// interface than bignumber.js
if (args && args.length) {
toAddress = args[TOKEN_PARAM_TO];
let value = args[TOKEN_PARAM_VALUE].toString();
if (tokenDecimals) {
// bignumber.js return value
value = calcTokenAmount(value, tokenDecimals).toFixed();
}
2018-07-26 21:15:34 +02:00
tokenAmount = roundExponential(value);
}
return {
toAddress,
tokenAmount,
};
},
);
export const contractExchangeRateSelector = createSelector(
contractExchangeRatesSelector,
tokenAddressSelector,
(contractExchangeRates, tokenAddress) =>
contractExchangeRates[
Object.keys(contractExchangeRates).find((address) =>
isEqualCaseInsensitive(address, tokenAddress),
)
],
);
export const transactionFeeSelector = function (state, txData) {
const currentCurrency = currentCurrencySelector(state);
const conversionRate = conversionRateSelector(state);
const nativeCurrency = getNativeCurrency(state);
const gasFeeEstimates = getGasFeeEstimates(state) || {};
const gasEstimateType = getGasEstimateType(state);
2022-07-31 20:26:40 +02:00
const networkAndAccountSupportsEIP1559 =
checkNetworkAndAccountSupports1559(state);
const gasEstimationObject = {
gasLimit: txData.txParams?.gas ?? '0x0',
};
if (networkAndAccountSupportsEIP1559) {
const { gasPrice = '0' } = gasFeeEstimates;
const selectedGasEstimates = gasFeeEstimates[txData.userFeeLevel] || {};
if (txData.txParams?.type === TransactionEnvelopeType.legacy) {
gasEstimationObject.gasPrice =
txData.txParams?.gasPrice ?? decGWEIToHexWEI(gasPrice);
} else {
2022-07-31 20:26:40 +02:00
const { suggestedMaxPriorityFeePerGas, suggestedMaxFeePerGas } =
selectedGasEstimates;
gasEstimationObject.maxFeePerGas =
txData.txParams?.maxFeePerGas &&
(txData.userFeeLevel === CUSTOM_GAS_ESTIMATE || !suggestedMaxFeePerGas)
? txData.txParams?.maxFeePerGas
: decGWEIToHexWEI(suggestedMaxFeePerGas || gasPrice);
gasEstimationObject.maxPriorityFeePerGas =
txData.txParams?.maxPriorityFeePerGas &&
(txData.userFeeLevel === CUSTOM_GAS_ESTIMATE ||
!suggestedMaxPriorityFeePerGas)
? txData.txParams?.maxPriorityFeePerGas
: (suggestedMaxPriorityFeePerGas &&
decGWEIToHexWEI(suggestedMaxPriorityFeePerGas)) ||
gasEstimationObject.maxFeePerGas;
gasEstimationObject.baseFeePerGas = decGWEIToHexWEI(
gasFeeEstimates.estimatedBaseFee,
);
}
} else {
switch (gasEstimateType) {
case GasEstimateTypes.none:
gasEstimationObject.gasPrice = txData.txParams?.gasPrice ?? '0x0';
break;
case GasEstimateTypes.ethGasPrice:
gasEstimationObject.gasPrice =
txData.txParams?.gasPrice ??
decGWEIToHexWEI(gasFeeEstimates.gasPrice);
break;
case GasEstimateTypes.legacy:
gasEstimationObject.gasPrice =
txData.txParams?.gasPrice ?? getAveragePriceEstimateInHexWEI(state);
break;
case GasEstimateTypes.feeMarket:
break;
default:
break;
}
}
const { txParams: { value = '0x0' } = {} } = txData;
const fiatTransactionAmount = getValueFromWeiHex({
2020-11-03 00:41:28 +01:00
value,
fromCurrency: nativeCurrency,
toCurrency: currentCurrency,
conversionRate,
numberOfDecimals: 2,
});
const ethTransactionAmount = getValueFromWeiHex({
2020-11-03 00:41:28 +01:00
value,
fromCurrency: nativeCurrency,
toCurrency: nativeCurrency,
conversionRate,
numberOfDecimals: 6,
});
2022-07-31 20:26:40 +02:00
const hexMinimumTransactionFee =
getMinimumGasTotalInHexWei(gasEstimationObject);
const hexMaximumTransactionFee =
getMaximumGasTotalInHexWei(gasEstimationObject);
const fiatMinimumTransactionFee = getTransactionFee({
value: hexMinimumTransactionFee,
fromCurrency: nativeCurrency,
toCurrency: currentCurrency,
numberOfDecimals: 2,
conversionRate,
});
const fiatMaximumTransactionFee = getTransactionFee({
value: hexMaximumTransactionFee,
fromCurrency: nativeCurrency,
toCurrency: currentCurrency,
numberOfDecimals: 2,
conversionRate,
});
const ethTransactionFee = getTransactionFee({
value: hexMinimumTransactionFee,
fromCurrency: nativeCurrency,
toCurrency: nativeCurrency,
numberOfDecimals: 6,
conversionRate,
});
2020-11-03 00:41:28 +01:00
const fiatTransactionTotal = addFiat(
fiatMinimumTransactionFee,
2020-11-03 00:41:28 +01:00
fiatTransactionAmount,
);
const ethTransactionTotal = addEth(ethTransactionFee, ethTransactionAmount);
const hexTransactionTotal = sumHexes(value, hexMinimumTransactionFee);
return {
hexTransactionAmount: value,
fiatTransactionAmount,
ethTransactionAmount,
hexMinimumTransactionFee,
fiatMinimumTransactionFee,
hexMaximumTransactionFee,
fiatMaximumTransactionFee,
ethTransactionFee,
fiatTransactionTotal,
ethTransactionTotal,
hexTransactionTotal,
gasEstimationObject,
};
};