Skip to content

Commit

Permalink
fix(api): marketplace api
Browse files Browse the repository at this point in the history
  • Loading branch information
remiroyc committed Nov 18, 2024
1 parent 8f16f01 commit e82d7e3
Show file tree
Hide file tree
Showing 2 changed files with 94 additions and 53 deletions.
116 changes: 77 additions & 39 deletions apps/web/src/server/api/helpers/l2nfts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -31,6 +31,7 @@ type PortfolioApiResponse = {
name: null | string;
youtube_url: null | string;
};
token_id: string;
}>;
token_count: number;
};
Expand All @@ -45,77 +46,114 @@ 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),
token_id: token.token_id,
})),
});

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,
Expand Down
31 changes: 17 additions & 14 deletions apps/web/src/server/api/routers/l2Nfts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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: "" };
}
Expand All @@ -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<Collection> = response.data.map((contract) => {
const media = getMediaObjectFromUrl(contract.metadata.image);
const media = getMediaObjectFromUrl(contract.metadata?.image);
const isBridgeable =
whitelistedCollections === undefined ||
whitelistedCollections.some(
Expand Down Expand Up @@ -89,21 +92,21 @@ export const l2NftsRouter = createTRPCRouter({
}))
);

return nfts.result
return nfts
.filter(
(nft) =>
ownerAddress === undefined ||
validateAndParseAddress(nft.owner) ===
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) {
Expand Down Expand Up @@ -134,24 +137,24 @@ export const l2NftsRouter = createTRPCRouter({
try {
const nfts = await getL2NftsForOwner(userAddress, contractAddress);

const ownedNfts: Array<Nft> = nfts.result.map((nft) => {
const media = getMediaObjectFromUrl(
nft.metadata?.normalized.image ?? undefined
);
const ownedNfts: Array<Nft> = 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 };
Expand Down

0 comments on commit e82d7e3

Please sign in to comment.