2021-06-28 17:23:17 +02:00
|
|
|
import { isHexString } from 'ethereumjs-util';
|
|
|
|
|
2021-03-01 16:15:42 +01:00
|
|
|
export function transactionMatchesNetwork(transaction, chainId, networkId) {
|
|
|
|
if (typeof transaction.chainId !== 'undefined') {
|
|
|
|
return transaction.chainId === chainId;
|
|
|
|
}
|
|
|
|
return transaction.metamaskNetworkId === networkId;
|
|
|
|
}
|
2021-06-28 17:23:17 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Determines if the maxFeePerGas and maxPriorityFeePerGas fields are supplied
|
|
|
|
* and valid inputs. This will return false for non hex string inputs.
|
2022-01-07 16:57:33 +01:00
|
|
|
*
|
2021-06-28 17:23:17 +02:00
|
|
|
* @param {import("../constants/transaction").TransactionMeta} transaction -
|
|
|
|
* the transaction to check
|
|
|
|
* @returns {boolean} true if transaction uses valid EIP1559 fields
|
|
|
|
*/
|
|
|
|
export function isEIP1559Transaction(transaction) {
|
|
|
|
return (
|
2021-07-26 16:16:57 +02:00
|
|
|
isHexString(transaction?.txParams?.maxFeePerGas) &&
|
|
|
|
isHexString(transaction?.txParams?.maxPriorityFeePerGas)
|
2021-06-28 17:23:17 +02:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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.
|
2022-01-07 16:57:33 +01:00
|
|
|
*
|
2021-06-28 17:23:17 +02:00
|
|
|
* @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))
|
|
|
|
);
|
|
|
|
}
|
2021-08-17 22:08:13 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Determine if a transactions gas fees in txParams match those in its dappSuggestedGasFees property
|
2022-01-07 16:57:33 +01:00
|
|
|
*
|
2021-08-17 22:08:13 +02:00
|
|
|
* @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)
|
|
|
|
);
|
|
|
|
}
|