Skip to content

Commit

Permalink
improve to fetch a collection only once
Browse files Browse the repository at this point in the history
  • Loading branch information
MarkSackerberg committed Dec 19, 2024
1 parent 562627f commit e4c5ba1
Showing 1 changed file with 25 additions and 1 deletion.
26 changes: 25 additions & 1 deletion clients/js/src/helpers/fetch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,31 @@ export const fetchAllAssets = async (
assets: Array<PublicKey | string>,
options: { skipDerivePlugins?: boolean } & RpcGetAccountOptions = {}
): Promise<AssetV1[]> => {
return Promise.all(assets.map((asset) => fetchAsset(umi, asset, options)));
const assetV1s = await Promise.all(
assets.map((asset) => fetchAssetV1(umi, publicKey(asset)))
);

if (options.skipDerivePlugins) {
return assetV1s;
}

const collectionKeys = Array.from(
new Set(assetV1s.map((asset) => collectionAddress(asset)))
).filter((collection): collection is PublicKey => !!collection);

const collections = await fetchAllCollectionV1(umi, collectionKeys);

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

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

/**
Expand Down

0 comments on commit e4c5ba1

Please sign in to comment.