11import type { RestrictedControllerMessenger } from '@metamask/base-controller' ;
22import { BaseControllerV2 } from '@metamask/base-controller' ;
3- import { convertHexToDecimal , safelyExecute } from '@metamask/controller-utils' ;
3+ import { ChainId , convertHexToDecimal , safelyExecute } from '@metamask/controller-utils' ;
44import EthQuery from '@metamask/eth-query' ;
55import type {
6+ NetworkClientId ,
7+ NetworkController ,
8+ NetworkControllerGetEIP1559CompatibilityAction ,
9+ NetworkControllerGetNetworkClientByIdAction ,
610 NetworkControllerGetStateAction ,
711 NetworkControllerStateChangeEvent ,
812 NetworkState ,
@@ -20,6 +24,7 @@ import {
2024 fetchEthGasPriceEstimate ,
2125 calculateTimeEstimate ,
2226} from './gas-util' ;
27+ import GasFeeControllerPolling from './GasFeeControllerPolling' ;
2328
2429export const LEGACY_GAS_PRICES_API_URL = `https://api.metaswap.codefi.network/gasPrices` ;
2530
@@ -150,6 +155,10 @@ type FallbackGasFeeEstimates = {
150155} ;
151156
152157const metadata = {
158+ gasFeeEstimatesByChainId : {
159+ persist : true ,
160+ anonymous : false ,
161+ } ,
153162 gasFeeEstimates : { persist : true , anonymous : false } ,
154163 estimatedGasFeeTimeBounds : { persist : true , anonymous : false } ,
155164 gasEstimateType : { persist : true , anonymous : false } ,
@@ -190,12 +199,18 @@ export type FetchGasFeeEstimateOptions = {
190199 * @property gasFeeEstimates - Gas fee estimate data based on new EIP-1559 properties
191200 * @property estimatedGasFeeTimeBounds - Estimates representing the minimum and maximum
192201 */
193- export type GasFeeState =
202+ export type GasFeeStateOld =
194203 | GasFeeStateEthGasPrice
195204 | GasFeeStateFeeMarket
196205 | GasFeeStateLegacy
197206 | GasFeeStateNoEstimates ;
198207
208+ export type GasFeeEstimatesByChainId = {
209+ gasFeeEstimatesByChainId : Record < string , GasFeeStateOld > ;
210+ } ;
211+
212+ export type GasFeeState = GasFeeEstimatesByChainId & GasFeeStateOld ;
213+
199214const name = 'GasFeeController' ;
200215
201216export type GasFeeStateChange = {
@@ -210,13 +225,19 @@ export type GetGasFeeState = {
210225
211226type GasFeeMessenger = RestrictedControllerMessenger <
212227 typeof name ,
213- GetGasFeeState | NetworkControllerGetStateAction ,
228+ | GetGasFeeState
229+ | NetworkControllerGetStateAction
230+ | NetworkControllerGetNetworkClientByIdAction
231+ | NetworkControllerGetEIP1559CompatibilityAction ,
214232 GasFeeStateChange | NetworkControllerStateChangeEvent ,
215- NetworkControllerGetStateAction [ 'type' ] ,
233+ | NetworkControllerGetStateAction [ 'type' ]
234+ | NetworkControllerGetNetworkClientByIdAction [ 'type' ]
235+ | NetworkControllerGetEIP1559CompatibilityAction [ 'type' ] ,
216236 NetworkControllerStateChangeEvent [ 'type' ]
217237> ;
218238
219239const defaultState : GasFeeState = {
240+ gasFeeEstimatesByChainId : { } ,
220241 gasFeeEstimates : { } ,
221242 estimatedGasFeeTimeBounds : { } ,
222243 gasEstimateType : GAS_ESTIMATE_TYPES . NONE ,
@@ -225,7 +246,7 @@ const defaultState: GasFeeState = {
225246/**
226247 * Controller that retrieves gas fee estimate data and polls for updated data on a set interval
227248 */
228- export class GasFeeController extends BaseControllerV2 <
249+ export class GasFeeController extends GasFeeControllerPolling <
229250 typeof name ,
230251 GasFeeState ,
231252 GasFeeMessenger
@@ -373,6 +394,65 @@ export class GasFeeController extends BaseControllerV2<
373394 return _pollToken ;
374395 }
375396
397+ async #fetchGasFeeEstimateForNetworkClientId( networkClientId : string ) {
398+ let isEIP1559Compatible = false ;
399+
400+ const networkClient = await this . messagingSystem . call (
401+ 'NetworkController:getNetworkClientById' ,
402+ networkClientId ,
403+ ) ;
404+
405+ const isLegacyGasAPICompatible =
406+ networkClient . configuration . chainId === '0x38' ;
407+
408+ const decimalChainId = convertHexToDecimal (
409+ networkClient . configuration . chainId ,
410+ ) ;
411+
412+ try {
413+ const result = await this . messagingSystem . call (
414+ 'NetworkController:getEIP1559Compatibility' ,
415+ networkClientId ,
416+ ) ;
417+
418+ isEIP1559Compatible = result || false ;
419+ } catch ( e ) {
420+ console . error ( e ) ;
421+ isEIP1559Compatible = false ;
422+ }
423+
424+ const ethQuery = new EthQuery ( networkClient . provider ) ;
425+
426+ const gasFeeCalculations = await determineGasFeeCalculations ( {
427+ isEIP1559Compatible,
428+ isLegacyGasAPICompatible,
429+ fetchGasEstimates,
430+ fetchGasEstimatesUrl : this . EIP1559APIEndpoint . replace (
431+ '<chain_id>' ,
432+ `${ decimalChainId } ` ,
433+ ) ,
434+ fetchGasEstimatesViaEthFeeHistory,
435+ fetchLegacyGasPriceEstimates,
436+ fetchLegacyGasPriceEstimatesUrl : this . legacyAPIEndpoint . replace (
437+ '<chain_id>' ,
438+ `${ decimalChainId } ` ,
439+ ) ,
440+ fetchEthGasPriceEstimate,
441+ calculateTimeEstimate,
442+ clientId : this . clientId ,
443+ ethQuery,
444+ } ) ;
445+
446+ // update state per chainid?
447+ this . update ( ( state ) => {
448+ state . gasFeeEstimatesByChainId [ networkClient . configuration . chainId ] = {
449+ gasFeeEstimates : gasFeeCalculations . gasFeeEstimates ,
450+ estimatedGasFeeTimeBounds : gasFeeCalculations . estimatedGasFeeTimeBounds ,
451+ gasEstimateType : gasFeeCalculations . gasEstimateType ,
452+ } as any ;
453+ } ) ;
454+ }
455+
376456 /**
377457 * Gets and sets gasFeeEstimates in state.
378458 *
@@ -470,6 +550,10 @@ export class GasFeeController extends BaseControllerV2<
470550 } , this . intervalDelay ) ;
471551 }
472552
553+ async executePoll ( networkClientId : string ) : Promise < void > {
554+ await this . #fetchGasFeeEstimateForNetworkClientId( networkClientId ) ;
555+ }
556+
473557 private resetState ( ) {
474558 this . update ( ( ) => {
475559 return defaultState ;
0 commit comments