@@ -104,7 +104,7 @@ async function getNFTsFromInsight(
104
104
) : Promise < NFT [ ] > {
105
105
const { contract, start, count = Number ( DEFAULT_QUERY_ALL_COUNT ) } = options ;
106
106
107
- const [ result , supply ] = await Promise . all ( [
107
+ const [ result , supplyInfo ] = await Promise . all ( [
108
108
getContractNFTs ( {
109
109
client : contract . client ,
110
110
chains : [ contract . chain ] ,
@@ -115,13 +115,21 @@ async function getNFTsFromInsight(
115
115
page : start ? Math . floor ( start / count ) : undefined ,
116
116
} ,
117
117
} ) ,
118
- totalSupply ( options ) ,
118
+ getSupplyInfo ( options ) . catch ( ( ) => ( {
119
+ maxSupply : 0 ,
120
+ startTokenId : 0 ,
121
+ } ) ) ,
119
122
] ) ;
120
123
121
124
const currentOffset = start ?? 0 ;
122
125
const expectedResultLength = Math . min (
123
126
count ,
124
- Math . max ( 0 , Number ( supply ) - currentOffset ) ,
127
+ Math . max (
128
+ 0 ,
129
+ Number ( supplyInfo . maxSupply ) -
130
+ Number ( supplyInfo . startTokenId ) -
131
+ currentOffset ,
132
+ ) ,
125
133
) ;
126
134
if ( result . length < expectedResultLength ) {
127
135
// fresh contracts might be delayed in indexing, so we fallback to RPC
@@ -134,6 +142,27 @@ async function getNFTsFromInsight(
134
142
async function getNFTsFromRPC (
135
143
options : BaseTransactionOptions < GetNFTsParams > ,
136
144
) : 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 > ) {
137
166
const [ startTokenId_ , maxSupply ] = await Promise . allSettled ( [
138
167
startTokenId ( options ) ,
139
168
nextTokenIdToMint ( options ) ,
@@ -158,23 +187,9 @@ async function getNFTsFromRPC(
158
187
}
159
188
return [ startTokenId__ , maxSupply_ ] as const ;
160
189
} ) ;
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 > [ ] = [ ] ;
167
190
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
+ } ;
180
195
}
0 commit comments