Skip to content

Commit

Permalink
fix: exclude shared contracts from fetching contract information
Browse files Browse the repository at this point in the history
  • Loading branch information
sahar-fehri committed Jul 2, 2024
1 parent 7a20016 commit eff4e0b
Show file tree
Hide file tree
Showing 4 changed files with 276 additions and 89 deletions.
36 changes: 31 additions & 5 deletions packages/assets-controllers/src/NftController.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -305,11 +305,13 @@ describe('NftController', () => {
tokens: [
{
token: {
contract: '0x1',
kind: 'erc1155',
name: 'Name',
description: 'Description',
image: 'url',
collection: {
id: '0x1',
creator: 'Oxaddress',
tokenCount: 0,
},
Expand Down Expand Up @@ -1659,7 +1661,7 @@ describe('NftController', () => {
};

nock(NFT_API_BASE_URL)
.get(`/collections?chainIds=1&contract=0x01`)
.get(`/collections?chainId=1&id=0x1`)
.reply(200, {
collections: [
{
Expand All @@ -1686,6 +1688,7 @@ describe('NftController', () => {
tokenURI: '',
creator: 'Oxaddress',
collection: {
id: '0x1',
creator: 'Oxaddress',
tokenCount: 0,
contractDeployedAt: 'timestampTest',
Expand Down Expand Up @@ -1717,17 +1720,21 @@ describe('NftController', () => {
tokens: [
{
token: {
contract: `${ERC721_KUDOSADDRESS}`,
kind: 'erc721',
name: 'Kudos Name',
description: 'Kudos Description',
image: 'url',
collection: {
id: `${ERC721_KUDOSADDRESS}`,
},
},
},
],
});

nock(NFT_API_BASE_URL)
.get(`/collections?chainIds=1&contract=${ERC721_KUDOSADDRESS}`)
.get(`/collections?chainId=1&id=${ERC721_KUDOSADDRESS}`)
.replyWithError(new Error('Failed to fetch'));

nock('https://ipfs.gitcoin.co:443')
Expand All @@ -1753,6 +1760,14 @@ describe('NftController', () => {
isCurrentlyOwned: true,
tokenURI:
'https://ipfs.gitcoin.co:443/api/v0/cat/QmPmt6EAaioN78ECnW5oCL8v2YvVSpoBjLCjrXhhsAvoov',
collection: {
contractDeployedAt: undefined,
creator: undefined,
id: ERC721_KUDOSADDRESS,
openseaVerificationStatus: undefined,
ownerCount: undefined,
topBid: undefined,
},
});

expect(
Expand Down Expand Up @@ -1787,17 +1802,21 @@ describe('NftController', () => {
tokens: [
{
token: {
contract: ERC721_KUDOSADDRESS,
kind: 'erc721',
name: 'Kudos Name',
description: 'Kudos Description',
image: 'url',
collection: {
id: ERC721_KUDOSADDRESS,
},
},
},
],
});

nock(NFT_API_BASE_URL)
.get(`/collections?chainIds=1&contract=${ERC721_KUDOSADDRESS}`)
.get(`/collections?chainId=1&id=${ERC721_KUDOSADDRESS}`)
.reply(200, {
collections: [
{
Expand Down Expand Up @@ -1833,6 +1852,7 @@ describe('NftController', () => {
tokenURI:
'https://ipfs.gitcoin.co:443/api/v0/cat/QmPmt6EAaioN78ECnW5oCL8v2YvVSpoBjLCjrXhhsAvoov',
collection: {
id: ERC721_KUDOSADDRESS,
creator: '0xcreator',
contractDeployedAt: 'timestampTest',
ownerCount: '989',
Expand Down Expand Up @@ -2151,11 +2171,13 @@ describe('NftController', () => {
tokens: [
{
token: {
contract: ERC721_KUDOSADDRESS,
kind: 'erc721',
name: 'Kudos Name',
description: 'Kudos Description',
image: 'Kudos image (from proxy API)',
collection: {
id: ERC721_KUDOSADDRESS,
name: 'Kudos',
tokenCount: '10',
image: 'Kudos logo (from proxy API)',
Expand All @@ -2166,7 +2188,7 @@ describe('NftController', () => {
});

nock(NFT_API_BASE_URL)
.get(`/collections?chainIds=1&contract=${ERC721_KUDOSADDRESS}`)
.get(`/collections?chainId=1&id=${ERC721_KUDOSADDRESS}`)
.replyWithError(new Error('Failed to fetch'));

await nftController.addNft(
Expand Down Expand Up @@ -2207,6 +2229,7 @@ describe('NftController', () => {
isCurrentlyOwned: true,
tokenURI: null,
collection: {
id: ERC721_KUDOSADDRESS,
tokenCount: '10',
image: 'Kudos logo (from proxy API)',
name: 'Kudos',
Expand Down Expand Up @@ -2263,11 +2286,13 @@ describe('NftController', () => {
tokens: [
{
token: {
contract: ERC721_KUDOSADDRESS,
kind: 'erc721',
name: 'Kudos Name',
description: 'Kudos Description',
image: 'Kudos image (from proxy API)',
collection: {
id: ERC721_KUDOSADDRESS,
name: 'Kudos',
tokenCount: '10',
image: 'Kudos logo (from proxy API)',
Expand All @@ -2278,7 +2303,7 @@ describe('NftController', () => {
});

nock(NFT_API_BASE_URL)
.get(`/collections?chainIds=1&contract=${ERC721_KUDOSADDRESS}`)
.get(`/collections?chainId=1&id=${ERC721_KUDOSADDRESS}`)
.reply(200, {
collections: [
{
Expand Down Expand Up @@ -2326,6 +2351,7 @@ describe('NftController', () => {
isCurrentlyOwned: true,
tokenURI: null,
collection: {
id: ERC721_KUDOSADDRESS,
tokenCount: '10',
image: 'Kudos logo (from proxy API)',
name: 'Kudos',
Expand Down
52 changes: 23 additions & 29 deletions packages/assets-controllers/src/NftController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import {
ERC1155,
ApprovalType,
NFT_API_BASE_URL,
NFT_API_VERSION,
} from '@metamask/controller-utils';
import { type InternalAccount } from '@metamask/keyring-api';
import type {
Expand Down Expand Up @@ -537,39 +538,32 @@ export class NftController extends BaseController<
includeAttributes: 'true',
includeLastSale: 'true',
}).toString();

// First fetch token information
const nftInformation: ReservoirResponse | undefined =
await fetchWithErrorHandling({
url: `${this.getNftApi()}?${urlParams}`,
options: {
headers: {
Version: NFT_API_VERSION,
},
},
});
// Params for getCollections API call
const getCollectionParams = new URLSearchParams({
chainIds: '1',
contract: `${contractAddress}`,
chainId: '1',
id: `${nftInformation?.tokens[0]?.token?.collection?.id as string}`,
}).toString();

const [nftInformation, collectionInformation]: [
ReservoirResponse | undefined,
GetCollectionsResponse,
] = await Promise.all([
safelyExecute(() =>
fetchWithErrorHandling({
url: `${this.getNftApi()}?${urlParams}`,
options: {
headers: {
Version: '1',
},
},
}),
),
safelyExecute(() =>
fetchWithErrorHandling({
url: `${
NFT_API_BASE_URL as string
}/collections?${getCollectionParams}`,
options: {
headers: {
Version: '1',
},
// Fetch collection information using collectionId
const collectionInformation: GetCollectionsResponse | undefined =
await fetchWithErrorHandling({
url: `${NFT_API_BASE_URL as string}/collections?${getCollectionParams}`,
options: {
headers: {
Version: NFT_API_VERSION,
},
}),
),
]);
},
});
// if we were still unable to fetch the data we return out the default/null of `NftMetadata`
if (!nftInformation?.tokens?.[0]?.token) {
return {
Expand Down
Loading

0 comments on commit eff4e0b

Please sign in to comment.