Skip to content
Draft
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
144 changes: 2 additions & 142 deletions packages/assets-controllers/src/NftController.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2216,47 +2216,6 @@ describe('NftController', () => {
defaultSelectedAccount: OWNER_ACCOUNT,
});

const testTopBid = {
id: 'id',
sourceDomain: 'opensea.io',
price: {
currency: {
contract: '0x01',
name: 'Wrapped Ether',
symbol: 'WETH',
decimals: 18,
},
amount: {
raw: '201300000000000000',
decimal: 0.2013,
usd: 716.46131,
native: 0.2013,
},
netAmount: {
raw: '196267500000000000',
decimal: 0.19627,
usd: 698.54978,
native: 0.19627,
},
},
maker: 'testMaker',
validFrom: 1719228327,
validUntil: 1719228927,
};

nock(NFT_API_BASE_URL)
.get(`/collections?chainId=1&id=0x1`)
.reply(200, {
collections: [
{
contractDeployedAt: 'timestampTest',
ownerCount: '989',
openseaVerificationStatus: 'verified',
topBid: testTopBid,
},
],
});

await nftController.addNft('0x01', '1', 'mainnet');
expect(
nftController.state.allNfts[OWNER_ACCOUNT.address][ChainId.mainnet][0],
Expand All @@ -2276,10 +2235,6 @@ describe('NftController', () => {
id: '0x1',
creator: 'Oxaddress',
tokenCount: 0,
contractDeployedAt: 'timestampTest',
ownerCount: '989',
openseaVerificationStatus: 'verified',
topBid: testTopBid,
},
});
});
Expand Down Expand Up @@ -2316,10 +2271,6 @@ describe('NftController', () => {
],
});

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

