From e4c5ba196c762850b2a5b68c4c97c9dbfc017935 Mon Sep 17 00:00:00 2001 From: MarkSackerberg <93528482+MarkSackerberg@users.noreply.github.com> Date: Thu, 19 Dec 2024 23:31:35 +0100 Subject: [PATCH] improve to fetch a collection only once --- clients/js/src/helpers/fetch.ts | 26 +++++++++++++++++++++++++- 1 file changed, 25 insertions(+), 1 deletion(-) diff --git a/clients/js/src/helpers/fetch.ts b/clients/js/src/helpers/fetch.ts index bb228889..8aee59fa 100644 --- a/clients/js/src/helpers/fetch.ts +++ b/clients/js/src/helpers/fetch.ts @@ -160,7 +160,31 @@ export const fetchAllAssets = async ( assets: Array, options: { skipDerivePlugins?: boolean } & RpcGetAccountOptions = {} ): Promise => { - 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); + }); }; /**