@@ -104,7 +104,7 @@ async function getNFTsFromInsight(
104104) : Promise < NFT [ ] > {
105105 const { contract, start, count = Number ( DEFAULT_QUERY_ALL_COUNT ) } = options ;
106106
107- const [ result , supply ] = await Promise . all ( [
107+ const [ result , supplyInfo ] = await Promise . all ( [
108108 getContractNFTs ( {
109109 client : contract . client ,
110110 chains : [ contract . chain ] ,
@@ -115,13 +115,21 @@ async function getNFTsFromInsight(
115115 page : start ? Math . floor ( start / count ) : undefined ,
116116 } ,
117117 } ) ,
118- totalSupply ( options ) ,
118+ getSupplyInfo ( options ) . catch ( ( ) => ( {
119+ maxSupply : 0 ,
120+ startTokenId : 0 ,
121+ } ) ) ,
119122 ] ) ;
120123
121124 const currentOffset = start ?? 0 ;
122125 const expectedResultLength = Math . min (
123126 count ,
124- Math . max ( 0 , Number ( supply ) - currentOffset ) ,
127+ Math . max (
128+ 0 ,
129+ Number ( supplyInfo . maxSupply ) -
130+ Number ( supplyInfo . startTokenId ) -
131+ currentOffset ,
132+ ) ,
125133 ) ;
126134 if ( result . length < expectedResultLength ) {
127135 // fresh contracts might be delayed in indexing, so we fallback to RPC
@@ -134,6 +142,27 @@ async function getNFTsFromInsight(
134142async function getNFTsFromRPC (
135143 options : BaseTransactionOptions < GetNFTsParams > ,
136144) : Promise < NFT [ ] > {
145+ const { startTokenId, maxSupply } = await getSupplyInfo ( options ) ;
146+ const start = BigInt ( options . start ?? 0 ) + startTokenId ;
147+ const count = BigInt ( options . count ?? DEFAULT_QUERY_ALL_COUNT ) ;
148+ const maxId = min ( maxSupply , start + count ) ;
149+ const promises : ReturnType < typeof getNFT > [ ] = [ ] ;
150+
151+ for ( let i = start ; i < maxId ; i ++ ) {
152+ promises . push (
153+ getNFT ( {
154+ ...options ,
155+ tokenId : i ,
156+ includeOwner : options . includeOwners ?? false ,
157+ useIndexer : false ,
158+ } ) ,
159+ ) ;
160+ }
161+
162+ return await Promise . all ( promises ) ;
163+ }
164+
165+ async function getSupplyInfo ( options : BaseTransactionOptions < GetNFTsParams > ) {
137166 const [ startTokenId_ , maxSupply ] = await Promise . allSettled ( [
138167 startTokenId ( options ) ,
139168 nextTokenIdToMint ( options ) ,
@@ -158,23 +187,9 @@ async function getNFTsFromRPC(
158187 }
159188 return [ startTokenId__ , maxSupply_ ] as const ;
160189 } ) ;
161- const start = BigInt ( options . start ?? 0 ) + startTokenId_ ;
162- const count = BigInt ( options . count ?? DEFAULT_QUERY_ALL_COUNT ) ;
163-
164- const maxId = min ( maxSupply + startTokenId_ , start + count ) ;
165-
166- const promises : ReturnType < typeof getNFT > [ ] = [ ] ;
167190
168- for ( let i = start ; i < maxId ; i ++ ) {
169- promises . push (
170- getNFT ( {
171- ...options ,
172- tokenId : i ,
173- includeOwner : options . includeOwners ?? false ,
174- useIndexer : false ,
175- } ) ,
176- ) ;
177- }
178-
179- return await Promise . all ( promises ) ;
191+ return {
192+ startTokenId : startTokenId_ ,
193+ maxSupply,
194+ } ;
180195}
0 commit comments