nock('https://ipfs.gitcoin.co:443')
.get('/api/v0/cat/QmPmt6EAaioN78ECnW5oCL8v2YvVSpoBjLCjrXhhsAvoov')
.reply(200, {
Expand Down Expand Up @@ -2349,12 +2300,7 @@ describe('NftController', () => {
tokenURI:
'https://ipfs.gitcoin.co:443/api/v0/cat/QmPmt6EAaioN78ECnW5oCL8v2YvVSpoBjLCjrXhhsAvoov',
collection: {
contractDeployedAt: undefined,
creator: undefined,
id: ERC721_KUDOSADDRESS,
openseaVerificationStatus: undefined,
ownerCount: undefined,
topBid: undefined,
},
});

Expand All @@ -2369,7 +2315,7 @@ describe('NftController', () => {
schemaName: ERC721,
});
});
it('should add NFT erc721 and aggregate NFT data from both contract and NFT-API when call to Get Collections succeeds', async () => {
it('should add NFT erc721 and aggregate NFT data from both contract and NFT-API', async () => {
const { nftController } = setupController({
getERC721AssetName: jest.fn().mockResolvedValue('KudosToken'),
getERC721AssetSymbol: jest.fn().mockResolvedValue('KDO'),
Expand Down Expand Up @@ -2401,19 +2347,6 @@ describe('NftController', () => {
],
});

nock(NFT_API_BASE_URL)
.get(`/collections?chainId=1&id=${ERC721_KUDOSADDRESS}`)
.reply(200, {
collections: [
{
contractDeployedAt: 'timestampTest',
ownerCount: '989',
openseaVerificationStatus: 'verified',
creator: '0xcreator',
},
],
});

nock('https://ipfs.gitcoin.co:443')
.get('/api/v0/cat/QmPmt6EAaioN78ECnW5oCL8v2YvVSpoBjLCjrXhhsAvoov')
.reply(200, {
Expand Down Expand Up @@ -2444,11 +2377,6 @@ describe('NftController', () => {
'https://ipfs.gitcoin.co:443/api/v0/cat/QmPmt6EAaioN78ECnW5oCL8v2YvVSpoBjLCjrXhhsAvoov',
collection: {
id: ERC721_KUDOSADDRESS,
creator: '0xcreator',
contractDeployedAt: 'timestampTest',
ownerCount: '989',
openseaVerificationStatus: 'verified',
topBid: undefined,
},
});

Expand Down Expand Up @@ -2802,10 +2730,6 @@ describe('NftController', () => {
],
});

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

await nftController.addNft(
'0x6EbeAf8e8E946F0716E6533A6f2cefc83f60e8Ab',
'123',
Expand Down Expand Up @@ -2855,11 +2779,6 @@ describe('NftController', () => {
tokenCount: '10',
image: 'Kudos logo (from proxy API)',
name: 'Kudos',
creator: undefined,
openseaVerificationStatus: undefined,
ownerCount: undefined,
contractDeployedAt: undefined,
topBid: undefined,
},
},
]);
Expand All @@ -2886,7 +2805,7 @@ describe('NftController', () => {
});
});

it('should add an nft and nftContract when there is valid contract information and source is "detected" when call to get collections succeeds', async () => {
it('should add an nft and nftContract when there is valid contract information and source is "detected"', async () => {
const mockOnNftAdded = jest.fn();
const { nftController } = setupController({
options: {
Expand Down Expand Up @@ -2924,17 +2843,6 @@ describe('NftController', () => {
],
});

nock(NFT_API_BASE_URL)
.get(`/collections?chainId=1&id=${ERC721_KUDOSADDRESS}`)
.reply(200, {
collections: [
{
creator: '0xcreator',
openseaVerificationStatus: 'verified',
},
],
});

await nftController.addNft(
'0x6EbeAf8e8E946F0716E6533A6f2cefc83f60e8Ab',
'123',
Expand Down Expand Up @@ -2984,11 +2892,6 @@ describe('NftController', () => {
tokenCount: '10',
image: 'Kudos logo (from proxy API)',
name: 'Kudos',
creator: '0xcreator',
openseaVerificationStatus: 'verified',
ownerCount: undefined,
contractDeployedAt: undefined,
topBid: undefined,
},
},
]);
Expand Down Expand Up @@ -5470,49 +5373,6 @@ 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,
});
});
});

describe('resetState', () => {
it('resets the state to default state', () => {
const initialState: NftControllerState = {
Expand Down
64 changes: 2 additions & 62 deletions packages/assets-controllers/src/NftController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,6 @@ import type {
Collection,
Attributes,
LastSale,
GetCollectionsResponse,
TopBid,
} from './NftDetectionController';
import type { NetworkControllerFindNetworkClientIdByChainIdAction } from '../../network-controller/src/NetworkController';
Expand Down Expand Up @@ -524,10 +523,6 @@ export class NftController extends BaseController<
});
}

#getNftCollectionApi(): string {
return `${NFT_API_BASE_URL}/collections`;
}

/**
* Request individual NFT information from NFT API.
*
Expand Down Expand Up @@ -559,21 +554,7 @@ export class NftController extends BaseController<
},
},
});
// Params for getCollections API call
const getCollectionParams = new URLSearchParams({
chainId: '1',
id: `${nftInformation?.tokens[0]?.token?.collection?.id as string}`,
}).toString();
// 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 Expand Up @@ -618,20 +599,7 @@ export class NftController extends BaseController<
},
rarityRank && { rarityRank },
rarity && { rarity },
(collection || collectionInformation) && {
collection: {
...(collection || {}),
creator:
collection?.creator ||
collectionInformation?.collections[0].creator,
openseaVerificationStatus:
collectionInformation?.collections[0].openseaVerificationStatus,
contractDeployedAt:
collectionInformation?.collections[0].contractDeployedAt,
ownerCount: collectionInformation?.collections[0].ownerCount,
topBid: collectionInformation?.collections[0].topBid,
},
},
collection && { collection },
);

return nftMetadata;
Expand Down Expand Up @@ -2036,34 +2004,6 @@ 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.messenger.call(
'ApprovalController:addRequest',
Expand Down
4 changes: 0 additions & 4 deletions packages/assets-controllers/src/NftDetectionController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -304,10 +304,6 @@ export type Attributes = {
createdAt?: string;
};

export type GetCollectionsResponse = {
collections: CollectionResponse[];
};

export type CollectionResponse = {
id?: string;
chainId?: number;
Expand Down
Loading