Skip to content

Commit 6ea5485

Browse files
committed
stash
1 parent e1a71e5 commit 6ea5485

File tree

4 files changed

+150
-171
lines changed

4 files changed

+150
-171
lines changed

packages/assets-controllers/src/AssetsContractController.test.ts

Lines changed: 62 additions & 70 deletions
Original file line numberDiff line numberDiff line change
@@ -9,22 +9,20 @@ import {
99
} from '@metamask/controller-utils';
1010
import HttpProvider from '@metamask/ethjs-provider-http';
1111
import type {
12-
NetworkClientId,
13-
NetworkControllerMessenger,
1412
Provider,
13+
NetworkControllerEvents,
14+
NetworkControllerActions,
1515
} from '@metamask/network-controller';
1616
import {
1717
NetworkController,
1818
NetworkClientType,
1919
} from '@metamask/network-controller';
20-
import {
21-
getDefaultPreferencesState,
22-
type PreferencesState,
23-
} from '@metamask/preferences-controller';
20+
import { getDefaultPreferencesState } from '@metamask/preferences-controller';
2421
import assert from 'assert';
2522

2623
import { mockNetwork } from '../../../tests/mock-network';
2724
import { buildInfuraNetworkClientConfiguration } from '../../network-controller/tests/helpers';
25+
import type { AllowedActions, AllowedEvents } from './AssetsContractController';
2826
import {
2927
AssetsContractController,
3028
MISSING_PROVIDER_ERROR,
@@ -65,22 +63,25 @@ async function setupAssetContractControllers({
6563
} = {}) {
6664
const networkClientConfiguration = {
6765
type: NetworkClientType.Infura,
68-
network: 'mainnet',
66+
network: NetworkType.mainnet,
6967
infuraProjectId,
7068
chainId: BUILT_IN_NETWORKS.mainnet.chainId,
7169
ticker: BUILT_IN_NETWORKS.mainnet.ticker,
7270
} as const;
7371
let provider: Provider;
7472

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+
});
8182
const networkController = new NetworkController({
8283
infuraProjectId,
83-
messenger,
84+
messenger: networkMessenger,
8485
trackMetaMetricsEvent: jest.fn(),
8586
});
8687
if (useNetworkControllerProvider) {
@@ -94,41 +95,27 @@ async function setupAssetContractControllers({
9495
);
9596
}
9697

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+
});
109106
const assetsContract = new AssetsContractController({
110107
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,
117109
...options,
118110
});
119111

120112
return {
121-
messenger,
113+
messenger: controllerMessenger,
122114
network: networkController,
123115
assetsContract,
124116
provider,
125117
networkClientConfiguration,
126118
infuraProjectId,
127-
triggerPreferencesStateChange: (state: PreferencesState) => {
128-
for (const listener of preferencesStateChangeListeners) {
129-
listener(state);
130-
}
131-
},
132119
};
133120
}
134121

