From f04b1250de53e8dfaf4c9d626c4197fe8e7bc757 Mon Sep 17 00:00:00 2001 From: Nhan Phan Date: Mon, 11 Mar 2024 15:38:21 -0700 Subject: [PATCH] plugin authority pair helpers --- clients/js/src/hooked/pluginHelpers.ts | 50 +++++++++- clients/js/test/burn.test.ts | 7 +- clients/js/test/collect.test.ts | 7 +- clients/js/test/create.test.ts | 17 ++-- clients/js/test/createCollection.test.ts | 15 ++- .../js/test/plugins/asset/delegate.test.ts | 5 +- .../plugins/asset/delegateTransfer.test.ts | 4 +- .../plugins/asset/permanentFreeze.test.ts | 10 +- .../js/test/plugins/asset/royalties.test.ts | 92 +++++++++---------- .../collectionUpdateDelegate.test.ts | 6 +- clients/js/test/removePlugin.test.ts | 4 +- clients/js/test/revokeAuthority.test.ts | 10 +- clients/js/test/update.test.ts | 15 ++- 13 files changed, 147 insertions(+), 95 deletions(-) diff --git a/clients/js/src/hooked/pluginHelpers.ts b/clients/js/src/hooked/pluginHelpers.ts index d87a3a34..0a1f4b84 100644 --- a/clients/js/src/hooked/pluginHelpers.ts +++ b/clients/js/src/hooked/pluginHelpers.ts @@ -1,9 +1,19 @@ +import { none, some } from '@metaplex-foundation/umi'; + import { Key, PluginHeader, Plugin, getPluginSerializer, RegistryRecord, + PluginAuthorityPair, + Authority, + RoyaltiesArgs, + FreezeArgs, + BurnArgs, + TransferArgs, + UpdateDelegateArgs, + PermanentFreezeArgs, } from '../generated'; import { BaseAuthority, PluginsList } from './types'; import { mapAuthority } from './authorityHelpers'; @@ -18,12 +28,46 @@ export function formPluginHeader( }; } +export type PluginAuthorityPairHelperArgs = { + type: 'Royalties' + authority?: Authority + data: RoyaltiesArgs +} | { + type: 'Freeze' + authority?: Authority + data: FreezeArgs +} | { + type: 'Burn' + authority?: Authority + data?: BurnArgs +} | { + type: 'Transfer' + authority?: Authority + data?: TransferArgs +} | { + type: 'UpdateDelegate' + authority?: Authority + data?: UpdateDelegateArgs +} | { + type: 'PermanentFreeze' + authority?: Authority + data: PermanentFreezeArgs +}; + +export function pluginAuthorityPair( args: PluginAuthorityPairHelperArgs): PluginAuthorityPair { + const { type, authority, data } = args; + return { + plugin: { __kind: type, fields: [data as any || {}] }, + authority: authority ? some(authority) : none(), + }; +} + export function mapPluginFields(fields: Array>) { return fields.reduce((acc2, field) => ({ ...acc2, ...field }), {}); } export function mapPlugin({ - plugin, + plugin: plug, authority, offset, }: { @@ -31,7 +75,7 @@ export function mapPlugin({ authority: BaseAuthority; offset: bigint; }): PluginsList { - const pluginKey = toWords(plugin.__kind) + const pluginKey = toWords(plug.__kind) .toLowerCase() .split(' ') .reduce((s, c) => s + (c.charAt(0).toUpperCase() + c.slice(1))); @@ -40,7 +84,7 @@ export function mapPlugin({ [pluginKey]: { authority, offset, - ...('fields' in plugin ? mapPluginFields(plugin.fields) : {}), + ...('fields' in plug ? mapPluginFields(plug.fields) : {}), }, }; } diff --git a/clients/js/test/burn.test.ts b/clients/js/test/burn.test.ts index 8ce370d7..afc0d75b 100644 --- a/clients/js/test/burn.test.ts +++ b/clients/js/test/burn.test.ts @@ -5,7 +5,7 @@ import { } from '@metaplex-foundation/umi'; import test from 'ava'; -import { burn, Key, updateAuthority, plugin } from '../src'; +import { burn, Key, updateAuthority, pluginAuthorityPair } from '../src'; import { DEFAULT_ASSET, assertAsset, createAsset, createUmi } from './_setup'; test('it can burn an asset as the owner', async (t) => { @@ -65,7 +65,10 @@ test('it cannot burn an asset if it is frozen', async (t) => { const asset = await createAsset(umi, { plugins: [ - { plugin: plugin('Freeze', [{ frozen: true }]), authority: null }, + pluginAuthorityPair({ + type: 'Freeze', + data: { frozen: true } + }), ], }); diff --git a/clients/js/test/collect.test.ts b/clients/js/test/collect.test.ts index c2e4c617..a51edfb6 100644 --- a/clients/js/test/collect.test.ts +++ b/clients/js/test/collect.test.ts @@ -1,7 +1,7 @@ import { PublicKey, Umi, sol } from '@metaplex-foundation/umi'; import test from 'ava'; -import { PluginType, addPlugin, plugin, removePlugin } from '../src'; +import { PluginType, addPlugin, plugin, pluginAuthorityPair, removePlugin } from '../src'; import { createAsset, createUmi } from './_setup'; const hasCollectAmount = async (umi: Umi, address: PublicKey) => { @@ -47,7 +47,10 @@ test('it can add remove asset plugin with collect amount', async (t) => { const umi = await createUmi(); const asset = await createAsset(umi, { plugins: [ - { plugin: plugin('Freeze', [{ frozen: true }]), authority: null }, + pluginAuthorityPair({ + type: 'Freeze', + data: { frozen: true }, + }) ], }); diff --git a/clients/js/test/create.test.ts b/clients/js/test/create.test.ts index 451ffc39..64890c09 100644 --- a/clients/js/test/create.test.ts +++ b/clients/js/test/create.test.ts @@ -9,7 +9,7 @@ import { fetchHashedAsset, getAssetAccountDataSerializer, updateAuthority, - plugin, + pluginAuthorityPair, } from '../src'; import { DEFAULT_ASSET, assertAsset, createAsset, createUmi } from './_setup'; @@ -121,12 +121,10 @@ test('it can create a new asset in account state with plugins', async (t) => { asset: assetAddress, name: 'Test Bread', uri: 'https://example.com/bread', - plugins: [ - { - plugin: plugin('Freeze', [{ frozen: false }]), - authority: null, - }, - ], + plugins: [ pluginAuthorityPair({ + type: 'Freeze', + data: { frozen: false }, + })], }).sendAndConfirm(umi); await assertAsset(t, umi, { @@ -175,7 +173,10 @@ test('it can create a new asset in account state with plugins with a different u asset: assetAddress, updateAuthority: updateAuth.publicKey, plugins: [ - { plugin: plugin('Freeze', [{ frozen: false }]), authority: null }, + pluginAuthorityPair({ + type: 'Freeze', + data: { frozen: false }, + }) ], }); diff --git a/clients/js/test/createCollection.test.ts b/clients/js/test/createCollection.test.ts index 4ccc455b..ea2327d3 100644 --- a/clients/js/test/createCollection.test.ts +++ b/clients/js/test/createCollection.test.ts @@ -4,7 +4,7 @@ import { PluginType, approveCollectionPluginAuthority, authority, - plugin, + pluginAuthorityPair, updateAuthority, } from '../src'; import { @@ -40,7 +40,10 @@ test('it can create a new collection with plugins', async (t) => { const collection = await createCollection(umi, { plugins: [ - { plugin: plugin('Freeze', [{ frozen: false }]), authority: null }, + pluginAuthorityPair({ + type: 'Freeze', + data: { frozen: false }, + }), ], }); @@ -64,7 +67,7 @@ test('it can create a new asset with a collection', async (t) => { umi, {}, { - plugins: [{ plugin: plugin('UpdateDelegate', [{}]), authority: null }], + plugins: [ pluginAuthorityPair({ type: 'UpdateDelegate' })] } ); @@ -93,7 +96,11 @@ test('it can create a new asset with a collection with collection delegate', asy const delegate = generateSigner(umi); const collection = await createCollection(umi, { - plugins: [{ plugin: plugin('UpdateDelegate', [{}]), authority: null }], + plugins: [ + pluginAuthorityPair({ + type: 'UpdateDelegate', + }) + ] }); await approveCollectionPluginAuthority(umi, { diff --git a/clients/js/test/plugins/asset/delegate.test.ts b/clients/js/test/plugins/asset/delegate.test.ts index d1d6a1ae..0bf5f899 100644 --- a/clients/js/test/plugins/asset/delegate.test.ts +++ b/clients/js/test/plugins/asset/delegate.test.ts @@ -7,6 +7,7 @@ import { updatePlugin, plugin, authority, + pluginAuthorityPair, } from '../../../src'; import { DEFAULT_ASSET, @@ -22,7 +23,7 @@ test('it can delegate a new authority', async (t) => { const asset = await createAsset(umi, { plugins: [ - { plugin: plugin('Freeze', [{ frozen: false }]), authority: null }, + pluginAuthorityPair({ type: 'Freeze', data: { frozen: false }}), ], }); @@ -54,7 +55,7 @@ test('a delegate can freeze the token', async (t) => { const asset = await createAsset(umi, { plugins: [ - { plugin: plugin('Freeze', [{ frozen: false }]), authority: null }, + pluginAuthorityPair({ type: 'Freeze', data: { frozen: false }}), ], }); diff --git a/clients/js/test/plugins/asset/delegateTransfer.test.ts b/clients/js/test/plugins/asset/delegateTransfer.test.ts index 7d198ca4..daa07f8b 100644 --- a/clients/js/test/plugins/asset/delegateTransfer.test.ts +++ b/clients/js/test/plugins/asset/delegateTransfer.test.ts @@ -5,8 +5,8 @@ import { approvePluginAuthority, transfer, updateAuthority, - plugin, authority, + pluginAuthorityPair, } from '../../../src'; import { DEFAULT_ASSET, @@ -22,7 +22,7 @@ test('a delegate can transfer the asset', async (t) => { const newOwnerAddress = generateSigner(umi); const asset = await createAsset(umi, { - plugins: [{ plugin: plugin('Transfer', [{}]), authority: null }], + plugins: [pluginAuthorityPair({ type: 'Transfer' }),], }); await approvePluginAuthority(umi, { diff --git a/clients/js/test/plugins/asset/permanentFreeze.test.ts b/clients/js/test/plugins/asset/permanentFreeze.test.ts index 86227de0..a4cbbf0e 100644 --- a/clients/js/test/plugins/asset/permanentFreeze.test.ts +++ b/clients/js/test/plugins/asset/permanentFreeze.test.ts @@ -1,6 +1,6 @@ import test from 'ava'; import { generateSigner } from '@metaplex-foundation/umi'; -import { addPlugin, plugin, transfer, updateAuthority, updatePlugin } from '../../../src'; +import { addPlugin, plugin, pluginAuthorityPair, transfer, updateAuthority, updatePlugin } from '../../../src'; import { DEFAULT_ASSET, assertAsset, @@ -15,7 +15,9 @@ test('it can freeze and unfreeze an asset', async (t) => { const asset = await createAsset(umi, { owner, - plugins: [{ plugin: plugin('PermanentFreeze', [{ frozen: true }]), authority: null }] + plugins: [ + pluginAuthorityPair({ type: 'PermanentFreeze', data: { frozen: true }}) + ] }); await assertAsset(t, umi, { @@ -58,7 +60,9 @@ test('it cannot be transferred while frozen', async (t) => { const asset = await createAsset(umi, { owner, - plugins: [{ plugin: plugin('PermanentFreeze', [{ frozen: true }]), authority: null }] + plugins: [ + pluginAuthorityPair({ type: 'PermanentFreeze', data: { frozen: true }}), + ] }); await assertAsset(t, umi, { diff --git a/clients/js/test/plugins/asset/royalties.test.ts b/clients/js/test/plugins/asset/royalties.test.ts index fb458aeb..74468e2f 100644 --- a/clients/js/test/plugins/asset/royalties.test.ts +++ b/clients/js/test/plugins/asset/royalties.test.ts @@ -6,7 +6,7 @@ import { } from '@metaplex-foundation/mpl-toolbox'; import { MPL_CORE_PROGRAM_ID, - plugin, + pluginAuthorityPair, ruleSet, transfer, updateAuthority, @@ -23,16 +23,14 @@ test('it can transfer an asset with royalties', async (t) => { const umi = await createUmi(); const asset = await createAsset(umi, { plugins: [ - { - plugin: plugin('Royalties', [ - { - percentage: 5, - creators: [{ address: umi.identity.publicKey, percentage: 100 }], - ruleSet: ruleSet('None'), - }, - ]), - authority: null, - }, + pluginAuthorityPair({ + type: 'Royalties', + data: { + percentage: 5, + creators: [{ address: umi.identity.publicKey, percentage: 100 }], + ruleSet: ruleSet('None'), + }, + }), ], }); @@ -73,16 +71,14 @@ test('it can transfer an asset with royalties to an allowlisted program address' const asset = await createAsset(umi, { plugins: [ - { - plugin: plugin('Royalties', [ - { - percentage: 5, - creators: [{ address: umi.identity.publicKey, percentage: 100 }], - ruleSet: ruleSet('ProgramAllowList', [[MPL_CORE_PROGRAM_ID]]), - }, - ]), - authority: null, - }, + pluginAuthorityPair({ + type: 'Royalties', + data: { + percentage: 5, + creators: [{ address: umi.identity.publicKey, percentage: 100 }], + ruleSet: ruleSet('ProgramAllowList', [[MPL_CORE_PROGRAM_ID]]), + }, + }), ], }); @@ -133,16 +129,14 @@ test('it cannot transfer an asset with royalties to a program address not on the // Creating a new asset to transfer. const asset = await createAsset(umi, { plugins: [ - { - plugin: plugin('Royalties', [ - { - percentage: 5, - creators: [{ address: umi.identity.publicKey, percentage: 100 }], - ruleSet: ruleSet('ProgramAllowList', [[SPL_SYSTEM_PROGRAM_ID]]), - }, - ]), - authority: null, - }, + pluginAuthorityPair({ + type: 'Royalties', + data: { + percentage: 5, + creators: [{ address: umi.identity.publicKey, percentage: 100 }], + ruleSet: ruleSet('ProgramAllowList', [[SPL_SYSTEM_PROGRAM_ID]]), + }, + }), ], }); @@ -177,16 +171,14 @@ test('it can transfer an asset with royalties to a program address not on the de // Creating a new asset to transfer. const asset = await createAsset(umi, { plugins: [ - { - plugin: plugin('Royalties', [ - { - percentage: 5, - creators: [{ address: umi.identity.publicKey, percentage: 100 }], - ruleSet: ruleSet('ProgramDenyList', [[SPL_TOKEN_PROGRAM_ID]]), - }, - ]), - authority: null, - }, + pluginAuthorityPair({ + type: 'Royalties', + data: { + percentage: 5, + creators: [{ address: umi.identity.publicKey, percentage: 100 }], + ruleSet: ruleSet('ProgramDenyList', [[SPL_TOKEN_PROGRAM_ID]]), + }, + }), ], }); @@ -219,16 +211,14 @@ test('it cannot transfer an asset with royalties to a denylisted program', async // Creating a new asset to transfer. const asset = await createAsset(umi, { plugins: [ - { - plugin: plugin('Royalties', [ - { - percentage: 5, - creators: [{ address: umi.identity.publicKey, percentage: 100 }], - ruleSet: ruleSet('ProgramDenyList', [[MPL_CORE_PROGRAM_ID]]), - }, - ]), - authority: null, - }, + pluginAuthorityPair({ + type: 'Royalties', + data: { + percentage: 5, + creators: [{ address: umi.identity.publicKey, percentage: 100 }], + ruleSet: ruleSet('ProgramDenyList', [[MPL_CORE_PROGRAM_ID]]), + }, + }), ], }); diff --git a/clients/js/test/plugins/collection/collectionUpdateDelegate.test.ts b/clients/js/test/plugins/collection/collectionUpdateDelegate.test.ts index f94e297b..2651ece9 100644 --- a/clients/js/test/plugins/collection/collectionUpdateDelegate.test.ts +++ b/clients/js/test/plugins/collection/collectionUpdateDelegate.test.ts @@ -5,8 +5,8 @@ import { PluginType, updateAuthority, approveCollectionPluginAuthority, - plugin, authority, + pluginAuthorityPair, } from '../../../src'; import { DEFAULT_ASSET, @@ -25,7 +25,9 @@ test('it can create a new asset with a collection if it is the collection update // When we create a new account. const collection = await createCollection(umi, { - plugins: [{ plugin: plugin('UpdateDelegate', [{}]), authority: null }], + plugins: [ + pluginAuthorityPair({ type: 'UpdateDelegate' }), + ] }); await approveCollectionPluginAuthority(umi, { diff --git a/clients/js/test/removePlugin.test.ts b/clients/js/test/removePlugin.test.ts index 7ba4c084..5546d63f 100644 --- a/clients/js/test/removePlugin.test.ts +++ b/clients/js/test/removePlugin.test.ts @@ -4,7 +4,7 @@ import { PluginType, fetchAssetWithPlugins, removePlugin, - plugin, + pluginAuthorityPair, } from '../src'; import { assertAsset, createAsset, createUmi } from './_setup'; @@ -14,7 +14,7 @@ test('it can remove a plugin from an asset', async (t) => { const asset = await createAsset(umi, { plugins: [ - { plugin: plugin('Freeze', [{ frozen: false }]), authority: null }, + pluginAuthorityPair({ type: 'Freeze', data: { frozen: false }}), ], }); diff --git a/clients/js/test/revokeAuthority.test.ts b/clients/js/test/revokeAuthority.test.ts index 43898193..76db4c2f 100644 --- a/clients/js/test/revokeAuthority.test.ts +++ b/clients/js/test/revokeAuthority.test.ts @@ -4,11 +4,11 @@ import { generateSignerWithSol } from '@metaplex-foundation/umi-bundle-tests'; import { PluginType, approvePluginAuthority, - plugin, revokePluginAuthority, updateAuthority, getPubkeyAuthority, getNoneAuthority, + pluginAuthorityPair, } from '../src'; import { assertAsset, createAsset, createUmi } from './_setup'; @@ -19,7 +19,7 @@ test('it can remove an authority from a plugin', async (t) => { const asset = await createAsset(umi, { plugins: [ - { plugin: plugin('Freeze', [{ frozen: false }]), authority: null }, + pluginAuthorityPair({ type: 'Freeze', data: { frozen: false }}), ], }); @@ -65,7 +65,7 @@ test('it can remove the default authority from a plugin to make it immutable', a const umi = await createUmi(); const asset = await createAsset(umi, { plugins: [ - { plugin: plugin('Freeze', [{ frozen: false }]), authority: null }, + pluginAuthorityPair({ type: 'Freeze', data: { frozen: false }}), ], }); @@ -95,7 +95,7 @@ test('it can remove a pubkey authority from a plugin if that pubkey is the signe const asset = await createAsset(umi, { plugins: [ - { plugin: plugin('Freeze', [{ frozen: false }]), authority: null }, + pluginAuthorityPair({ type: 'Freeze', data: { frozen: false }}), ], }); @@ -145,7 +145,7 @@ test('it cannot remove a none authority from a plugin', async (t) => { const umi = await createUmi(); const asset = await createAsset(umi, { plugins: [ - { plugin: plugin('Freeze', [{ frozen: false }]), authority: null }, + pluginAuthorityPair({ type: 'Freeze', data: { frozen: false }}), ], }); diff --git a/clients/js/test/update.test.ts b/clients/js/test/update.test.ts index 7d78cd44..5308035f 100644 --- a/clients/js/test/update.test.ts +++ b/clients/js/test/update.test.ts @@ -3,7 +3,7 @@ import test from 'ava'; import { update, updateAuthority, - plugin, + pluginAuthorityPair, } from '../src'; import { assertAsset, createAsset, createUmi } from './_setup'; @@ -53,10 +53,8 @@ test('it can update an asset with plugins to be larger', async (t) => { const asset = await createAsset(umi, { name: 'short', uri: 'https://short.com', - plugins: [{ - plugin: plugin('Freeze', [{ frozen: false }]), - authority: null, - }], + plugins: [ + pluginAuthorityPair({ type: 'Freeze', data: { frozen: false }}),], }); // const asset = await createAsset(umi, { @@ -95,10 +93,9 @@ test('it can update an asset with plugins to be smaller', async (t) => { const asset = await createAsset(umi, { name: 'Test Bread 2', uri: 'https://example.com/bread2', - plugins: [{ - plugin: plugin('Freeze', [{ frozen: false }]), - authority: null, - }], + plugins: [ + pluginAuthorityPair({ type: 'Freeze', data: { frozen: false }}), + ], }); await update(umi, {