1
0
mirror of https://github.com/kremalicious/metamask-extension.git synced 2024-11-25 11:28:51 +01:00
metamask-extension/shared/modules/transaction.utils.js
Dan J Miller 29487ef277
Allow submission of transactions with dapp suggested gas fees, while estimates are loading (#11858)
* Allow submission of transactions with dapp suggested gas fees, while estimates are loading

* Allow editing of transactions with dapp suggested feeds, while estimates are still loading

* Ensure that advanced gas is always editible inline when gas is loading

* Ensure that insufficient balance error is shown when gas is loading if the user has customized the gas

* Only set gas price insufficient errors if the current network is non-eip-1559, or the txparams actually have a gas price

* Remove unnecessary param

* lint fix

* ensure insufficient balance warning is showing when loading

* Ensure that eip1559 network transactions do not combined eip1559 and non-eip1559 gas fee properties

* Lint fix
2021-08-17 17:38:13 -02:30

61 lines
2.3 KiB
JavaScript

import { isHexString } from 'ethereumjs-util';
export function transactionMatchesNetwork(transaction, chainId, networkId) {
if (typeof transaction.chainId !== 'undefined') {
return transaction.chainId === chainId;
}
return transaction.metamaskNetworkId === networkId;
}
/**
* Determines if the maxFeePerGas and maxPriorityFeePerGas fields are supplied
* and valid inputs. This will return false for non hex string inputs.
* @param {import("../constants/transaction").TransactionMeta} transaction -
* the transaction to check
* @returns {boolean} true if transaction uses valid EIP1559 fields
*/
export function isEIP1559Transaction(transaction) {
return (
isHexString(transaction?.txParams?.maxFeePerGas) &&
isHexString(transaction?.txParams?.maxPriorityFeePerGas)
);
}
/**
* Determine if the maxFeePerGas and maxPriorityFeePerGas fields are not
* supplied and that the gasPrice field is valid if it is provided. This will
* return false if gasPrice is a non hex string.
* @param {import("../constants/transaction").TransactionMeta} transaction -
* the transaction to check
* @returns {boolean} true if transaction uses valid Legacy fields OR lacks
* EIP1559 fields
*/
export function isLegacyTransaction(transaction) {
return (
typeof transaction.txParams.maxFeePerGas === 'undefined' &&
typeof transaction.txParams.maxPriorityFeePerGas === 'undefined' &&
(typeof transaction.txParams.gasPrice === 'undefined' ||
isHexString(transaction.txParams.gasPrice))
);
}
/**
* Determine if a transactions gas fees in txParams match those in its dappSuggestedGasFees property
* @param {import("../constants/transaction").TransactionMeta} transaction -
* the transaction to check
* @returns {boolean} true if both the txParams and dappSuggestedGasFees are objects with truthy gas fee properties,
* and those properties are strictly equal
*/
export function txParamsAreDappSuggested(transaction) {
const { gasPrice, maxPriorityFeePerGas, maxFeePerGas } =
transaction?.txParams || {};
return (
(gasPrice && gasPrice === transaction?.dappSuggestedGasFees?.gasPrice) ||
(maxPriorityFeePerGas &&
maxFeePerGas &&
transaction?.dappSuggestedGasFees?.maxPriorityFeePerGas ===
maxPriorityFeePerGas &&
transaction?.dappSuggestedGasFees?.maxFeePerGas === maxFeePerGas)
);
}