From 5da97d5c745cf09f34ed6677e6b52bc0bf67afa6 Mon Sep 17 00:00:00 2001 From: Jongsun Suh Date: Wed, 26 Jun 2024 17:24:07 -0400 Subject: [PATCH] [assets-contract] Adjust downstream tests to use messenger actions instead of class method callbacks --- .../src/AssetsContractController.test.ts | 123 +++- ...tractControllerWithNetworkClientId.test.ts | 124 ++-- .../src/NftController.test.ts | 656 +++++++++--------- .../src/TokenBalancesController.test.ts | 21 +- 4 files changed, 502 insertions(+), 422 deletions(-) diff --git a/packages/assets-controllers/src/AssetsContractController.test.ts b/packages/assets-controllers/src/AssetsContractController.test.ts index f1c2319d5dc..28668b5354f 100644 --- a/packages/assets-controllers/src/AssetsContractController.test.ts +++ b/packages/assets-controllers/src/AssetsContractController.test.ts @@ -24,6 +24,8 @@ import assert from 'assert'; import { mockNetwork } from '../../../tests/mock-network'; import { buildInfuraNetworkClientConfiguration } from '../../network-controller/tests/helpers'; import type { + AssetsContractControllerActions, + AssetsContractControllerEvents, AllowedActions as AssetsContractAllowedActions, AllowedEvents as AssetsContractAllowedEvents, } from './AssetsContractController'; @@ -61,7 +63,9 @@ async function setupAssetContractControllers({ useNetworkControllerProvider, infuraProjectId = '341eacb578dd44a1a049cbc5f6fd4035', }: { - options?: Partial[0]>; + options?: Partial< + Omit[0], 'messenger'> + >; useNetworkControllerProvider?: boolean; infuraProjectId?: string; } = {}) { @@ -75,8 +79,12 @@ async function setupAssetContractControllers({ let provider: Provider; const controllerMessenger = new ControllerMessenger< - NetworkControllerActions | AssetsContractAllowedActions, - NetworkControllerEvents | AssetsContractAllowedEvents + | NetworkControllerActions + | AssetsContractControllerActions + | AssetsContractAllowedActions, + | NetworkControllerEvents + | AssetsContractControllerEvents + | AssetsContractAllowedEvents >(); const networkMessenger = controllerMessenger.getRestricted({ name: 'NetworkController', @@ -227,7 +235,8 @@ describe('AssetsContractController', () => { const { assetsContract, messenger } = await setupAssetContractControllers(); assetsContract.provider = undefined; await expect( - assetsContract.getERC20BalanceOf( + messenger.call( + `AssetsContractController:getERC20BalanceOf`, ERC20_UNI_ADDRESS, TEST_ACCOUNT_PUBLIC_ADDRESS, ), @@ -239,7 +248,10 @@ describe('AssetsContractController', () => { const { assetsContract, messenger } = await setupAssetContractControllers(); assetsContract.provider = undefined; await expect( - assetsContract.getERC20TokenDecimals(ERC20_UNI_ADDRESS), + messenger.call( + `AssetsContractController:getERC20TokenDecimals`, + ERC20_UNI_ADDRESS, + ), ).rejects.toThrow(MISSING_PROVIDER_ERROR); messenger.clearEventSubscriptions('NetworkController:networkDidChange'); }); @@ -285,11 +297,13 @@ describe('AssetsContractController', () => { }, ], }); - const UNIBalance = await assetsContract.getERC20BalanceOf( + const UNIBalance = await messenger.call( + `AssetsContractController:getERC20BalanceOf`, ERC20_UNI_ADDRESS, TEST_ACCOUNT_PUBLIC_ADDRESS, ); - const UNINoBalance = await assetsContract.getERC20BalanceOf( + const UNINoBalance = await messenger.call( + `AssetsContractController:getERC20BalanceOf`, ERC20_UNI_ADDRESS, '0x202637dAAEfbd7f131f90338a4A6c69F6Cd5CE91', ); @@ -323,7 +337,8 @@ describe('AssetsContractController', () => { }, ], }); - const tokenId = await assetsContract.getERC721NftTokenId( + const tokenId = await messenger.call( + `AssetsContractController:getERC721NftTokenId`, ERC721_GODS_ADDRESS, '0x9a90bd8d1149a88b42a99cf62215ad955d6f498a', 0, @@ -336,7 +351,8 @@ describe('AssetsContractController', () => { const { assetsContract, messenger } = await setupAssetContractControllers(); assetsContract.provider = undefined; await expect( - assetsContract.getTokenStandardAndDetails( + messenger.call( + `AssetsContractController:getTokenStandardAndDetails`, ERC20_UNI_ADDRESS, TEST_ACCOUNT_PUBLIC_ADDRESS, ), @@ -350,7 +366,8 @@ describe('AssetsContractController', () => { assetsContract.provider = provider; const error = 'Unable to determine contract standard'; await expect( - assetsContract.getTokenStandardAndDetails( + messenger.call( + `AssetsContractController:getTokenStandardAndDetails`, 'BaDeRc20AdDrEsS', TEST_ACCOUNT_PUBLIC_ADDRESS, ), @@ -415,7 +432,8 @@ describe('AssetsContractController', () => { }, ], }); - const standardAndDetails = await assetsContract.getTokenStandardAndDetails( + const standardAndDetails = await messenger.call( + `AssetsContractController:getTokenStandardAndDetails`, ERC721_GODS_ADDRESS, TEST_ACCOUNT_PUBLIC_ADDRESS, ); @@ -496,7 +514,8 @@ describe('AssetsContractController', () => { }, ], }); - const standardAndDetails = await assetsContract.getTokenStandardAndDetails( + const standardAndDetails = await messenger.call( + `AssetsContractController:getTokenStandardAndDetails`, ERC1155_ADDRESS, TEST_ACCOUNT_PUBLIC_ADDRESS, ); @@ -597,7 +616,8 @@ describe('AssetsContractController', () => { }, ], }); - const standardAndDetails = await assetsContract.getTokenStandardAndDetails( + const standardAndDetails = await messenger.call( + `AssetsContractController:getTokenStandardAndDetails`, ERC20_UNI_ADDRESS, TEST_ACCOUNT_PUBLIC_ADDRESS, ); @@ -646,7 +666,8 @@ describe('AssetsContractController', () => { }, ], }); - const tokenId = await assetsContract.getERC721TokenURI( + const tokenId = await messenger.call( + `AssetsContractController:getERC721TokenURI`, ERC721_GODS_ADDRESS, '0', ); @@ -699,7 +720,8 @@ describe('AssetsContractController', () => { }, ], }); - const uri = await assetsContract.getERC721TokenURI( + const uri = await messenger.call( + `AssetsContractController:getERC721TokenURI`, '0x0000000000000000000000000000000000000000', '0', ); @@ -737,7 +759,10 @@ describe('AssetsContractController', () => { }, ], }); - const name = await assetsContract.getERC721AssetName(ERC721_GODS_ADDRESS); + const name = await messenger.call( + `AssetsContractController:getERC721AssetName`, + ERC721_GODS_ADDRESS, + ); expect(name).toBe('Gods Unchained'); messenger.clearEventSubscriptions('NetworkController:networkDidChange'); }); @@ -767,7 +792,8 @@ describe('AssetsContractController', () => { }, ], }); - const symbol = await assetsContract.getERC721AssetSymbol( + const symbol = await messenger.call( + `AssetsContractController:getERC721AssetSymbol`, ERC721_GODS_ADDRESS, ); expect(symbol).toBe('GODS'); @@ -775,9 +801,12 @@ describe('AssetsContractController', () => { }); it('should throw missing provider error when getting ERC-721 NFT symbol when missing provider', async () => { - const { assetsContract, messenger } = await setupAssetContractControllers(); + const { messenger } = await setupAssetContractControllers(); await expect( - assetsContract.getERC721AssetSymbol(ERC721_GODS_ADDRESS), + messenger.call( + `AssetsContractController:getERC721AssetSymbol`, + ERC721_GODS_ADDRESS, + ), ).rejects.toThrow(MISSING_PROVIDER_ERROR); messenger.clearEventSubscriptions('NetworkController:networkDidChange'); }); @@ -807,7 +836,8 @@ describe('AssetsContractController', () => { }, ], }); - const decimals = await assetsContract.getERC20TokenDecimals( + const decimals = await messenger.call( + `AssetsContractController:getERC20TokenDecimals`, ERC20_SAI_ADDRESS, ); expect(Number(decimals)).toBe(18); @@ -840,7 +870,10 @@ describe('AssetsContractController', () => { ], }); - const name = await assetsContract.getERC20TokenName(ERC20_DAI_ADDRESS); + const name = await messenger.call( + `AssetsContractController:getERC20TokenName`, + ERC20_DAI_ADDRESS, + ); expect(name).toBe('Dai Stablecoin'); messenger.clearEventSubscriptions('NetworkController:networkDidChange'); @@ -871,7 +904,8 @@ describe('AssetsContractController', () => { }, ], }); - const tokenId = await assetsContract.getERC721OwnerOf( + const tokenId = await messenger.call( + `AssetsContractController:getERC721OwnerOf`, ERC721_GODS_ADDRESS, '148332', ); @@ -880,9 +914,13 @@ describe('AssetsContractController', () => { }); it('should throw missing provider error when getting ERC-721 NFT ownership', async () => { - const { assetsContract, messenger } = await setupAssetContractControllers(); + const { messenger } = await setupAssetContractControllers(); await expect( - assetsContract.getERC721OwnerOf(ERC721_GODS_ADDRESS, '148332'), + messenger.call( + `AssetsContractController:getERC721OwnerOf`, + ERC721_GODS_ADDRESS, + '148332', + ), ).rejects.toThrow(MISSING_PROVIDER_ERROR); messenger.clearEventSubscriptions('NetworkController:networkDidChange'); }); @@ -912,7 +950,8 @@ describe('AssetsContractController', () => { }, ], }); - const balances = await assetsContract.getBalancesInSingleCall( + const balances = await messenger.call( + `AssetsContractController:getBalancesInSingleCall`, ERC20_SAI_ADDRESS, [ERC20_SAI_ADDRESS], ); @@ -988,7 +1027,7 @@ describe('AssetsContractController', () => { }, ], }); - const { assetsContract, network, provider } = + const { assetsContract, messenger, network, provider } = await setupAssetContractControllers({ options: { chainId: ChainId.mainnet, @@ -998,7 +1037,8 @@ describe('AssetsContractController', () => { }); assetsContract.provider = provider; - const balancesOnMainnet = await assetsContract.getBalancesInSingleCall( + const balancesOnMainnet = await messenger.call( + `AssetsContractController:getBalancesInSingleCall`, ERC20_SAI_ADDRESS, [ERC20_SAI_ADDRESS], ); @@ -1008,9 +1048,11 @@ describe('AssetsContractController', () => { await network.setActiveNetwork(InfuraNetworkType['linea-mainnet']); - const balancesOnLineaMainnet = await assetsContract.getBalancesInSingleCall( + const balancesOnLineaMainnet = await messenger.call( + `AssetsContractController:getBalancesInSingleCall`, ERC20_SAI_ADDRESS, [ERC20_SAI_ADDRESS], + InfuraNetworkType['linea-mainnet'], ); expect(balancesOnLineaMainnet).toStrictEqual({ [ERC20_SAI_ADDRESS]: BigNumber.from('0xa0155d09733ed8ef4c4'), @@ -1078,7 +1120,8 @@ describe('AssetsContractController', () => { ], }); - const balances = await assetsContract.getBalancesInSingleCall( + const balances = await messenger.call( + `AssetsContractController:getBalancesInSingleCall`, ERC20_SAI_ADDRESS, [ERC20_SAI_ADDRESS], ); @@ -1086,7 +1129,8 @@ describe('AssetsContractController', () => { await network.setActiveNetwork(NetworkType.sepolia); - const noBalances = await assetsContract.getBalancesInSingleCall( + const noBalances = await messenger.call( + `AssetsContractController:getBalancesInSingleCall`, ERC20_SAI_ADDRESS, [ERC20_SAI_ADDRESS], ); @@ -1098,7 +1142,8 @@ describe('AssetsContractController', () => { const { assetsContract, messenger } = await setupAssetContractControllers(); assetsContract.provider = undefined; await expect( - assetsContract.transferSingleERC1155( + messenger.call( + `AssetsContractController:transferSingleERC1155`, ERC1155_ADDRESS, TEST_ACCOUNT_PUBLIC_ADDRESS, TEST_ACCOUNT_PUBLIC_ADDRESS, @@ -1135,7 +1180,8 @@ describe('AssetsContractController', () => { ], }); await expect( - assetsContract.transferSingleERC1155( + messenger.call( + `AssetsContractController:transferSingleERC1155`, ERC1155_ADDRESS, '0x0', TEST_ACCOUNT_PUBLIC_ADDRESS, @@ -1171,19 +1217,21 @@ describe('AssetsContractController', () => { }, ], }); - const balance = await assetsContract.getERC1155BalanceOf( + const balance = await messenger.call( + `AssetsContractController:getERC1155BalanceOf`, TEST_ACCOUNT_PUBLIC_ADDRESS, ERC1155_ADDRESS, ERC1155_ID, ); - expect(Number(balance)).toBeGreaterThan(0); + expect(balance.isZero() || balance.isNeg()).toBe(false); messenger.clearEventSubscriptions('NetworkController:networkDidChange'); }); it('should throw missing provider error when getting the balance of a ERC-1155 NFT when missing provider', async () => { - const { assetsContract, messenger } = await setupAssetContractControllers(); + const { messenger } = await setupAssetContractControllers(); await expect( - assetsContract.getERC1155BalanceOf( + messenger.call( + `AssetsContractController:getERC1155BalanceOf`, TEST_ACCOUNT_PUBLIC_ADDRESS, ERC1155_ADDRESS, ERC1155_ID, @@ -1218,7 +1266,8 @@ describe('AssetsContractController', () => { ], }); const expectedUri = `https://api.opensea.io/api/v1/metadata/${ERC1155_ADDRESS}/0x{id}`; - const uri = await assetsContract.getERC1155TokenURI( + const uri = await messenger.call( + `AssetsContractController:getERC1155TokenURI`, ERC1155_ADDRESS, ERC1155_ID, ); diff --git a/packages/assets-controllers/src/AssetsContractControllerWithNetworkClientId.test.ts b/packages/assets-controllers/src/AssetsContractControllerWithNetworkClientId.test.ts index d064192b686..365685397a7 100644 --- a/packages/assets-controllers/src/AssetsContractControllerWithNetworkClientId.test.ts +++ b/packages/assets-controllers/src/AssetsContractControllerWithNetworkClientId.test.ts @@ -19,9 +19,10 @@ const TEST_ACCOUNT_PUBLIC_ADDRESS = describe('AssetsContractController with NetworkClientId', () => { it('should throw when getting ERC-20 token balance when networkClientId is invalid', async () => { - const { assetsContract, messenger } = await setupAssetContractControllers(); + const { messenger } = await setupAssetContractControllers(); await expect( - assetsContract.getERC20BalanceOf( + messenger.call( + `AssetsContractController:getERC20BalanceOf`, ERC20_UNI_ADDRESS, TEST_ACCOUNT_PUBLIC_ADDRESS, 'invalidNetworkClientId', @@ -31,9 +32,10 @@ describe('AssetsContractController with NetworkClientId', () => { }); it('should throw when getting ERC-20 token decimal when networkClientId is invalid', async () => { - const { assetsContract, messenger } = await setupAssetContractControllers(); + const { messenger } = await setupAssetContractControllers(); await expect( - assetsContract.getERC20TokenDecimals( + messenger.call( + `AssetsContractController:getERC20TokenDecimals`, ERC20_UNI_ADDRESS, 'invalidNetworkClientId', ), @@ -42,7 +44,7 @@ describe('AssetsContractController with NetworkClientId', () => { }); it('should get balance of ERC-20 token contract correctly', async () => { - const { assetsContract, messenger, networkClientConfiguration } = + const { messenger, networkClientConfiguration } = await setupAssetContractControllers(); mockNetworkWithDefaultChainId({ networkClientConfiguration, @@ -81,12 +83,14 @@ describe('AssetsContractController with NetworkClientId', () => { }, ], }); - const UNIBalance = await assetsContract.getERC20BalanceOf( + const UNIBalance = await messenger.call( + `AssetsContractController:getERC20BalanceOf`, ERC20_UNI_ADDRESS, TEST_ACCOUNT_PUBLIC_ADDRESS, 'mainnet', ); - const UNINoBalance = await assetsContract.getERC20BalanceOf( + const UNINoBalance = await messenger.call( + `AssetsContractController:getERC20BalanceOf`, ERC20_UNI_ADDRESS, '0x202637dAAEfbd7f131f90338a4A6c69F6Cd5CE91', 'mainnet', @@ -97,7 +101,7 @@ describe('AssetsContractController with NetworkClientId', () => { }); it('should get ERC-721 NFT tokenId correctly', async () => { - const { assetsContract, messenger, networkClientConfiguration } = + const { messenger, networkClientConfiguration } = await setupAssetContractControllers(); mockNetworkWithDefaultChainId({ networkClientConfiguration, @@ -120,7 +124,8 @@ describe('AssetsContractController with NetworkClientId', () => { }, ], }); - const tokenId = await assetsContract.getERC721NftTokenId( + const tokenId = await messenger.call( + `AssetsContractController:getERC721NftTokenId`, ERC721_GODS_ADDRESS, '0x9a90bd8d1149a88b42a99cf62215ad955d6f498a', 0, @@ -131,9 +136,10 @@ describe('AssetsContractController with NetworkClientId', () => { }); it('should throw error when getting ERC-721 token standard and details when networkClientId is invalid', async () => { - const { assetsContract, messenger } = await setupAssetContractControllers(); + const { messenger } = await setupAssetContractControllers(); await expect( - assetsContract.getTokenStandardAndDetails( + messenger.call( + `AssetsContractController:getTokenStandardAndDetails`, ERC20_UNI_ADDRESS, TEST_ACCOUNT_PUBLIC_ADDRESS, undefined, @@ -144,10 +150,11 @@ describe('AssetsContractController with NetworkClientId', () => { }); it('should throw contract standard error when getting ERC-20 token standard and details when provided with invalid ERC-20 address', async () => { - const { assetsContract, messenger } = await setupAssetContractControllers(); + const { messenger } = await setupAssetContractControllers(); const error = 'Unable to determine contract standard'; await expect( - assetsContract.getTokenStandardAndDetails( + messenger.call( + `AssetsContractController:getTokenStandardAndDetails`, 'BaDeRc20AdDrEsS', TEST_ACCOUNT_PUBLIC_ADDRESS, undefined, @@ -158,7 +165,7 @@ describe('AssetsContractController with NetworkClientId', () => { }); it('should get ERC-721 token standard and details', async () => { - const { assetsContract, messenger, networkClientConfiguration } = + const { messenger, networkClientConfiguration } = await setupAssetContractControllers(); mockNetworkWithDefaultChainId({ networkClientConfiguration, @@ -213,7 +220,8 @@ describe('AssetsContractController with NetworkClientId', () => { }, ], }); - const standardAndDetails = await assetsContract.getTokenStandardAndDetails( + const standardAndDetails = await messenger.call( + `AssetsContractController:getTokenStandardAndDetails`, ERC721_GODS_ADDRESS, TEST_ACCOUNT_PUBLIC_ADDRESS, undefined, @@ -224,7 +232,7 @@ describe('AssetsContractController with NetworkClientId', () => { }); it('should get ERC-1155 token standard and details', async () => { - const { assetsContract, messenger, networkClientConfiguration } = + const { messenger, networkClientConfiguration } = await setupAssetContractControllers(); mockNetworkWithDefaultChainId({ networkClientConfiguration, @@ -263,7 +271,8 @@ describe('AssetsContractController with NetworkClientId', () => { }, ], }); - const standardAndDetails = await assetsContract.getTokenStandardAndDetails( + const standardAndDetails = await messenger.call( + `AssetsContractController:getTokenStandardAndDetails`, ERC1155_ADDRESS, TEST_ACCOUNT_PUBLIC_ADDRESS, undefined, @@ -274,7 +283,7 @@ describe('AssetsContractController with NetworkClientId', () => { }); it('should get ERC-20 token standard and details', async () => { - const { assetsContract, messenger, networkClientConfiguration } = + const { messenger, networkClientConfiguration } = await setupAssetContractControllers(); mockNetworkWithDefaultChainId({ networkClientConfiguration, @@ -361,7 +370,8 @@ describe('AssetsContractController with NetworkClientId', () => { }, ], }); - const standardAndDetails = await assetsContract.getTokenStandardAndDetails( + const standardAndDetails = await messenger.call( + `AssetsContractController:getTokenStandardAndDetails`, ERC20_UNI_ADDRESS, TEST_ACCOUNT_PUBLIC_ADDRESS, undefined, @@ -372,7 +382,7 @@ describe('AssetsContractController with NetworkClientId', () => { }); it('should get ERC-721 NFT tokenURI correctly', async () => { - const { assetsContract, messenger, networkClientConfiguration } = + const { messenger, networkClientConfiguration } = await setupAssetContractControllers(); mockNetworkWithDefaultChainId({ networkClientConfiguration, @@ -411,7 +421,8 @@ describe('AssetsContractController with NetworkClientId', () => { }, ], }); - const tokenId = await assetsContract.getERC721TokenURI( + const tokenId = await messenger.call( + `AssetsContractController:getERC721TokenURI`, ERC721_GODS_ADDRESS, '0', 'mainnet', @@ -464,7 +475,8 @@ describe('AssetsContractController with NetworkClientId', () => { .mockImplementationOnce(() => { /**/ }); - const uri = await assetsContract.getERC721TokenURI( + const uri = await messenger.call( + `AssetsContractController:getERC721TokenURI`, '0x0000000000000000000000000000000000000000', '0', 'mainnet', @@ -478,7 +490,7 @@ describe('AssetsContractController with NetworkClientId', () => { }); it('should get ERC-721 NFT name', async () => { - const { assetsContract, messenger, networkClientConfiguration } = + const { messenger, networkClientConfiguration } = await setupAssetContractControllers(); mockNetworkWithDefaultChainId({ networkClientConfiguration, @@ -501,7 +513,8 @@ describe('AssetsContractController with NetworkClientId', () => { }, ], }); - const name = await assetsContract.getERC721AssetName( + const name = await messenger.call( + `AssetsContractController:getERC721AssetName`, ERC721_GODS_ADDRESS, 'mainnet', ); @@ -510,7 +523,7 @@ describe('AssetsContractController with NetworkClientId', () => { }); it('should get ERC-721 NFT symbol', async () => { - const { assetsContract, messenger, networkClientConfiguration } = + const { messenger, networkClientConfiguration } = await setupAssetContractControllers(); mockNetworkWithDefaultChainId({ networkClientConfiguration, @@ -533,7 +546,8 @@ describe('AssetsContractController with NetworkClientId', () => { }, ], }); - const symbol = await assetsContract.getERC721AssetSymbol( + const symbol = await messenger.call( + `AssetsContractController:getERC721AssetSymbol`, ERC721_GODS_ADDRESS, 'mainnet', ); @@ -542,9 +556,10 @@ describe('AssetsContractController with NetworkClientId', () => { }); it('should throw error when getting ERC-721 NFT symbol when networkClientId is invalid', async () => { - const { assetsContract, messenger } = await setupAssetContractControllers(); + const { messenger } = await setupAssetContractControllers(); await expect( - assetsContract.getERC721AssetSymbol( + messenger.call( + `AssetsContractController:getERC721AssetSymbol`, ERC721_GODS_ADDRESS, 'invalidNetworkClientId', ), @@ -553,7 +568,7 @@ describe('AssetsContractController with NetworkClientId', () => { }); it('should get ERC-20 token decimals', async () => { - const { assetsContract, messenger, networkClientConfiguration } = + const { messenger, networkClientConfiguration } = await setupAssetContractControllers(); mockNetworkWithDefaultChainId({ networkClientConfiguration, @@ -576,7 +591,8 @@ describe('AssetsContractController with NetworkClientId', () => { }, ], }); - const decimals = await assetsContract.getERC20TokenDecimals( + const decimals = await messenger.call( + `AssetsContractController:getERC20TokenDecimals`, ERC20_SAI_ADDRESS, 'mainnet', ); @@ -585,7 +601,7 @@ describe('AssetsContractController with NetworkClientId', () => { }); it('should get ERC-20 token name', async () => { - const { assetsContract, messenger, networkClientConfiguration } = + const { messenger, networkClientConfiguration } = await setupAssetContractControllers(); mockNetworkWithDefaultChainId({ networkClientConfiguration, @@ -609,7 +625,8 @@ describe('AssetsContractController with NetworkClientId', () => { ], }); - const name = await assetsContract.getERC20TokenName( + const name = await messenger.call( + `AssetsContractController:getERC20TokenName`, ERC20_DAI_ADDRESS, 'mainnet', ); @@ -619,7 +636,7 @@ describe('AssetsContractController with NetworkClientId', () => { }); it('should get ERC-721 NFT ownership', async () => { - const { assetsContract, messenger, networkClientConfiguration } = + const { messenger, networkClientConfiguration } = await setupAssetContractControllers(); mockNetworkWithDefaultChainId({ networkClientConfiguration, @@ -642,7 +659,8 @@ describe('AssetsContractController with NetworkClientId', () => { }, ], }); - const tokenId = await assetsContract.getERC721OwnerOf( + const tokenId = await messenger.call( + `AssetsContractController:getERC721OwnerOf`, ERC721_GODS_ADDRESS, '148332', 'mainnet', @@ -652,9 +670,10 @@ describe('AssetsContractController with NetworkClientId', () => { }); it('should throw error when getting ERC-721 NFT ownership using networkClientId that is invalid', async () => { - const { assetsContract, messenger } = await setupAssetContractControllers(); + const { messenger } = await setupAssetContractControllers(); await expect( - assetsContract.getERC721OwnerOf( + messenger.call( + `AssetsContractController:getERC721OwnerOf`, ERC721_GODS_ADDRESS, '148332', 'invalidNetworkClientId', @@ -664,7 +683,7 @@ describe('AssetsContractController with NetworkClientId', () => { }); it('should get balance of ERC-20 token in a single call on network with token detection support', async () => { - const { assetsContract, messenger, networkClientConfiguration } = + const { messenger, networkClientConfiguration } = await setupAssetContractControllers(); mockNetworkWithDefaultChainId({ networkClientConfiguration, @@ -687,7 +706,8 @@ describe('AssetsContractController with NetworkClientId', () => { }, ], }); - const balances = await assetsContract.getBalancesInSingleCall( + const balances = await messenger.call( + `AssetsContractController:getBalancesInSingleCall`, ERC20_SAI_ADDRESS, [ERC20_SAI_ADDRESS], 'mainnet', @@ -697,7 +717,7 @@ describe('AssetsContractController with NetworkClientId', () => { }); it('should not have balance in a single call after switching to network without token detection support', async () => { - const { assetsContract, messenger, networkClientConfiguration } = + const { messenger, networkClientConfiguration } = await setupAssetContractControllers(); mockNetworkWithDefaultChainId({ networkClientConfiguration, @@ -751,14 +771,16 @@ describe('AssetsContractController with NetworkClientId', () => { ], }); - const balances = await assetsContract.getBalancesInSingleCall( + const balances = await messenger.call( + `AssetsContractController:getBalancesInSingleCall`, ERC20_SAI_ADDRESS, [ERC20_SAI_ADDRESS], 'mainnet', ); expect(balances[ERC20_SAI_ADDRESS]).toBeDefined(); - const noBalances = await assetsContract.getBalancesInSingleCall( + const noBalances = await messenger.call( + `AssetsContractController:getBalancesInSingleCall`, ERC20_SAI_ADDRESS, [ERC20_SAI_ADDRESS], 'sepolia', @@ -768,9 +790,10 @@ describe('AssetsContractController with NetworkClientId', () => { }); it('should throw error when transferring single ERC-1155 when networkClientId is invalid', async () => { - const { assetsContract, messenger } = await setupAssetContractControllers(); + const { messenger } = await setupAssetContractControllers(); await expect( - assetsContract.transferSingleERC1155( + messenger.call( + `AssetsContractController:transferSingleERC1155`, ERC1155_ADDRESS, TEST_ACCOUNT_PUBLIC_ADDRESS, TEST_ACCOUNT_PUBLIC_ADDRESS, @@ -783,7 +806,7 @@ describe('AssetsContractController with NetworkClientId', () => { }); it('should get the balance of a ERC-1155 NFT for a given address', async () => { - const { assetsContract, messenger, networkClientConfiguration } = + const { messenger, networkClientConfiguration } = await setupAssetContractControllers(); mockNetworkWithDefaultChainId({ networkClientConfiguration, @@ -806,7 +829,8 @@ describe('AssetsContractController with NetworkClientId', () => { }, ], }); - const balance = await assetsContract.getERC1155BalanceOf( + const balance = await messenger.call( + `AssetsContractController:getERC1155BalanceOf`, TEST_ACCOUNT_PUBLIC_ADDRESS, ERC1155_ADDRESS, ERC1155_ID, @@ -817,9 +841,10 @@ describe('AssetsContractController with NetworkClientId', () => { }); it('should throw error when getting the balance of a ERC-1155 NFT when networkClientId is invalid', async () => { - const { assetsContract, messenger } = await setupAssetContractControllers(); + const { messenger } = await setupAssetContractControllers(); await expect( - assetsContract.getERC1155BalanceOf( + messenger.call( + `AssetsContractController:getERC1155BalanceOf`, TEST_ACCOUNT_PUBLIC_ADDRESS, ERC1155_ADDRESS, ERC1155_ID, @@ -830,7 +855,7 @@ describe('AssetsContractController with NetworkClientId', () => { }); it('should get the URI of a ERC-1155 NFT', async () => { - const { assetsContract, messenger, networkClientConfiguration } = + const { messenger, networkClientConfiguration } = await setupAssetContractControllers(); mockNetworkWithDefaultChainId({ networkClientConfiguration, @@ -854,7 +879,8 @@ describe('AssetsContractController with NetworkClientId', () => { ], }); const expectedUri = `https://api.opensea.io/api/v1/metadata/${ERC1155_ADDRESS}/0x{id}`; - const uri = await assetsContract.getERC1155TokenURI( + const uri = await messenger.call( + `AssetsContractController:getERC1155TokenURI`, ERC1155_ADDRESS, ERC1155_ID, 'mainnet', diff --git a/packages/assets-controllers/src/NftController.test.ts b/packages/assets-controllers/src/NftController.test.ts index 05e29616432..09cd56d82db 100644 --- a/packages/assets-controllers/src/NftController.test.ts +++ b/packages/assets-controllers/src/NftController.test.ts @@ -1,15 +1,6 @@ import type { Network } from '@ethersproject/providers'; -import type { - AccountsControllerGetAccountAction, - AccountsControllerGetSelectedAccountAction, - AccountsControllerSelectedAccountChangeEvent, - AccountsControllerSelectedEvmAccountChangeEvent, -} from '@metamask/accounts-controller'; -import type { - AddApprovalRequest, - ApprovalStateChange, - ApprovalControllerMessenger, -} from '@metamask/approval-controller'; +import type { AccountsControllerSelectedAccountChangeEvent } from '@metamask/accounts-controller'; +import type { ApprovalControllerMessenger } from '@metamask/approval-controller'; import { ApprovalController } from '@metamask/approval-controller'; import { ControllerMessenger } from '@metamask/base-controller'; import { @@ -29,11 +20,8 @@ import type { InternalAccount } from '@metamask/keyring-api'; import type { NetworkClientConfiguration, NetworkClientId, - NetworkControllerGetNetworkClientByIdAction, - NetworkControllerNetworkDidChangeEvent, } from '@metamask/network-controller'; import { defaultState as defaultNetworkState } from '@metamask/network-controller'; -import type { PreferencesControllerStateChangeEvent } from '@metamask/preferences-controller'; import { getDefaultPreferencesState, type PreferencesState, @@ -52,6 +40,14 @@ import { buildCustomNetworkClientConfiguration, buildMockGetNetworkClientById, } from '../../network-controller/tests/helpers'; +import type { + AssetsContractControllerGetERC1155BalanceOfAction, + AssetsContractControllerGetERC1155TokenURIAction, + AssetsContractControllerGetERC721AssetNameAction, + AssetsContractControllerGetERC721AssetSymbolAction, + AssetsContractControllerGetERC721OwnerOfAction, + AssetsContractControllerGetERC721TokenURIAction, +} from './AssetsContractController'; import { getFormattedIpfsUrl } from './assetsUtil'; import { Source } from './constants'; import type { @@ -104,17 +100,6 @@ const GOERLI = { ticker: NetworksTicker.goerli, }; -type ApprovalActions = - | AddApprovalRequest - | AccountsControllerGetAccountAction - | AccountsControllerGetSelectedAccountAction - | NetworkControllerGetNetworkClientByIdAction; -type ApprovalEvents = - | ApprovalStateChange - | PreferencesControllerStateChangeEvent - | NetworkControllerNetworkDidChangeEvent - | AccountsControllerSelectedEvmAccountChangeEvent; - const controllerName = 'NftController' as const; // Mock out detectNetwork function for cleaner tests, Ethers calls this a bunch of times because the Web3Provider is paranoid. @@ -148,6 +133,18 @@ jest.mock('uuid', () => { * * @param args - Arguments to this function. * @param args.options - Controller options. + * @param args.getERC721AssetName - Used to construct mock versions of the + * `AssetsContractController:getERC721AssetName` action. + * @param args.getERC721AssetSymbol - Used to construct mock versions of the + * `AssetsContractController:getERC721AssetSymbol` action. + * @param args.getERC721TokenURI - Used to construct mock versions of the + * `AssetsContractController:getERC721TokenURI` action. + * @param args.getERC721OwnerOf - Used to construct mock versions of the + * `AssetsContractController:getERC721OwnerOf` action. + * @param args.getERC1155BalanceOf - Used to construct mock versions of the + * `AssetsContractController:getERC1155BalanceOf` action. + * @param args.getERC1155TokenURI - Used to construct mock versions of the + * `AssetsContractController:getERC1155TokenURI` action. * @param args.mockNetworkClientConfigurationsByNetworkClientId - Used to construct * mock versions of network clients and ultimately mock the * `NetworkController:getNetworkClientById` action. @@ -156,10 +153,40 @@ jest.mock('uuid', () => { */ function setupController({ options = {}, + getERC721AssetName, + getERC721AssetSymbol, + getERC721TokenURI, + getERC721OwnerOf, + getERC1155BalanceOf, + getERC1155TokenURI, mockNetworkClientConfigurationsByNetworkClientId = {}, defaultSelectedAccount = OWNER_ACCOUNT, }: { options?: Partial[0]>; + getERC721AssetName?: jest.Mock< + ReturnType, + Parameters + >; + getERC721AssetSymbol?: jest.Mock< + ReturnType, + Parameters + >; + getERC721TokenURI?: jest.Mock< + ReturnType, + Parameters + >; + getERC721OwnerOf?: jest.Mock< + ReturnType, + Parameters + >; + getERC1155BalanceOf?: jest.Mock< + ReturnType, + Parameters + >; + getERC1155TokenURI?: jest.Mock< + ReturnType, + Parameters + >; mockNetworkClientConfigurationsByNetworkClientId?: Record< NetworkClientId, NetworkClientConfiguration @@ -202,6 +229,72 @@ function setupController({ mockGetSelectedAccount, ); + const mockGetERC721AssetName = + getERC721AssetName ?? + jest.fn< + ReturnType, + Parameters + >(); + messenger.registerActionHandler( + 'AssetsContractController:getERC721AssetName', + mockGetERC721AssetName, + ); + + const mockGetERC721AssetSymbol = + getERC721AssetSymbol ?? + jest.fn< + ReturnType, + Parameters + >(); + messenger.registerActionHandler( + 'AssetsContractController:getERC721AssetSymbol', + mockGetERC721AssetSymbol, + ); + + const mockGetERC721TokenURI = + getERC721TokenURI ?? + jest.fn< + ReturnType, + Parameters + >(); + messenger.registerActionHandler( + 'AssetsContractController:getERC721TokenURI', + mockGetERC721TokenURI, + ); + + const mockGetERC721OwnerOf = + getERC721OwnerOf ?? + jest.fn< + ReturnType, + Parameters + >(); + messenger.registerActionHandler( + 'AssetsContractController:getERC721OwnerOf', + mockGetERC721OwnerOf, + ); + + const mockGetERC1155BalanceOf = + getERC1155BalanceOf ?? + jest.fn< + ReturnType, + Parameters + >(); + messenger.registerActionHandler( + 'AssetsContractController:getERC1155BalanceOf', + mockGetERC1155BalanceOf, + ); + + const mockGetERC1155TokenURI = + getERC1155TokenURI ?? + jest.fn< + ReturnType, + Parameters + >(); + messenger.registerActionHandler( + 'AssetsContractController:getERC1155TokenURI', + mockGetERC1155TokenURI, + ); + const approvalControllerMessenger = messenger.getRestricted({ name: 'ApprovalController', allowedActions: [], @@ -213,25 +306,21 @@ function setupController({ showApprovalRequest: jest.fn(), }); - const nftControllerMessenger = messenger.getRestricted< - typeof controllerName, - ApprovalActions['type'], - Extract< - ApprovalEvents, - | PreferencesControllerStateChangeEvent - | AccountsControllerSelectedEvmAccountChangeEvent - | NetworkControllerNetworkDidChangeEvent - >['type'] - >({ + const nftControllerMessenger = messenger.getRestricted({ name: controllerName, allowedActions: [ 'ApprovalController:addRequest', 'AccountsController:getSelectedAccount', 'AccountsController:getAccount', 'NetworkController:getNetworkClientById', + 'AssetsContractController:getERC721AssetName', + 'AssetsContractController:getERC721AssetSymbol', + 'AssetsContractController:getERC721TokenURI', + 'AssetsContractController:getERC721OwnerOf', + 'AssetsContractController:getERC1155BalanceOf', + 'AssetsContractController:getERC1155TokenURI', ], allowedEvents: [ - // @ts-expect-error - Adding this for test 'AccountsController:selectedAccountChange', 'AccountsController:selectedEvmAccountChange', 'PreferencesController:stateChange', @@ -241,13 +330,8 @@ function setupController({ const nftController = new NftController({ chainId: ChainId.mainnet, - getERC721AssetName: jest.fn(), - getERC721AssetSymbol: jest.fn(), - getERC721TokenURI: jest.fn(), - getERC721OwnerOf: jest.fn(), - getERC1155BalanceOf: jest.fn(), - getERC1155TokenURI: jest.fn(), onNftAdded: jest.fn(), + // @ts-expect-error - Added incompatible event `AccountsController:selectedAccountChange` to allowlist for testing purposes messenger: nftControllerMessenger, ...options, }); @@ -292,6 +376,12 @@ function setupController({ triggerSelectedAccountChange, mockGetAccount, mockGetSelectedAccount, + mockGetERC1155BalanceOf, + mockGetERC1155TokenURI, + mockGetERC721AssetName, + mockGetERC721AssetSymbol, + mockGetERC721OwnerOf, + mockGetERC721TokenURI, }; } @@ -406,12 +496,10 @@ describe('NftController', () => { }), ); const { nftController } = setupController({ - options: { - getERC721TokenURI: jest - .fn() - .mockImplementation(() => 'https://testtokenuri.com'), - getERC721OwnerOf: jest.fn().mockImplementation(() => OWNER_ADDRESS), - }, + getERC721TokenURI: jest + .fn() + .mockImplementation(() => 'https://testtokenuri.com'), + getERC721OwnerOf: jest.fn().mockImplementation(() => OWNER_ADDRESS), }); // eslint-disable-next-line @typescript-eslint/ban-ts-comment @@ -483,9 +571,7 @@ describe('NftController', () => { it('should error if the user does not own the suggested ERC721 NFT', async function () { const { nftController, messenger } = setupController({ - options: { - getERC721OwnerOf: jest.fn().mockImplementation(() => '0x12345abcefg'), - }, + getERC721OwnerOf: jest.fn().mockImplementation(() => '0x12345abcefg'), }); const callActionSpy = jest.spyOn(messenger, 'call'); @@ -518,9 +604,7 @@ describe('NftController', () => { it('should error if the user does not own the suggested ERC1155 NFT', async function () { const { nftController, messenger } = setupController({ - options: { - getERC1155BalanceOf: jest.fn().mockImplementation(() => new BN(0)), - }, + getERC1155BalanceOf: jest.fn().mockImplementation(() => new BN(0)), }); const callActionSpy = jest.spyOn(messenger, 'call'); @@ -529,7 +613,11 @@ describe('NftController', () => { nftController.watchNft(ERC1155_NFT, ERC1155, 'https://test-dapp.com'), ).rejects.toThrow('Suggested NFT is not owned by the selected account'); // First call is to get InternalAccount - expect(callActionSpy).toHaveBeenCalledTimes(1); + expect(callActionSpy).toHaveBeenNthCalledWith( + 1, + 'AccountsController:getAccount', + expect.any(String), + ); }); it('should handle ERC721 type and add pending request to ApprovalController with the OpenSea API disabled and IPFS gateway enabled', async function () { @@ -549,12 +637,10 @@ describe('NftController', () => { triggerPreferencesStateChange, triggerSelectedAccountChange, } = setupController({ - options: { - getERC721TokenURI: jest - .fn() - .mockImplementation(() => 'https://testtokenuri.com'), - getERC721OwnerOf: jest.fn().mockImplementation(() => OWNER_ADDRESS), - }, + getERC721TokenURI: jest + .fn() + .mockImplementation(() => 'https://testtokenuri.com'), + getERC721OwnerOf: jest.fn().mockImplementation(() => OWNER_ADDRESS), }); triggerSelectedAccountChange(OWNER_ACCOUNT); triggerPreferencesStateChange({ @@ -620,12 +706,10 @@ describe('NftController', () => { triggerPreferencesStateChange, triggerSelectedAccountChange, } = setupController({ - options: { - getERC721TokenURI: jest - .fn() - .mockImplementation(() => 'https://testtokenuri.com'), - getERC721OwnerOf: jest.fn().mockImplementation(() => OWNER_ADDRESS), - }, + getERC721TokenURI: jest + .fn() + .mockImplementation(() => 'https://testtokenuri.com'), + getERC721OwnerOf: jest.fn().mockImplementation(() => OWNER_ADDRESS), }); triggerSelectedAccountChange(OWNER_ACCOUNT); triggerPreferencesStateChange({ @@ -691,12 +775,10 @@ describe('NftController', () => { triggerPreferencesStateChange, triggerSelectedAccountChange, } = setupController({ - options: { - getERC721TokenURI: jest - .fn() - .mockImplementation(() => 'ipfs://testtokenuri.com'), - getERC721OwnerOf: jest.fn().mockImplementation(() => OWNER_ADDRESS), - }, + getERC721TokenURI: jest + .fn() + .mockImplementation(() => 'ipfs://testtokenuri.com'), + getERC721OwnerOf: jest.fn().mockImplementation(() => OWNER_ADDRESS), }); triggerSelectedAccountChange(OWNER_ACCOUNT); triggerPreferencesStateChange({ @@ -762,12 +844,10 @@ describe('NftController', () => { triggerPreferencesStateChange, triggerSelectedAccountChange, } = setupController({ - options: { - getERC721TokenURI: jest - .fn() - .mockImplementation(() => 'ipfs://testtokenuri.com'), - getERC721OwnerOf: jest.fn().mockImplementation(() => OWNER_ADDRESS), - }, + getERC721TokenURI: jest + .fn() + .mockImplementation(() => 'ipfs://testtokenuri.com'), + getERC721OwnerOf: jest.fn().mockImplementation(() => OWNER_ADDRESS), }); triggerSelectedAccountChange(OWNER_ACCOUNT); @@ -835,15 +915,13 @@ describe('NftController', () => { triggerPreferencesStateChange, triggerSelectedAccountChange, } = setupController({ - options: { - getERC721TokenURI: jest - .fn() - .mockRejectedValue(new Error('Not an ERC721 contract')), - getERC1155TokenURI: jest - .fn() - .mockImplementation(() => 'https://testtokenuri.com'), - getERC1155BalanceOf: jest.fn().mockImplementation(() => new BN(1)), - }, + getERC721TokenURI: jest + .fn() + .mockRejectedValue(new Error('Not an ERC721 contract')), + getERC1155TokenURI: jest + .fn() + .mockImplementation(() => 'https://testtokenuri.com'), + getERC1155BalanceOf: jest.fn().mockImplementation(() => new BN(1)), }); triggerSelectedAccountChange(OWNER_ACCOUNT); @@ -910,15 +988,13 @@ describe('NftController', () => { const { nftController, messenger, triggerPreferencesStateChange } = setupController({ - options: { - getERC721TokenURI: jest - .fn() - .mockRejectedValue(new Error('Not an ERC721 contract')), - getERC1155TokenURI: jest - .fn() - .mockImplementation(() => 'https://testtokenuri.com'), - getERC1155BalanceOf: jest.fn().mockImplementation(() => new BN(1)), - }, + getERC721TokenURI: jest + .fn() + .mockRejectedValue(new Error('Not an ERC721 contract')), + getERC1155TokenURI: jest + .fn() + .mockImplementation(() => 'https://testtokenuri.com'), + getERC1155BalanceOf: jest.fn().mockImplementation(() => new BN(1)), }); triggerPreferencesStateChange({ ...getDefaultPreferencesState(), @@ -989,20 +1065,18 @@ describe('NftController', () => { triggerPreferencesStateChange, triggerSelectedAccountChange, } = setupController({ - options: { - getERC721OwnerOf: jest - .fn() - .mockImplementation(() => SECOND_OWNER_ADDRESS), - getERC721TokenURI: jest - .fn() - .mockImplementation(() => 'https://testtokenuri.com'), - getERC721AssetName: jest - .fn() - .mockImplementation(() => 'testERC721Name'), - getERC721AssetSymbol: jest - .fn() - .mockImplementation(() => 'testERC721Symbol'), - }, + getERC721OwnerOf: jest + .fn() + .mockImplementation(() => SECOND_OWNER_ADDRESS), + getERC721TokenURI: jest + .fn() + .mockImplementation(() => 'https://testtokenuri.com'), + getERC721AssetName: jest + .fn() + .mockImplementation(() => 'testERC721Name'), + getERC721AssetSymbol: jest + .fn() + .mockImplementation(() => 'testERC721Symbol'), }); const requestId = 'approval-request-id-1'; @@ -1095,18 +1169,16 @@ describe('NftController', () => { triggerSelectedAccountChange, changeNetwork, } = setupController({ - options: { - getERC721OwnerOf: jest.fn().mockImplementation(() => OWNER_ADDRESS), - getERC721TokenURI: jest - .fn() - .mockImplementation(() => 'https://testtokenuri.com'), - getERC721AssetName: jest - .fn() - .mockImplementation(() => 'testERC721Name'), - getERC721AssetSymbol: jest - .fn() - .mockImplementation(() => 'testERC721Symbol'), - }, + getERC721OwnerOf: jest.fn().mockImplementation(() => OWNER_ADDRESS), + getERC721TokenURI: jest + .fn() + .mockImplementation(() => 'https://testtokenuri.com'), + getERC721AssetName: jest + .fn() + .mockImplementation(() => 'testERC721Name'), + getERC721AssetSymbol: jest + .fn() + .mockImplementation(() => 'testERC721Symbol'), }); const requestId = 'approval-request-id-1'; @@ -1218,8 +1290,8 @@ describe('NftController', () => { const { nftController } = setupController({ options: { chainId: ChainId.mainnet, - getERC721AssetName: jest.fn().mockResolvedValue('Name'), }, + getERC721AssetName: jest.fn().mockResolvedValue('Name'), }); await nftController.addNft('0x01', '1', { @@ -1332,10 +1404,8 @@ describe('NftController', () => { triggerSelectedAccountChange, mockGetAccount, } = setupController({ - options: { - getERC721TokenURI: mockGetERC721TokenURI, - getERC1155TokenURI: mockGetERC1155TokenURI, - }, + getERC721TokenURI: mockGetERC721TokenURI, + getERC1155TokenURI: mockGetERC1155TokenURI, }); const firstAddress = '0x123'; const firstAccount = createMockInternalAccount({ address: firstAddress }); @@ -1386,7 +1456,6 @@ describe('NftController', () => { it('should update NFT if image is different', async () => { const { nftController } = setupController({ - options: {}, defaultSelectedAccount: OWNER_ACCOUNT, }); @@ -1439,7 +1508,6 @@ describe('NftController', () => { it('should not duplicate NFT nor NFT contract if already added', async () => { const { nftController } = setupController({ - options: {}, defaultSelectedAccount: OWNER_ACCOUNT, }); @@ -1476,14 +1544,12 @@ describe('NftController', () => { it('should add NFT and get information from NFT-API', async () => { const { nftController } = setupController({ - options: { - getERC721TokenURI: jest - .fn() - .mockRejectedValue(new Error('Not an ERC721 contract')), - getERC1155TokenURI: jest - .fn() - .mockRejectedValue(new Error('Not an ERC1155 contract')), - }, + getERC721TokenURI: jest + .fn() + .mockRejectedValue(new Error('Not an ERC721 contract')), + getERC1155TokenURI: jest + .fn() + .mockRejectedValue(new Error('Not an ERC1155 contract')), defaultSelectedAccount: OWNER_ACCOUNT, }); @@ -1507,15 +1573,13 @@ describe('NftController', () => { it('should add NFT erc721 and aggregate NFT data from both contract and NFT-API', async () => { const { nftController } = setupController({ - options: { - getERC721AssetName: jest.fn().mockResolvedValue('KudosToken'), - getERC721AssetSymbol: jest.fn().mockResolvedValue('KDO'), - getERC721TokenURI: jest - .fn() - .mockResolvedValue( - 'https://ipfs.gitcoin.co:443/api/v0/cat/QmPmt6EAaioN78ECnW5oCL8v2YvVSpoBjLCjrXhhsAvoov', - ), - }, + getERC721AssetName: jest.fn().mockResolvedValue('KudosToken'), + getERC721AssetSymbol: jest.fn().mockResolvedValue('KDO'), + getERC721TokenURI: jest + .fn() + .mockResolvedValue( + 'https://ipfs.gitcoin.co:443/api/v0/cat/QmPmt6EAaioN78ECnW5oCL8v2YvVSpoBjLCjrXhhsAvoov', + ), defaultSelectedAccount: OWNER_ACCOUNT, }); nock(NFT_API_BASE_URL) @@ -1574,16 +1638,14 @@ describe('NftController', () => { it('should add NFT erc1155 and get NFT information from contract when NFT API call fail', async () => { const { nftController } = setupController({ - options: { - getERC721TokenURI: jest - .fn() - .mockRejectedValue(new Error('Not a 721 contract')), - getERC1155TokenURI: jest - .fn() - .mockResolvedValue( - 'https://api.opensea.io/api/v1/metadata/0x495f947276749Ce646f68AC8c248420045cb7b5e/0x{id}', - ), - }, + getERC721TokenURI: jest + .fn() + .mockRejectedValue(new Error('Not a 721 contract')), + getERC1155TokenURI: jest + .fn() + .mockResolvedValue( + 'https://api.opensea.io/api/v1/metadata/0x495f947276749Ce646f68AC8c248420045cb7b5e/0x{id}', + ), defaultSelectedAccount: OWNER_ACCOUNT, }); nock('https://api.opensea.io') @@ -1622,18 +1684,16 @@ describe('NftController', () => { it('should add NFT erc721 and get NFT information only from contract', async () => { const { nftController } = setupController({ - options: { - getERC721AssetName: jest.fn().mockResolvedValue('KudosToken'), - getERC721AssetSymbol: jest.fn().mockResolvedValue('KDO'), - getERC721TokenURI: jest.fn().mockImplementation((tokenAddress) => { - switch (tokenAddress) { - case ERC721_KUDOSADDRESS: - return 'https://ipfs.gitcoin.co:443/api/v0/cat/QmPmt6EAaioN78ECnW5oCL8v2YvVSpoBjLCjrXhhsAvoov'; - default: - throw new Error('Not an ERC721 token'); - } - }), - }, + getERC721AssetName: jest.fn().mockResolvedValue('KudosToken'), + getERC721AssetSymbol: jest.fn().mockResolvedValue('KDO'), + getERC721TokenURI: jest.fn().mockImplementation((tokenAddress) => { + switch (tokenAddress) { + case ERC721_KUDOSADDRESS: + return 'https://ipfs.gitcoin.co:443/api/v0/cat/QmPmt6EAaioN78ECnW5oCL8v2YvVSpoBjLCjrXhhsAvoov'; + default: + throw new Error('Not an ERC721 token'); + } + }), defaultSelectedAccount: OWNER_ACCOUNT, }); nock('https://ipfs.gitcoin.co:443') @@ -1683,9 +1743,7 @@ describe('NftController', () => { const tokenURI = 'https://url/'; const mockGetERC721TokenURI = jest.fn().mockResolvedValue(tokenURI); const { nftController, changeNetwork } = setupController({ - options: { - getERC721TokenURI: mockGetERC721TokenURI, - }, + getERC721TokenURI: mockGetERC721TokenURI, defaultSelectedAccount: OWNER_ACCOUNT, }); nock('https://url').get('/').reply(200, { @@ -1731,10 +1789,10 @@ describe('NftController', () => { const { nftController } = setupController({ options: { onNftAdded: mockOnNftAdded, - getERC721AssetSymbol: mockGetERC721AssetSymbol, - getERC721AssetName: mockGetERC721AssetName, - getERC721TokenURI: mockGetERC721TokenURI, }, + getERC721AssetSymbol: mockGetERC721AssetSymbol, + getERC721AssetName: mockGetERC721AssetName, + getERC721TokenURI: mockGetERC721TokenURI, defaultSelectedAccount: OWNER_ACCOUNT, }); @@ -1793,10 +1851,10 @@ describe('NftController', () => { const { nftController, changeNetwork } = setupController({ options: { onNftAdded: mockOnNftAdded, - getERC721AssetSymbol: mockGetERC721AssetSymbol, - getERC721AssetName: mockGetERC721AssetName, - getERC721TokenURI: mockGetERC721TokenURI, }, + getERC721AssetSymbol: mockGetERC721AssetSymbol, + getERC721AssetName: mockGetERC721AssetName, + getERC721TokenURI: mockGetERC721TokenURI, }); nock('https://url').get('/').reply(200, { name: 'name', @@ -1853,13 +1911,13 @@ describe('NftController', () => { const { nftController } = setupController({ options: { onNftAdded: mockOnNftAdded, - getERC721AssetName: jest - .fn() - .mockRejectedValue(new Error('Failed to fetch')), - getERC721AssetSymbol: jest - .fn() - .mockRejectedValue(new Error('Failed to fetch')), }, + getERC721AssetName: jest + .fn() + .mockRejectedValue(new Error('Failed to fetch')), + getERC721AssetSymbol: jest + .fn() + .mockRejectedValue(new Error('Failed to fetch')), defaultSelectedAccount: OWNER_ACCOUNT, }); nock(NFT_API_BASE_URL) @@ -1956,13 +2014,13 @@ describe('NftController', () => { const { nftController } = setupController({ options: { onNftAdded: mockOnNftAdded, - getERC721AssetName: jest - .fn() - .mockRejectedValue(new Error('Failed to fetch')), - getERC721AssetSymbol: jest - .fn() - .mockRejectedValue(new Error('Failed to fetch')), }, + getERC721AssetName: jest + .fn() + .mockRejectedValue(new Error('Failed to fetch')), + getERC721AssetSymbol: jest + .fn() + .mockRejectedValue(new Error('Failed to fetch')), defaultSelectedAccount: OWNER_ACCOUNT, }); nock(NFT_API_BASE_URL) @@ -1990,7 +2048,6 @@ describe('NftController', () => { it('should not add duplicate NFTs to the ignoredNfts list', async () => { const { nftController } = setupController({ - options: {}, defaultSelectedAccount: OWNER_ACCOUNT, }); @@ -2047,23 +2104,21 @@ describe('NftController', () => { it('should add NFT with metadata hosted in IPFS', async () => { const { nftController, triggerPreferencesStateChange, mockGetAccount } = setupController({ - options: { - getERC721AssetName: jest - .fn() - .mockResolvedValue("Maltjik.jpg's Depressionists"), - getERC721AssetSymbol: jest.fn().mockResolvedValue('DPNS'), - getERC721TokenURI: jest.fn().mockImplementation((tokenAddress) => { - switch (tokenAddress) { - case ERC721_DEPRESSIONIST_ADDRESS: - return `ipfs://${DEPRESSIONIST_CID_V1}`; - default: - throw new Error('Not an ERC721 token'); - } - }), - getERC1155TokenURI: jest - .fn() - .mockRejectedValue(new Error('Not an ERC1155 token')), - }, + getERC721AssetName: jest + .fn() + .mockResolvedValue("Maltjik.jpg's Depressionists"), + getERC721AssetSymbol: jest.fn().mockResolvedValue('DPNS'), + getERC721TokenURI: jest.fn().mockImplementation((tokenAddress) => { + switch (tokenAddress) { + case ERC721_DEPRESSIONIST_ADDRESS: + return `ipfs://${DEPRESSIONIST_CID_V1}`; + default: + throw new Error('Not an ERC721 token'); + } + }), + getERC1155TokenURI: jest + .fn() + .mockRejectedValue(new Error('Not an ERC1155 token')), }); mockGetAccount.mockReturnValue(OWNER_ACCOUNT); triggerPreferencesStateChange({ @@ -2162,26 +2217,24 @@ describe('NftController', () => { ); const { nftController } = setupController({ - options: { - getERC721TokenURI: jest.fn().mockImplementation((tokenAddress) => { - switch (tokenAddress) { - case '0x01': - return 'https://testtokenuri-1.com'; - case '0x02': - return 'https://testtokenuri-2.com'; - default: - throw new Error('Not an ERC721 token'); - } - }), - getERC1155TokenURI: jest.fn().mockImplementation((tokenAddress) => { - switch (tokenAddress) { - case '0x03': - return 'https://testtokenuri-3.com'; - default: - throw new Error('Not an ERC1155 token'); - } - }), - }, + getERC721TokenURI: jest.fn().mockImplementation((tokenAddress) => { + switch (tokenAddress) { + case '0x01': + return 'https://testtokenuri-1.com'; + case '0x02': + return 'https://testtokenuri-2.com'; + default: + throw new Error('Not an ERC721 token'); + } + }), + getERC1155TokenURI: jest.fn().mockImplementation((tokenAddress) => { + switch (tokenAddress) { + case '0x03': + return 'https://testtokenuri-3.com'; + default: + throw new Error('Not an ERC1155 token'); + } + }), mockNetworkClientConfigurationsByNetworkClientId: { 'customNetworkClientId-1': buildCustomNetworkClientConfiguration({ chainId: '0xa', @@ -2282,26 +2335,24 @@ describe('NftController', () => { ); const { nftController, changeNetwork } = setupController({ - options: { - getERC721TokenURI: jest.fn().mockImplementation((tokenAddress) => { - switch (tokenAddress) { - case '0x01': - return 'https://testtokenuri-1.com'; - case '0x02': - return 'https://testtokenuri-2.com'; - default: - throw new Error('Not an ERC721 token'); - } - }), - getERC1155TokenURI: jest.fn().mockImplementation((tokenAddress) => { - switch (tokenAddress) { - case '0x03': - return 'https://testtokenuri-3.com'; - default: - throw new Error('Not an ERC1155 token'); - } - }), - }, + getERC721TokenURI: jest.fn().mockImplementation((tokenAddress) => { + switch (tokenAddress) { + case '0x01': + return 'https://testtokenuri-1.com'; + case '0x02': + return 'https://testtokenuri-2.com'; + default: + throw new Error('Not an ERC721 token'); + } + }), + getERC1155TokenURI: jest.fn().mockImplementation((tokenAddress) => { + switch (tokenAddress) { + case '0x03': + return 'https://testtokenuri-3.com'; + default: + throw new Error('Not an ERC1155 token'); + } + }), }); await nftController.addNft('0x01', '1234', { @@ -2368,8 +2419,8 @@ describe('NftController', () => { const { nftController, mockGetAccount } = setupController({ options: { chainId: ChainId.mainnet, - getERC721AssetName: jest.fn().mockResolvedValue('Name'), }, + getERC721AssetName: jest.fn().mockResolvedValue('Name'), }); mockGetAccount.mockReturnValue(null); @@ -2403,9 +2454,7 @@ describe('NftController', () => { triggerPreferencesStateChange, triggerSelectedAccountChange, } = setupController({ - options: { - getERC721TokenURI: mockGetERC721TokenURI, - }, + getERC721TokenURI: mockGetERC721TokenURI, }); const firstAddress = '0x123'; const firstAccount = createMockInternalAccount({ @@ -2493,9 +2542,7 @@ describe('NftController', () => { mockGetAccount, triggerSelectedAccountChange, } = setupController({ - options: { - getERC721TokenURI: mockGetERC721TokenURI, - }, + getERC721TokenURI: mockGetERC721TokenURI, }); const firstAddress = '0x123'; @@ -2575,9 +2622,7 @@ describe('NftController', () => { triggerPreferencesStateChange, triggerSelectedAccountChange, } = setupController({ - options: { - getERC721TokenURI: mockGetERC721TokenURI, - }, + getERC721TokenURI: mockGetERC721TokenURI, }); // Ensure that the currently selected address is not the same as either of the userAddresses triggerSelectedAccountChange(OWNER_ACCOUNT); @@ -2640,7 +2685,6 @@ describe('NftController', () => { describe('removeNft', () => { it('should remove NFT and NFT contract', async () => { const { nftController } = setupController({ - options: {}, defaultSelectedAccount: OWNER_ACCOUNT, }); @@ -2705,9 +2749,7 @@ describe('NftController', () => { mockGetAccount, triggerSelectedAccountChange, } = setupController({ - options: { - getERC721TokenURI: mockGetERC721TokenURI, - }, + getERC721TokenURI: mockGetERC721TokenURI, }); nock('https://url').get('/').reply(200, { name: 'name', @@ -2765,9 +2807,7 @@ describe('NftController', () => { const tokenURI = 'https://url/'; const mockGetERC721TokenURI = jest.fn().mockResolvedValue(tokenURI); const { nftController, changeNetwork } = setupController({ - options: { - getERC721TokenURI: mockGetERC721TokenURI, - }, + getERC721TokenURI: mockGetERC721TokenURI, defaultSelectedAccount: OWNER_ACCOUNT, }); @@ -2878,7 +2918,6 @@ describe('NftController', () => { it('should be able to clear the ignoredNfts list', async () => { const { nftController } = setupController({ - options: {}, defaultSelectedAccount: OWNER_ACCOUNT, }); @@ -2914,10 +2953,8 @@ describe('NftController', () => { .fn() .mockRejectedValue(new Error('ERC1155 error')); const { nftController } = setupController({ - options: { - getERC721OwnerOf: mockGetERC721OwnerOf, - getERC1155BalanceOf: mockGetERC1155BalanceOf, - }, + getERC721OwnerOf: mockGetERC721OwnerOf, + getERC1155BalanceOf: mockGetERC1155BalanceOf, }); const isOwner = await nftController.isNftOwner( @@ -2935,10 +2972,8 @@ describe('NftController', () => { .fn() .mockRejectedValue(new Error('ERC1155 error')); const { nftController } = setupController({ - options: { - getERC721OwnerOf: mockGetERC721OwnerOf, - getERC1155BalanceOf: mockGetERC1155BalanceOf, - }, + getERC721OwnerOf: mockGetERC721OwnerOf, + getERC1155BalanceOf: mockGetERC1155BalanceOf, }); const isOwner = await nftController.isNftOwner( @@ -2955,10 +2990,8 @@ describe('NftController', () => { .fn() .mockRejectedValue(new Error('ERC1155 error')); const { nftController } = setupController({ - options: { - getERC721OwnerOf: mockGetERC721OwnerOf, - getERC1155BalanceOf: mockGetERC1155BalanceOf, - }, + getERC721OwnerOf: mockGetERC721OwnerOf, + getERC1155BalanceOf: mockGetERC1155BalanceOf, }); const isOwner = await nftController.isNftOwner( @@ -2975,10 +3008,8 @@ describe('NftController', () => { .mockRejectedValue(new Error('ERC721 error')); const mockGetERC1155BalanceOf = jest.fn().mockResolvedValue(new BN(1)); const { nftController } = setupController({ - options: { - getERC721OwnerOf: mockGetERC721OwnerOf, - getERC1155BalanceOf: mockGetERC1155BalanceOf, - }, + getERC721OwnerOf: mockGetERC721OwnerOf, + getERC1155BalanceOf: mockGetERC1155BalanceOf, }); const isOwner = await nftController.isNftOwner( @@ -2995,10 +3026,8 @@ describe('NftController', () => { .mockRejectedValue(new Error('ERC721 error')); const mockGetERC1155BalanceOf = jest.fn().mockResolvedValue(new BN(0)); const { nftController } = setupController({ - options: { - getERC721OwnerOf: mockGetERC721OwnerOf, - getERC1155BalanceOf: mockGetERC1155BalanceOf, - }, + getERC721OwnerOf: mockGetERC721OwnerOf, + getERC1155BalanceOf: mockGetERC1155BalanceOf, }); const isOwner = await nftController.isNftOwner( @@ -3018,10 +3047,8 @@ describe('NftController', () => { .fn() .mockRejectedValue(new Error('ERC1155 error')); const { nftController } = setupController({ - options: { - getERC721OwnerOf: mockGetERC721OwnerOf, - getERC1155BalanceOf: mockGetERC1155BalanceOf, - }, + getERC721OwnerOf: mockGetERC721OwnerOf, + getERC1155BalanceOf: mockGetERC1155BalanceOf, }); const error = "Unable to verify ownership. Possibly because the standard is not supported or the user's currently selected network does not match the chain of the asset in question."; @@ -3041,10 +3068,8 @@ describe('NftController', () => { triggerPreferencesStateChange, triggerSelectedAccountChange, } = setupController({ - options: { - getERC721TokenURI: jest.fn().mockRejectedValue(''), - getERC1155TokenURI: jest.fn().mockResolvedValue('ipfs://*'), - }, + getERC721TokenURI: jest.fn().mockRejectedValue(''), + getERC1155TokenURI: jest.fn().mockResolvedValue('ipfs://*'), defaultSelectedAccount: OWNER_ACCOUNT, }); triggerSelectedAccountChange(OWNER_ACCOUNT); @@ -3075,7 +3100,6 @@ describe('NftController', () => { describe('updateNftFavoriteStatus', () => { it('should not set NFT as favorite if nft not found', async () => { const { nftController } = setupController({ - options: {}, defaultSelectedAccount: OWNER_ACCOUNT, }); @@ -3103,7 +3127,6 @@ describe('NftController', () => { }); it('should set NFT as favorite', async () => { const { nftController } = setupController({ - options: {}, defaultSelectedAccount: OWNER_ACCOUNT, }); @@ -3132,7 +3155,6 @@ describe('NftController', () => { it('should set NFT as favorite and then unset it', async () => { const { nftController } = setupController({ - options: {}, defaultSelectedAccount: OWNER_ACCOUNT, }); @@ -3177,7 +3199,6 @@ describe('NftController', () => { it('should keep the favorite status as true after updating metadata', async () => { const { nftController } = setupController({ - options: {}, defaultSelectedAccount: OWNER_ACCOUNT, }); @@ -3237,7 +3258,6 @@ describe('NftController', () => { it('should keep the favorite status as false after updating metadata', async () => { const { nftController } = setupController({ - options: {}, defaultSelectedAccount: OWNER_ACCOUNT, }); @@ -3368,7 +3388,6 @@ describe('NftController', () => { describe('checkAndUpdateAllNftsOwnershipStatus', () => { it('should check whether NFTs for the current selectedAccount/chainId combination are still owned by the selectedAccount and update the isCurrentlyOwned value to false when NFT is not still owned', async () => { const { nftController } = setupController({ - options: {}, defaultSelectedAccount: OWNER_ACCOUNT, }); jest.spyOn(nftController, 'isNftOwner').mockResolvedValue(false); @@ -3397,7 +3416,6 @@ describe('NftController', () => { it('should check whether NFTs for the current selectedAccount/chainId combination are still owned by the selectedAccount and leave/set the isCurrentlyOwned value to true when NFT is still owned', async () => { const { nftController } = setupController({ - options: {}, defaultSelectedAccount: OWNER_ACCOUNT, }); jest.spyOn(nftController, 'isNftOwner').mockResolvedValue(true); @@ -3426,7 +3444,6 @@ describe('NftController', () => { it('should check whether NFTs for the current selectedAccount/chainId combination are still owned by the selectedAccount and leave the isCurrentlyOwned value as is when NFT ownership check fails', async () => { const { nftController } = setupController({ - options: {}, defaultSelectedAccount: OWNER_ACCOUNT, }); jest @@ -3505,9 +3522,7 @@ describe('NftController', () => { }); it('should handle default case where selectedAccount is not set', async () => { - const { nftController, mockGetAccount } = setupController({ - options: {}, - }); + const { nftController, mockGetAccount } = setupController({}); mockGetAccount.mockReturnValue(null); jest.spyOn(nftController, 'isNftOwner').mockResolvedValue(false); @@ -3531,7 +3546,6 @@ describe('NftController', () => { describe('checkAndUpdateSingleNftOwnershipStatus', () => { it('should check whether the passed NFT is still owned by the the current selectedAccount/chainId combination and update its isCurrentlyOwned property in state if batch is false and isNftOwner returns false', async () => { const { nftController } = setupController({ - options: {}, defaultSelectedAccount: OWNER_ACCOUNT, }); @@ -3566,7 +3580,6 @@ describe('NftController', () => { it('should check whether the passed NFT is still owned by the the current selectedAddress/chainId combination and return the updated NFT object without updating state if batch is true', async () => { const { nftController } = setupController({ - options: {}, defaultSelectedAccount: OWNER_ACCOUNT, }); @@ -3741,7 +3754,6 @@ describe('NftController', () => { it('should return null if the NFT does not exist in the state', async () => { const { nftController } = setupController({ - options: {}, defaultSelectedAccount: OWNER_ACCOUNT, }); @@ -3829,7 +3841,6 @@ describe('NftController', () => { it('should return undefined if the NFT does not exist', () => { const { nftController } = setupController({ - options: {}, defaultSelectedAccount: OWNER_ACCOUNT, }); @@ -3863,7 +3874,6 @@ describe('NftController', () => { it('should not update any NFT state and should return false when passed a transaction id that does not match that of any NFT', async () => { const { nftController } = setupController({ - options: {}, defaultSelectedAccount: OWNER_ACCOUNT, }); @@ -3912,9 +3922,7 @@ describe('NftController', () => { const tokenURI = 'https://api.pudgypenguins.io/lil/4'; const mockGetERC721TokenURI = jest.fn().mockResolvedValue(tokenURI); const { nftController, mockGetAccount } = setupController({ - options: { - getERC721TokenURI: mockGetERC721TokenURI, - }, + getERC721TokenURI: mockGetERC721TokenURI, defaultSelectedAccount: OWNER_ACCOUNT, }); const spy = jest.spyOn(nftController, 'updateNft'); @@ -3969,9 +3977,7 @@ describe('NftController', () => { const tokenURI = 'https://url/'; const mockGetERC721TokenURI = jest.fn().mockResolvedValue(tokenURI); const { nftController, mockGetAccount } = setupController({ - options: { - getERC721TokenURI: mockGetERC721TokenURI, - }, + getERC721TokenURI: mockGetERC721TokenURI, defaultSelectedAccount: OWNER_ACCOUNT, }); const updateNftSpy = jest.spyOn(nftController, 'updateNft'); @@ -4035,9 +4041,7 @@ describe('NftController', () => { const tokenURI = 'https://url/'; const mockGetERC721TokenURI = jest.fn().mockResolvedValue(tokenURI); const { nftController, mockGetAccount } = setupController({ - options: { - getERC721TokenURI: mockGetERC721TokenURI, - }, + getERC721TokenURI: mockGetERC721TokenURI, defaultSelectedAccount: OWNER_ACCOUNT, }); const spy = jest.spyOn(nftController, 'updateNft'); @@ -4133,9 +4137,7 @@ describe('NftController', () => { changeNetwork, triggerSelectedAccountChange, } = setupController({ - options: { - getERC721TokenURI: mockGetERC721TokenURI, - }, + getERC721TokenURI: mockGetERC721TokenURI, }); changeNetwork({ selectedNetworkClientId: InfuraNetworkType.sepolia }); const spy = jest.spyOn(nftController, 'updateNftMetadata'); @@ -4183,9 +4185,7 @@ describe('NftController', () => { changeNetwork, triggerSelectedAccountChange, } = setupController({ - options: { - getERC721TokenURI: mockGetERC721TokenURI, - }, + getERC721TokenURI: mockGetERC721TokenURI, }); changeNetwork({ selectedNetworkClientId: InfuraNetworkType.sepolia }); const spy = jest.spyOn(nftController, 'updateNftMetadata'); @@ -4229,9 +4229,7 @@ describe('NftController', () => { const tokenURI = 'https://api.pudgypenguins.io/lil/4'; const mockGetERC721TokenURI = jest.fn().mockResolvedValue(tokenURI); const { nftController, triggerPreferencesStateChange } = setupController({ - options: { - getERC721TokenURI: mockGetERC721TokenURI, - }, + getERC721TokenURI: mockGetERC721TokenURI, }); const selectedAddress = OWNER_ADDRESS; const spy = jest.spyOn(nftController, 'updateNft'); @@ -4336,8 +4334,8 @@ describe('NftController', () => { const { nftController, messenger } = setupController({ options: { openSeaEnabled: true, - getERC721TokenURI: mockGetERC721TokenURI, }, + getERC721TokenURI: mockGetERC721TokenURI, }); const updateNftMetadataSpy = jest.spyOn(nftController, 'updateNftMetadata'); messenger.publish( diff --git a/packages/assets-controllers/src/TokenBalancesController.test.ts b/packages/assets-controllers/src/TokenBalancesController.test.ts index 49390c64fe3..e57d13c77f2 100644 --- a/packages/assets-controllers/src/TokenBalancesController.test.ts +++ b/packages/assets-controllers/src/TokenBalancesController.test.ts @@ -8,6 +8,8 @@ import { createMockInternalAccount } from '../../accounts-controller/src/tests/m import type { AllowedActions, AllowedEvents, + TokenBalancesControllerActions, + TokenBalancesControllerEvents, TokenBalancesControllerMessenger, } from './TokenBalancesController'; import { TokenBalancesController } from './TokenBalancesController'; @@ -27,13 +29,16 @@ const controllerName = 'TokenBalancesController'; */ function getMessenger( controllerMessenger = new ControllerMessenger< - AllowedActions, - AllowedEvents + TokenBalancesControllerActions | AllowedActions, + TokenBalancesControllerEvents | AllowedEvents >(), ): TokenBalancesControllerMessenger { return controllerMessenger.getRestricted({ name: controllerName, - allowedActions: ['AccountsController:getSelectedAccount'], + allowedActions: [ + 'AccountsController:getSelectedAccount', + 'AssetsContractController:getERC20BalanceOf', + ], allowedEvents: ['TokensController:stateChange'], }); } @@ -55,8 +60,8 @@ const setupController = ({ triggerTokensStateChange: (state: TokensControllerState) => Promise; } => { const controllerMessenger = new ControllerMessenger< - AllowedActions, - AllowedEvents + TokenBalancesControllerActions | AllowedActions, + TokenBalancesControllerEvents | AllowedEvents >(); const messenger = getMessenger(controllerMessenger); @@ -67,9 +72,12 @@ const setupController = ({ 'AccountsController:getSelectedAccount', mockSelectedAccount, ); + controllerMessenger.registerActionHandler( + 'AssetsContractController:getERC20BalanceOf', + mockGetERC20BalanceOf, + ); const controller = new TokenBalancesController({ - getERC20BalanceOf: mockGetERC20BalanceOf, messenger, ...config, }); @@ -114,7 +122,6 @@ describe('TokenBalancesController', () => { new TokenBalancesController({ interval: 10, - getERC20BalanceOf: jest.fn(), messenger: getMessenger(new ControllerMessenger()), }); await flushPromises();