11import type { Patch } from 'immer' ;
22
3+ import EthQuery from 'eth-query' ;
34import { v1 as random } from 'uuid' ;
45import { isHexString } from 'ethereumjs-util' ;
56import { BaseController } from '../BaseControllerV2' ;
@@ -9,6 +10,8 @@ import type {
910 NetworkControllerGetEthQueryAction ,
1011 NetworkControllerGetProviderConfigAction ,
1112 NetworkControllerProviderChangeEvent ,
13+ NetworkController ,
14+ NetworkState ,
1215} from '../network/NetworkController' ;
1316import {
1417 fetchGasEstimates ,
@@ -269,6 +272,10 @@ export class GasFeeController extends BaseController<
269272 * current network is compatible with the legacy gas price API.
270273 * @param options.getCurrentAccountEIP1559Compatibility - Determines whether or not the current
271274 * account is EIP-1559 compatible.
275+ * @param options.getChainId - Returns the current chain ID.
276+ * @param options.getProvider - Returns a network provider for the current network.
277+ * @param options.onNetworkStateChange - A function for registering an event handler for the
278+ * network state change event.
272279 * @param options.legacyAPIEndpoint - The legacy gas price API URL. This option is primarily for
273280 * testing purposes.
274281 * @param options.EIP1559APIEndpoint - The EIP-1559 gas price API URL. This option is primarily
@@ -282,7 +289,10 @@ export class GasFeeController extends BaseController<
282289 state,
283290 getCurrentNetworkEIP1559Compatibility,
284291 getCurrentAccountEIP1559Compatibility,
292+ getChainId,
285293 getCurrentNetworkLegacyGasAPICompatibility,
294+ getProvider,
295+ onNetworkStateChange,
286296 legacyAPIEndpoint = LEGACY_GAS_PRICES_API_URL ,
287297 EIP1559APIEndpoint = GAS_FEE_API ,
288298 clientId,
@@ -293,6 +303,9 @@ export class GasFeeController extends BaseController<
293303 getCurrentNetworkEIP1559Compatibility : ( ) => Promise < boolean > ;
294304 getCurrentNetworkLegacyGasAPICompatibility : ( ) => boolean ;
295305 getCurrentAccountEIP1559Compatibility ?: ( ) => boolean ;
306+ getChainId ?: ( ) => `0x${string } ` | `${number } ` | number ;
307+ getProvider ?: ( ) => NetworkController [ 'provider' ] ;
308+ onNetworkStateChange ?: ( listener : ( state : NetworkState ) => void ) => void ;
296309 legacyAPIEndpoint ?: string ;
297310 EIP1559APIEndpoint ?: string ;
298311 clientId ?: string ;
@@ -315,25 +328,41 @@ export class GasFeeController extends BaseController<
315328 getCurrentAccountEIP1559Compatibility ;
316329 this . EIP1559APIEndpoint = EIP1559APIEndpoint ;
317330 this . legacyAPIEndpoint = legacyAPIEndpoint ;
318- const providerConfig = this . messagingSystem . call (
319- 'NetworkController:getProviderConfig' ,
320- ) ;
321- this . currentChainId = providerConfig . chainId ;
322- this . ethQuery = this . messagingSystem . call ( 'NetworkController:getEthQuery' ) ;
323331 this . clientId = clientId ;
324- this . messagingSystem . subscribe (
325- 'NetworkController:providerChange' ,
326- async ( provider ) => {
327- this . ethQuery = this . messagingSystem . call (
328- 'NetworkController:getEthQuery' ,
329- ) ;
330-
331- if ( this . currentChainId !== provider . chainId ) {
332- this . currentChainId = provider . chainId ;
332+ if ( onNetworkStateChange && getChainId && getProvider ) {
333+ this . currentChainId = getChainId ( ) ;
334+ onNetworkStateChange ( async ( ) => {
335+ const newProvider = getProvider ( ) ;
336+ const newChainId = getChainId ( ) ;
337+ this . ethQuery = new EthQuery ( newProvider ) ;
338+ if ( this . currentChainId !== newChainId ) {
339+ this . currentChainId = newChainId ;
333340 await this . resetPolling ( ) ;
334341 }
335- } ,
336- ) ;
342+ } ) ;
343+ } else {
344+ const providerConfig = this . messagingSystem . call (
345+ 'NetworkController:getProviderConfig' ,
346+ ) ;
347+ this . currentChainId = providerConfig . chainId ;
348+ this . ethQuery = this . messagingSystem . call (
349+ 'NetworkController:getEthQuery' ,
350+ ) ;
351+
352+ this . messagingSystem . subscribe (
353+ 'NetworkController:providerChange' ,
354+ async ( provider ) => {
355+ this . ethQuery = this . messagingSystem . call (
356+ 'NetworkController:getEthQuery' ,
357+ ) ;
358+
359+ if ( this . currentChainId !== provider . chainId ) {
360+ this . currentChainId = provider . chainId ;
361+ await this . resetPolling ( ) ;
362+ }
363+ } ,
364+ ) ;
365+ }
337366 }
338367
339368 async resetPolling ( ) {
0 commit comments