From d1be68698864ebc768669da5d7ab5dd9c8124c55 Mon Sep 17 00:00:00 2001 From: migue toscano Date: Tue, 26 Apr 2022 09:46:35 -0300 Subject: [PATCH] Integrated nft pagination method --- src/idls/dab_registries/nft_registry.did.ts | 7 +++++++ src/interfaces/dab_registries/nft_registry.ts | 7 +++++++ src/registries/nfts_registry.ts | 18 ++++++++++++++++++ 3 files changed, 32 insertions(+) diff --git a/src/idls/dab_registries/nft_registry.did.ts b/src/idls/dab_registries/nft_registry.did.ts index a7e2ab0..d2ddcf0 100644 --- a/src/idls/dab_registries/nft_registry.did.ts +++ b/src/idls/dab_registries/nft_registry.did.ts @@ -21,6 +21,12 @@ export default ({ IDL }) => { 'details' : IDL.Vec(IDL.Tuple(IDL.Text, detail_value)), 'principal_id' : IDL.Principal, }); + const paginated_nft_canisters = IDL.Record({ + 'offset': IDL.Nat64, + 'limit': IDL.Nat64, + 'amount': IDL.Nat64, + 'nft_canisters': IDL.Vec(nft_canister), + }); const operation_error = IDL.Variant({ 'NotAuthorized' : IDL.Null, 'BadParameters' : IDL.Null, @@ -35,6 +41,7 @@ export default ({ IDL }) => { 'add' : IDL.Func([nft_canister], [operation_response], []), 'get' : IDL.Func([IDL.Principal], [IDL.Opt(nft_canister)], ['query']), 'get_all' : IDL.Func([], [IDL.Vec(nft_canister)], ['query']), + 'get_all_paginated' : IDL.Func([IDL.Nat64, IDL.Nat64], [paginated_nft_canisters], 'query'), 'name' : IDL.Func([], [IDL.Text], ['query']), 'remove' : IDL.Func([IDL.Principal], [operation_response], []), 'set_controller' : IDL.Func([IDL.Principal], [operation_response], []), diff --git a/src/interfaces/dab_registries/nft_registry.ts b/src/interfaces/dab_registries/nft_registry.ts index 3615318..7975190 100644 --- a/src/interfaces/dab_registries/nft_registry.ts +++ b/src/interfaces/dab_registries/nft_registry.ts @@ -19,6 +19,12 @@ export interface NFTCanisterMetadata { 'principal_id' : Principal, 'details' : Array<[string, DetailValue]>, } +export interface PaginatedNftCanisters { + 'offset': number, + 'limit': number, + 'amount': number, + 'nft_canisters': Array, +} export type Error = { 'NotAuthorized' : null } | { 'BadParameters' : null } | { 'Unknown' : string } | @@ -36,6 +42,7 @@ export default interface NFTRegistryInterface extends RegistryStandard { ) => Promise, 'get' : (arg_0: Principal) => Promise<[] | [NFTCanisterMetadata]>, 'get_all' : () => Promise>, + 'get_all_paginated': (arg_0: number, arg_1: number) => Promise 'name' : () => Promise, 'remove' : (arg_0: Principal) => Promise, 'set_controller' : (arg_0: Principal) => Promise, diff --git a/src/registries/nfts_registry.ts b/src/registries/nfts_registry.ts index 07d992f..9b32ef6 100644 --- a/src/registries/nfts_registry.ts +++ b/src/registries/nfts_registry.ts @@ -4,6 +4,7 @@ import { Principal } from '@dfinity/principal'; import fetch from 'cross-fetch'; import NFTRegistryInterface from '../interfaces/dab_registries/nft_registry'; +import { PaginatedNftCanisters } from '../interfaces/dab_registries/nft_registry'; import { NFTStandards, NFTCollection } from '../interfaces/nft'; import { DABCollection } from '../interfaces/dab_nfts'; @@ -71,6 +72,10 @@ export class NFTRegistry extends Registry { const canistersMetadata = await (this.actor as ActorSubclass).get_all(); return canistersMetadata.map(formatMetadata); } + public getAllPaginated = async(offset: number, limit: number): Promise => { + const canistersMetadata = await (this.actor as ActorSubclass).get_all_paginated(offset, limit); + return canistersMetadata; + } } export const getUserCollectionTokens = async ( @@ -144,6 +149,19 @@ export const getAllNFTS = async ( return allNFTs.map((nft) => ({ ...nft, icon: nft.thumbnail, standard: nft.details.standard as string })); }; +export const getAllNFTSPaginated = async ( + { agent = DEFAULT_AGENT }: { agent?: HttpAgent } = {}, offset: number, limit: number +): Promise => { + const registry = new NFTRegistry(agent); + const allNFTs = await registry.getAllPaginated(offset, limit); + return { + offset: allNFTs.offset, + limit: allNFTs.limit, + amount: allNFTs.amount, + nft_canisters: allNFTs.nft_canisters, + } +}; + export const getAllUserNFTs = async ( { user, agent = DEFAULT_AGENT }: GetAllUserNFTsParams