Skip to content

Commit

Permalink
fix(api): use marketplace api
Browse files Browse the repository at this point in the history
  • Loading branch information
remiroyc committed Nov 18, 2024
1 parent fa874a6 commit bd99965
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 43 deletions.
48 changes: 31 additions & 17 deletions apps/web/src/server/api/helpers/l2nfts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,29 +8,41 @@ const requestsHeader = {
};
const nftApiUrl = process.env.NEXT_PUBLIC_ARK_API_DOMAIN ?? "";

type ArkCollectionsApiResponse = {
result: Array<{
contract_address: string;
contract_type: string;
image?: string;
name: string;
symbol: string;
tokens_count: number;
type PortfolioApiResponse = {
data: Array<{
best_offer: string | null;
collection_address: string;
collection_name: string;
currency_address: string;
floor: string;
list_price: string;
metadata: {
animation_key: string | null;
animation_mime_type: string | null;
animation_url: string | null;
attributes: string | null;
background_color: string | null;
description: string | null;
external_url: string | null;
image: string | null;
image_data: string | null;
image_key: string | null;
image_mime_type: string | null;
name: string | null;
youtube_url: string | null;
};
}>;
total_count: number;
token_count: number;
};
export async function getL2ContractsForOwner(address: string) {
const url = `${nftApiUrl}/v1/owners/${validateAndParseAddress(
address
)}/contracts`;
const url = `${nftApiUrl}/portfolio/${validateAndParseAddress(address)}`;

const contractsResponse = await fetch(url, {
headers: requestsHeader,
});
const contracts =
(await contractsResponse.json()) as ArkCollectionsApiResponse;
const apiResponse = (await contractsResponse.json()) as PortfolioApiResponse;

return contracts;
return apiResponse;
}

type ArkBatchNftsApiResponse = {
Expand Down Expand Up @@ -152,11 +164,13 @@ export async function getL2WhitelistedCollections() {
}
}

export function getMediaObjectFromUrl(image: string | undefined): NftMedia {
export function getMediaObjectFromUrl(
image: string | undefined | null
): NftMedia {
if (image === undefined) {
return { format: "image", src: undefined };
}
const mediaSrc = image.replace("ipfs://", process.env.IPFS_GATEWAY ?? "");
const mediaSrc = image?.replace("ipfs://", process.env.IPFS_GATEWAY ?? "");
const mediaFormat = mediaSrc?.split(".").pop() === "mp4" ? "video" : "image";

return { format: mediaFormat, src: mediaSrc };
Expand Down
45 changes: 20 additions & 25 deletions apps/web/src/server/api/routers/l2Nfts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,35 +36,30 @@ export const l2NftsRouter = createTRPCRouter({
} = input;

try {
const contractsForOwner = await getL2ContractsForOwner(address);
const response = await getL2ContractsForOwner(address);
const whitelistedCollections = await getL2WhitelistedCollections();

const collections: Array<Collection> = contractsForOwner.result.map(
(contract) => {
const media = getMediaObjectFromUrl(contract.image);
const isBridgeable =
whitelistedCollections === undefined ||
whitelistedCollections.some(
(whitelistedCollection) =>
validateAndParseAddress(
whitelistedCollection
).toLowerCase() ===
validateAndParseAddress(
contract.contract_address
).toLowerCase()
);
const collections: Array<Collection> = response.data.map((contract) => {
const media = getMediaObjectFromUrl(contract.metadata.image);
const isBridgeable =
whitelistedCollections === undefined ||
whitelistedCollections.some(
(whitelistedCollection) =>
validateAndParseAddress(whitelistedCollection).toLowerCase() ===
validateAndParseAddress(
contract.collection_address
).toLowerCase()
);

return {
contractAddress: contract.contract_address,
isBridgeable,
media,
name: contract.name ?? contract.symbol ?? "Unknown",
totalBalance: contract.tokens_count,
};
}
);
return {
contractAddress: contract.collection_address,
isBridgeable,
media,
name: contract.collection_name ?? "Unknown",
};
});

return { collections, totalCount: contractsForOwner.total_count };
return { collections, totalCount: response.token_count };
} catch (error) {
console.error("getL2NftCollectionsByWallet error: ", error);
return { collections: [], totalCount: 0 };
Expand Down
1 change: 0 additions & 1 deletion apps/web/src/server/api/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,5 +12,4 @@ export type Collection = {
isBridgeable: boolean;
media: NftMedia;
name: string;
totalBalance: number;
};

0 comments on commit bd99965

Please sign in to comment.