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 0d2b3e3
Show file tree
Hide file tree
Showing 7 changed files with 8,516 additions and 6,301 deletions.
1 change: 1 addition & 0 deletions apps/web/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
"next-themes": "^0.2.1",
"react": "18.2.0",
"react-dom": "18.2.0",
"sharp": "^0.33.5",
"starknet": "^5.25.5",
"superjson": "1.12.2",
"tailwindcss-animate": "^1.0.7",
Expand Down
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
46 changes: 21 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,31 @@ 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",
totalBalance: 1,
};
});

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;
};
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,5 +29,6 @@
},
"dependencies": {
"dotenv-cli": "^7.0.0"
}
},
"packageManager": "[email protected]"
}
Loading

0 comments on commit 0d2b3e3

Please sign in to comment.