8
8
import {
9
9
AssetV1 ,
10
10
CollectionV1 ,
11
+ fetchAllAssetV1 ,
11
12
fetchAllCollectionV1 ,
12
13
fetchAssetV1 ,
13
14
fetchCollectionV1 ,
@@ -152,17 +153,33 @@ export const fetchAsset = async (
152
153
*
153
154
* @param umi Context
154
155
* @param assets Array of asset addresses to fetch
155
- * @param options Options, `skipDerivePlugins` plugins from collection is false by default
156
+ * @param options Options, `skipDerivePlugins` plugins from collection is false by default; `chunksize` how many assets to fetch in a single rpc call.
156
157
* @returns Promise of a list of `AssetV1`
157
158
*/
158
159
export const fetchAllAssets = async (
159
160
umi : Context ,
160
161
assets : Array < PublicKey | string > ,
161
- options : { skipDerivePlugins ?: boolean } & RpcGetAccountOptions = { }
162
+ options : {
163
+ skipDerivePlugins ?: boolean ;
164
+ chunkSize ?: number ;
165
+ } & RpcGetAccountOptions = { }
162
166
) : Promise < AssetV1 [ ] > => {
163
- const assetV1s = await Promise . all (
164
- assets . map ( ( asset ) => fetchAssetV1 ( umi , publicKey ( asset ) ) )
165
- ) ;
167
+ const chunkSize = options . chunkSize ?? 1000 ;
168
+ const assetChunks = [ ] ;
169
+ for ( let i = 0 ; i < assets . length ; i += chunkSize ) {
170
+ assetChunks . push ( assets . slice ( i , i + chunkSize ) ) ;
171
+ }
172
+
173
+ const assetV1s = (
174
+ await Promise . all (
175
+ assetChunks . map ( ( chunk ) =>
176
+ fetchAllAssetV1 (
177
+ umi ,
178
+ chunk . map ( ( asset ) => publicKey ( asset ) )
179
+ )
180
+ )
181
+ )
182
+ ) . flat ( ) ;
166
183
167
184
if ( options . skipDerivePlugins ) {
168
185
return assetV1s ;
@@ -173,17 +190,20 @@ export const fetchAllAssets = async (
173
190
) . filter ( ( collection ) : collection is PublicKey => ! ! collection ) ;
174
191
175
192
const collections = await fetchAllCollectionV1 ( umi , collectionKeys ) ;
193
+ const collectionMap = collections . reduce (
194
+ ( map , collection ) => {
195
+ map [ collection . publicKey ] = collection ;
196
+ return map ;
197
+ } ,
198
+ { } as { [ key : string ] : CollectionV1 }
199
+ ) ;
176
200
177
201
return assetV1s . map ( ( assetV1 ) => {
178
202
const collection = collectionAddress ( assetV1 ) ;
179
203
if ( ! collection ) {
180
204
return assetV1 ;
181
205
}
182
-
183
- const matchingCollection = collections . find (
184
- ( c ) => c . publicKey === collection
185
- ) ;
186
- return deriveAssetPlugins ( assetV1 , matchingCollection ) ;
206
+ return deriveAssetPlugins ( assetV1 , collectionMap [ collection ] ) ;
187
207
} ) ;
188
208
} ;
189
209
0 commit comments