@@ -18,20 +18,29 @@ import {
1818
1919export type unknownString = 'unknown' ;
2020
21+ export type FeeMarketEstimateType = 'fee-market' ;
22+ export type LegacyEstimateType = 'legacy' ;
23+ export type EthGasPriceEstimateType = 'eth_gasPrice' ;
24+ export type NoEstimateType = 'none' ;
25+
2126/**
2227 * Indicates which type of gasEstimate the controller is currently returning.
2328 * This is useful as a way of asserting that the shape of gasEstimates matches
2429 * expectations. NONE is a special case indicating that no previous gasEstimate
2530 * has been fetched.
2631 */
2732export const GAS_ESTIMATE_TYPES = {
28- FEE_MARKET : 'fee-market' as const ,
29- LEGACY : 'legacy' as const ,
30- ETH_GASPRICE : 'eth_gasPrice' as const ,
31- NONE : 'none' as const ,
33+ FEE_MARKET : 'fee-market' as FeeMarketEstimateType ,
34+ LEGACY : 'legacy' as LegacyEstimateType ,
35+ ETH_GASPRICE : 'eth_gasPrice' as EthGasPriceEstimateType ,
36+ NONE : 'none' as NoEstimateType ,
3237} ;
3338
34- export type GasEstimateType = typeof GAS_ESTIMATE_TYPES [ keyof typeof GAS_ESTIMATE_TYPES ] ;
39+ export type GasEstimateType =
40+ | FeeMarketEstimateType
41+ | EthGasPriceEstimateType
42+ | LegacyEstimateType
43+ | NoEstimateType ;
3544
3645export interface EstimatedGasFeeTimeBounds {
3746 lowerTimeBound : number | null ;
@@ -87,16 +96,6 @@ export interface Eip1559GasFee {
8796 suggestedMaxFeePerGas : string ; // a GWEI decimal number
8897}
8998
90- function isEIP1559GasFee ( object : any ) : object is Eip1559GasFee {
91- return (
92- 'minWaitTimeEstimate' in object &&
93- 'maxWaitTimeEstimate' in object &&
94- 'suggestedMaxPriorityFeePerGas' in object &&
95- 'suggestedMaxFeePerGas' in object &&
96- Object . keys ( object ) . length === 4
97- ) ;
98- }
99-
10099/**
101100 * @type GasFeeEstimates
102101 *
@@ -115,24 +114,36 @@ export interface GasFeeEstimates {
115114 estimatedBaseFee : string ;
116115}
117116
118- function isEIP1559Estimate ( object : any ) : object is GasFeeEstimates {
119- return (
120- 'low' in object &&
121- isEIP1559GasFee ( object . low ) &&
122- 'medium' in object &&
123- isEIP1559GasFee ( object . medium ) &&
124- 'high' in object &&
125- isEIP1559GasFee ( object . high ) &&
126- 'estimatedBaseFee' in object
127- ) ;
128- }
129-
130117const metadata = {
131118 gasFeeEstimates : { persist : true , anonymous : false } ,
132119 estimatedGasFeeTimeBounds : { persist : true , anonymous : false } ,
133120 gasEstimateType : { persist : true , anonymous : false } ,
134121} ;
135122
123+ export type GasFeeStateEthGasPrice = {
124+ gasFeeEstimates : EthGasPriceEstimate ;
125+ estimatedGasFeeTimeBounds : Record < string , never > ;
126+ gasEstimateType : EthGasPriceEstimateType ;
127+ } ;
128+
129+ export type GasFeeStateFeeMarket = {
130+ gasFeeEstimates : GasFeeEstimates ;
131+ estimatedGasFeeTimeBounds : EstimatedGasFeeTimeBounds | Record < string , never > ;
132+ gasEstimateType : FeeMarketEstimateType ;
133+ } ;
134+
135+ export type GasFeeStateLegacy = {
136+ gasFeeEstimates : LegacyGasPriceEstimate ;
137+ estimatedGasFeeTimeBounds : Record < string , never > ;
138+ gasEstimateType : LegacyEstimateType ;
139+ } ;
140+
141+ export type GasFeeStateNoEstimates = {
142+ gasFeeEstimates : Record < string , never > ;
143+ estimatedGasFeeTimeBounds : Record < string , never > ;
144+ gasEstimateType : NoEstimateType ;
145+ } ;
146+
136147/**
137148 * @type GasFeeState
138149 *
@@ -141,15 +152,11 @@ const metadata = {
141152 * @property gasFeeEstimates - Gas fee estimate data based on new EIP-1559 properties
142153 * @property estimatedGasFeeTimeBounds - Estimates representing the minimum and maximum
143154 */
144- export type GasFeeState = {
145- gasFeeEstimates :
146- | GasFeeEstimates
147- | EthGasPriceEstimate
148- | LegacyGasPriceEstimate
149- | Record < string , never > ;
150- estimatedGasFeeTimeBounds : EstimatedGasFeeTimeBounds | Record < string , never > ;
151- gasEstimateType : GasEstimateType ;
152- } ;
155+ export type GasFeeState =
156+ | GasFeeStateEthGasPrice
157+ | GasFeeStateFeeMarket
158+ | GasFeeStateLegacy
159+ | GasFeeStateNoEstimates ;
153160
154161const name = 'GasFeeController' ;
155162
@@ -163,7 +170,7 @@ export type GetGasFeeState = {
163170 handler : ( ) => GasFeeState ;
164171} ;
165172
166- const defaultState = {
173+ const defaultState : GasFeeState = {
167174 gasFeeEstimates : { } ,
168175 estimatedGasFeeTimeBounds : { } ,
169176 gasEstimateType : GAS_ESTIMATE_TYPES . NONE ,
@@ -218,7 +225,7 @@ export class GasFeeController extends BaseController<typeof name, GasFeeState> {
218225 never ,
219226 never
220227 > ;
221- state ?: Partial < GasFeeState > ;
228+ state ?: GasFeeState ;
222229 fetchGasEstimates ?: typeof defaultFetchGasEstimates ;
223230 fetchEthGasPriceEstimate ?: typeof defaultFetchEthGasPriceEstimate ;
224231 fetchLegacyGasPriceEstimates ?: typeof defaultFetchLegacyGasPriceEstimates ;
@@ -275,10 +282,7 @@ export class GasFeeController extends BaseController<typeof name, GasFeeState> {
275282 * @returns GasFeeEstimates
276283 */
277284 async _fetchGasFeeEstimateData ( ) : Promise < GasFeeState | undefined > {
278- let estimates : GasFeeState [ 'gasFeeEstimates' ] ;
279- let estimatedGasFeeTimeBounds = { } ;
280285 let isEIP1559Compatible ;
281- let gasEstimateType : GasEstimateType = GAS_ESTIMATE_TYPES . NONE ;
282286 const isMainnet = this . getIsMainnet ( ) ;
283287 try {
284288 isEIP1559Compatible = await this . getEIP1559Compatibility ( ) ;
@@ -287,41 +291,53 @@ export class GasFeeController extends BaseController<typeof name, GasFeeState> {
287291 isEIP1559Compatible = false ;
288292 }
289293
294+ let newState : GasFeeState = {
295+ gasFeeEstimates : { } ,
296+ estimatedGasFeeTimeBounds : { } ,
297+ gasEstimateType : GAS_ESTIMATE_TYPES . NONE ,
298+ } ;
299+
290300 try {
291301 if ( isEIP1559Compatible ) {
292- estimates = await this . fetchGasEstimates ( ) ;
302+ const estimates = await this . fetchGasEstimates ( ) ;
293303 const {
294304 suggestedMaxPriorityFeePerGas,
295305 suggestedMaxFeePerGas,
296306 } = estimates . medium ;
297- estimatedGasFeeTimeBounds = this . getTimeEstimate (
307+ const estimatedGasFeeTimeBounds = this . getTimeEstimate (
298308 suggestedMaxPriorityFeePerGas ,
299309 suggestedMaxFeePerGas ,
300310 ) ;
301- gasEstimateType = GAS_ESTIMATE_TYPES . FEE_MARKET ;
311+ newState = {
312+ gasFeeEstimates : estimates ,
313+ estimatedGasFeeTimeBounds,
314+ gasEstimateType : GAS_ESTIMATE_TYPES . FEE_MARKET ,
315+ } ;
302316 } else if ( isMainnet ) {
303- estimates = await this . fetchLegacyGasPriceEstimates ( ) ;
304- gasEstimateType = GAS_ESTIMATE_TYPES . LEGACY ;
317+ const estimates = await this . fetchLegacyGasPriceEstimates ( ) ;
318+ newState = {
319+ gasFeeEstimates : estimates ,
320+ estimatedGasFeeTimeBounds : { } ,
321+ gasEstimateType : GAS_ESTIMATE_TYPES . LEGACY ,
322+ } ;
305323 } else {
306324 throw new Error ( 'Main gas fee/price estimation failed. Use fallback' ) ;
307325 }
308326 } catch {
309327 try {
310- estimates = await this . fetchEthGasPriceEstimate ( this . ethQuery ) ;
311- gasEstimateType = GAS_ESTIMATE_TYPES . ETH_GASPRICE ;
328+ const estimates = await this . fetchEthGasPriceEstimate ( this . ethQuery ) ;
329+ newState = {
330+ gasFeeEstimates : estimates ,
331+ estimatedGasFeeTimeBounds : { } ,
332+ gasEstimateType : GAS_ESTIMATE_TYPES . ETH_GASPRICE ,
333+ } ;
312334 } catch ( error ) {
313335 throw new Error (
314336 `Gas fee/price estimation failed. Message: ${ error . message } ` ,
315337 ) ;
316338 }
317339 }
318340
319- const newState : GasFeeState = {
320- gasFeeEstimates : estimates ,
321- estimatedGasFeeTimeBounds,
322- gasEstimateType,
323- } ;
324-
325341 this . update ( ( ) => {
326342 return newState ;
327343 } ) ;
@@ -396,7 +412,7 @@ export class GasFeeController extends BaseController<typeof name, GasFeeState> {
396412 ) : EstimatedGasFeeTimeBounds | Record < string , never > {
397413 if (
398414 ! this . state . gasFeeEstimates ||
399- ! isEIP1559Estimate ( this . state . gasFeeEstimates )
415+ this . state . gasEstimateType !== GAS_ESTIMATE_TYPES . FEE_MARKET
400416 ) {
401417 return { } ;
402418 }
0 commit comments