1
1
import { useEffect , useMemo } from 'react' ;
2
2
import { useDispatch , useSelector } from 'react-redux' ;
3
3
import {
4
+ GasFeeEstimateLevel ,
4
5
TransactionType ,
6
+ UserFeeLevel ,
5
7
type TransactionMeta ,
6
8
type FeeMarketGasFeeEstimates ,
9
+ type GasFeeEstimates ,
7
10
type GasPriceGasFeeEstimates ,
8
11
type LegacyGasFeeEstimates ,
9
12
} from '@metamask/transaction-controller' ;
@@ -24,25 +27,6 @@ import { HEX_ZERO } from '../shared/constants';
24
27
import { useTransactionEventFragment } from '../../../../hooks/useTransactionEventFragment' ;
25
28
import { useSupportsEIP1559 } from './useSupportsEIP1559' ;
26
29
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
-
46
30
// This hook is used to refresh the max value of the transaction
47
31
// when the user is in max amount mode only for the transaction type simpleSend
48
32
// It subtracts the native fee from the balance and updates the value of the transaction
@@ -95,3 +79,44 @@ export const useMaxValueRefresher = () => {
95
79
) ;
96
80
} , [ isMaxAmountMode , balance , maxFee ] ) ;
97
81
} ;
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