Skip to content

Commit

Permalink
feat: add getNFTContractInfo (#4524)
Browse files Browse the repository at this point in the history
## Explanation

<!--
Thanks for your contribution! Take a moment to answer these questions so
that reviewers have the information they need to properly understand
your changes:

* What is the current state of things and why does it need to change?
* What is the solution your changes offer and how does it work?
* Are there any changes whose purpose might not obvious to those
unfamiliar with the domain?
* If your primary goal was to update one package but you found you had
to update another one along the way, why did you do so?
* If you had to upgrade a dependency, why did you do so?
-->

This PR exposes NFT `collections` api through NFT controller.

## References

<!--
Are there any issues that this pull request is tied to? Are there other
links that reviewers should consult to understand these changes better?

For example:

* Fixes #12345
* Related to #67890
-->

Related to: MetaMask/MetaMask-planning#2507
Extension PR using this PR's preview build:
MetaMask/metamask-extension#25692

## Changelog

<!--
If you're making any consumer-facing changes, list those changes here as
if you were updating a changelog, using the template below as a guide.

(CATEGORY is one of BREAKING, ADDED, CHANGED, DEPRECATED, REMOVED, or
FIXED. For security-related issues, follow the Security Advisory
process.)

Please take care to name the exact pieces of the API you've added or
changed (e.g. types, interfaces, functions, or methods).

If there are any breaking changes, make sure to offer a solution for
consumers to follow once they upgrade to the changes.

Finally, if you're only making changes to development scripts or tests,
you may replace the template below with "None".
-->

### `@metamask/assets-controllers`

- **ADDED**: Add `fetchNftCollectionMetadata` to `NFTController` api

## Checklist

- [X] I've updated the test suite for new or updated code as appropriate
- [X] I've updated documentation (JSDoc, Markdown, etc.) for new or
updated code as appropriate
- [X] I've highlighted breaking changes using the "BREAKING" category
above as appropriate

---------

Co-authored-by: Elliot Winkler <elliot.winkler@gmail.com>
  • Loading branch information
OGPoyraz and mcmire authored Jul 25, 2024
1 parent a99ce6a commit a3bfbce
Show file tree
Hide file tree
Showing 2 changed files with 77 additions and 0 deletions.
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
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

0 comments on commit a3bfbce

Please sign in to comment.