// Copyright © Endless // Copyright © Aptos Foundation // SPDX-License-Identifier: Apache-2.0 import { EndlessConfig } from "../api/endlessConfig"; import { AccountAddress, AccountAddressInput } from "../core/accountAddress"; import { getEndlessIndexer, postEndlessIndexer } from "../client"; import { CollectionResponse, NftMetrics, NftTokenResponse, PageableCollectionResponse, PageableHoldersItem, PageableNftCollectionHistoryRecord, PageableNftTokenHistoryRecord, PageableNftTokenResponse, PaginationArgsOfIndexer, } from "../types"; /** * Get all collections * * @param args.endlessConfig The config for the Endless SDK * @param args.options.page The page number to retrieve * @param args.options.pageSize The number of items to retrieve per page * @returns The collections */ export async function getCollections(args: { endlessConfig: EndlessConfig; options?: PaginationArgsOfIndexer; }): Promise { const { endlessConfig, options } = args; const { data } = await getEndlessIndexer<{}, PageableCollectionResponse>({ endlessConfig, originMethod: "getCollections", path: "collections", params: { page: options?.page, pageSize: options?.pageSize, }, }); return data; } /** * Batch get collections by IDs * * @param args.endlessConfig The config for the Endless SDK * @param args.ids The IDs of the collections to retrieve * @returns The collections */ export async function batchGetCollections(args: { endlessConfig: EndlessConfig; ids: AccountAddressInput[]; }): Promise { const { endlessConfig, ids } = args; const { data } = await postEndlessIndexer<{}, CollectionResponse[]>({ endlessConfig, originMethod: "batchGetCollections", path: "collections", body: ids.map((id) => AccountAddress.from(id).toString()), }); return data; } /** * Get a collection by ID * * @param args.endlessConfig The config for the Endless SDK * @param args.id The ID of the collection to retrieve * @returns The collection */ export async function getCollection(args: { endlessConfig: EndlessConfig; id: AccountAddressInput; }): Promise { const { endlessConfig, id } = args; const { data } = await getEndlessIndexer<{}, CollectionResponse>({ endlessConfig, originMethod: "getCollection", path: `collections/${AccountAddress.from(id).toString()}`, }); return data; } /** * Get collection history by ID * * @param args.endlessConfig The config for the Endless SDK * @param args.id The ID of the collection to retrieve history for * @param args.options.page The page number to retrieve * @param args.options.pageSize The number of items to retrieve per page * @returns The collection history */ export async function getCollectionHistory(args: { endlessConfig: EndlessConfig; id: AccountAddressInput; options?: PaginationArgsOfIndexer; }): Promise { const { endlessConfig, id, options } = args; const { data } = await getEndlessIndexer<{}, PageableNftCollectionHistoryRecord>({ endlessConfig, originMethod: "getCollectionHistory", path: `collections/${AccountAddress.from(id).toString()}/history`, params: { page: options?.page, pageSize: options?.pageSize, }, }); return data; } /** * Get collection holders by ID * * @param args.endlessConfig The config for the Endless SDK * @param args.id The ID of the collection to retrieve holders for * @param args.options.page The page number to retrieve * @param args.options.pageSize The number of items to retrieve per page * @returns The collection holders */ export async function getCollectionHolders(args: { endlessConfig: EndlessConfig; id: AccountAddressInput; options?: PaginationArgsOfIndexer; }): Promise { const { endlessConfig, id, options } = args; const { data } = await getEndlessIndexer<{}, PageableHoldersItem>({ endlessConfig, originMethod: "getCollectionHolders", path: `collections/${AccountAddress.from(id).toString()}/holders`, params: { page: options?.page, pageSize: options?.pageSize, }, }); return data; } /** * Get collection tokens by ID * * @param args.endlessConfig The config for the Endless SDK * @param args.id The ID of the collection to retrieve tokens for * @param args.options.page The page number to retrieve * @param args.options.pageSize The number of items to retrieve per page * @returns The collection tokens */ export async function getCollectionTokens(args: { endlessConfig: EndlessConfig; id: AccountAddressInput; options?: PaginationArgsOfIndexer; }): Promise { const { endlessConfig, id, options } = args; const { data } = await getEndlessIndexer<{}, PageableNftTokenResponse>({ endlessConfig, originMethod: "getCollectionTokens", path: `collections/${AccountAddress.from(id).toString()}/tokens`, params: { page: options?.page, pageSize: options?.pageSize, }, }); return data; } /** * Get an NFT by ID * * @param args.endlessConfig The config for the Endless SDK * @param args.id The ID of the NFT to retrieve * @returns The NFT */ export async function getNft(args: { endlessConfig: EndlessConfig; id: AccountAddressInput; }): Promise { const { endlessConfig, id } = args; const { data } = await getEndlessIndexer<{}, NftTokenResponse>({ endlessConfig, originMethod: "getNft", path: `nfts/${AccountAddress.from(id).toString()}`, }); return data; } /** * Get NFT history by ID * * @param args.endlessConfig The config for the Endless SDK * @param args.id The ID of the NFT to retrieve history for * @param args.options.page The page number to retrieve * @param args.options.pageSize The number of items to retrieve per page * @returns The NFT history */ export async function getNftHistory(args: { endlessConfig: EndlessConfig; id: AccountAddressInput; options?: PaginationArgsOfIndexer; }): Promise { const { endlessConfig, id, options } = args; const { data } = await getEndlessIndexer<{}, PageableNftTokenHistoryRecord>({ endlessConfig, originMethod: "getNftHistory", path: `nfts/${AccountAddress.from(id).toString()}/history`, params: { page: options?.page, pageSize: options?.pageSize, }, }); return data; } /** * Get NFT metrics * * @param args.endlessConfig The config for the Endless SDK * @returns The NFT metrics */ export async function getNftMetrics(args: { endlessConfig: EndlessConfig; }): Promise { const { endlessConfig } = args; const { data } = await getEndlessIndexer<{}, NftMetrics>({ endlessConfig, originMethod: "getNftMetrics", path: "status/nfts", }); return data; }