From 562627f65233d751d44d6667274912cde76e352c Mon Sep 17 00:00:00 2001 From: MarkSackerberg <93528482+MarkSackerberg@users.noreply.github.com> Date: Thu, 19 Dec 2024 23:07:10 +0100 Subject: [PATCH] add fetchAllAssets helper --- clients/js/src/helpers/fetch.ts | 16 +++++ clients/js/test/helps/fetch.test.ts | 91 +++++++++++++++++++++++++++++ 2 files changed, 107 insertions(+) diff --git a/clients/js/src/helpers/fetch.ts b/clients/js/src/helpers/fetch.ts index d9f45c7e..bb228889 100644 --- a/clients/js/src/helpers/fetch.ts +++ b/clients/js/src/helpers/fetch.ts @@ -147,6 +147,22 @@ export const fetchAsset = async ( return deriveAssetPlugins(assetV1, await fetchCollectionV1(umi, collection)); }; +/** + * Helper function to fetch multiple assets and derive plugins from their collections if applicable. + * + * @param umi Context + * @param assets Array of asset addresses to fetch + * @param options Options, `skipDerivePlugins` plugins from collection is false by default + * @returns Promise of a list of `AssetV1` + */ +export const fetchAllAssets = async ( + umi: Context, + assets: Array, + options: { skipDerivePlugins?: boolean } & RpcGetAccountOptions = {} +): Promise => { + return Promise.all(assets.map((asset) => fetchAsset(umi, asset, options))); +}; + /** * Helper function to fetch a collection. * diff --git a/clients/js/test/helps/fetch.test.ts b/clients/js/test/helps/fetch.test.ts index 6e60c594..930b8be3 100644 --- a/clients/js/test/helps/fetch.test.ts +++ b/clients/js/test/helps/fetch.test.ts @@ -5,6 +5,7 @@ import { fetchAssetsByCollection, fetchAssetsByOwner, fetchCollectionsByUpdateAuthority, + fetchAllAssets, } from '../../src'; import { createUmi } from '../_setupRaw'; import { createAsset, createCollection } from '../_setupSdk'; @@ -161,3 +162,93 @@ test('it can use helper to fetch assets by collection and derive plugins', async } }); }); + +test('it can use helper to fetch all assets', async (t) => { + const umi = await createUmi(); + + const collection = await createCollection(umi, { + plugins: [ + { + type: 'ImmutableMetadata', + }, + ], + }); + + const assetsOfOwner1 = await Promise.all( + Array(2) + .fill(0) + .map((_, index) => + createAsset(umi, { + collection, + name: `Asset ${index + 1}`, + plugins: [ + { + type: 'Attributes', + attributeList: [ + { + key: 'asset', + value: 'asset', + }, + ], + }, + ], + }) + ) + ); + + const assetsOfOwner2 = await Promise.all( + Array(2) + .fill(0) + .map((_, index) => + createAsset(umi, { + collection, + name: `Asset ${index + 1}`, + plugins: [ + { + type: 'Attributes', + attributeList: [ + { + key: 'asset', + value: 'asset', + }, + ], + }, + ], + }) + ) + ); + + const allCreatedAssets = [...assetsOfOwner1, ...assetsOfOwner2]; + + const assetPublicKeys = [...assetsOfOwner1, ...assetsOfOwner2].map( + (asset) => asset.publicKey + ); + + const fetchedAssets = await fetchAllAssets(umi, assetPublicKeys); + + const createdAssetPubkeys = allCreatedAssets + .map((asset) => asset.publicKey) + .sort(); + const fetchedAssetPubkeys = fetchedAssets + .map((asset) => asset.publicKey) + .filter((pubkey) => createdAssetPubkeys.includes(pubkey)) + .sort(); + + t.deepEqual( + fetchedAssetPubkeys, + createdAssetPubkeys, + 'All created assets should be found in fetched assets' + ); + + t.deepEqual( + fetchedAssets[0].attributes, + allCreatedAssets[0].attributes, + 'Asset level attribute plugin should be found in fetched assets' + ); + + t.deepEqual( + fetchedAssets[0].immutableMetadata, + collection.immutableMetadata, + 'Collection level immutableMetadata plugin should be found in fetched assets' + ); +});