From e82d7e3c35b9f48106e43b23008d7c8a9a94b7d3 Mon Sep 17 00:00:00 2001 From: remiroyc Date: Mon, 18 Nov 2024 20:30:46 +0100 Subject: [PATCH] fix(api): marketplace api --- apps/web/src/server/api/helpers/l2nfts.ts | 116 ++++++++++++++-------- apps/web/src/server/api/routers/l2Nfts.ts | 31 +++--- 2 files changed, 94 insertions(+), 53 deletions(-) diff --git a/apps/web/src/server/api/helpers/l2nfts.ts b/apps/web/src/server/api/helpers/l2nfts.ts index 6e668844..2b551173 100644 --- a/apps/web/src/server/api/helpers/l2nfts.ts +++ b/apps/web/src/server/api/helpers/l2nfts.ts @@ -16,7 +16,7 @@ type PortfolioApiResponse = { currency_address: string; floor: string; list_price: string; - metadata: { + metadata?: { animation_key: null | string; animation_mime_type: null | string; animation_url: null | string; @@ -31,6 +31,7 @@ type PortfolioApiResponse = { name: null | string; youtube_url: null | string; }; + token_id: string; }>; token_count: number; }; @@ -45,20 +46,34 @@ export async function getL2ContractsForOwner(address: string) { return apiResponse; } -type ArkBatchNftsApiResponse = { - result: Array<{ - contract_address: string; - contract_name: string; - metadata?: { normalized: { image?: string; name?: string } }; +type TokenApiResponse = { + data: Array<{ + collection_address: string; + collection_image: null | string; + collection_name: null | string; + metadata: { + animation_key: null | string; + animation_mime_type: null | string; + animation_url: null | string; + background_color: null | string; + description: null | string; + external_url: null | string; + image: null | string; + image_data: null | string; + image_key: null | string; + image_key_540_540: null | string; + image_mime_type: null | string; + name: null | string; + youtube_url: null | string; + }; owner: string; token_id: string; }>; }; + export async function getL2NftsMetadataBatch( tokens: Array<{ contract_address: string; token_id: string }> ) { - const url = `${nftApiUrl}/v1/tokens/batch`; - const body = JSON.stringify({ tokens: tokens.map((token) => ({ contract_address: validateAndParseAddress(token.contract_address), @@ -66,56 +81,79 @@ export async function getL2NftsMetadataBatch( })), }); - const nftsResponse = await fetch(url, { - body, - headers: requestsHeader, - method: "POST", - }); - - const nfts = (await nftsResponse.json()) as ArkBatchNftsApiResponse; + let nfts: { + collection_address: string; + collection_image: null | string; + collection_name: null | string; + metadata: { + animation_key: null | string; + animation_mime_type: null | string; + animation_url: null | string; + background_color: null | string; + description: null | string; + external_url: null | string; + image: null | string; + image_data: null | string; + image_key: null | string; + image_key_540_540: null | string; + image_mime_type: null | string; + name: null | string; + youtube_url: null | string; + }; + owner: string; + token_id: string; + }[] = []; + + for (const token of tokens) { + const url = `${nftApiUrl}/tokens/${token.contract_address}/0x534e5f4d41494e/${token.token_id}`; + const nftsResponse = await fetch(url, { + body, + headers: requestsHeader, + method: "POST", + }); + + const response = (await nftsResponse.json()) as TokenApiResponse; + nfts = nfts.concat(response.data); + } return nfts; } -type ArkNftsApiResponse = { - result: Array<{ - contract_address: string; - metadata: { - normalized: { image: null | string; name: null | string }; - } | null; - owner: string; - token_id: string; - }>; - total_count: number; -}; export async function getL2NftsForOwner( userAddress: string, contractAddress: string | undefined ) { - const url = `${nftApiUrl}/v1/owners/${validateAndParseAddress( - userAddress - )}/tokens${ - contractAddress !== undefined - ? `?contract_address=${validateAndParseAddress(contractAddress)}` - : "" - }`; + const url = `${nftApiUrl}/portfolio/${validateAndParseAddress(userAddress)}`; const nftsResponse = await fetch(url, { headers: requestsHeader, }); - const nfts = (await nftsResponse.json()) as ArkNftsApiResponse; - - return nfts; + const nfts = (await nftsResponse.json()) as PortfolioApiResponse; + + return { + data: contractAddress + ? nfts.data.filter( + (d) => + d.collection_address === validateAndParseAddress(contractAddress) + ) + : nfts.data, + token_count: nfts.token_count, + }; } type ArkCollectionInfoApiResponse = { - result: { contract_address: string; name: string; symbol: string }; + data: { + address: string; + description: null | string; + image: null | string; + name: null | string; + }; }; export async function getL2ContractMetadata(contractAddress: string) { - const url = `${nftApiUrl}/v1/contracts/${validateAndParseAddress( + const url = `${nftApiUrl}/collections/${validateAndParseAddress( contractAddress - )}`; + )}/0x534e5f4d41494e`; const contractInfoResponse = await fetch(url, { headers: requestsHeader, diff --git a/apps/web/src/server/api/routers/l2Nfts.ts b/apps/web/src/server/api/routers/l2Nfts.ts index 78379f32..0f19a105 100644 --- a/apps/web/src/server/api/routers/l2Nfts.ts +++ b/apps/web/src/server/api/routers/l2Nfts.ts @@ -22,7 +22,7 @@ export const l2NftsRouter = createTRPCRouter({ try { const contractInfo = await getL2ContractMetadata(contractAddress); - return { name: contractInfo.result.name }; + return { name: contractInfo.data.name }; } catch (error) { return { name: "" }; } @@ -37,10 +37,13 @@ export const l2NftsRouter = createTRPCRouter({ try { const response = await getL2ContractsForOwner(address); + + // console.log("=> response", response); + const whitelistedCollections = await getL2WhitelistedCollections(); const collections: Array = response.data.map((contract) => { - const media = getMediaObjectFromUrl(contract.metadata.image); + const media = getMediaObjectFromUrl(contract.metadata?.image); const isBridgeable = whitelistedCollections === undefined || whitelistedCollections.some( @@ -89,7 +92,7 @@ export const l2NftsRouter = createTRPCRouter({ })) ); - return nfts.result + return nfts .filter( (nft) => ownerAddress === undefined || @@ -97,13 +100,13 @@ export const l2NftsRouter = createTRPCRouter({ validateAndParseAddress(ownerAddress) ) .map((nft) => { - const media = getMediaObjectFromUrl(nft.metadata?.normalized.image); + const media = getMediaObjectFromUrl(nft.metadata?.image); return { - collectionName: nft.contract_name, + collectionName: nft.collection_name, media, tokenId: nft.token_id, - tokenName: nft.metadata?.normalized.name ?? `#${nft.token_id}`, + tokenName: nft.metadata?.name ?? `#${nft.token_id}`, }; }); } catch (error) { @@ -134,24 +137,24 @@ export const l2NftsRouter = createTRPCRouter({ try { const nfts = await getL2NftsForOwner(userAddress, contractAddress); - const ownedNfts: Array = nfts.result.map((nft) => { - const media = getMediaObjectFromUrl( - nft.metadata?.normalized.image ?? undefined - ); + const ownedNfts: Array = nfts.data.map((nft) => { + console.log("=> nft", nft); + + const media = getMediaObjectFromUrl(nft.metadata?.image); const name = - nft.metadata?.normalized.name?.length ?? 0 > 0 - ? nft.metadata?.normalized?.name ?? "" + nft.metadata?.name?.length ?? 0 > 0 + ? nft.metadata?.name ?? "" : `${nft.token_id}`; return { - contractAddress: nft.contract_address, + contractAddress: nft.collection_address, media, name, tokenId: nft.token_id, }; }); - return { ownedNfts, totalCount: nfts.total_count }; + return { ownedNfts, totalCount: nfts.token_count }; } catch (error) { console.error("getL2OwnerNftsFromCollection error: ", error); return { ownedNfts: [], totalCount: 0 };