@@ -170,7 +157,8 @@ export { setupAssetContractControllers, mockNetworkWithDefaultChainId };
170157
describe('AssetsContractController', () => {
171158
it('should set default config', async () => {
172159
const { assetsContract, messenger } = await setupAssetContractControllers();
173-
expect(assetsContract.config).toStrictEqual({
160+
const { chainId, ipfsGateway, provider } = assetsContract;
161+
expect({ chainId, ipfsGateway, provider }).toStrictEqual({
174162
chainId: SupportedTokenDetectionNetworks.mainnet,
175163
ipfsGateway: IPFS_DEFAULT_GATEWAY_URL,
176164
provider: undefined,
@@ -179,20 +167,24 @@ describe('AssetsContractController', () => {
179167
});
180168

181169
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({
185173
chainId: SupportedTokenDetectionNetworks.mainnet,
186174
ipfsGateway: IPFS_DEFAULT_GATEWAY_URL,
187175
provider: undefined,
188176
});
189177

190-
triggerPreferencesStateChange({
191-
...getDefaultPreferencesState(),
192-
ipfsGateway: 'newIPFSGateWay',
193-
});
178+
messenger.publish(
179+
'PreferencesController:stateChange',
180+
{
181+
...getDefaultPreferencesState(),
182+
ipfsGateway: 'newIPFSGateWay',
183+
},
184+
[],
185+
);
194186

195-
expect(assetsContract.config).toStrictEqual({
187+
expect({ chainId, ipfsGateway, provider }).toStrictEqual({
196188
ipfsGateway: 'newIPFSGateWay',
197189
chainId: SupportedTokenDetectionNetworks.mainnet,
198190
provider: undefined,
@@ -211,7 +203,7 @@ describe('AssetsContractController', () => {
211203

212204
it('should throw missing provider error when getting ERC-20 token balance when missing provider', async () => {
213205
const { assetsContract, messenger } = await setupAssetContractControllers();
214-
assetsContract.configure({ provider: undefined });
206+
assetsContract.provider = undefined;
215207
await expect(
216208
assetsContract.getERC20BalanceOf(
217209
ERC20_UNI_ADDRESS,
@@ -223,7 +215,7 @@ describe('AssetsContractController', () => {
223215

224216
it('should throw missing provider error when getting ERC-20 token decimal when missing provider', async () => {
225217
const { assetsContract, messenger } = await setupAssetContractControllers();
226-
assetsContract.configure({ provider: undefined });
218+
assetsContract.provider = undefined;
227219
await expect(
228220
assetsContract.getERC20TokenDecimals(ERC20_UNI_ADDRESS),
229221
).rejects.toThrow(MISSING_PROVIDER_ERROR);
@@ -233,7 +225,7 @@ describe('AssetsContractController', () => {
233225
it('should get balance of ERC-20 token contract correctly', async () => {
234226
const { assetsContract, messenger, provider, networkClientConfiguration } =
235227
await setupAssetContractControllers();
236-
assetsContract.configure({ provider });
228+
assetsContract.provider = provider;
237229
mockNetworkWithDefaultChainId({
238230
networkClientConfiguration,
239231
mocks: [
@@ -287,7 +279,7 @@ describe('AssetsContractController', () => {
287279
it('should get ERC-721 NFT tokenId correctly', async () => {
288280
const { assetsContract, messenger, provider, networkClientConfiguration } =
289281
await setupAssetContractControllers();
290-
assetsContract.configure({ provider });
282+
assetsContract.provider = provider;
291283
mockNetworkWithDefaultChainId({
292284
networkClientConfiguration,
293285
mocks: [
@@ -320,7 +312,7 @@ describe('AssetsContractController', () => {
320312

321313
it('should throw missing provider error when getting ERC-721 token standard and details when missing provider', async () => {
322314
const { assetsContract, messenger } = await setupAssetContractControllers();
323-
assetsContract.configure({ provider: undefined });
315+
assetsContract.provider = undefined;
324316
await expect(
325317
assetsContract.getTokenStandardAndDetails(
326318
ERC20_UNI_ADDRESS,
@@ -333,7 +325,7 @@ describe('AssetsContractController', () => {
333325
it('should throw contract standard error when getting ERC-20 token standard and details when provided with invalid ERC-20 address', async () => {
334326
const { assetsContract, messenger, provider } =
335327
await setupAssetContractControllers();
336-
assetsContract.configure({ provider });
328+
assetsContract.provider = provider;
337329
const error = 'Unable to determine contract standard';
338330
await expect(
339331
assetsContract.getTokenStandardAndDetails(
@@ -347,7 +339,7 @@ describe('AssetsContractController', () => {
347339
it('should get ERC-721 token standard and details', async () => {
348340
const { assetsContract, messenger, provider, networkClientConfiguration } =
349341
await setupAssetContractControllers();
350-
assetsContract.configure({ provider });
342+
assetsContract.provider = provider;
351343
mockNetworkWithDefaultChainId({
352344
networkClientConfiguration,
353345
mocks: [
@@ -412,7 +404,7 @@ describe('AssetsContractController', () => {
412404
it('should get ERC-1155 token standard and details', async () => {
413405
const { assetsContract, messenger, provider, networkClientConfiguration } =
414406
await setupAssetContractControllers();
415-
assetsContract.configure({ provider });
407+
assetsContract.provider = provider;
416408
mockNetworkWithDefaultChainId({
417409
networkClientConfiguration,
418410
mocks: [
@@ -497,7 +489,7 @@ describe('AssetsContractController', () => {
497489
it('should get ERC-20 token standard and details', async () => {
498490
const { assetsContract, messenger, provider, networkClientConfiguration } =
499491
await setupAssetContractControllers();
500-
assetsContract.configure({ provider });
492+
assetsContract.provider = provider;
501493
mockNetworkWithDefaultChainId({
502494
networkClientConfiguration,
503495
mocks: [
@@ -594,7 +586,7 @@ describe('AssetsContractController', () => {
594586
it('should get ERC-721 NFT tokenURI correctly', async () => {
595587
const { assetsContract, messenger, provider, networkClientConfiguration } =
596588
await setupAssetContractControllers();
597-
assetsContract.configure({ provider });
589+
assetsContract.provider = provider;
598590
mockNetworkWithDefaultChainId({
599591
networkClientConfiguration,
600592
mocks: [
@@ -643,7 +635,7 @@ describe('AssetsContractController', () => {
643635
it('should not throw an error when address given does not support NFT Metadata interface', async () => {
644636
const { assetsContract, messenger, provider, networkClientConfiguration } =
645637
await setupAssetContractControllers();
646-
assetsContract.configure({ provider });
638+
assetsContract.provider = provider;
647639
const errorLogSpy = jest
648640
.spyOn(console, 'error')
649641
.mockImplementationOnce(() => {
@@ -701,7 +693,7 @@ describe('AssetsContractController', () => {
701693
it('should get ERC-721 NFT name', async () => {
702694
const { assetsContract, messenger, provider, networkClientConfiguration } =
703695
await setupAssetContractControllers();
704-
assetsContract.configure({ provider });
696+
assetsContract.provider = provider;
705697
mockNetworkWithDefaultChainId({
706698
networkClientConfiguration,
707699
mocks: [
@@ -731,7 +723,7 @@ describe('AssetsContractController', () => {
731723
it('should get ERC-721 NFT symbol', async () => {
732724
const { assetsContract, messenger, provider, networkClientConfiguration } =
733725
await setupAssetContractControllers();
734-
assetsContract.configure({ provider });
726+
assetsContract.provider = provider;
735727
mockNetworkWithDefaultChainId({
736728
networkClientConfiguration,
737729
mocks: [
@@ -771,7 +763,7 @@ describe('AssetsContractController', () => {
771763
it('should get ERC-20 token decimals', async () => {
772764
const { assetsContract, messenger, provider, networkClientConfiguration } =
773765
await setupAssetContractControllers();
774-
assetsContract.configure({ provider });
766+
assetsContract.provider = provider;
775767
mockNetworkWithDefaultChainId({
776768
networkClientConfiguration,
777769
mocks: [
@@ -803,7 +795,7 @@ describe('AssetsContractController', () => {
803795
it('should get ERC-20 token name', async () => {
804796
const { assetsContract, messenger, provider, networkClientConfiguration } =
805797
await setupAssetContractControllers();
806-
assetsContract.configure({ provider });
798+
assetsContract.provider = provider;
807799
mockNetworkWithDefaultChainId({
808800
networkClientConfiguration,
809801
mocks: [
@@ -835,7 +827,7 @@ describe('AssetsContractController', () => {
835827
it('should get ERC-721 NFT ownership', async () => {
836828
const { assetsContract, messenger, provider, networkClientConfiguration } =
837829
await setupAssetContractControllers();
838-
assetsContract.configure({ provider });
830+
assetsContract.provider = provider;
839831
mockNetworkWithDefaultChainId({
840832
networkClientConfiguration,
841833
mocks: [
@@ -876,7 +868,7 @@ describe('AssetsContractController', () => {
876868
it('should get balance of ERC-20 token in a single call on network with token detection support', async () => {
877869
const { assetsContract, messenger, provider, networkClientConfiguration } =
878870
await setupAssetContractControllers();
879-
assetsContract.configure({ provider });
871+
assetsContract.provider = provider;
880872
mockNetworkWithDefaultChainId({
881873
networkClientConfiguration,
882874
mocks: [
@@ -982,7 +974,7 @@ describe('AssetsContractController', () => {
982974
useNetworkControllerProvider: true,
983975
infuraProjectId,
984976
});
985-
assetsContract.configure({ provider });
977+
assetsContract.provider = provider;
986978

987979
const balancesOnMainnet = await assetsContract.getBalancesInSingleCall(
988980
ERC20_SAI_ADDRESS,
@@ -1011,7 +1003,7 @@ describe('AssetsContractController', () => {
10111003
provider,
10121004
networkClientConfiguration,
10131005
} = await setupAssetContractControllers();
1014-
assetsContract.configure({ provider });
1006+
assetsContract.provider = provider;
10151007
mockNetworkWithDefaultChainId({
10161008
networkClientConfiguration,
10171009
mocks: [
@@ -1082,7 +1074,7 @@ describe('AssetsContractController', () => {
10821074

10831075
it('should throw missing provider error when transferring single ERC-1155 when missing provider', async () => {
10841076
const { assetsContract, messenger } = await setupAssetContractControllers();
1085-
assetsContract.configure({ provider: undefined });
1077+
assetsContract.provider = undefined;
10861078
await expect(
10871079
assetsContract.transferSingleERC1155(
10881080
ERC1155_ADDRESS,
@@ -1098,7 +1090,7 @@ describe('AssetsContractController', () => {
10981090
it('should throw when ERC1155 function transferSingle is not defined', async () => {
10991091
const { assetsContract, messenger, provider, networkClientConfiguration } =
11001092
await setupAssetContractControllers();
1101-
assetsContract.configure({ provider });
1093+
assetsContract.provider = provider;
11021094
mockNetworkWithDefaultChainId({
11031095
networkClientConfiguration,
11041096
mocks: [
@@ -1135,7 +1127,7 @@ describe('AssetsContractController', () => {
11351127
it('should get the balance of a ERC-1155 NFT for a given address', async () => {
11361128
const { assetsContract, messenger, provider, networkClientConfiguration } =
11371129
await setupAssetContractControllers();
1138-
assetsContract.configure({ provider });
1130+
assetsContract.provider = provider;
11391131
mockNetworkWithDefaultChainId({
11401132
networkClientConfiguration,
11411133
mocks: [
@@ -1181,7 +1173,7 @@ describe('AssetsContractController', () => {
11811173
it('should get the URI of a ERC-1155 NFT', async () => {
11821174
const { assetsContract, messenger, provider, networkClientConfiguration } =
11831175
await setupAssetContractControllers();
1184-
assetsContract.configure({ provider });
1176+
assetsContract.provider = provider;
11851177
mockNetworkWithDefaultChainId({
11861178
networkClientConfiguration,
11871179
mocks: [

0 commit comments

Comments
 (0)