Skip to content

Commit a4eccbf

Browse files
getMultipleAccounts with chunks
1 parent e4c5ba1 commit a4eccbf

File tree

1 file changed

+30
-10
lines changed

1 file changed

+30
-10
lines changed

clients/js/src/helpers/fetch.ts

+30-10
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import {
88
import {
99
AssetV1,
1010
CollectionV1,
11+
fetchAllAssetV1,
1112
fetchAllCollectionV1,
1213
fetchAssetV1,
1314
fetchCollectionV1,
@@ -152,17 +153,33 @@ export const fetchAsset = async (
152153
*
153154
* @param umi Context
154155
* @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.
156157
* @returns Promise of a list of `AssetV1`
157158
*/
158159
export const fetchAllAssets = async (
159160
umi: Context,
160161
assets: Array<PublicKey | string>,
161-
options: { skipDerivePlugins?: boolean } & RpcGetAccountOptions = {}
162+
options: {
163+
skipDerivePlugins?: boolean;
164+
chunkSize?: number;
165+
} & RpcGetAccountOptions = {}
162166
): 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();
166183

167184
if (options.skipDerivePlugins) {
168185
return assetV1s;
@@ -173,17 +190,20 @@ export const fetchAllAssets = async (
173190
).filter((collection): collection is PublicKey => !!collection);
174191

175192
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+
);
176200

177201
return assetV1s.map((assetV1) => {
178202
const collection = collectionAddress(assetV1);
179203
if (!collection) {
180204
return assetV1;
181205
}
182-
183-
const matchingCollection = collections.find(
184-
(c) => c.publicKey === collection
185-
);
186-
return deriveAssetPlugins(assetV1, matchingCollection);
206+
return deriveAssetPlugins(assetV1, collectionMap[collection]);
187207
});
188208
};
189209

0 commit comments

Comments
 (0)