From 06634264a819f76cf524251d3fa9a4a0b109cc18 Mon Sep 17 00:00:00 2001 From: OGPoyraz Date: Mon, 15 Jul 2024 13:31:17 +0200 Subject: [PATCH 1/6] Add getNFTContractInfo --- .../src/NftController.test.ts | 43 +++++++++++++++++++ .../assets-controllers/src/NftController.ts | 30 +++++++++++++ 2 files changed, 73 insertions(+) diff --git a/packages/assets-controllers/src/NftController.test.ts b/packages/assets-controllers/src/NftController.test.ts index 69e47377fd..68fe9178a6 100644 --- a/packages/assets-controllers/src/NftController.test.ts +++ b/packages/assets-controllers/src/NftController.test.ts @@ -104,6 +104,22 @@ const GOERLI = { ticker: NetworksTicker.goerli, }; +const CRYPTOPUNK_COLLECTION_MOCK = { + id: CRYPTOPUNK_ADDRESS, + name: 'CryptoPunks', + slug: 'cryptopunks', + symbol: 'PUNK', + imageUrl: 'url', +}; + +const ERC721_KUDOS_COLLECTION_MOCK = { + id: ERC721_KUDOSADDRESS, + name: 'Kudos', + slug: 'kudos', + symbol: 'KUDOS', + imageUrl: 'url', +}; + type ApprovalActions = | AddApprovalRequest | AccountsControllerGetAccountAction @@ -4813,4 +4829,31 @@ describe('NftController', () => { expect(updateNftMetadataSpy).not.toHaveBeenCalled(); }); + + + describe('getNFTContractInfo', () => { + it('fetches NFT collections metadata successfully', async () => { + nock(NFT_API_BASE_URL) + .get( + `/collections?chainId=0x1&contract=${CRYPTOPUNK_ADDRESS}&contract=${ERC721_KUDOSADDRESS}`, + ) + .reply(200, { + collections: [ + CRYPTOPUNK_COLLECTION_MOCK, + ERC721_KUDOS_COLLECTION_MOCK, + ], + }); + + const { nftController } = setupController(); + + const response = await nftController.getNFTContractInfo( + [CRYPTOPUNK_ADDRESS, ERC721_KUDOSADDRESS], + ChainId.mainnet, + ); + + expect(response).toStrictEqual({ + collections: [CRYPTOPUNK_COLLECTION_MOCK, ERC721_KUDOS_COLLECTION_MOCK], + }); + }); + }); }); diff --git a/packages/assets-controllers/src/NftController.ts b/packages/assets-controllers/src/NftController.ts index c305eeece6..2ca969cb27 100644 --- a/packages/assets-controllers/src/NftController.ts +++ b/packages/assets-controllers/src/NftController.ts @@ -520,6 +520,12 @@ export class NftController extends BaseController< }); } + #getNftCollectionApi(): string { + // False negative. + // eslint-disable-next-line @typescript-eslint/restrict-template-expressions + return `${NFT_API_BASE_URL}/collections`; + } + /** * Request individual NFT information from NFT API. * @@ -1985,6 +1991,30 @@ export class NftController extends BaseController< return true; } + /** + * Fetches NFT Collection Metadata from the NFT API. + * + * @param contractAddresses - The contract addresses of the NFTs. + * @param chainId - The chain ID of the network where the NFT is located. + * @returns NFT collections metadata. + */ + async getNFTContractInfo( + contractAddresses: string[], + chainId: Hex, + ): Promise<{ + collections: Collection[]; + }> { + const url = new URL(this.#getNftCollectionApi()); + + url.searchParams.append('chainId', chainId); + + for (const address of contractAddresses) { + url.searchParams.append('contract', address); + } + + return await handleFetch(url); + } + async _requestApproval(suggestedNftMeta: SuggestedNftMeta) { return this.messagingSystem.call( 'ApprovalController:addRequest', From c6e0ec0ec9e2723a59df72d430a80cecff4df3f4 Mon Sep 17 00:00:00 2001 From: OGPoyraz Date: Mon, 15 Jul 2024 13:49:29 +0200 Subject: [PATCH 2/6] Fix lint --- packages/assets-controllers/src/NftController.test.ts | 1 - 1 file changed, 1 deletion(-) diff --git a/packages/assets-controllers/src/NftController.test.ts b/packages/assets-controllers/src/NftController.test.ts index 68fe9178a6..bd3bef2675 100644 --- a/packages/assets-controllers/src/NftController.test.ts +++ b/packages/assets-controllers/src/NftController.test.ts @@ -4830,7 +4830,6 @@ describe('NftController', () => { expect(updateNftMetadataSpy).not.toHaveBeenCalled(); }); - describe('getNFTContractInfo', () => { it('fetches NFT collections metadata successfully', async () => { nock(NFT_API_BASE_URL) From 4340bdc0740f14bf766551f2bfffd30e7d893a70 Mon Sep 17 00:00:00 2001 From: OGPoyraz Date: Mon, 15 Jul 2024 14:10:23 +0200 Subject: [PATCH 3/6] Add api version --- packages/assets-controllers/src/NftController.ts | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/packages/assets-controllers/src/NftController.ts b/packages/assets-controllers/src/NftController.ts index 2ca969cb27..bab97ed448 100644 --- a/packages/assets-controllers/src/NftController.ts +++ b/packages/assets-controllers/src/NftController.ts @@ -2012,7 +2012,11 @@ export class NftController extends BaseController< url.searchParams.append('contract', address); } - return await handleFetch(url); + return await handleFetch(url, { + headers: { + Version: NFT_API_VERSION, + }, + }); } async _requestApproval(suggestedNftMeta: SuggestedNftMeta) { From 2df0c2633959c763d57c0d6b80e0bf585a794912 Mon Sep 17 00:00:00 2001 From: OGPoyraz Date: Wed, 17 Jul 2024 15:17:29 +0200 Subject: [PATCH 4/6] Fix tests --- .../src/NftController.test.ts | 21 +++++++++---------- 1 file changed, 10 insertions(+), 11 deletions(-) diff --git a/packages/assets-controllers/src/NftController.test.ts b/packages/assets-controllers/src/NftController.test.ts index bd3bef2675..688b2e5b3f 100644 --- a/packages/assets-controllers/src/NftController.test.ts +++ b/packages/assets-controllers/src/NftController.test.ts @@ -104,16 +104,18 @@ const GOERLI = { ticker: NetworksTicker.goerli, }; -const CRYPTOPUNK_COLLECTION_MOCK = { - id: CRYPTOPUNK_ADDRESS, +const ERC_721_1_ADDRESS = '0xb47e3cd837dDF8e4c57F05d70Ab865de6e193BBB'; +const ERC_721_1_COLLECTION_MOCK = { + id: ERC_721_1_ADDRESS, name: 'CryptoPunks', slug: 'cryptopunks', symbol: 'PUNK', imageUrl: 'url', }; -const ERC721_KUDOS_COLLECTION_MOCK = { - id: ERC721_KUDOSADDRESS, +const ERC_721_2_ADDRESS = '0xb47e3cd837dDF8e4c57F05d70Ab865de6e193BBB'; +const ERC_721_2_COLLECTION_MOCK = { + id: ERC_721_2_ADDRESS, name: 'Kudos', slug: 'kudos', symbol: 'KUDOS', @@ -4834,24 +4836,21 @@ describe('NftController', () => { it('fetches NFT collections metadata successfully', async () => { nock(NFT_API_BASE_URL) .get( - `/collections?chainId=0x1&contract=${CRYPTOPUNK_ADDRESS}&contract=${ERC721_KUDOSADDRESS}`, + `/collections?chainId=0x1&contract=${ERC_721_1_ADDRESS}&contract=${ERC_721_2_ADDRESS}`, ) .reply(200, { - collections: [ - CRYPTOPUNK_COLLECTION_MOCK, - ERC721_KUDOS_COLLECTION_MOCK, - ], + collections: [ERC_721_1_COLLECTION_MOCK, ERC_721_2_COLLECTION_MOCK], }); const { nftController } = setupController(); const response = await nftController.getNFTContractInfo( - [CRYPTOPUNK_ADDRESS, ERC721_KUDOSADDRESS], + [ERC_721_1_ADDRESS, ERC_721_2_ADDRESS], ChainId.mainnet, ); expect(response).toStrictEqual({ - collections: [CRYPTOPUNK_COLLECTION_MOCK, ERC721_KUDOS_COLLECTION_MOCK], + collections: [ERC_721_1_COLLECTION_MOCK, ERC_721_2_COLLECTION_MOCK], }); }); }); From 35117b3a39f8de67df5f07066b28bb7894e74811 Mon Sep 17 00:00:00 2001 From: OGPoyraz Date: Thu, 25 Jul 2024 11:26:16 +0200 Subject: [PATCH 5/6] Update packages/assets-controllers/src/NftController.test.ts Co-authored-by: Elliot Winkler --- .../src/NftController.test.ts | 28 ++++++++++++++++--- 1 file changed, 24 insertions(+), 4 deletions(-) diff --git a/packages/assets-controllers/src/NftController.test.ts b/packages/assets-controllers/src/NftController.test.ts index 688b2e5b3f..7a1d668562 100644 --- a/packages/assets-controllers/src/NftController.test.ts +++ b/packages/assets-controllers/src/NftController.test.ts @@ -4834,23 +4834,43 @@ describe('NftController', () => { describe('getNFTContractInfo', () => { it('fetches NFT collections metadata successfully', async () => { + const contractAddresses = [ + '0xb47e3cd837dDF8e4c57F05d70Ab865de6e193BBB', + '0xb47e3cd837dDF8e4c57F05d70Ab865de6e193BBB', + ]; + const collections = [ + { + id: contractAddresses[0], + name: 'CryptoPunks', + slug: 'cryptopunks', + symbol: 'PUNK', + imageUrl: 'url', + }, + { + id: contractAddresses[1], + name: 'Kudos', + slug: 'kudos', + symbol: 'KUDOS', + imageUrl: 'url', + }, + ]; nock(NFT_API_BASE_URL) .get( - `/collections?chainId=0x1&contract=${ERC_721_1_ADDRESS}&contract=${ERC_721_2_ADDRESS}`, + `/collections?chainId=0x1&contract=${contractAddresses[0]}&contract=${contractAddresses[1]}`, ) .reply(200, { - collections: [ERC_721_1_COLLECTION_MOCK, ERC_721_2_COLLECTION_MOCK], + collections, }); const { nftController } = setupController(); const response = await nftController.getNFTContractInfo( - [ERC_721_1_ADDRESS, ERC_721_2_ADDRESS], + contractAddresses, ChainId.mainnet, ); expect(response).toStrictEqual({ - collections: [ERC_721_1_COLLECTION_MOCK, ERC_721_2_COLLECTION_MOCK], + collections, }); }); }); From eafbc14e37d54b3d62ec51f98554530aeaf4200f Mon Sep 17 00:00:00 2001 From: OGPoyraz Date: Thu, 25 Jul 2024 11:30:24 +0200 Subject: [PATCH 6/6] Fix lint --- .../src/NftController.test.ts | 18 ------------------ 1 file changed, 18 deletions(-) diff --git a/packages/assets-controllers/src/NftController.test.ts b/packages/assets-controllers/src/NftController.test.ts index 7a1d668562..8d646564e5 100644 --- a/packages/assets-controllers/src/NftController.test.ts +++ b/packages/assets-controllers/src/NftController.test.ts @@ -104,24 +104,6 @@ const GOERLI = { ticker: NetworksTicker.goerli, }; -const ERC_721_1_ADDRESS = '0xb47e3cd837dDF8e4c57F05d70Ab865de6e193BBB'; -const ERC_721_1_COLLECTION_MOCK = { - id: ERC_721_1_ADDRESS, - name: 'CryptoPunks', - slug: 'cryptopunks', - symbol: 'PUNK', - imageUrl: 'url', -}; - -const ERC_721_2_ADDRESS = '0xb47e3cd837dDF8e4c57F05d70Ab865de6e193BBB'; -const ERC_721_2_COLLECTION_MOCK = { - id: ERC_721_2_ADDRESS, - name: 'Kudos', - slug: 'kudos', - symbol: 'KUDOS', - imageUrl: 'url', -}; - type ApprovalActions = | AddApprovalRequest | AccountsControllerGetAccountAction