Skip to content

Commit

Permalink
refactor collection update delegate tests
Browse files Browse the repository at this point in the history
  • Loading branch information
nhanphan committed Mar 9, 2024
1 parent 481a57d commit 1f190c6
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 95 deletions.
16 changes: 10 additions & 6 deletions clients/js/test/_setup.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ import { createUmi as basecreateUmi } from '@metaplex-foundation/umi-bundle-test
import { Assertions } from 'ava';
import { PublicKey, Signer, Umi, generateSigner, publicKey } from '@metaplex-foundation/umi';
import {
Collection,
DataState,
Key,
create,
Expand All @@ -27,6 +26,7 @@ export type CreateAssetHelperArgs = {
dataState?: DataState;
name?: string;
uri?: string;
authority?: Signer;
updateAuthority?: PublicKey | Signer
collection?: PublicKey
// TODO use PluginList type here
Expand Down Expand Up @@ -61,6 +61,7 @@ export const createAsset = async (
uri: input.uri || DEFAULT_ASSET.uri,
plugins: input.plugins || [],
collection: input.collection,
authority: input.authority
}).sendAndConfirm(umi);

return fetchAssetWithPlugins(umi, publicKey(asset));
Expand Down Expand Up @@ -93,10 +94,10 @@ export const createCollection = async (umi: Umi, input:CreateCollectionHelperArg

export const createAssetWithCollection: (
umi: Umi,
assetInput: CreateAssetHelperArgs & { collection?: Collection },
assetInput: CreateAssetHelperArgs & { collection?: PublicKey | Signer},
collectionInput?: CreateCollectionHelperArgs
) => Promise<{ asset: AssetWithPlugins; collection: CollectionWithPlugins }> = async (umi, assetInput, collectionInput = {}) => {
const collection = assetInput.collection ? await fetchCollectionWithPlugins(umi, assetInput.collection.publicKey) : await createCollection(umi, {
const collection = assetInput.collection ? await fetchCollectionWithPlugins(umi, publicKey(assetInput.collection)) : await createCollection(umi, {
payer: assetInput.payer,
updateAuthority: assetInput.updateAuthority,
...collectionInput
Expand Down Expand Up @@ -159,7 +160,7 @@ export const assertCollection = async (
umi: Umi,
input: {
collection: PublicKey | Signer;
updateAuthority: PublicKey | Signer;
updateAuthority?: PublicKey | Signer;
name?: string | RegExp;
uri?: string | RegExp;
numMinted?: number;
Expand All @@ -169,8 +170,7 @@ export const assertCollection = async (
}
) => {
const collectionAddress = publicKey(input.collection);
const updateAuthority = publicKey(input.updateAuthority);
const { name, uri, numMinted, currentSize } = input;
const { name, uri, numMinted, currentSize, updateAuthority } = input;
const collection = await fetchCollectionWithPlugins(umi, collectionAddress);

// Name.
Expand All @@ -196,6 +196,10 @@ export const assertCollection = async (
testObj.currentSize = currentSize;
}

if(updateAuthority) {
testObj.updateAuthority = publicKey(updateAuthority);
}

t.like(collection, testObj);

};
112 changes: 23 additions & 89 deletions clients/js/test/plugins/collection/collectionUpdateDelegate.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,88 +2,38 @@ import { generateSigner } from '@metaplex-foundation/umi';
import test from 'ava';
import { generateSignerWithSol } from '@metaplex-foundation/umi-bundle-tests';
import {
AssetWithPlugins,
CollectionWithPlugins,
DataState,
PluginType,
create,
createCollection,
fetchAssetWithPlugins,
fetchCollectionWithPlugins,
updateAuthority,
addCollectionPlugin,
approveCollectionPluginAuthority,
plugin,
authority,
} from '../../../src';
import { createUmi } from '../../_setup';
import { DEFAULT_ASSET, DEFAULT_COLLECTION, assertAsset, assertCollection, createAsset, createCollection, createUmi } from '../../_setup';

test('it can create a new asset with a collection if it is the collection update delegate', async (t) => {
// Given a Umi instance and a new signer.
const umi = await createUmi();
const collectionAddress = generateSigner(umi);
const assetAddress = generateSigner(umi);
const updateDelegate = await generateSignerWithSol(umi);

// When we create a new account.
await createCollection(umi, {
collection: collectionAddress,
name: 'Test Bread Collection',
uri: 'https://example.com/bread',
plugins: [],
}).sendAndConfirm(umi);

await addCollectionPlugin(umi, {
collection: collectionAddress.publicKey,
plugin: {
__kind: 'UpdateDelegate',
fields: [{}],
},
initAuthority: null
}).sendAndConfirm(umi);

// console.log(JSON.stringify(await fetchCollectionWithPlugins(umi, collectionAddress.publicKey), (_, v) => typeof v === 'bigint' ? v.toString() : v, 2));
const collection = await createCollection(umi, {
plugins: [plugin('UpdateDelegate', [{}])]
})

await approveCollectionPluginAuthority(umi, {
collection: collectionAddress.publicKey,
collection: collection.publicKey,
pluginType: PluginType.UpdateDelegate,
newAuthority: {
__kind: 'Pubkey',
address: updateDelegate.publicKey,
},
newAuthority: authority('Pubkey', { address: updateDelegate.publicKey }),
}).sendAndConfirm(umi);

const collection = await fetchCollectionWithPlugins(
umi,
collectionAddress.publicKey
);
// console.log("Account State:", collection);
t.like(collection, <CollectionWithPlugins>{
publicKey: collectionAddress.publicKey,
await assertCollection(t, umi, {
...DEFAULT_COLLECTION,
collection: collection.publicKey,
updateAuthority: umi.identity.publicKey,
name: 'Test Bread Collection',
uri: 'https://example.com/bread',
pluginHeader: {
key: 3,
pluginRegistryOffset: BigInt(105),
},
pluginRegistry: {
key: 4,
registry: [
{
pluginType: PluginType.UpdateDelegate,
offset: BigInt(104),
authority:
{ __kind: 'Pubkey', address: updateDelegate.publicKey },
},
],
},
plugins: [
{
authority:
{ __kind: 'Pubkey', address: updateDelegate.publicKey },
plugin: {
__kind: 'UpdateDelegate',
fields: [{}],
},
authority: authority('Pubkey', { address: updateDelegate.publicKey }),
plugin: plugin('UpdateDelegate', [{}]),
},
],
});
Expand All @@ -92,33 +42,17 @@ test('it can create a new asset with a collection if it is the collection update
umi.payer = updateDelegate;
const owner = generateSigner(umi);
// When we create a new account.
await create(umi, {
owner: owner.publicKey,
dataState: DataState.AccountState,
asset: assetAddress,
name: 'Test Bread',
uri: 'https://example.com/bread',
collection: collectionAddress.publicKey,
plugins: [],
}).sendAndConfirm(umi);

const asset = await fetchAssetWithPlugins(umi, assetAddress.publicKey);
// console.log("Asset State:", asset);
t.like(asset, <AssetWithPlugins>{
publicKey: assetAddress.publicKey,
updateAuthority: updateAuthority('Collection', [
collectionAddress.publicKey,
]),
const asset = await createAsset(umi, {
collection: collection.publicKey,
owner,
authority: updateDelegate,
})

await assertAsset(t, umi, {
...DEFAULT_ASSET,
asset: asset.publicKey,
owner: owner.publicKey,
name: 'Test Bread',
uri: 'https://example.com/bread',
pluginHeader: {
key: 3,
pluginRegistryOffset: BigInt(118),
},
pluginRegistry: {
key: 4,
},
updateAuthority: updateAuthority('Collection', [collection.publicKey]),
});

t.assert(asset.pluginRegistry?.registry.length === 0);
Expand Down

0 comments on commit 1f190c6

Please sign in to comment.