Skip to content

Commit

Permalink
getMultipleAccounts with chunks
Browse files Browse the repository at this point in the history
  • Loading branch information
MarkSackerberg committed Dec 19, 2024
1 parent e4c5ba1 commit a4eccbf
Showing 1 changed file with 30 additions and 10 deletions.
40 changes: 30 additions & 10 deletions clients/js/src/helpers/fetch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import {
import {
AssetV1,
CollectionV1,
fetchAllAssetV1,
fetchAllCollectionV1,
fetchAssetV1,
fetchCollectionV1,
Expand Down Expand Up @@ -152,17 +153,33 @@ export const fetchAsset = async (
*
* @param umi Context
* @param assets Array of asset addresses to fetch
* @param options Options, `skipDerivePlugins` plugins from collection is false by default
* @param options Options, `skipDerivePlugins` plugins from collection is false by default; `chunksize` how many assets to fetch in a single rpc call.
* @returns Promise of a list of `AssetV1`
*/
export const fetchAllAssets = async (
umi: Context,
assets: Array<PublicKey | string>,
options: { skipDerivePlugins?: boolean } & RpcGetAccountOptions = {}
options: {
skipDerivePlugins?: boolean;
chunkSize?: number;
} & RpcGetAccountOptions = {}
): Promise<AssetV1[]> => {
const assetV1s = await Promise.all(
assets.map((asset) => fetchAssetV1(umi, publicKey(asset)))
);
const chunkSize = options.chunkSize ?? 1000;
const assetChunks = [];
for (let i = 0; i < assets.length; i += chunkSize) {
assetChunks.push(assets.slice(i, i + chunkSize));
}

const assetV1s = (
await Promise.all(
assetChunks.map((chunk) =>
fetchAllAssetV1(
umi,
chunk.map((asset) => publicKey(asset))
)
)
)
).flat();

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

const collections = await fetchAllCollectionV1(umi, collectionKeys);
const collectionMap = collections.reduce(
(map, collection) => {
map[collection.publicKey] = collection;
return map;
},
{} as { [key: string]: CollectionV1 }
);

return assetV1s.map((assetV1) => {
const collection = collectionAddress(assetV1);
if (!collection) {
return assetV1;
}

const matchingCollection = collections.find(
(c) => c.publicKey === collection
);
return deriveAssetPlugins(assetV1, matchingCollection);
return deriveAssetPlugins(assetV1, collectionMap[collection]);
});
};

Expand Down

0 comments on commit a4eccbf

Please sign in to comment.