Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add getNFTContractInfo #4524

Merged
merged 8 commits into from
Jul 25, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 43 additions & 0 deletions packages/assets-controllers/src/NftController.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4901,4 +4901,47 @@ describe('NftController', () => {

expect(updateNftMetadataSpy).not.toHaveBeenCalled();
});

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=${contractAddresses[0]}&contract=${contractAddresses[1]}`,
)
.reply(200, {
collections,
});

const { nftController } = setupController();

const response = await nftController.getNFTContractInfo(
contractAddresses,
ChainId.mainnet,
);

expect(response).toStrictEqual({
collections,
});
});
});
});
34 changes: 34 additions & 0 deletions packages/assets-controllers/src/NftController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -496,6 +496,12 @@ export class NftController extends BaseController<
});
}

#getNftCollectionApi(): string {
// False negative.
// eslint-disable-next-line @typescript-eslint/restrict-template-expressions
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do you know if this line is required?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Unfortunately yes, a thread here #4506 (comment)

return `${NFT_API_BASE_URL}/collections`;
}

/**
* Request individual NFT information from NFT API.
*
Expand Down Expand Up @@ -1973,6 +1979,34 @@ 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, {
headers: {
Version: NFT_API_VERSION,
},
});
}

async _requestApproval(suggestedNftMeta: SuggestedNftMeta) {
return this.messagingSystem.call(
'ApprovalController:addRequest',
Expand Down
Loading