@@ -9,22 +9,20 @@ import {
9
9
} from '@metamask/controller-utils' ;
10
10
import HttpProvider from '@metamask/ethjs-provider-http' ;
11
11
import type {
12
- NetworkClientId ,
13
- NetworkControllerMessenger ,
14
12
Provider ,
13
+ NetworkControllerEvents ,
14
+ NetworkControllerActions ,
15
15
} from '@metamask/network-controller' ;
16
16
import {
17
17
NetworkController ,
18
18
NetworkClientType ,
19
19
} from '@metamask/network-controller' ;
20
- import {
21
- getDefaultPreferencesState ,
22
- type PreferencesState ,
23
- } from '@metamask/preferences-controller' ;
20
+ import { getDefaultPreferencesState } from '@metamask/preferences-controller' ;
24
21
import assert from 'assert' ;
25
22
26
23
import { mockNetwork } from '../../../tests/mock-network' ;
27
24
import { buildInfuraNetworkClientConfiguration } from '../../network-controller/tests/helpers' ;
25
+ import type { AllowedActions , AllowedEvents } from './AssetsContractController' ;
28
26
import {
29
27
AssetsContractController ,
30
28
MISSING_PROVIDER_ERROR ,
@@ -65,22 +63,25 @@ async function setupAssetContractControllers({
65
63
} = { } ) {
66
64
const networkClientConfiguration = {
67
65
type : NetworkClientType . Infura ,
68
- network : ' mainnet' ,
66
+ network : NetworkType . mainnet ,
69
67
infuraProjectId,
70
68
chainId : BUILT_IN_NETWORKS . mainnet . chainId ,
71
69
ticker : BUILT_IN_NETWORKS . mainnet . ticker ,
72
70
} as const ;
73
71
let provider : Provider ;
74
72
75
- const messenger : NetworkControllerMessenger =
76
- new ControllerMessenger ( ) . getRestricted ( {
77
- name : 'NetworkController' ,
78
- allowedActions : [ ] ,
79
- allowedEvents : [ ] ,
80
- } ) ;
73
+ const controllerMessenger = new ControllerMessenger <
74
+ NetworkControllerActions | AllowedActions ,
75
+ NetworkControllerEvents | AllowedEvents
76
+ > ( ) ;
77
+ const networkMessenger = controllerMessenger . getRestricted ( {
78
+ name : 'NetworkController' ,
79
+ allowedActions : [ ] ,
80
+ allowedEvents : [ ] ,
81
+ } ) ;
81
82
const networkController = new NetworkController ( {
82
83
infuraProjectId,
83
- messenger,
84
+ messenger : networkMessenger ,
84
85
trackMetaMetricsEvent : jest . fn ( ) ,
85
86
} ) ;
86
87
if ( useNetworkControllerProvider ) {
@@ -94,41 +95,27 @@ async function setupAssetContractControllers({
94
95
) ;
95
96
}
96
97
97
- const getNetworkClientById = useNetworkControllerProvider
98
- ? networkController . getNetworkClientById . bind ( networkController )
99
- : ( networkClientId : NetworkClientId ) =>
100
- ( {
101
- ...networkController . getNetworkClientById ( networkClientId ) ,
102
- provider,
103
- // TODO: Replace `any` with type
104
- // eslint-disable-next-line @typescript-eslint/no-explicit-any
105
- } as any ) ;
106
-
107
- const preferencesStateChangeListeners : ( ( state : PreferencesState ) => void ) [ ] =
108
- [ ] ;
98
+ const assetsContractMessenger = controllerMessenger . getRestricted ( {
99
+ name : 'AssetsContractController' ,
100
+ allowedActions : [ 'NetworkController:getNetworkClientById' ] ,
101
+ allowedEvents : [
102
+ 'PreferencesController:stateChange' ,
103
+ 'NetworkController:networkDidChange' ,
104
+ ] ,
105
+ } ) ;
109
106
const assetsContract = new AssetsContractController ( {
110
107
chainId : ChainId . mainnet ,
111
- onPreferencesStateChange : ( listener ) => {
112
- preferencesStateChangeListeners . push ( listener ) ;
113
- } ,
114
- onNetworkDidChange : ( listener ) =>
115
- messenger . subscribe ( 'NetworkController:networkDidChange' , listener ) ,
116
- getNetworkClientById,
108
+ messenger : assetsContractMessenger ,
117
109
...options ,
118
110
} ) ;
119
111
120
112
return {
121
- messenger,
113
+ messenger : controllerMessenger ,
122
114
network : networkController ,
123
115
assetsContract,
124
116
provider,
125
117
networkClientConfiguration,
126
118
infuraProjectId,
127
- triggerPreferencesStateChange : ( state : PreferencesState ) => {
128
- for ( const listener of preferencesStateChangeListeners ) {
129
- listener ( state ) ;
130
- }
131
- } ,
132
119
} ;
133
120
}
134
121
@@ -170,7 +157,8 @@ export { setupAssetContractControllers, mockNetworkWithDefaultChainId };
170
157
describe ( 'AssetsContractController' , ( ) => {
171
158
it ( 'should set default config' , async ( ) => {
172
159
const { assetsContract, messenger } = await setupAssetContractControllers ( ) ;
173
- expect ( assetsContract . config ) . toStrictEqual ( {
160
+ const { chainId, ipfsGateway, provider } = assetsContract ;
161
+ expect ( { chainId, ipfsGateway, provider } ) . toStrictEqual ( {
174
162
chainId : SupportedTokenDetectionNetworks . mainnet ,
175
163
ipfsGateway : IPFS_DEFAULT_GATEWAY_URL ,
176
164
provider : undefined ,
@@ -179,20 +167,24 @@ describe('AssetsContractController', () => {
179
167
} ) ;
180
168
181
169
it ( 'should update the ipfsGateWay config value when this value is changed in the preferences controller' , async ( ) => {
182
- const { assetsContract, messenger, triggerPreferencesStateChange } =
183
- await setupAssetContractControllers ( ) ;
184
- expect ( assetsContract . config ) . toStrictEqual ( {
170
+ const { assetsContract, messenger } = await setupAssetContractControllers ( ) ;
171
+ const { chainId , ipfsGateway , provider } = assetsContract ;
172
+ expect ( { chainId , ipfsGateway , provider } ) . toStrictEqual ( {
185
173
chainId : SupportedTokenDetectionNetworks . mainnet ,
186
174
ipfsGateway : IPFS_DEFAULT_GATEWAY_URL ,
187
175
provider : undefined ,
188
176
} ) ;
189
177
190
- triggerPreferencesStateChange ( {
191
- ...getDefaultPreferencesState ( ) ,
192
- ipfsGateway : 'newIPFSGateWay' ,
193
- } ) ;
178
+ messenger . publish (
179
+ 'PreferencesController:stateChange' ,
180
+ {
181
+ ...getDefaultPreferencesState ( ) ,
182
+ ipfsGateway : 'newIPFSGateWay' ,
183
+ } ,
184
+ [ ] ,
185
+ ) ;
194
186
195
- expect ( assetsContract . config ) . toStrictEqual ( {
187
+ expect ( { chainId , ipfsGateway , provider } ) . toStrictEqual ( {
196
188
ipfsGateway : 'newIPFSGateWay' ,
197
189
chainId : SupportedTokenDetectionNetworks . mainnet ,
198
190
provider : undefined ,
@@ -211,7 +203,7 @@ describe('AssetsContractController', () => {
211
203
212
204
it ( 'should throw missing provider error when getting ERC-20 token balance when missing provider' , async ( ) => {
213
205
const { assetsContract, messenger } = await setupAssetContractControllers ( ) ;
214
- assetsContract . configure ( { provider : undefined } ) ;
206
+ assetsContract . provider = undefined ;
215
207
await expect (
216
208
assetsContract . getERC20BalanceOf (
217
209
ERC20_UNI_ADDRESS ,
@@ -223,7 +215,7 @@ describe('AssetsContractController', () => {
223
215
224
216
it ( 'should throw missing provider error when getting ERC-20 token decimal when missing provider' , async ( ) => {
225
217
const { assetsContract, messenger } = await setupAssetContractControllers ( ) ;
226
- assetsContract . configure ( { provider : undefined } ) ;
218
+ assetsContract . provider = undefined ;
227
219
await expect (
228
220
assetsContract . getERC20TokenDecimals ( ERC20_UNI_ADDRESS ) ,
229
221
) . rejects . toThrow ( MISSING_PROVIDER_ERROR ) ;
@@ -233,7 +225,7 @@ describe('AssetsContractController', () => {
233
225
it ( 'should get balance of ERC-20 token contract correctly' , async ( ) => {
234
226
const { assetsContract, messenger, provider, networkClientConfiguration } =
235
227
await setupAssetContractControllers ( ) ;
236
- assetsContract . configure ( { provider } ) ;
228
+ assetsContract . provider = provider ;
237
229
mockNetworkWithDefaultChainId ( {
238
230
networkClientConfiguration,
239
231
mocks : [
@@ -287,7 +279,7 @@ describe('AssetsContractController', () => {
287
279
it ( 'should get ERC-721 NFT tokenId correctly' , async ( ) => {
288
280
const { assetsContract, messenger, provider, networkClientConfiguration } =
289
281
await setupAssetContractControllers ( ) ;
290
- assetsContract . configure ( { provider } ) ;
282
+ assetsContract . provider = provider ;
291
283
mockNetworkWithDefaultChainId ( {
292
284
networkClientConfiguration,
293
285
mocks : [
@@ -320,7 +312,7 @@ describe('AssetsContractController', () => {
320
312
321
313
it ( 'should throw missing provider error when getting ERC-721 token standard and details when missing provider' , async ( ) => {
322
314
const { assetsContract, messenger } = await setupAssetContractControllers ( ) ;
323
- assetsContract . configure ( { provider : undefined } ) ;
315
+ assetsContract . provider = undefined ;
324
316
await expect (
325
317
assetsContract . getTokenStandardAndDetails (
326
318
ERC20_UNI_ADDRESS ,
@@ -333,7 +325,7 @@ describe('AssetsContractController', () => {
333
325
it ( 'should throw contract standard error when getting ERC-20 token standard and details when provided with invalid ERC-20 address' , async ( ) => {
334
326
const { assetsContract, messenger, provider } =
335
327
await setupAssetContractControllers ( ) ;
336
- assetsContract . configure ( { provider } ) ;
328
+ assetsContract . provider = provider ;
337
329
const error = 'Unable to determine contract standard' ;
338
330
await expect (
339
331
assetsContract . getTokenStandardAndDetails (
@@ -347,7 +339,7 @@ describe('AssetsContractController', () => {
347
339
it ( 'should get ERC-721 token standard and details' , async ( ) => {
348
340
const { assetsContract, messenger, provider, networkClientConfiguration } =
349
341
await setupAssetContractControllers ( ) ;
350
- assetsContract . configure ( { provider } ) ;
342
+ assetsContract . provider = provider ;
351
343
mockNetworkWithDefaultChainId ( {
352
344
networkClientConfiguration,
353
345
mocks : [
@@ -412,7 +404,7 @@ describe('AssetsContractController', () => {
412
404
it ( 'should get ERC-1155 token standard and details' , async ( ) => {
413
405
const { assetsContract, messenger, provider, networkClientConfiguration } =
414
406
await setupAssetContractControllers ( ) ;
415
- assetsContract . configure ( { provider } ) ;
407
+ assetsContract . provider = provider ;
416
408
mockNetworkWithDefaultChainId ( {
417
409
networkClientConfiguration,
418
410
mocks : [
@@ -497,7 +489,7 @@ describe('AssetsContractController', () => {
497
489
it ( 'should get ERC-20 token standard and details' , async ( ) => {
498
490
const { assetsContract, messenger, provider, networkClientConfiguration } =
499
491
await setupAssetContractControllers ( ) ;
500
- assetsContract . configure ( { provider } ) ;
492
+ assetsContract . provider = provider ;
501
493
mockNetworkWithDefaultChainId ( {
502
494
networkClientConfiguration,
503
495
mocks : [
@@ -594,7 +586,7 @@ describe('AssetsContractController', () => {
594
586
it ( 'should get ERC-721 NFT tokenURI correctly' , async ( ) => {
595
587
const { assetsContract, messenger, provider, networkClientConfiguration } =
596
588
await setupAssetContractControllers ( ) ;
597
- assetsContract . configure ( { provider } ) ;
589
+ assetsContract . provider = provider ;
598
590
mockNetworkWithDefaultChainId ( {
599
591
networkClientConfiguration,
600
592
mocks : [
@@ -643,7 +635,7 @@ describe('AssetsContractController', () => {
643
635
it ( 'should not throw an error when address given does not support NFT Metadata interface' , async ( ) => {
644
636
const { assetsContract, messenger, provider, networkClientConfiguration } =
645
637
await setupAssetContractControllers ( ) ;
646
- assetsContract . configure ( { provider } ) ;
638
+ assetsContract . provider = provider ;
647
639
const errorLogSpy = jest
648
640
. spyOn ( console , 'error' )
649
641
. mockImplementationOnce ( ( ) => {
@@ -701,7 +693,7 @@ describe('AssetsContractController', () => {
701
693
it ( 'should get ERC-721 NFT name' , async ( ) => {
702
694
const { assetsContract, messenger, provider, networkClientConfiguration } =
703
695
await setupAssetContractControllers ( ) ;
704
- assetsContract . configure ( { provider } ) ;
696
+ assetsContract . provider = provider ;
705
697
mockNetworkWithDefaultChainId ( {
706
698
networkClientConfiguration,
707
699
mocks : [
@@ -731,7 +723,7 @@ describe('AssetsContractController', () => {
731
723
it ( 'should get ERC-721 NFT symbol' , async ( ) => {
732
724
const { assetsContract, messenger, provider, networkClientConfiguration } =
733
725
await setupAssetContractControllers ( ) ;
734
- assetsContract . configure ( { provider } ) ;
726
+ assetsContract . provider = provider ;
735
727
mockNetworkWithDefaultChainId ( {
736
728
networkClientConfiguration,
737
729
mocks : [
@@ -771,7 +763,7 @@ describe('AssetsContractController', () => {
771
763
it ( 'should get ERC-20 token decimals' , async ( ) => {
772
764
const { assetsContract, messenger, provider, networkClientConfiguration } =
773
765
await setupAssetContractControllers ( ) ;
774
- assetsContract . configure ( { provider } ) ;
766
+ assetsContract . provider = provider ;
775
767
mockNetworkWithDefaultChainId ( {
776
768
networkClientConfiguration,
777
769
mocks : [
@@ -803,7 +795,7 @@ describe('AssetsContractController', () => {
803
795
it ( 'should get ERC-20 token name' , async ( ) => {
804
796
const { assetsContract, messenger, provider, networkClientConfiguration } =
805
797
await setupAssetContractControllers ( ) ;
806
- assetsContract . configure ( { provider } ) ;
798
+ assetsContract . provider = provider ;
807
799
mockNetworkWithDefaultChainId ( {
808
800
networkClientConfiguration,
809
801
mocks : [
@@ -835,7 +827,7 @@ describe('AssetsContractController', () => {
835
827
it ( 'should get ERC-721 NFT ownership' , async ( ) => {
836
828
const { assetsContract, messenger, provider, networkClientConfiguration } =
837
829
await setupAssetContractControllers ( ) ;
838
- assetsContract . configure ( { provider } ) ;
830
+ assetsContract . provider = provider ;
839
831
mockNetworkWithDefaultChainId ( {
840
832
networkClientConfiguration,
841
833
mocks : [
@@ -876,7 +868,7 @@ describe('AssetsContractController', () => {
876
868
it ( 'should get balance of ERC-20 token in a single call on network with token detection support' , async ( ) => {
877
869
const { assetsContract, messenger, provider, networkClientConfiguration } =
878
870
await setupAssetContractControllers ( ) ;
879
- assetsContract . configure ( { provider } ) ;
871
+ assetsContract . provider = provider ;
880
872
mockNetworkWithDefaultChainId ( {
881
873
networkClientConfiguration,
882
874
mocks : [
@@ -982,7 +974,7 @@ describe('AssetsContractController', () => {
982
974
useNetworkControllerProvider : true ,
983
975
infuraProjectId,
984
976
} ) ;
985
- assetsContract . configure ( { provider } ) ;
977
+ assetsContract . provider = provider ;
986
978
987
979
const balancesOnMainnet = await assetsContract . getBalancesInSingleCall (
988
980
ERC20_SAI_ADDRESS ,
@@ -1011,7 +1003,7 @@ describe('AssetsContractController', () => {
1011
1003
provider,
1012
1004
networkClientConfiguration,
1013
1005
} = await setupAssetContractControllers ( ) ;
1014
- assetsContract . configure ( { provider } ) ;
1006
+ assetsContract . provider = provider ;
1015
1007
mockNetworkWithDefaultChainId ( {
1016
1008
networkClientConfiguration,
1017
1009
mocks : [
@@ -1082,7 +1074,7 @@ describe('AssetsContractController', () => {
1082
1074
1083
1075
it ( 'should throw missing provider error when transferring single ERC-1155 when missing provider' , async ( ) => {
1084
1076
const { assetsContract, messenger } = await setupAssetContractControllers ( ) ;
1085
- assetsContract . configure ( { provider : undefined } ) ;
1077
+ assetsContract . provider = undefined ;
1086
1078
await expect (
1087
1079
assetsContract . transferSingleERC1155 (
1088
1080
ERC1155_ADDRESS ,
@@ -1098,7 +1090,7 @@ describe('AssetsContractController', () => {
1098
1090
it ( 'should throw when ERC1155 function transferSingle is not defined' , async ( ) => {
1099
1091
const { assetsContract, messenger, provider, networkClientConfiguration } =
1100
1092
await setupAssetContractControllers ( ) ;
1101
- assetsContract . configure ( { provider } ) ;
1093
+ assetsContract . provider = provider ;
1102
1094
mockNetworkWithDefaultChainId ( {
1103
1095
networkClientConfiguration,
1104
1096
mocks : [
@@ -1135,7 +1127,7 @@ describe('AssetsContractController', () => {
1135
1127
it ( 'should get the balance of a ERC-1155 NFT for a given address' , async ( ) => {
1136
1128
const { assetsContract, messenger, provider, networkClientConfiguration } =
1137
1129
await setupAssetContractControllers ( ) ;
1138
- assetsContract . configure ( { provider } ) ;
1130
+ assetsContract . provider = provider ;
1139
1131
mockNetworkWithDefaultChainId ( {
1140
1132
networkClientConfiguration,
1141
1133
mocks : [
@@ -1181,7 +1173,7 @@ describe('AssetsContractController', () => {
1181
1173
it ( 'should get the URI of a ERC-1155 NFT' , async ( ) => {
1182
1174
const { assetsContract, messenger, provider, networkClientConfiguration } =
1183
1175
await setupAssetContractControllers ( ) ;
1184
- assetsContract . configure ( { provider } ) ;
1176
+ assetsContract . provider = provider ;
1185
1177
mockNetworkWithDefaultChainId ( {
1186
1178
networkClientConfiguration,
1187
1179
mocks : [
0 commit comments