2018-06-23 08:52:45 +02:00
|
|
|
import {
|
|
|
|
conversionRateSelector,
|
|
|
|
currentCurrencySelector,
|
|
|
|
unconfirmedTransactionsHashSelector,
|
2021-02-04 19:15:23 +01:00
|
|
|
} from '../../selectors';
|
2021-06-24 01:50:24 +02:00
|
|
|
import { getNativeCurrency, getTokens } from '../metamask/metamask';
|
2018-06-23 08:52:45 +02:00
|
|
|
|
|
|
|
import {
|
|
|
|
getTransactionFee,
|
|
|
|
getHexGasTotal,
|
|
|
|
addFiat,
|
|
|
|
addEth,
|
2021-02-04 19:15:23 +01:00
|
|
|
} from '../../helpers/utils/confirm-tx.util';
|
2018-06-23 08:52:45 +02:00
|
|
|
|
2023-01-20 18:04:37 +01:00
|
|
|
import {
|
|
|
|
getValueFromWeiHex,
|
2023-01-24 15:44:49 +01:00
|
|
|
hexToDecimal,
|
2023-01-20 18:04:37 +01:00
|
|
|
sumHexes,
|
|
|
|
} from '../../../shared/modules/conversion.utils';
|
2021-06-15 16:17:42 +02:00
|
|
|
import { getAveragePriceEstimateInHexWEI } from '../../selectors/custom-gas';
|
2022-03-07 19:54:36 +01:00
|
|
|
import { isEqualCaseInsensitive } from '../../../shared/modules/string-utils';
|
2022-03-17 19:35:40 +01:00
|
|
|
import { parseStandardTokenTransactionData } from '../../../shared/modules/transaction.utils';
|
2018-06-23 08:52:45 +02:00
|
|
|
|
|
|
|
// Actions
|
2021-02-04 19:15:23 +01:00
|
|
|
const createActionType = (action) => `metamask/confirm-transaction/${action}`;
|
|
|
|
|
|
|
|
const UPDATE_TX_DATA = createActionType('UPDATE_TX_DATA');
|
|
|
|
const UPDATE_TOKEN_DATA = createActionType('UPDATE_TOKEN_DATA');
|
2021-06-24 01:50:24 +02:00
|
|
|
const UPDATE_TOKEN_PROPS = createActionType('UPDATE_TOKEN_PROPS');
|
2021-02-04 19:15:23 +01:00
|
|
|
const CLEAR_CONFIRM_TRANSACTION = createActionType('CLEAR_CONFIRM_TRANSACTION');
|
2020-11-03 00:41:28 +01:00
|
|
|
const UPDATE_TRANSACTION_AMOUNTS = createActionType(
|
|
|
|
'UPDATE_TRANSACTION_AMOUNTS',
|
2021-02-04 19:15:23 +01:00
|
|
|
);
|
|
|
|
const UPDATE_TRANSACTION_FEES = createActionType('UPDATE_TRANSACTION_FEES');
|
|
|
|
const UPDATE_TRANSACTION_TOTALS = createActionType('UPDATE_TRANSACTION_TOTALS');
|
|
|
|
const UPDATE_NONCE = createActionType('UPDATE_NONCE');
|
2018-06-23 08:52:45 +02:00
|
|
|
|
|
|
|
// Initial state
|
|
|
|
const initState = {
|
|
|
|
txData: {},
|
|
|
|
tokenData: {},
|
2021-06-24 01:50:24 +02:00
|
|
|
tokenProps: {},
|
2018-06-23 08:52:45 +02:00
|
|
|
fiatTransactionAmount: '',
|
|
|
|
fiatTransactionFee: '',
|
|
|
|
fiatTransactionTotal: '',
|
|
|
|
ethTransactionAmount: '',
|
|
|
|
ethTransactionFee: '',
|
|
|
|
ethTransactionTotal: '',
|
2018-10-17 01:03:29 +02:00
|
|
|
hexTransactionAmount: '',
|
|
|
|
hexTransactionFee: '',
|
|
|
|
hexTransactionTotal: '',
|
2018-06-23 08:52:45 +02:00
|
|
|
nonce: '',
|
2021-02-04 19:15:23 +01:00
|
|
|
};
|
2018-06-23 08:52:45 +02:00
|
|
|
|
|
|
|
// Reducer
|
2020-11-03 00:41:28 +01:00
|
|
|
export default function reducer(state = initState, action = {}) {
|
2018-06-23 08:52:45 +02:00
|
|
|
switch (action.type) {
|
|
|
|
case UPDATE_TX_DATA:
|
|
|
|
return {
|
2020-02-06 17:38:14 +01:00
|
|
|
...state,
|
2018-06-23 08:52:45 +02:00
|
|
|
txData: {
|
|
|
|
...action.payload,
|
|
|
|
},
|
2021-02-04 19:15:23 +01:00
|
|
|
};
|
2018-06-23 08:52:45 +02:00
|
|
|
case UPDATE_TOKEN_DATA:
|
|
|
|
return {
|
2020-02-06 17:38:14 +01:00
|
|
|
...state,
|
2018-06-23 08:52:45 +02:00
|
|
|
tokenData: {
|
|
|
|
...action.payload,
|
|
|
|
},
|
2021-02-04 19:15:23 +01:00
|
|
|
};
|
2021-06-24 01:50:24 +02:00
|
|
|
case UPDATE_TOKEN_PROPS:
|
|
|
|
return {
|
|
|
|
...state,
|
|
|
|
tokenProps: {
|
|
|
|
...action.payload,
|
|
|
|
},
|
|
|
|
};
|
2020-08-12 18:41:27 +02:00
|
|
|
case UPDATE_TRANSACTION_AMOUNTS: {
|
2020-11-03 00:41:28 +01:00
|
|
|
const {
|
|
|
|
fiatTransactionAmount,
|
|
|
|
ethTransactionAmount,
|
|
|
|
hexTransactionAmount,
|
2021-02-04 19:15:23 +01:00
|
|
|
} = action.payload;
|
2018-06-23 08:52:45 +02:00
|
|
|
return {
|
2020-02-06 17:38:14 +01:00
|
|
|
...state,
|
2020-11-03 00:41:28 +01:00
|
|
|
fiatTransactionAmount:
|
|
|
|
fiatTransactionAmount || state.fiatTransactionAmount,
|
|
|
|
ethTransactionAmount:
|
|
|
|
ethTransactionAmount || state.ethTransactionAmount,
|
|
|
|
hexTransactionAmount:
|
|
|
|
hexTransactionAmount || state.hexTransactionAmount,
|
2021-02-04 19:15:23 +01:00
|
|
|
};
|
2020-08-12 18:41:27 +02:00
|
|
|
}
|
|
|
|
case UPDATE_TRANSACTION_FEES: {
|
2022-07-31 20:26:40 +02:00
|
|
|
const { fiatTransactionFee, ethTransactionFee, hexTransactionFee } =
|
|
|
|
action.payload;
|
2018-06-23 08:52:45 +02:00
|
|
|
return {
|
2020-02-06 17:38:14 +01:00
|
|
|
...state,
|
|
|
|
fiatTransactionFee: fiatTransactionFee || state.fiatTransactionFee,
|
|
|
|
ethTransactionFee: ethTransactionFee || state.ethTransactionFee,
|
|
|
|
hexTransactionFee: hexTransactionFee || state.hexTransactionFee,
|
2021-02-04 19:15:23 +01:00
|
|
|
};
|
2020-08-12 18:41:27 +02:00
|
|
|
}
|
|
|
|
case UPDATE_TRANSACTION_TOTALS: {
|
2022-07-31 20:26:40 +02:00
|
|
|
const { fiatTransactionTotal, ethTransactionTotal, hexTransactionTotal } =
|
|
|
|
action.payload;
|
2018-06-23 08:52:45 +02:00
|
|
|
return {
|
2020-02-06 17:38:14 +01:00
|
|
|
...state,
|
2020-11-03 00:41:28 +01:00
|
|
|
fiatTransactionTotal:
|
|
|
|
fiatTransactionTotal || state.fiatTransactionTotal,
|
2020-02-06 17:38:14 +01:00
|
|
|
ethTransactionTotal: ethTransactionTotal || state.ethTransactionTotal,
|
|
|
|
hexTransactionTotal: hexTransactionTotal || state.hexTransactionTotal,
|
2021-02-04 19:15:23 +01:00
|
|
|
};
|
2020-08-12 18:41:27 +02:00
|
|
|
}
|
2018-06-23 08:52:45 +02:00
|
|
|
case UPDATE_NONCE:
|
|
|
|
return {
|
2020-02-06 17:38:14 +01:00
|
|
|
...state,
|
2018-06-23 08:52:45 +02:00
|
|
|
nonce: action.payload,
|
2021-02-04 19:15:23 +01:00
|
|
|
};
|
2018-06-23 08:52:45 +02:00
|
|
|
case CLEAR_CONFIRM_TRANSACTION:
|
2021-02-04 19:15:23 +01:00
|
|
|
return initState;
|
2018-06-23 08:52:45 +02:00
|
|
|
default:
|
2021-02-04 19:15:23 +01:00
|
|
|
return state;
|
2018-06-23 08:52:45 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Action Creators
|
2020-11-03 00:41:28 +01:00
|
|
|
export function updateTxData(txData) {
|
2018-06-23 08:52:45 +02:00
|
|
|
return {
|
|
|
|
type: UPDATE_TX_DATA,
|
|
|
|
payload: txData,
|
2021-02-04 19:15:23 +01:00
|
|
|
};
|
2018-06-23 08:52:45 +02:00
|
|
|
}
|
|
|
|
|
2020-11-03 00:41:28 +01:00
|
|
|
export function updateTokenData(tokenData) {
|
2018-06-23 08:52:45 +02:00
|
|
|
return {
|
|
|
|
type: UPDATE_TOKEN_DATA,
|
|
|
|
payload: tokenData,
|
2021-02-04 19:15:23 +01:00
|
|
|
};
|
2018-06-23 08:52:45 +02:00
|
|
|
}
|
|
|
|
|
2021-06-24 01:50:24 +02:00
|
|
|
export function updateTokenProps(tokenProps) {
|
|
|
|
return {
|
|
|
|
type: UPDATE_TOKEN_PROPS,
|
|
|
|
payload: tokenProps,
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2020-11-03 00:41:28 +01:00
|
|
|
export function updateTransactionAmounts(amounts) {
|
2018-06-23 08:52:45 +02:00
|
|
|
return {
|
|
|
|
type: UPDATE_TRANSACTION_AMOUNTS,
|
|
|
|
payload: amounts,
|
2021-02-04 19:15:23 +01:00
|
|
|
};
|
2018-06-23 08:52:45 +02:00
|
|
|
}
|
|
|
|
|
2020-11-03 00:41:28 +01:00
|
|
|
export function updateTransactionFees(fees) {
|
2018-06-23 08:52:45 +02:00
|
|
|
return {
|
|
|
|
type: UPDATE_TRANSACTION_FEES,
|
|
|
|
payload: fees,
|
2021-02-04 19:15:23 +01:00
|
|
|
};
|
2018-06-23 08:52:45 +02:00
|
|
|
}
|
|
|
|
|
2020-11-03 00:41:28 +01:00
|
|
|
export function updateTransactionTotals(totals) {
|
2018-06-23 08:52:45 +02:00
|
|
|
return {
|
|
|
|
type: UPDATE_TRANSACTION_TOTALS,
|
|
|
|
payload: totals,
|
2021-02-04 19:15:23 +01:00
|
|
|
};
|
2018-06-23 08:52:45 +02:00
|
|
|
}
|
|
|
|
|
2020-11-03 00:41:28 +01:00
|
|
|
export function updateNonce(nonce) {
|
2018-06-23 08:52:45 +02:00
|
|
|
return {
|
|
|
|
type: UPDATE_NONCE,
|
|
|
|
payload: nonce,
|
2021-02-04 19:15:23 +01:00
|
|
|
};
|
2018-06-23 08:52:45 +02:00
|
|
|
}
|
|
|
|
|
2020-11-03 00:41:28 +01:00
|
|
|
export function updateTxDataAndCalculate(txData) {
|
2018-06-23 08:52:45 +02:00
|
|
|
return (dispatch, getState) => {
|
2021-02-04 19:15:23 +01:00
|
|
|
const state = getState();
|
|
|
|
const currentCurrency = currentCurrencySelector(state);
|
|
|
|
const conversionRate = conversionRateSelector(state);
|
|
|
|
const nativeCurrency = getNativeCurrency(state);
|
2018-06-23 08:52:45 +02:00
|
|
|
|
2021-02-04 19:15:23 +01:00
|
|
|
dispatch(updateTxData(txData));
|
2018-06-23 08:52:45 +02:00
|
|
|
|
2021-06-15 16:17:42 +02:00
|
|
|
const { txParams: { value = '0x0', gas: gasLimit = '0x0' } = {} } = txData;
|
|
|
|
|
|
|
|
// if the gas price from our infura endpoint is null or undefined
|
|
|
|
// use the metaswap average price estimation as a fallback
|
2021-06-21 16:28:35 +02:00
|
|
|
let { txParams: { gasPrice } = {} } = txData;
|
2021-06-15 16:17:42 +02:00
|
|
|
if (!gasPrice) {
|
|
|
|
gasPrice = getAveragePriceEstimateInHexWEI(state) || '0x0';
|
|
|
|
}
|
2018-06-23 08:52:45 +02:00
|
|
|
|
2018-07-31 07:03:20 +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
|
|
|
});
|
2018-07-31 07:03:20 +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
|
|
|
});
|
2018-06-23 08:52:45 +02:00
|
|
|
|
2020-11-03 00:41:28 +01:00
|
|
|
dispatch(
|
|
|
|
updateTransactionAmounts({
|
|
|
|
fiatTransactionAmount,
|
|
|
|
ethTransactionAmount,
|
|
|
|
hexTransactionAmount: value,
|
|
|
|
}),
|
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 hexTransactionFee = getHexGasTotal({ gasLimit, gasPrice });
|
2018-06-23 08:52:45 +02:00
|
|
|
|
|
|
|
const fiatTransactionFee = getTransactionFee({
|
2018-10-17 01:03:29 +02:00
|
|
|
value: hexTransactionFee,
|
2018-10-26 10:26:43 +02:00
|
|
|
fromCurrency: nativeCurrency,
|
2018-06-23 08:52:45 +02:00
|
|
|
toCurrency: currentCurrency,
|
|
|
|
numberOfDecimals: 2,
|
|
|
|
conversionRate,
|
2021-02-04 19:15:23 +01:00
|
|
|
});
|
2018-06-23 08:52:45 +02:00
|
|
|
const ethTransactionFee = getTransactionFee({
|
2018-10-17 01:03:29 +02:00
|
|
|
value: hexTransactionFee,
|
2018-10-26 10:26:43 +02:00
|
|
|
fromCurrency: nativeCurrency,
|
|
|
|
toCurrency: nativeCurrency,
|
2018-06-23 08:52:45 +02:00
|
|
|
numberOfDecimals: 6,
|
|
|
|
conversionRate,
|
2021-02-04 19:15:23 +01:00
|
|
|
});
|
2018-06-23 08:52:45 +02:00
|
|
|
|
2020-11-03 00:41:28 +01:00
|
|
|
dispatch(
|
|
|
|
updateTransactionFees({
|
|
|
|
fiatTransactionFee,
|
|
|
|
ethTransactionFee,
|
|
|
|
hexTransactionFee,
|
|
|
|
}),
|
2021-02-04 19:15:23 +01:00
|
|
|
);
|
2018-06-23 08:52:45 +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);
|
2018-10-17 01:03:29 +02:00
|
|
|
|
2020-11-03 00:41:28 +01:00
|
|
|
dispatch(
|
|
|
|
updateTransactionTotals({
|
|
|
|
fiatTransactionTotal,
|
|
|
|
ethTransactionTotal,
|
|
|
|
hexTransactionTotal,
|
|
|
|
}),
|
2021-02-04 19:15:23 +01:00
|
|
|
);
|
|
|
|
};
|
2018-06-23 08:52:45 +02:00
|
|
|
}
|
|
|
|
|
2020-11-03 00:41:28 +01:00
|
|
|
export function setTransactionToConfirm(transactionId) {
|
2019-06-18 14:17:14 +02:00
|
|
|
return (dispatch, getState) => {
|
2021-02-04 19:15:23 +01:00
|
|
|
const state = getState();
|
2022-07-31 20:26:40 +02:00
|
|
|
const unconfirmedTransactionsHash =
|
|
|
|
unconfirmedTransactionsHashSelector(state);
|
2021-02-04 19:15:23 +01:00
|
|
|
const transaction = unconfirmedTransactionsHash[transactionId];
|
2018-06-23 08:52:45 +02:00
|
|
|
|
|
|
|
if (!transaction) {
|
2021-02-04 19:15:23 +01:00
|
|
|
console.error(`Transaction with id ${transactionId} not found`);
|
|
|
|
return;
|
2018-06-23 08:52:45 +02:00
|
|
|
}
|
|
|
|
|
2018-06-25 21:06:57 +02:00
|
|
|
if (transaction.txParams) {
|
2021-06-29 18:02:00 +02:00
|
|
|
dispatch(updateTxDataAndCalculate(transaction));
|
2021-02-04 19:15:23 +01:00
|
|
|
const { txParams } = transaction;
|
2018-06-25 21:06:57 +02:00
|
|
|
|
|
|
|
if (txParams.data) {
|
2021-06-24 01:50:24 +02:00
|
|
|
const { to: tokenAddress, data } = txParams;
|
2018-11-13 18:19:12 +01:00
|
|
|
|
2022-03-17 19:35:40 +01:00
|
|
|
const tokenData = parseStandardTokenTransactionData(data);
|
2021-06-24 01:50:24 +02:00
|
|
|
const tokens = getTokens(state);
|
2021-09-10 19:37:19 +02:00
|
|
|
const currentToken = tokens?.find(({ address }) =>
|
|
|
|
isEqualCaseInsensitive(tokenAddress, address),
|
2021-06-24 01:50:24 +02:00
|
|
|
);
|
|
|
|
|
|
|
|
dispatch(
|
|
|
|
updateTokenProps({
|
|
|
|
decimals: currentToken?.decimals,
|
|
|
|
symbol: currentToken?.symbol,
|
|
|
|
}),
|
|
|
|
);
|
2021-02-04 19:15:23 +01:00
|
|
|
dispatch(updateTokenData(tokenData));
|
2018-06-25 21:06:57 +02:00
|
|
|
}
|
2018-06-23 08:52:45 +02:00
|
|
|
|
2018-06-25 21:06:57 +02:00
|
|
|
if (txParams.nonce) {
|
2023-01-24 15:44:49 +01:00
|
|
|
const nonce = hexToDecimal(txParams.nonce);
|
2018-06-23 08:52:45 +02:00
|
|
|
|
2021-02-04 19:15:23 +01:00
|
|
|
dispatch(updateNonce(nonce));
|
2018-06-25 21:06:57 +02:00
|
|
|
}
|
|
|
|
} else {
|
2021-02-04 19:15:23 +01:00
|
|
|
dispatch(updateTxData(transaction));
|
2018-06-23 08:52:45 +02:00
|
|
|
}
|
2021-02-04 19:15:23 +01:00
|
|
|
};
|
2018-06-23 08:52:45 +02:00
|
|
|
}
|
|
|
|
|
2020-11-03 00:41:28 +01:00
|
|
|
export function clearConfirmTransaction() {
|
2018-06-23 08:52:45 +02:00
|
|
|
return {
|
|
|
|
type: CLEAR_CONFIRM_TRANSACTION,
|
2021-02-04 19:15:23 +01:00
|
|
|
};
|
2018-06-23 08:52:45 +02:00
|
|
|
}
|