1
0
mirror of https://github.com/kremalicious/metamask-extension.git synced 2024-11-22 18:00:18 +01:00

Use higher gasfees when attempting to speedup or cancel a transaction (#11936)

This commit is contained in:
Alex Donesky 2021-08-25 18:04:18 -05:00 committed by ryanml
parent ff1a63cf75
commit 13c2614b0d
3 changed files with 54 additions and 5 deletions

View File

@ -6,6 +6,14 @@ import { getConversionRate, getSelectedAccount } from '../selectors';
import { increaseLastGasPrice } from '../helpers/utils/confirm-tx.util'; import { increaseLastGasPrice } from '../helpers/utils/confirm-tx.util';
import { useCancelTransaction } from './useCancelTransaction'; import { useCancelTransaction } from './useCancelTransaction';
jest.mock('../store/actions', () => ({
disconnectGasFeeEstimatePoller: jest.fn(),
getGasFeeEstimatesAndStartPolling: jest
.fn()
.mockImplementation(() => Promise.resolve()),
addPollingTokenToAppState: jest.fn(),
}));
describe('useCancelTransaction', function () { describe('useCancelTransaction', function () {
let useSelector; let useSelector;
const dispatch = sinon.spy(); const dispatch = sinon.spy();

View File

@ -1,7 +1,10 @@
import BigNumber from 'bignumber.js';
import { addHexPrefix } from 'ethereumjs-util'; import { addHexPrefix } from 'ethereumjs-util';
import { useMemo } from 'react'; import { useMemo } from 'react';
import { multiplyCurrencies } from '../../shared/modules/conversion.utils'; import { multiplyCurrencies } from '../../shared/modules/conversion.utils';
import { isEIP1559Transaction } from '../../shared/modules/transaction.utils'; import { isEIP1559Transaction } from '../../shared/modules/transaction.utils';
import { decGWEIToHexWEI } from '../helpers/utils/conversions.util';
import { useGasFeeEstimates } from './useGasFeeEstimates';
/** /**
* Simple helper to save on duplication to multiply the supplied wei hex string * Simple helper to save on duplication to multiply the supplied wei hex string
@ -20,6 +23,26 @@ function addTenPercent(hexStringValue) {
); );
} }
/**
* Helper that returns the higher of two options for a new gas fee:
* The original fee + 10% or
* the current medium suggested fee from our gas estimation api
*
* @param {string} originalFee - hexWei vale of the original fee (maxFee or maxPriority)
* @param {string} currentEstimate - decGwei value of the current medium gasFee estimate (maxFee or maxPriorityfee)
* @returns {string} - hexWei value of the higher of the two inputs.
*/
function getHighestIncrementedFee(originalFee, currentEstimate) {
const buffedOriginalHexWei = addTenPercent(originalFee);
const currentEstimateHexWei = decGWEIToHexWEI(currentEstimate);
return new BigNumber(buffedOriginalHexWei, 16).greaterThan(
new BigNumber(currentEstimateHexWei, 16),
)
? buffedOriginalHexWei
: currentEstimateHexWei;
}
/** /**
* When initializing cancellations or speed ups we need to set the baseline * When initializing cancellations or speed ups we need to set the baseline
* gas fees to be 10% higher, which is the bare minimum that the network will * gas fees to be 10% higher, which is the bare minimum that the network will
@ -35,6 +58,8 @@ function addTenPercent(hexStringValue) {
export function useIncrementedGasFees(transactionGroup) { export function useIncrementedGasFees(transactionGroup) {
const { primaryTransaction } = transactionGroup; const { primaryTransaction } = transactionGroup;
const { gasFeeEstimates = {} } = useGasFeeEstimates();
// We memoize this value so that it can be relied upon in other hooks. // We memoize this value so that it can be relied upon in other hooks.
const customGasSettings = useMemo(() => { const customGasSettings = useMemo(() => {
// This hook is called indiscriminantly on all transactions appearing in // This hook is called indiscriminantly on all transactions appearing in
@ -46,29 +71,45 @@ export function useIncrementedGasFees(transactionGroup) {
gasLimit: primaryTransaction.txParams?.gas, gasLimit: primaryTransaction.txParams?.gas,
gas: primaryTransaction.txParams?.gas, gas: primaryTransaction.txParams?.gas,
}; };
const suggestedMaxFeePerGas =
gasFeeEstimates?.medium?.suggestedMaxFeePerGas ?? '0';
const suggestedMaxPriorityFeePerGas =
gasFeeEstimates?.medium?.suggestedMaxPriorityFeePerGas ?? '0';
if (isEIP1559Transaction(primaryTransaction)) { if (isEIP1559Transaction(primaryTransaction)) {
const transactionMaxFeePerGas = primaryTransaction.txParams?.maxFeePerGas; const transactionMaxFeePerGas = primaryTransaction.txParams?.maxFeePerGas;
const transactionMaxPriorityFeePerGas = const transactionMaxPriorityFeePerGas =
primaryTransaction.txParams?.maxPriorityFeePerGas; primaryTransaction.txParams?.maxPriorityFeePerGas;
temporaryGasSettings.maxFeePerGas = temporaryGasSettings.maxFeePerGas =
transactionMaxFeePerGas === undefined || transactionMaxFeePerGas === undefined ||
transactionMaxFeePerGas.startsWith('-') transactionMaxFeePerGas.startsWith('-')
? '0x0' ? '0x0'
: addTenPercent(transactionMaxFeePerGas); : getHighestIncrementedFee(
transactionMaxFeePerGas,
suggestedMaxFeePerGas,
);
temporaryGasSettings.maxPriorityFeePerGas = temporaryGasSettings.maxPriorityFeePerGas =
transactionMaxPriorityFeePerGas === undefined || transactionMaxPriorityFeePerGas === undefined ||
transactionMaxPriorityFeePerGas.startsWith('-') transactionMaxPriorityFeePerGas.startsWith('-')
? '0x0' ? '0x0'
: addTenPercent(transactionMaxPriorityFeePerGas); : getHighestIncrementedFee(
transactionMaxPriorityFeePerGas,
suggestedMaxPriorityFeePerGas,
);
} else { } else {
const transactionGasPrice = primaryTransaction.txParams?.gasPrice; const transactionGasPrice = primaryTransaction.txParams?.gasPrice;
temporaryGasSettings.gasPrice = temporaryGasSettings.gasPrice =
transactionGasPrice === undefined || transactionGasPrice.startsWith('-') transactionGasPrice === undefined || transactionGasPrice.startsWith('-')
? '0x0' ? '0x0'
: addTenPercent(transactionGasPrice); : getHighestIncrementedFee(
transactionGasPrice,
suggestedMaxFeePerGas,
);
} }
return temporaryGasSettings; return temporaryGasSettings;
}, [primaryTransaction]); }, [primaryTransaction, gasFeeEstimates]);
return customGasSettings; return customGasSettings;
} }

View File

@ -8,7 +8,7 @@ import * as metricEventHook from './useMetricEvent';
import { useRetryTransaction } from './useRetryTransaction'; import { useRetryTransaction } from './useRetryTransaction';
jest.mock('./useGasFeeEstimates', () => ({ jest.mock('./useGasFeeEstimates', () => ({
useGasFeeEstimates: jest.fn(), useGasFeeEstimates: jest.fn().mockImplementation(() => Promise.resolve({})),
})); }));
describe('useRetryTransaction', () => { describe('useRetryTransaction', () => {