1
1
import { ControllerMessenger } from '@metamask/base-controller' ;
2
2
import { NetworkType , toHex } from '@metamask/controller-utils' ;
3
3
import EthQuery from '@metamask/eth-query' ;
4
- import { NetworkController } from '@metamask/network-controller' ;
4
+ import { NetworkController , NetworkStatus } from '@metamask/network-controller' ;
5
5
import type {
6
+ NetworkControllerGetEIP1559CompatibilityAction ,
7
+ NetworkControllerGetNetworkClientByIdAction ,
6
8
NetworkControllerGetStateAction ,
7
9
NetworkControllerNetworkDidChangeEvent ,
8
10
NetworkControllerStateChangeEvent ,
@@ -40,7 +42,10 @@ const mockedDetermineGasFeeCalculations =
40
42
const name = 'GasFeeController' ;
41
43
42
44
type MainControllerMessenger = ControllerMessenger <
43
- GetGasFeeState | NetworkControllerGetStateAction ,
45
+ | GetGasFeeState
46
+ | NetworkControllerGetStateAction
47
+ | NetworkControllerGetNetworkClientByIdAction
48
+ | NetworkControllerGetEIP1559CompatibilityAction ,
44
49
| GasFeeStateChange
45
50
| NetworkControllerStateChangeEvent
46
51
| NetworkControllerNetworkDidChangeEvent
@@ -61,7 +66,11 @@ const setupNetworkController = async ({
61
66
} ) => {
62
67
const restrictedMessenger = unrestrictedMessenger . getRestricted ( {
63
68
name : 'NetworkController' ,
64
- allowedActions : [ 'NetworkController:getState' ] ,
69
+ allowedActions : [
70
+ 'NetworkController:getState' ,
71
+ 'NetworkController:getNetworkClientById' ,
72
+ 'NetworkController:getEIP1559Compatibility' ,
73
+ ] ,
65
74
allowedEvents : [
66
75
'NetworkController:stateChange' ,
67
76
'NetworkController:networkDidChange' ,
@@ -89,7 +98,11 @@ const getRestrictedMessenger = (
89
98
) => {
90
99
const messenger = controllerMessenger . getRestricted ( {
91
100
name,
92
- allowedActions : [ 'NetworkController:getState' ] ,
101
+ allowedActions : [
102
+ 'NetworkController:getState' ,
103
+ 'NetworkController:getNetworkClientById' ,
104
+ 'NetworkController:getEIP1559Compatibility' ,
105
+ ] ,
93
106
allowedEvents : [ 'NetworkController:stateChange' ] ,
94
107
} ) ;
95
108
@@ -216,6 +229,7 @@ describe('GasFeeController', () => {
216
229
* @param options.networkControllerState - State object to initialize
217
230
* NetworkController with.
218
231
* @param options.interval - The polling interval.
232
+ * @param options.state - The initial GasFeeController state
219
233
*/
220
234
async function setupGasFeeController ( {
221
235
getIsEIP1559Compatible = jest . fn ( ) . mockResolvedValue ( true ) ,
@@ -227,6 +241,7 @@ describe('GasFeeController', () => {
227
241
clientId,
228
242
getChainId,
229
243
networkControllerState = { } ,
244
+ state,
230
245
interval,
231
246
} : {
232
247
getChainId ?: jest . Mock < Hex > ;
@@ -236,6 +251,7 @@ describe('GasFeeController', () => {
236
251
EIP1559APIEndpoint ?: string ;
237
252
clientId ?: string ;
238
253
networkControllerState ?: Partial < NetworkState > ;
254
+ state ?: GasFeeState ;
239
255
interval ?: number ;
240
256
} = { } ) {
241
257
const controllerMessenger = getControllerMessenger ( ) ;
@@ -253,6 +269,7 @@ describe('GasFeeController', () => {
253
269
getCurrentNetworkEIP1559Compatibility : getIsEIP1559Compatible , // change this for networkDetails.state.networkDetails.isEIP1559Compatible ???
254
270
legacyAPIEndpoint,
255
271
EIP1559APIEndpoint,
272
+ state,
256
273
clientId,
257
274
interval,
258
275
} ) ;
@@ -851,4 +868,91 @@ describe('GasFeeController', () => {
851
868
} ) ;
852
869
} ) ;
853
870
} ) ;
871
+ describe ( 'executePoll' , ( ) => {
872
+ it ( 'should call determineGasFeeCalculations with a URL that contains the chain ID' , async ( ) => {
873
+ await setupGasFeeController ( {
874
+ getIsEIP1559Compatible : jest . fn ( ) . mockResolvedValue ( false ) ,
875
+ getCurrentNetworkLegacyGasAPICompatibility : jest
876
+ . fn ( )
877
+ . mockReturnValue ( true ) ,
878
+ legacyAPIEndpoint : 'https://some-legacy-endpoint/<chain_id>' ,
879
+ EIP1559APIEndpoint : 'https://some-eip-1559-endpoint/<chain_id>' ,
880
+ networkControllerState : {
881
+ networksMetadata : {
882
+ mainnet : {
883
+ EIPS : {
884
+ 1559 : true ,
885
+ } ,
886
+ status : NetworkStatus . Available ,
887
+ } ,
888
+ sepolia : {
889
+ EIPS : {
890
+ 1559 : true ,
891
+ } ,
892
+ status : NetworkStatus . Available ,
893
+ } ,
894
+ } ,
895
+ } ,
896
+ clientId : '99999' ,
897
+ } ) ;
898
+
899
+ await gasFeeController . executePoll ( 'mainnet' ) ;
900
+ await gasFeeController . executePoll ( 'sepolia' ) ;
901
+
902
+ expect ( mockedDetermineGasFeeCalculations ) . toHaveBeenCalledWith (
903
+ expect . objectContaining ( {
904
+ fetchGasEstimatesUrl : 'https://some-eip-1559-endpoint/1' ,
905
+ } ) ,
906
+ ) ;
907
+ expect ( mockedDetermineGasFeeCalculations ) . toHaveBeenCalledWith (
908
+ expect . objectContaining ( {
909
+ fetchGasEstimatesUrl : 'https://some-eip-1559-endpoint/11155111' ,
910
+ } ) ,
911
+ ) ;
912
+ expect ( mockedDetermineGasFeeCalculations ) . not . toHaveBeenCalledWith (
913
+ expect . objectContaining ( {
914
+ fetchGasEstimatesUrl : 'https://some-eip-1559-endpoint/5' ,
915
+ } ) ,
916
+ ) ;
917
+ } ) ;
918
+ } ) ;
919
+
920
+ describe ( 'polling (by networkClientId)' , ( ) => {
921
+ it ( 'should call determineGasFeeCalculations (via executePoll) with a URL that contains the chain ID after the interval passed via the constructor' , async ( ) => {
922
+ const pollingInterval = 10000 ;
923
+ await setupGasFeeController ( {
924
+ getIsEIP1559Compatible : jest . fn ( ) . mockResolvedValue ( false ) ,
925
+ getCurrentNetworkLegacyGasAPICompatibility : jest
926
+ . fn ( )
927
+ . mockReturnValue ( true ) ,
928
+ legacyAPIEndpoint : 'https://some-legacy-endpoint/<chain_id>' ,
929
+ EIP1559APIEndpoint : 'https://some-eip-1559-endpoint/<chain_id>' ,
930
+ networkControllerState : {
931
+ networksMetadata : {
932
+ goerli : {
933
+ EIPS : {
934
+ 1559 : true ,
935
+ } ,
936
+ status : NetworkStatus . Available ,
937
+ } ,
938
+ } ,
939
+ } ,
940
+ clientId : '99999' ,
941
+ interval : pollingInterval ,
942
+ } ) ;
943
+
944
+ gasFeeController . startPollingByNetworkClientId ( 'goerli' ) ;
945
+ await clock . tickAsync ( pollingInterval / 2 ) ;
946
+ expect ( mockedDetermineGasFeeCalculations ) . not . toHaveBeenCalled ( ) ;
947
+ await clock . tickAsync ( pollingInterval / 2 ) ;
948
+ expect ( mockedDetermineGasFeeCalculations ) . toHaveBeenCalledWith (
949
+ expect . objectContaining ( {
950
+ fetchGasEstimatesUrl : 'https://some-eip-1559-endpoint/5' ,
951
+ } ) ,
952
+ ) ;
953
+ expect (
954
+ gasFeeController . state . gasFeeEstimatesByChainId ?. [ '0x5' ] ,
955
+ ) . toStrictEqual ( buildMockGasFeeStateFeeMarket ( ) ) ;
956
+ } ) ;
957
+ } ) ;
854
958
} ) ;
0 commit comments