Skip to content

Commit

Permalink
add fetchAllAssets helper
Browse files Browse the repository at this point in the history
  • Loading branch information
MarkSackerberg committed Dec 19, 2024
1 parent 729fde8 commit 562627f
Show file tree
Hide file tree
Showing 2 changed files with 107 additions and 0 deletions.
16 changes: 16 additions & 0 deletions clients/js/src/helpers/fetch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<PublicKey | string>,
options: { skipDerivePlugins?: boolean } & RpcGetAccountOptions = {}
): Promise<AssetV1[]> => {
return Promise.all(assets.map((asset) => fetchAsset(umi, asset, options)));
};

/**
* Helper function to fetch a collection.
*
Expand Down
91 changes: 91 additions & 0 deletions clients/js/test/helps/fetch.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import {
fetchAssetsByCollection,
fetchAssetsByOwner,
fetchCollectionsByUpdateAuthority,
fetchAllAssets,
} from '../../src';
import { createUmi } from '../_setupRaw';
import { createAsset, createCollection } from '../_setupSdk';
Expand Down Expand Up @@ -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'
);
});

0 comments on commit 562627f

Please sign in to comment.