2021-02-04 19:15:23 +01:00
|
|
|
import { createSelector } from 'reselect';
|
|
|
|
import txHelper from '../../lib/tx-helper';
|
|
|
|
import { calcTokenAmount } from '../helpers/utils/token-util';
|
2019-07-03 22:33:44 +02:00
|
|
|
import {
|
|
|
|
roundExponential,
|
|
|
|
getValueFromWeiHex,
|
|
|
|
getHexGasTotal,
|
|
|
|
getTransactionFee,
|
|
|
|
addFiat,
|
|
|
|
addEth,
|
2021-02-04 19:15:23 +01:00
|
|
|
} from '../helpers/utils/confirm-tx.util';
|
|
|
|
import { sumHexes } from '../helpers/utils/transactions.util';
|
2021-03-01 16:15:42 +01:00
|
|
|
import { transactionMatchesNetwork } from '../../../shared/modules/transaction.utils';
|
2021-03-12 23:23:26 +01:00
|
|
|
import { getCurrentChainId, deprecatedGetCurrentNetworkId } from './selectors';
|
2021-02-04 19:15:23 +01:00
|
|
|
import { getNativeCurrency } from '.';
|
2018-06-23 08:52:45 +02:00
|
|
|
|
2021-02-04 19:15:23 +01:00
|
|
|
const unapprovedTxsSelector = (state) => state.metamask.unapprovedTxs;
|
|
|
|
const unapprovedMsgsSelector = (state) => state.metamask.unapprovedMsgs;
|
2020-11-03 00:41:28 +01:00
|
|
|
const unapprovedPersonalMsgsSelector = (state) =>
|
2021-02-04 19:15:23 +01:00
|
|
|
state.metamask.unapprovedPersonalMsgs;
|
2020-11-03 00:41:28 +01:00
|
|
|
const unapprovedDecryptMsgsSelector = (state) =>
|
2021-02-04 19:15:23 +01:00
|
|
|
state.metamask.unapprovedDecryptMsgs;
|
2020-11-03 00:41:28 +01:00
|
|
|
const unapprovedEncryptionPublicKeyMsgsSelector = (state) =>
|
2021-02-04 19:15:23 +01:00
|
|
|
state.metamask.unapprovedEncryptionPublicKeyMsgs;
|
2020-11-03 00:41:28 +01:00
|
|
|
const unapprovedTypedMessagesSelector = (state) =>
|
2021-02-04 19:15:23 +01:00
|
|
|
state.metamask.unapprovedTypedMessages;
|
2018-06-23 08:52:45 +02:00
|
|
|
|
|
|
|
export const unconfirmedTransactionsListSelector = createSelector(
|
|
|
|
unapprovedTxsSelector,
|
|
|
|
unapprovedMsgsSelector,
|
|
|
|
unapprovedPersonalMsgsSelector,
|
2020-02-19 19:24:16 +01:00
|
|
|
unapprovedDecryptMsgsSelector,
|
|
|
|
unapprovedEncryptionPublicKeyMsgsSelector,
|
2018-06-23 08:52:45 +02:00
|
|
|
unapprovedTypedMessagesSelector,
|
2021-03-12 23:23:26 +01:00
|
|
|
deprecatedGetCurrentNetworkId,
|
2021-03-01 16:15:42 +01:00
|
|
|
getCurrentChainId,
|
2018-06-23 08:52:45 +02:00
|
|
|
(
|
|
|
|
unapprovedTxs = {},
|
|
|
|
unapprovedMsgs = {},
|
|
|
|
unapprovedPersonalMsgs = {},
|
2020-02-19 19:24:16 +01:00
|
|
|
unapprovedDecryptMsgs = {},
|
|
|
|
unapprovedEncryptionPublicKeyMsgs = {},
|
2018-06-23 08:52:45 +02:00
|
|
|
unapprovedTypedMessages = {},
|
2020-07-14 17:20:41 +02:00
|
|
|
network,
|
2021-03-01 16:15:42 +01:00
|
|
|
chainId,
|
2020-11-03 00:41:28 +01:00
|
|
|
) =>
|
|
|
|
txHelper(
|
|
|
|
unapprovedTxs,
|
|
|
|
unapprovedMsgs,
|
|
|
|
unapprovedPersonalMsgs,
|
|
|
|
unapprovedDecryptMsgs,
|
|
|
|
unapprovedEncryptionPublicKeyMsgs,
|
|
|
|
unapprovedTypedMessages,
|
|
|
|
network,
|
2021-03-01 16:15:42 +01:00
|
|
|
chainId,
|
2020-11-03 00:41:28 +01:00
|
|
|
) || [],
|
2021-02-04 19:15:23 +01:00
|
|
|
);
|
2018-06-23 08:52:45 +02:00
|
|
|
|
|
|
|
export const unconfirmedTransactionsHashSelector = createSelector(
|
|
|
|
unapprovedTxsSelector,
|
|
|
|
unapprovedMsgsSelector,
|
|
|
|
unapprovedPersonalMsgsSelector,
|
2020-02-19 19:24:16 +01:00
|
|
|
unapprovedDecryptMsgsSelector,
|
|
|
|
unapprovedEncryptionPublicKeyMsgsSelector,
|
2018-06-23 08:52:45 +02:00
|
|
|
unapprovedTypedMessagesSelector,
|
2021-03-12 23:23:26 +01:00
|
|
|
deprecatedGetCurrentNetworkId,
|
2021-03-01 16:15:42 +01:00
|
|
|
getCurrentChainId,
|
2018-06-23 08:52:45 +02:00
|
|
|
(
|
|
|
|
unapprovedTxs = {},
|
|
|
|
unapprovedMsgs = {},
|
|
|
|
unapprovedPersonalMsgs = {},
|
2020-02-19 19:24:16 +01:00
|
|
|
unapprovedDecryptMsgs = {},
|
|
|
|
unapprovedEncryptionPublicKeyMsgs = {},
|
2018-06-23 08:52:45 +02:00
|
|
|
unapprovedTypedMessages = {},
|
2020-07-14 17:20:41 +02:00
|
|
|
network,
|
2021-03-01 16:15:42 +01:00
|
|
|
chainId,
|
2018-06-23 08:52:45 +02:00
|
|
|
) => {
|
2020-11-03 00:41:28 +01:00
|
|
|
const filteredUnapprovedTxs = Object.keys(unapprovedTxs).reduce(
|
|
|
|
(acc, address) => {
|
2021-02-04 19:15:23 +01:00
|
|
|
const transactions = { ...acc };
|
2018-06-23 08:52:45 +02:00
|
|
|
|
2021-03-01 16:15:42 +01:00
|
|
|
if (
|
|
|
|
transactionMatchesNetwork(unapprovedTxs[address], chainId, network)
|
|
|
|
) {
|
2021-02-04 19:15:23 +01:00
|
|
|
transactions[address] = unapprovedTxs[address];
|
2020-11-03 00:41:28 +01:00
|
|
|
}
|
2018-06-23 08:52:45 +02:00
|
|
|
|
2021-02-04 19:15:23 +01:00
|
|
|
return transactions;
|
2020-11-03 00:41:28 +01:00
|
|
|
},
|
|
|
|
{},
|
2021-02-04 19:15:23 +01:00
|
|
|
);
|
2018-06-23 08:52:45 +02:00
|
|
|
|
|
|
|
return {
|
|
|
|
...filteredUnapprovedTxs,
|
|
|
|
...unapprovedMsgs,
|
|
|
|
...unapprovedPersonalMsgs,
|
2020-02-19 19:24:16 +01:00
|
|
|
...unapprovedDecryptMsgs,
|
|
|
|
...unapprovedEncryptionPublicKeyMsgs,
|
2018-06-23 08:52:45 +02:00
|
|
|
...unapprovedTypedMessages,
|
2021-02-04 19:15:23 +01:00
|
|
|
};
|
2020-07-14 17:20:41 +02:00
|
|
|
},
|
2021-02-04 19:15:23 +01:00
|
|
|
);
|
2018-06-23 08:52:45 +02:00
|
|
|
|
2021-02-04 19:15:23 +01:00
|
|
|
const unapprovedMsgCountSelector = (state) => state.metamask.unapprovedMsgCount;
|
2020-11-03 00:41:28 +01:00
|
|
|
const unapprovedPersonalMsgCountSelector = (state) =>
|
2021-02-04 19:15:23 +01:00
|
|
|
state.metamask.unapprovedPersonalMsgCount;
|
2020-11-03 00:41:28 +01:00
|
|
|
const unapprovedDecryptMsgCountSelector = (state) =>
|
2021-02-04 19:15:23 +01:00
|
|
|
state.metamask.unapprovedDecryptMsgCount;
|
2020-11-03 00:41:28 +01:00
|
|
|
const unapprovedEncryptionPublicKeyMsgCountSelector = (state) =>
|
2021-02-04 19:15:23 +01:00
|
|
|
state.metamask.unapprovedEncryptionPublicKeyMsgCount;
|
2020-11-03 00:41:28 +01:00
|
|
|
const unapprovedTypedMessagesCountSelector = (state) =>
|
2021-02-04 19:15:23 +01:00
|
|
|
state.metamask.unapprovedTypedMessagesCount;
|
2018-07-24 05:25:04 +02:00
|
|
|
|
|
|
|
export const unconfirmedTransactionsCountSelector = createSelector(
|
|
|
|
unapprovedTxsSelector,
|
|
|
|
unapprovedMsgCountSelector,
|
|
|
|
unapprovedPersonalMsgCountSelector,
|
2020-02-19 19:24:16 +01:00
|
|
|
unapprovedDecryptMsgCountSelector,
|
|
|
|
unapprovedEncryptionPublicKeyMsgCountSelector,
|
2018-07-24 05:25:04 +02:00
|
|
|
unapprovedTypedMessagesCountSelector,
|
2021-03-12 23:23:26 +01:00
|
|
|
deprecatedGetCurrentNetworkId,
|
2021-03-01 16:15:42 +01:00
|
|
|
getCurrentChainId,
|
2018-07-24 05:25:04 +02:00
|
|
|
(
|
|
|
|
unapprovedTxs = {},
|
|
|
|
unapprovedMsgCount = 0,
|
|
|
|
unapprovedPersonalMsgCount = 0,
|
2020-02-19 19:24:16 +01:00
|
|
|
unapprovedDecryptMsgCount = 0,
|
|
|
|
unapprovedEncryptionPublicKeyMsgCount = 0,
|
2018-07-24 05:25:04 +02:00
|
|
|
unapprovedTypedMessagesCount = 0,
|
2020-07-14 17:20:41 +02:00
|
|
|
network,
|
2021-03-01 16:15:42 +01:00
|
|
|
chainId,
|
2018-07-24 05:25:04 +02:00
|
|
|
) => {
|
2021-03-01 16:15:42 +01:00
|
|
|
const filteredUnapprovedTxIds = Object.keys(unapprovedTxs).filter((txId) =>
|
|
|
|
transactionMatchesNetwork(unapprovedTxs[txId], chainId, network),
|
2021-02-04 19:15:23 +01:00
|
|
|
);
|
2018-07-24 05:25:04 +02:00
|
|
|
|
2020-11-03 00:41:28 +01:00
|
|
|
return (
|
|
|
|
filteredUnapprovedTxIds.length +
|
|
|
|
unapprovedTypedMessagesCount +
|
|
|
|
unapprovedMsgCount +
|
|
|
|
unapprovedPersonalMsgCount +
|
|
|
|
unapprovedDecryptMsgCount +
|
|
|
|
unapprovedEncryptionPublicKeyMsgCount
|
2021-02-04 19:15:23 +01:00
|
|
|
);
|
2020-07-14 17:20:41 +02:00
|
|
|
},
|
2021-02-04 19:15:23 +01:00
|
|
|
);
|
2018-07-24 05:25:04 +02:00
|
|
|
|
2021-02-04 19:15:23 +01:00
|
|
|
export const currentCurrencySelector = (state) =>
|
|
|
|
state.metamask.currentCurrency;
|
|
|
|
export const conversionRateSelector = (state) => state.metamask.conversionRate;
|
2018-07-14 22:47:07 +02:00
|
|
|
|
2021-02-04 19:15:23 +01:00
|
|
|
export const txDataSelector = (state) => state.confirmTransaction.txData;
|
|
|
|
const tokenDataSelector = (state) => state.confirmTransaction.tokenData;
|
|
|
|
const tokenPropsSelector = (state) => state.confirmTransaction.tokenProps;
|
2018-07-14 22:47:07 +02:00
|
|
|
|
2020-11-03 00:41:28 +01:00
|
|
|
const contractExchangeRatesSelector = (state) =>
|
2021-02-04 19:15:23 +01:00
|
|
|
state.metamask.contractExchangeRates;
|
2018-07-14 22:47:07 +02:00
|
|
|
|
|
|
|
const tokenDecimalsSelector = createSelector(
|
|
|
|
tokenPropsSelector,
|
2020-07-14 17:20:41 +02:00
|
|
|
(tokenProps) => tokenProps && tokenProps.tokenDecimals,
|
2021-02-04 19:15:23 +01:00
|
|
|
);
|
2018-07-14 22:47:07 +02:00
|
|
|
|
2020-08-22 04:29:19 +02:00
|
|
|
const tokenDataArgsSelector = createSelector(
|
2018-07-14 22:47:07 +02:00
|
|
|
tokenDataSelector,
|
2020-08-22 04:29:19 +02:00
|
|
|
(tokenData) => (tokenData && tokenData.args) || [],
|
2021-02-04 19:15:23 +01:00
|
|
|
);
|
2018-07-14 22:47:07 +02:00
|
|
|
|
|
|
|
const txParamsSelector = createSelector(
|
|
|
|
txDataSelector,
|
2020-07-14 17:20:41 +02:00
|
|
|
(txData) => (txData && txData.txParams) || {},
|
2021-02-04 19:15:23 +01:00
|
|
|
);
|
2018-07-14 22:47:07 +02:00
|
|
|
|
|
|
|
export const tokenAddressSelector = createSelector(
|
|
|
|
txParamsSelector,
|
2020-07-14 17:20:41 +02:00
|
|
|
(txParams) => txParams && txParams.to,
|
2021-02-04 19:15:23 +01:00
|
|
|
);
|
2018-07-14 22:47:07 +02:00
|
|
|
|
2021-02-04 19:15:23 +01:00
|
|
|
const TOKEN_PARAM_TO = '_to';
|
|
|
|
const TOKEN_PARAM_VALUE = '_value';
|
2018-07-14 22:47:07 +02:00
|
|
|
|
|
|
|
export const sendTokenTokenAmountAndToAddressSelector = createSelector(
|
2020-08-22 04:29:19 +02:00
|
|
|
tokenDataArgsSelector,
|
2018-07-14 22:47:07 +02:00
|
|
|
tokenDecimalsSelector,
|
2020-08-22 04:29:19 +02:00
|
|
|
(args, tokenDecimals) => {
|
2021-02-04 19:15:23 +01:00
|
|
|
let toAddress = '';
|
|
|
|
let tokenAmount = '0';
|
2018-07-14 22:47:07 +02:00
|
|
|
|
2020-09-10 07:24:11 +02:00
|
|
|
// Token params here are ethers BigNumbers, which have a different
|
|
|
|
// interface than bignumber.js
|
2020-08-22 04:29:19 +02:00
|
|
|
if (args && args.length) {
|
2021-02-04 19:15:23 +01:00
|
|
|
toAddress = args[TOKEN_PARAM_TO];
|
|
|
|
let value = args[TOKEN_PARAM_VALUE].toString();
|
2018-07-14 22:47:07 +02:00
|
|
|
|
|
|
|
if (tokenDecimals) {
|
2020-09-10 07:24:11 +02:00
|
|
|
// bignumber.js return value
|
2021-02-04 19:15:23 +01:00
|
|
|
value = calcTokenAmount(value, tokenDecimals).toFixed();
|
2018-07-14 22:47:07 +02:00
|
|
|
}
|
2018-07-26 21:15:34 +02:00
|
|
|
|
2021-02-04 19:15:23 +01:00
|
|
|
tokenAmount = roundExponential(value);
|
2018-07-14 22:47:07 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return {
|
|
|
|
toAddress,
|
|
|
|
tokenAmount,
|
2021-02-04 19:15:23 +01:00
|
|
|
};
|
2020-07-14 17:20:41 +02:00
|
|
|
},
|
2021-02-04 19:15:23 +01:00
|
|
|
);
|
2018-07-14 22:47:07 +02:00
|
|
|
|
|
|
|
export const contractExchangeRateSelector = createSelector(
|
|
|
|
contractExchangeRatesSelector,
|
|
|
|
tokenAddressSelector,
|
2020-07-14 17:20:41 +02:00
|
|
|
(contractExchangeRates, tokenAddress) => contractExchangeRates[tokenAddress],
|
2021-02-04 19:15:23 +01:00
|
|
|
);
|
2019-07-03 22:33:44 +02:00
|
|
|
|
|
|
|
export const transactionFeeSelector = function (state, txData) {
|
2021-02-04 19:15:23 +01:00
|
|
|
const currentCurrency = currentCurrencySelector(state);
|
|
|
|
const conversionRate = conversionRateSelector(state);
|
|
|
|
const nativeCurrency = getNativeCurrency(state);
|
2019-07-03 22:33:44 +02:00
|
|
|
|
2020-11-03 00:41:28 +01:00
|
|
|
const {
|
|
|
|
txParams: { value = '0x0', gas: gasLimit = '0x0', gasPrice = '0x0' } = {},
|
2021-02-04 19:15:23 +01:00
|
|
|
} = txData;
|
2019-07-03 22:33:44 +02:00
|
|
|
|
|
|
|
const fiatTransactionAmount = getValueFromWeiHex({
|
2020-11-03 00:41:28 +01:00
|
|
|
value,
|
|
|
|
fromCurrency: nativeCurrency,
|
|
|
|
toCurrency: currentCurrency,
|
|
|
|
conversionRate,
|
|
|
|
numberOfDecimals: 2,
|
2021-02-04 19:15:23 +01:00
|
|
|
});
|
2019-07-03 22:33:44 +02:00
|
|
|
const ethTransactionAmount = getValueFromWeiHex({
|
2020-11-03 00:41:28 +01:00
|
|
|
value,
|
|
|
|
fromCurrency: nativeCurrency,
|
|
|
|
toCurrency: nativeCurrency,
|
|
|
|
conversionRate,
|
|
|
|
numberOfDecimals: 6,
|
2021-02-04 19:15:23 +01:00
|
|
|
});
|
2019-07-03 22:33:44 +02:00
|
|
|
|
2021-02-04 19:15:23 +01:00
|
|
|
const hexTransactionFee = getHexGasTotal({ gasLimit, gasPrice });
|
2019-07-03 22:33:44 +02:00
|
|
|
|
|
|
|
const fiatTransactionFee = getTransactionFee({
|
|
|
|
value: hexTransactionFee,
|
|
|
|
fromCurrency: nativeCurrency,
|
|
|
|
toCurrency: currentCurrency,
|
|
|
|
numberOfDecimals: 2,
|
|
|
|
conversionRate,
|
2021-02-04 19:15:23 +01:00
|
|
|
});
|
2019-07-03 22:33:44 +02:00
|
|
|
const ethTransactionFee = getTransactionFee({
|
|
|
|
value: hexTransactionFee,
|
|
|
|
fromCurrency: nativeCurrency,
|
|
|
|
toCurrency: nativeCurrency,
|
|
|
|
numberOfDecimals: 6,
|
|
|
|
conversionRate,
|
2021-02-04 19:15:23 +01:00
|
|
|
});
|
2019-07-03 22:33:44 +02:00
|
|
|
|
2020-11-03 00:41:28 +01:00
|
|
|
const fiatTransactionTotal = addFiat(
|
|
|
|
fiatTransactionFee,
|
|
|
|
fiatTransactionAmount,
|
2021-02-04 19:15:23 +01:00
|
|
|
);
|
|
|
|
const ethTransactionTotal = addEth(ethTransactionFee, ethTransactionAmount);
|
|
|
|
const hexTransactionTotal = sumHexes(value, hexTransactionFee);
|
2019-07-03 22:33:44 +02:00
|
|
|
|
|
|
|
return {
|
|
|
|
hexTransactionAmount: value,
|
|
|
|
fiatTransactionAmount,
|
|
|
|
ethTransactionAmount,
|
|
|
|
hexTransactionFee,
|
|
|
|
fiatTransactionFee,
|
|
|
|
ethTransactionFee,
|
|
|
|
fiatTransactionTotal,
|
|
|
|
ethTransactionTotal,
|
|
|
|
hexTransactionTotal,
|
2021-02-04 19:15:23 +01:00
|
|
|
};
|
|
|
|
};
|