Skip to content

Commit 91c14f9

Browse files
committed
Fix use max value tests
1 parent 75fd32a commit 91c14f9

File tree

2 files changed

+71
-20
lines changed

2 files changed

+71
-20
lines changed

ui/pages/confirmations/components/confirm/info/hooks/useMaxValueRefresher.test.ts

+27-1
Original file line numberDiff line numberDiff line change
@@ -157,7 +157,33 @@ describe('useMaxValueRefresher', () => {
157157
expect(updateEditableParamsMock).toHaveBeenCalledWith(
158158
simpleSendTransactionMetaMock.id,
159159
{
160-
value: '0xf1',
160+
value: '0x110',
161+
},
162+
);
163+
});
164+
165+
it('if userFeeLevel is one of GasFeeEstimateLevel', () => {
166+
useConfirmContextMock.mockReturnValue({
167+
currentConfirmation: {
168+
...simpleSendTransactionMetaMock,
169+
txParams: {
170+
maxFeePerGas: '0x020',
171+
gas: '0x001',
172+
},
173+
gasFeeEstimates: {
174+
low: {
175+
maxFeePerGas: '0x010',
176+
},
177+
},
178+
},
179+
} as unknown as ReturnType<typeof useConfirmContext>);
180+
181+
renderHook(() => useMaxValueRefresher());
182+
183+
expect(updateEditableParamsMock).toHaveBeenCalledWith(
184+
simpleSendTransactionMetaMock.id,
185+
{
186+
value: '0x111',
161187
},
162188
);
163189
});

ui/pages/confirmations/components/confirm/info/hooks/useMaxValueRefresher.ts

+44-19
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,12 @@
11
import { useEffect, useMemo } from 'react';
22
import { useDispatch, useSelector } from 'react-redux';
33
import {
4+
GasFeeEstimateLevel,
45
TransactionType,
6+
UserFeeLevel,
57
type TransactionMeta,
68
type FeeMarketGasFeeEstimates,
9+
type GasFeeEstimates,
710
type GasPriceGasFeeEstimates,
811
type LegacyGasFeeEstimates,
912
} from '@metamask/transaction-controller';
@@ -24,25 +27,6 @@ import { HEX_ZERO } from '../shared/constants';
2427
import { useTransactionEventFragment } from '../../../../hooks/useTransactionEventFragment';
2528
import { useSupportsEIP1559 } from './useSupportsEIP1559';
2629

27-
function getMaxFeePerGas(transactionMeta: TransactionMeta): Hex {
28-
const isCustomEstimateUsed = transactionMeta.estimateUsed;
29-
30-
// Temporarily medium estimate is used - this will be adjusted depending on the failed transaction metrics later
31-
const { gasFeeEstimates } = transactionMeta;
32-
// TODO: Remove this once transactionMeta.txParams.maxFeePerGas is updated properly if no custom estimation is used
33-
let maxFeePerGas =
34-
(gasFeeEstimates as FeeMarketGasFeeEstimates)?.medium?.maxFeePerGas ||
35-
(gasFeeEstimates as LegacyGasFeeEstimates)?.medium ||
36-
(gasFeeEstimates as GasPriceGasFeeEstimates)?.gasPrice;
37-
38-
if (isCustomEstimateUsed) {
39-
// If custom estimation is used, the maxFeePerGas is updated in the transactionMeta.txParams.maxFeePerGas
40-
maxFeePerGas = transactionMeta.txParams.maxFeePerGas as Hex;
41-
}
42-
43-
return maxFeePerGas;
44-
}
45-
4630
// This hook is used to refresh the max value of the transaction
4731
// when the user is in max amount mode only for the transaction type simpleSend
4832
// It subtracts the native fee from the balance and updates the value of the transaction
@@ -95,3 +79,44 @@ export const useMaxValueRefresher = () => {
9579
);
9680
}, [isMaxAmountMode, balance, maxFee]);
9781
};
82+
83+
function getMaxFeePerGas(transactionMeta: TransactionMeta): Hex {
84+
const { gasFeeEstimates, userFeeLevel } = transactionMeta;
85+
86+
// Temporarily medium estimate is used - this will be adjusted depending on the failed transaction metrics later
87+
let maxFeePerGas = getMaxFeePerGasFromGasFeeEstimates(
88+
gasFeeEstimates as GasFeeEstimates,
89+
GasFeeEstimateLevel.Medium,
90+
);
91+
92+
// If custom estimation is used, the maxFeePerGas is updated in the transactionMeta.txParams.maxFeePerGas
93+
if (userFeeLevel === UserFeeLevel.CUSTOM) {
94+
maxFeePerGas = transactionMeta.txParams.maxFeePerGas as Hex;
95+
}
96+
97+
// TODO: Remove this once transactionMeta.txParams.maxFeePerGas is updated properly with
98+
// given userFeeLevel then use transactionMeta.txParams.maxFeePerGas
99+
// https://github.com/MetaMask/MetaMask-planning/issues/4287
100+
if (
101+
Object.values(GasFeeEstimateLevel).includes(
102+
userFeeLevel as GasFeeEstimateLevel,
103+
)
104+
) {
105+
maxFeePerGas = getMaxFeePerGasFromGasFeeEstimates(
106+
gasFeeEstimates as GasFeeEstimates,
107+
userFeeLevel as GasFeeEstimateLevel,
108+
);
109+
}
110+
111+
return maxFeePerGas;
112+
}
113+
114+
function getMaxFeePerGasFromGasFeeEstimates(
115+
gasFeeEstimates: GasFeeEstimates,
116+
userFeeLevel: GasFeeEstimateLevel,
117+
): Hex {
118+
return ((gasFeeEstimates as FeeMarketGasFeeEstimates)?.[userFeeLevel]
119+
?.maxFeePerGas ||
120+
(gasFeeEstimates as LegacyGasFeeEstimates)?.[userFeeLevel] ||
121+
(gasFeeEstimates as GasPriceGasFeeEstimates)?.gasPrice) as Hex;
122+
}

0 commit comments

Comments
 (0)