Skip to content

Commit

Permalink
Merge branch 'main' of github.com:metaplex-foundation/mpl-asset
Browse files Browse the repository at this point in the history
* 'main' of github.com:metaplex-foundation/mpl-asset:
  Minor helper and test changes.
  • Loading branch information
nhanphan committed Mar 8, 2024
2 parents 15c62b1 + 8bdfa1f commit 080703c
Show file tree
Hide file tree
Showing 26 changed files with 307 additions and 214 deletions.
19 changes: 17 additions & 2 deletions clients/js/src/generated/instructions/addCollectionPlugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@

import {
Context,
Option,
OptionOrNullable,
Pda,
PublicKey,
Signer,
Expand All @@ -17,6 +19,7 @@ import {
import {
Serializer,
mapSerializer,
option,
struct,
u8,
} from '@metaplex-foundation/umi/serializers';
Expand All @@ -25,7 +28,14 @@ import {
ResolvedAccountsWithIndices,
getAccountMetasAndSigners,
} from '../shared';
import { Plugin, PluginArgs, getPluginSerializer } from '../types';
import {
Authority,
AuthorityArgs,
Plugin,
PluginArgs,
getAuthoritySerializer,
getPluginSerializer,
} from '../types';

// Accounts.
export type AddCollectionPluginInstructionAccounts = {
Expand All @@ -45,9 +55,13 @@ export type AddCollectionPluginInstructionAccounts = {
export type AddCollectionPluginInstructionData = {
discriminator: number;
plugin: Plugin;
initAuthority: Option<Authority>;
};

export type AddCollectionPluginInstructionDataArgs = { plugin: PluginArgs };
export type AddCollectionPluginInstructionDataArgs = {
plugin: PluginArgs;
initAuthority: OptionOrNullable<AuthorityArgs>;
};

export function getAddCollectionPluginInstructionDataSerializer(): Serializer<
AddCollectionPluginInstructionDataArgs,
Expand All @@ -62,6 +76,7 @@ export function getAddCollectionPluginInstructionDataSerializer(): Serializer<
[
['discriminator', u8()],
['plugin', getPluginSerializer()],
['initAuthority', option(getAuthoritySerializer())],
],
{ description: 'AddCollectionPluginInstructionData' }
),
Expand Down
19 changes: 17 additions & 2 deletions clients/js/src/generated/instructions/addPlugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@

import {
Context,
Option,
OptionOrNullable,
Pda,
PublicKey,
Signer,
Expand All @@ -17,6 +19,7 @@ import {
import {
Serializer,
mapSerializer,
option,
struct,
u8,
} from '@metaplex-foundation/umi/serializers';
Expand All @@ -25,7 +28,14 @@ import {
ResolvedAccountsWithIndices,
getAccountMetasAndSigners,
} from '../shared';
import { Plugin, PluginArgs, getPluginSerializer } from '../types';
import {
Authority,
AuthorityArgs,
Plugin,
PluginArgs,
getAuthoritySerializer,
getPluginSerializer,
} from '../types';

// Accounts.
export type AddPluginInstructionAccounts = {
Expand All @@ -47,9 +57,13 @@ export type AddPluginInstructionAccounts = {
export type AddPluginInstructionData = {
discriminator: number;
plugin: Plugin;
initAuthority: Option<Authority>;
};

export type AddPluginInstructionDataArgs = { plugin: PluginArgs };
export type AddPluginInstructionDataArgs = {
plugin: PluginArgs;
initAuthority: OptionOrNullable<AuthorityArgs>;
};

export function getAddPluginInstructionDataSerializer(): Serializer<
AddPluginInstructionDataArgs,
Expand All @@ -64,6 +78,7 @@ export function getAddPluginInstructionDataSerializer(): Serializer<
[
['discriminator', u8()],
['plugin', getPluginSerializer()],
['initAuthority', option(getAuthoritySerializer())],
],
{ description: 'AddPluginInstructionData' }
),
Expand Down
76 changes: 74 additions & 2 deletions clients/js/test/addPlugin.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import {
PluginType,
addCollectionPlugin,
addPlugin,
authority,
create,
createCollection,
fetchAsset,
Expand Down Expand Up @@ -50,7 +51,8 @@ test('it can add a plugin to an asset', async (t) => {
plugin: {
__kind: 'Freeze',
fields: [{ frozen: false }],
}
},
initAuthority: null
}).sendAndConfirm(umi);

const asset1 = await fetchAssetWithPlugins(umi, assetAddress.publicKey);
Expand Down Expand Up @@ -87,6 +89,75 @@ test('it can add a plugin to an asset', async (t) => {
});
});

test('it can add a plugin to an asset with a different authority than the default', async (t) => {
// Given a Umi instance and a new signer.
const umi = await createUmi();
const assetAddress = generateSigner(umi);
const delegateAddress = generateSigner(umi);

// When we create a new account.
await create(umi, {
dataState: DataState.AccountState,
asset: assetAddress,
name: 'Test Bread',
uri: 'https://example.com/bread',
plugins: [],
}).sendAndConfirm(umi);

// Then an account was created with the correct data.
const asset = await fetchAsset(umi, assetAddress.publicKey);
// console.log("Account State:", asset);
t.like(asset, <Asset>{
publicKey: assetAddress.publicKey,
updateAuthority: updateAuthority('Address', [umi.identity.publicKey]),
owner: umi.identity.publicKey,
name: 'Test Bread',
uri: 'https://example.com/bread',
});

await addPlugin(umi, {
asset: assetAddress.publicKey,
plugin: {
__kind: 'Freeze',
fields: [{ frozen: false }],
},
initAuthority: authority('Pubkey', { address: delegateAddress.publicKey }),
}).sendAndConfirm(umi);

const asset1 = await fetchAssetWithPlugins(umi, assetAddress.publicKey);
// console.log(JSON.stringify(asset1, (_, v) => typeof v === 'bigint' ? v.toString() : v, 2));
t.like(asset1, <AssetWithPlugins>{
publicKey: assetAddress.publicKey,
updateAuthority: updateAuthority('Address', [umi.identity.publicKey]),
owner: umi.identity.publicKey,
name: 'Test Bread',
uri: 'https://example.com/bread',
pluginHeader: {
key: 3,
pluginRegistryOffset: BigInt(120),
},
pluginRegistry: {
key: 4,
registry: [
{
pluginType: 2,
offset: BigInt(118),
authority: authority('Pubkey', { address: delegateAddress.publicKey })
},
],
},
plugins: [
{
authority: authority('Pubkey', { address: delegateAddress.publicKey }),
plugin: {
__kind: 'Freeze',
fields: [{ frozen: false }],
},
},
],
});
});

test('it can add a plugin to a collection', async (t) => {
// Given a Umi instance and a new signer.
const umi = await createUmi();
Expand Down Expand Up @@ -115,7 +186,8 @@ test('it can add a plugin to a collection', async (t) => {
plugin: {
__kind: 'Freeze',
fields: [{ frozen: false }],
}
},
initAuthority: null
}).sendAndConfirm(umi);

const asset1 = await fetchCollectionWithPlugins(umi, collectionAddress.publicKey);
Expand Down
3 changes: 2 additions & 1 deletion clients/js/test/approveAuthority.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,8 @@ test('it can add an authority to a plugin', async (t) => {

await addPlugin(umi, {
asset: assetAddress.publicKey,
plugin: plugin('Freeze', [{ frozen: false }])
plugin: plugin('Freeze', [{ frozen: false }]),
initAuthority: null
})
.append(
approvePluginAuthority(umi, {
Expand Down
3 changes: 2 additions & 1 deletion clients/js/test/collect.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,8 @@ test('it can add asset plugin with collect amount', async (t) => {

await addPlugin(umi, {
asset: assetAddress.publicKey,
plugin: plugin('Freeze', [{ frozen: true }])
plugin: plugin('Freeze', [{ frozen: true }]),
initAuthority: null
}).sendAndConfirm(umi);

t.assert(await hasCollectAmount(umi, assetAddress.publicKey), 'Collect amount not found')
Expand Down
13 changes: 7 additions & 6 deletions clients/js/test/createCollection.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -130,13 +130,14 @@ test('it can create a new asset with a collection with collection delegate', asy
await addCollectionPlugin(umi, {
collection: collectionAddress.publicKey,
plugin: plugin('UpdateDelegate', [{}]),
initAuthority: null
}).sendAndConfirm(umi);

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

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


// When we create a new account.
Expand Down
2 changes: 2 additions & 0 deletions clients/js/test/plugins/asset/delegate.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ test('it can delegate a new authority', async (t) => {
__kind: 'Freeze',
fields: [{ frozen: false }],
},
initAuthority: null
}).sendAndConfirm(umi);

await approvePluginAuthority(umi, {
Expand Down Expand Up @@ -102,6 +103,7 @@ test('a delegate can freeze the token', async (t) => {
__kind: 'Freeze',
fields: [{ frozen: false }],
},
initAuthority: null
}).sendAndConfirm(umi);

await approvePluginAuthority(umi, {
Expand Down
1 change: 1 addition & 0 deletions clients/js/test/plugins/asset/delegateTransfer.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ test('a delegate can transfer the asset', async (t) => {
__kind: 'Transfer',
fields: [{}],
},
initAuthority: null
}).sendAndConfirm(umi);

await approvePluginAuthority(umi, {
Expand Down
1 change: 1 addition & 0 deletions clients/js/test/plugins/asset/freeze.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ test('it can freeze and unfreeze an asset', async (t) => {
await addPlugin(umi, {
asset: assetAddress.publicKey,
plugin: plugin('Freeze', [{ frozen: true }]),
initAuthority: null
}).sendAndConfirm(umi);

const asset = await fetchAssetWithPlugins(umi, assetAddress.publicKey);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ test('it can create a new asset with a collection if it is the collection update
__kind: 'UpdateDelegate',
fields: [{}],
},
initAuthority: null
}).sendAndConfirm(umi);

// console.log(JSON.stringify(await fetchCollectionWithPlugins(umi, collectionAddress.publicKey), (_, v) => typeof v === 'bigint' ? v.toString() : v, 2));
Expand Down
1 change: 1 addition & 0 deletions clients/js/test/removePlugin.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ test('it can remove a plugin from an asset', async (t) => {
__kind: 'Freeze',
fields: [{ frozen: false }],
},
initAuthority: null
}).sendAndConfirm(umi);

const asset1 = await fetchAssetWithPlugins(umi, assetAddress.publicKey);
Expand Down
5 changes: 5 additions & 0 deletions clients/js/test/revokeAuthority.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ test('it can remove an authority from a plugin', async (t) => {
__kind: 'Freeze',
fields: [{ frozen: false }],
},
initAuthority: null
})
.append(
approvePluginAuthority(umi, {
Expand Down Expand Up @@ -168,6 +169,7 @@ test('it can remove the default authority from a plugin to make it immutable', a
__kind: 'Freeze',
fields: [{ frozen: false }],
},
initAuthority: null
}).sendAndConfirm(umi);

await approvePluginAuthority(umi, {
Expand Down Expand Up @@ -230,6 +232,7 @@ test('it can remove a pubkey authority from a plugin if that pubkey is the signe
await addPlugin(umi, {
asset: assetAddress.publicKey,
plugin: plugin('Freeze', [{ frozen: false }]),
initAuthority: null
}).sendAndConfirm(umi);

await approvePluginAuthority(umi, {
Expand Down Expand Up @@ -308,6 +311,7 @@ test('it can remove a owner authority from a plugin with other authority', async
await addPlugin(umi, {
asset: assetAddress.publicKey,
plugin: plugin('Freeze', [{ frozen: false }]),
initAuthority: null
}).sendAndConfirm(umi);

await approvePluginAuthority(umi, {
Expand Down Expand Up @@ -384,6 +388,7 @@ test('it cannot remove a none authority from a plugin', async (t) => {
await addPlugin(umi, {
asset: assetAddress.publicKey,
plugin: plugin('Freeze', [{ frozen: false }]),
initAuthority: null
}).sendAndConfirm(umi);

await approvePluginAuthority(umi, {
Expand Down
2 changes: 2 additions & 0 deletions clients/js/test/update.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,7 @@ test('it can update an asset with plugins to be larger', async (t) => {
__kind: 'Freeze',
fields: [{ frozen: false }],
},
initAuthority: null
}).sendAndConfirm(umi);

await update(umi, {
Expand Down Expand Up @@ -158,6 +159,7 @@ test('it can update an asset with plugins to be smaller', async (t) => {
__kind: 'Freeze',
fields: [{ frozen: false }],
},
initAuthority: null
}).sendAndConfirm(umi);

await update(umi, {
Expand Down
Loading

0 comments on commit 080703c

Please sign in to comment.