mirror of
https://github.com/kremalicious/metamask-extension.git
synced 2024-11-26 12:29:06 +01:00
ed3cc404f2
The `network` store of the network controller crams two types of data into one place. It roughly tracks whether we have enough information to make requests to the network and whether the network is capable of receiving requests, but it also stores the ID of the network (as obtained via `net_version`). Generally we shouldn't be using the network ID for anything, as it has been completely replaced by chain ID, which all custom RPC endpoints have been required to support for over a year now. However, as the network ID is used in various places within the extension codebase, removing it entirely would be a non-trivial effort. So, minimally, this commit splits `network` into two stores: `networkId` and `networkStatus`. But it also expands the concept of network status. Previously, the network was in one of two states: "loading" and "not-loading". But now it can be in one of four states: - `available`: The network is able to receive and respond to requests. - `unavailable`: The network is not able to receive and respond to requests for unknown reasons. - `blocked`: The network is actively blocking requests based on the user's geolocation. (This is specific to Infura.) - `unknown`: We don't know whether the network can receive and respond to requests, either because we haven't checked or we tried to check and were unsuccessful. This commit also changes how the network status is determined — specifically, how many requests are used to determine that status, when they occur, and whether they are awaited. Previously, the network controller would make 2 to 3 requests during the course of running `lookupNetwork`. * First, if it was an Infura network, it would make a request for `eth_blockNumber` to determine whether Infura was blocking requests or not, then emit an appropriate event. This operation was not awaited. * Then, regardless of the network, it would fetch the network ID via `net_version`. This operation was awaited. * Finally, regardless of the network, it would fetch the latest block via `eth_getBlockByNumber`, then use the result to determine whether the network supported EIP-1559. This operation was awaited. Now: * One fewer request is made, specifically `eth_blockNumber`, as we don't need to make an extra request to determine whether Infura is blocking requests; we can reuse `eth_getBlockByNumber`; * All requests are awaited, which makes `lookupNetwork` run fully in-band instead of partially out-of-band; and * Both requests for `net_version` and `eth_getBlockByNumber` are performed in parallel to make `lookupNetwork` run slightly faster.
323 lines
9.8 KiB
JavaScript
323 lines
9.8 KiB
JavaScript
import { connect } from 'react-redux';
|
|
import { compose } from 'redux';
|
|
import { withRouter } from 'react-router-dom';
|
|
|
|
import { clearConfirmTransaction } from '../../ducks/confirm-transaction/confirm-transaction.duck';
|
|
|
|
import {
|
|
updateCustomNonce,
|
|
cancelTx,
|
|
cancelTxs,
|
|
updateAndApproveTx,
|
|
showModal,
|
|
getNextNonce,
|
|
tryReverseResolveAddress,
|
|
setDefaultHomeActiveTabName,
|
|
addToAddressBook,
|
|
} from '../../store/actions';
|
|
import { isBalanceSufficient } from '../send/send.utils';
|
|
import { shortenAddress, valuesFor } from '../../helpers/utils/util';
|
|
import {
|
|
getAdvancedInlineGasShown,
|
|
getCustomNonceValue,
|
|
getIsMainnet,
|
|
getKnownMethodData,
|
|
getMetaMaskAccounts,
|
|
getUseNonceField,
|
|
transactionFeeSelector,
|
|
getNoGasPriceFetched,
|
|
getIsEthGasPriceFetched,
|
|
getShouldShowFiat,
|
|
checkNetworkAndAccountSupports1559,
|
|
getPreferences,
|
|
doesAddressRequireLedgerHidConnection,
|
|
getTokenList,
|
|
getIsMultiLayerFeeNetwork,
|
|
getIsBuyableChain,
|
|
getEnsResolutionByAddress,
|
|
getUnapprovedTransaction,
|
|
getFullTxData,
|
|
getUseCurrencyRateCheck,
|
|
} from '../../selectors';
|
|
import { getMostRecentOverviewPage } from '../../ducks/history/history';
|
|
import {
|
|
isAddressLedger,
|
|
updateGasFees,
|
|
getIsGasEstimatesLoading,
|
|
getNativeCurrency,
|
|
getSendToAccounts,
|
|
} from '../../ducks/metamask/metamask';
|
|
import { addHexPrefix } from '../../../app/scripts/lib/util';
|
|
|
|
import {
|
|
parseStandardTokenTransactionData,
|
|
transactionMatchesNetwork,
|
|
txParamsAreDappSuggested,
|
|
} from '../../../shared/modules/transaction.utils';
|
|
import { toChecksumHexAddress } from '../../../shared/modules/hexstring-utils';
|
|
|
|
import { getGasLoadingAnimationIsShowing } from '../../ducks/app/app';
|
|
import { isLegacyTransaction } from '../../helpers/utils/transactions.util';
|
|
import { CUSTOM_GAS_ESTIMATE } from '../../../shared/constants/gas';
|
|
import {
|
|
TransactionStatus,
|
|
TransactionType,
|
|
} from '../../../shared/constants/transaction';
|
|
import { getTokenAddressParam } from '../../helpers/utils/token-util';
|
|
import { calcGasTotal } from '../../../shared/lib/transactions-controller-utils';
|
|
import ConfirmTransactionBase from './confirm-transaction-base.component';
|
|
|
|
let customNonceValue = '';
|
|
const customNonceMerge = (txData) =>
|
|
customNonceValue
|
|
? {
|
|
...txData,
|
|
customNonceValue,
|
|
}
|
|
: txData;
|
|
|
|
function addressIsNew(toAccounts, newAddress) {
|
|
const newAddressNormalized = newAddress.toLowerCase();
|
|
const foundMatching = toAccounts.some(
|
|
({ address }) => address.toLowerCase() === newAddressNormalized,
|
|
);
|
|
return !foundMatching;
|
|
}
|
|
|
|
const mapStateToProps = (state, ownProps) => {
|
|
const {
|
|
toAddress: propsToAddress,
|
|
customTxParamsData,
|
|
match: { params = {} },
|
|
} = ownProps;
|
|
const { id: paramsTransactionId } = params;
|
|
const isMainnet = getIsMainnet(state);
|
|
|
|
const isGasEstimatesLoading = getIsGasEstimatesLoading(state);
|
|
const gasLoadingAnimationIsShowing = getGasLoadingAnimationIsShowing(state);
|
|
const isBuyableChain = getIsBuyableChain(state);
|
|
const { confirmTransaction, metamask } = state;
|
|
const {
|
|
conversionRate,
|
|
identities,
|
|
addressBook,
|
|
networkId,
|
|
unapprovedTxs,
|
|
nextNonce,
|
|
provider: { chainId },
|
|
} = metamask;
|
|
const { tokenData, txData, tokenProps, nonce } = confirmTransaction;
|
|
const { txParams = {}, id: transactionId, type } = txData;
|
|
const txId = transactionId || Number(paramsTransactionId);
|
|
const transaction = getUnapprovedTransaction(state, txId);
|
|
const {
|
|
from: fromAddress,
|
|
to: txParamsToAddress,
|
|
gasPrice,
|
|
gas: gasLimit,
|
|
value: amount,
|
|
data,
|
|
} = (transaction && transaction.txParams) || txParams;
|
|
const accounts = getMetaMaskAccounts(state);
|
|
|
|
const transactionData = parseStandardTokenTransactionData(data);
|
|
const tokenToAddress = getTokenAddressParam(transactionData);
|
|
|
|
const { balance } = accounts[fromAddress];
|
|
const { name: fromName } = identities[fromAddress];
|
|
let toAddress = txParamsToAddress;
|
|
if (type !== TransactionType.simpleSend) {
|
|
toAddress = propsToAddress || tokenToAddress || txParamsToAddress;
|
|
}
|
|
|
|
const toAccounts = getSendToAccounts(state);
|
|
|
|
const tokenList = getTokenList(state);
|
|
|
|
const toName =
|
|
identities[toAddress]?.name ||
|
|
tokenList[toAddress?.toLowerCase()]?.name ||
|
|
shortenAddress(toChecksumHexAddress(toAddress));
|
|
|
|
const checksummedAddress = toChecksumHexAddress(toAddress);
|
|
const addressBookObject =
|
|
addressBook &&
|
|
addressBook[chainId] &&
|
|
addressBook[chainId][checksummedAddress];
|
|
const toEns = getEnsResolutionByAddress(state, checksummedAddress);
|
|
const toNickname = addressBookObject ? addressBookObject.name : '';
|
|
const transactionStatus = transaction ? transaction.status : '';
|
|
const supportsEIP1559 =
|
|
checkNetworkAndAccountSupports1559(state) && !isLegacyTransaction(txParams);
|
|
|
|
const {
|
|
hexTransactionAmount,
|
|
hexMinimumTransactionFee,
|
|
hexMaximumTransactionFee,
|
|
hexTransactionTotal,
|
|
gasEstimationObject,
|
|
} = transactionFeeSelector(state, transaction);
|
|
|
|
const currentNetworkUnapprovedTxs = Object.keys(unapprovedTxs)
|
|
.filter((key) =>
|
|
transactionMatchesNetwork(unapprovedTxs[key], chainId, networkId),
|
|
)
|
|
.reduce((acc, key) => ({ ...acc, [key]: unapprovedTxs[key] }), {});
|
|
const unapprovedTxCount = valuesFor(currentNetworkUnapprovedTxs).length;
|
|
|
|
const insufficientBalance = !isBalanceSufficient({
|
|
amount,
|
|
gasTotal: calcGasTotal(gasLimit, gasPrice),
|
|
balance,
|
|
conversionRate,
|
|
});
|
|
|
|
const methodData = getKnownMethodData(state, data) || {};
|
|
|
|
const fullTxData = getFullTxData(
|
|
state,
|
|
txId,
|
|
TransactionStatus.unapproved,
|
|
customTxParamsData,
|
|
);
|
|
|
|
customNonceValue = getCustomNonceValue(state);
|
|
const isEthGasPrice = getIsEthGasPriceFetched(state);
|
|
const noGasPrice = !supportsEIP1559 && getNoGasPriceFetched(state);
|
|
const { useNativeCurrencyAsPrimaryCurrency } = getPreferences(state);
|
|
const gasFeeIsCustom =
|
|
fullTxData.userFeeLevel === CUSTOM_GAS_ESTIMATE ||
|
|
txParamsAreDappSuggested(fullTxData);
|
|
const fromAddressIsLedger = isAddressLedger(state, fromAddress);
|
|
const nativeCurrency = getNativeCurrency(state);
|
|
|
|
const hardwareWalletRequiresConnection =
|
|
doesAddressRequireLedgerHidConnection(state, fromAddress);
|
|
|
|
const isMultiLayerFeeNetwork = getIsMultiLayerFeeNetwork(state);
|
|
|
|
return {
|
|
balance,
|
|
fromAddress,
|
|
fromName,
|
|
toAccounts,
|
|
toAddress,
|
|
toEns,
|
|
toName,
|
|
toNickname,
|
|
hexTransactionAmount,
|
|
hexMinimumTransactionFee,
|
|
hexMaximumTransactionFee,
|
|
hexTransactionTotal,
|
|
txData: fullTxData,
|
|
tokenData,
|
|
methodData,
|
|
tokenProps,
|
|
conversionRate,
|
|
transactionStatus,
|
|
nonce,
|
|
unapprovedTxs,
|
|
unapprovedTxCount,
|
|
customGas: {
|
|
gasLimit,
|
|
gasPrice,
|
|
},
|
|
advancedInlineGasShown: getAdvancedInlineGasShown(state),
|
|
useNonceField: getUseNonceField(state),
|
|
customNonceValue,
|
|
insufficientBalance,
|
|
hideFiatConversion: !getShouldShowFiat(state),
|
|
type,
|
|
nextNonce,
|
|
mostRecentOverviewPage: getMostRecentOverviewPage(state),
|
|
isMainnet,
|
|
isEthGasPrice,
|
|
noGasPrice,
|
|
supportsEIP1559,
|
|
gasIsLoading: isGasEstimatesLoading || gasLoadingAnimationIsShowing,
|
|
useNativeCurrencyAsPrimaryCurrency,
|
|
maxFeePerGas: gasEstimationObject.maxFeePerGas,
|
|
maxPriorityFeePerGas: gasEstimationObject.maxPriorityFeePerGas,
|
|
baseFeePerGas: gasEstimationObject.baseFeePerGas,
|
|
gasFeeIsCustom,
|
|
showLedgerSteps: fromAddressIsLedger,
|
|
nativeCurrency,
|
|
hardwareWalletRequiresConnection,
|
|
isMultiLayerFeeNetwork,
|
|
chainId,
|
|
isBuyableChain,
|
|
useCurrencyRateCheck: getUseCurrencyRateCheck(state),
|
|
};
|
|
};
|
|
|
|
export const mapDispatchToProps = (dispatch) => {
|
|
return {
|
|
tryReverseResolveAddress: (address) => {
|
|
return dispatch(tryReverseResolveAddress(address));
|
|
},
|
|
updateCustomNonce: (value) => {
|
|
customNonceValue = value;
|
|
dispatch(updateCustomNonce(value));
|
|
},
|
|
clearConfirmTransaction: () => dispatch(clearConfirmTransaction()),
|
|
showTransactionConfirmedModal: ({ onSubmit }) => {
|
|
return dispatch(showModal({ name: 'TRANSACTION_CONFIRMED', onSubmit }));
|
|
},
|
|
showRejectTransactionsConfirmationModal: ({
|
|
onSubmit,
|
|
unapprovedTxCount,
|
|
}) => {
|
|
return dispatch(
|
|
showModal({ name: 'REJECT_TRANSACTIONS', onSubmit, unapprovedTxCount }),
|
|
);
|
|
},
|
|
cancelTransaction: ({ id }) => dispatch(cancelTx({ id })),
|
|
cancelAllTransactions: (txList) => dispatch(cancelTxs(txList)),
|
|
sendTransaction: (txData) =>
|
|
dispatch(updateAndApproveTx(customNonceMerge(txData))),
|
|
getNextNonce: () => dispatch(getNextNonce()),
|
|
setDefaultHomeActiveTabName: (tabName) =>
|
|
dispatch(setDefaultHomeActiveTabName(tabName)),
|
|
updateTransactionGasFees: (gasFees) => {
|
|
dispatch(updateGasFees({ ...gasFees, expectHexWei: true }));
|
|
},
|
|
showBuyModal: () => dispatch(showModal({ name: 'DEPOSIT_ETHER' })),
|
|
addToAddressBookIfNew: (newAddress, toAccounts, nickname = '') => {
|
|
const hexPrefixedAddress = addHexPrefix(newAddress);
|
|
if (addressIsNew(toAccounts, hexPrefixedAddress)) {
|
|
dispatch(addToAddressBook(hexPrefixedAddress, nickname));
|
|
}
|
|
},
|
|
};
|
|
};
|
|
|
|
const mergeProps = (stateProps, dispatchProps, ownProps) => {
|
|
const { txData, unapprovedTxs } = stateProps;
|
|
|
|
const {
|
|
cancelAllTransactions: dispatchCancelAllTransactions,
|
|
updateTransactionGasFees: dispatchUpdateTransactionGasFees,
|
|
...otherDispatchProps
|
|
} = dispatchProps;
|
|
|
|
return {
|
|
...stateProps,
|
|
...otherDispatchProps,
|
|
...ownProps,
|
|
cancelAllTransactions: () =>
|
|
dispatchCancelAllTransactions(valuesFor(unapprovedTxs)),
|
|
updateGasAndCalculate: ({ gasLimit, gasPrice }) => {
|
|
dispatchUpdateTransactionGasFees({
|
|
gasLimit,
|
|
gasPrice,
|
|
transaction: txData,
|
|
});
|
|
},
|
|
};
|
|
};
|
|
|
|
export default compose(
|
|
withRouter,
|
|
connect(mapStateToProps, mapDispatchToProps, mergeProps),
|
|
)(ConfirmTransactionBase);
|