From 5347a3252cffd299a0670e227fb0b0622b9205ee Mon Sep 17 00:00:00 2001 From: Nhan Phan Date: Sat, 9 Mar 2024 17:08:47 -0800 Subject: [PATCH] refactor burn, collect tests --- clients/js/src/generated/instructions/burn.ts | 9 +- clients/js/test/burn.test.ts | 136 ++++++------------ clients/js/test/collect.test.ts | 68 ++------- clients/js/test/compress.test.ts | 31 +--- configs/kinobi.cjs | 7 + 5 files changed, 78 insertions(+), 173 deletions(-) diff --git a/clients/js/src/generated/instructions/burn.ts b/clients/js/src/generated/instructions/burn.ts index 07fb61c3..83630196 100644 --- a/clients/js/src/generated/instructions/burn.ts +++ b/clients/js/src/generated/instructions/burn.ts @@ -14,6 +14,7 @@ import { PublicKey, Signer, TransactionBuilder, + none, transactionBuilder, } from '@metaplex-foundation/umi'; import { @@ -57,7 +58,7 @@ export type BurnInstructionData = { }; export type BurnInstructionDataArgs = { - compressionProof: OptionOrNullable; + compressionProof?: OptionOrNullable; }; export function getBurnInstructionDataSerializer(): Serializer< @@ -72,7 +73,11 @@ export function getBurnInstructionDataSerializer(): Serializer< ], { description: 'BurnInstructionData' } ), - (value) => ({ ...value, discriminator: 12 }) + (value) => ({ + ...value, + discriminator: 12, + compressionProof: value.compressionProof ?? none(), + }) ) as Serializer; } diff --git a/clients/js/test/burn.test.ts b/clients/js/test/burn.test.ts index 9301a9b4..d1bb440b 100644 --- a/clients/js/test/burn.test.ts +++ b/clients/js/test/burn.test.ts @@ -4,51 +4,32 @@ import { sol, } from '@metaplex-foundation/umi'; import test from 'ava'; -// import { base58 } from '@metaplex-foundation/umi/serializers'; + import { - Asset, - DataState, - create, - fetchAsset, burn, Key, updateAuthority, plugin, } from '../src'; -import { createUmi } from './_setup'; +import { DEFAULT_ASSET, assertAsset, createAsset, createUmi } from './_setup'; test('it can burn an asset as the owner', async (t) => { // Given a Umi instance and a new signer. const umi = await createUmi(); - const assetAddress = 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 beforeAsset = await fetchAsset(umi, assetAddress.publicKey); - // console.log("Account State:", beforeAsset); - t.like(beforeAsset, { - publicKey: assetAddress.publicKey, - updateAuthority: updateAuthority('Address', [umi.identity.publicKey]), + const asset = await createAsset(umi, {}); + await assertAsset(t, umi, { + ...DEFAULT_ASSET, + asset: asset.publicKey, owner: umi.identity.publicKey, - name: 'Test Bread', - uri: 'https://example.com/bread', - }); + updateAuthority: updateAuthority('Address', [umi.identity.publicKey]), + }) await burn(umi, { - asset: assetAddress.publicKey, - compressionProof: null, + asset: asset.publicKey, }).sendAndConfirm(umi); // And the asset address still exists but was resized to 1. - const afterAsset = await umi.rpc.getAccount(assetAddress.publicKey); + const afterAsset = await umi.rpc.getAccount(asset.publicKey); t.true(afterAsset.exists); assertAccountExists(afterAsset); t.deepEqual(afterAsset.lamports, sol(0.00089784 + 0.0015)); @@ -59,91 +40,66 @@ test('it can burn an asset as the owner', async (t) => { test('it cannot burn an asset if not the owner', async (t) => { // Given a Umi instance and a new signer. const umi = await createUmi(); - const assetAddress = generateSigner(umi); const attacker = 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 beforeAsset = await fetchAsset(umi, assetAddress.publicKey); - // console.log("Account State:", beforeAsset); - t.like(beforeAsset, { - publicKey: assetAddress.publicKey, - updateAuthority: updateAuthority('Address', [umi.identity.publicKey]), + const asset = await createAsset(umi, {}); + await assertAsset(t, umi, { + ...DEFAULT_ASSET, + asset: asset.publicKey, owner: umi.identity.publicKey, - name: 'Test Bread', - uri: 'https://example.com/bread', - }); + updateAuthority: updateAuthority('Address', [umi.identity.publicKey]), + }) const result = burn(umi, { - asset: assetAddress.publicKey, - compressionProof: null, + asset: asset.publicKey, authority: attacker, }).sendAndConfirm(umi); await t.throwsAsync(result, { name: 'InvalidAuthority' }); - - const afterAsset = await fetchAsset(umi, assetAddress.publicKey); - // console.log("Account State:", afterAsset); - t.like(afterAsset, { - publicKey: assetAddress.publicKey, - updateAuthority: updateAuthority('Address', [umi.identity.publicKey]), + await assertAsset(t, umi, { + ...DEFAULT_ASSET, + asset: asset.publicKey, owner: umi.identity.publicKey, - name: 'Test Bread', - uri: 'https://example.com/bread', - }); + updateAuthority: updateAuthority('Address', [umi.identity.publicKey]), + }) }); test('it cannot burn an asset if it is frozen', async (t) => { // Given a Umi instance and a new signer. const umi = await createUmi(); - const assetAddress = generateSigner(umi); - const attacker = 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: [ - plugin('Freeze', [{ frozen: true }]), - ], - }).sendAndConfirm(umi); + const asset = await createAsset(umi, { + plugins: [plugin('Freeze', [{ frozen: true }])], + }); - // Then an account was created with the correct data. - const beforeAsset = await fetchAsset(umi, assetAddress.publicKey); - // console.log("Account State:", beforeAsset); - t.like(beforeAsset, { - publicKey: assetAddress.publicKey, - updateAuthority: updateAuthority('Address', [umi.identity.publicKey]), + await assertAsset(t, umi, { + ...DEFAULT_ASSET, + asset: asset.publicKey, owner: umi.identity.publicKey, - name: 'Test Bread', - uri: 'https://example.com/bread', + updateAuthority: updateAuthority('Address', [umi.identity.publicKey]), + freeze: { + authority: { + owner: true, + }, + frozen: true, + }, }); const result = burn(umi, { - asset: assetAddress.publicKey, - compressionProof: null, - authority: attacker, + asset: asset.publicKey, }).sendAndConfirm(umi); await t.throwsAsync(result, { name: 'InvalidAuthority' }); - - const afterAsset = await fetchAsset(umi, assetAddress.publicKey); - // console.log("Account State:", afterAsset); - t.like(afterAsset, { - publicKey: assetAddress.publicKey, - updateAuthority: updateAuthority('Address', [umi.identity.publicKey]), + await assertAsset(t, umi, { + ...DEFAULT_ASSET, + asset: asset.publicKey, owner: umi.identity.publicKey, - name: 'Test Bread', - uri: 'https://example.com/bread', + updateAuthority: updateAuthority('Address', [umi.identity.publicKey]), + freeze: { + authority: { + owner: true, + }, + frozen: true, + }, }); }); diff --git a/clients/js/test/collect.test.ts b/clients/js/test/collect.test.ts index b6f78baa..ead6a5ce 100644 --- a/clients/js/test/collect.test.ts +++ b/clients/js/test/collect.test.ts @@ -1,18 +1,13 @@ -import { PublicKey, Umi, generateSigner, sol } from '@metaplex-foundation/umi'; +import { PublicKey, Umi, sol } from '@metaplex-foundation/umi'; import test from 'ava'; import { - AssetWithPlugins, - DataState, PluginType, addPlugin, - create, - fetchAssetWithPlugins, plugin, removePlugin, - updateAuthority, } from '../src'; -import { createUmi } from './_setup'; +import { createAsset, createUmi } from './_setup'; const hasCollectAmount = async (umi: Umi, address: PublicKey) => { const account = await umi.rpc.getAccount(address); @@ -27,74 +22,37 @@ const hasCollectAmount = async (umi: Umi, address: PublicKey) => { test('it can create a new asset with collect amount', async (t) => { // Given a Umi instance and a new signer. const umi = await createUmi(); - const assetAddress = generateSigner(umi); + const asset = await createAsset(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 fetchAssetWithPlugins(umi, assetAddress.publicKey); - // console.log("Account State:", asset); - t.like(asset, { - publicKey: assetAddress.publicKey, - updateAuthority: updateAuthority('Address', [umi.identity.publicKey]), - owner: umi.identity.publicKey, - name: 'Test Bread', - uri: 'https://example.com/bread', - }); - - t.assert(await hasCollectAmount(umi, assetAddress.publicKey), 'Collect amount not found') + t.assert(await hasCollectAmount(umi, asset.publicKey), 'Collect amount not found') }); test('it can add asset plugin with collect amount', async (t) => { // Given a Umi instance and a new signer. const umi = await createUmi(); - const assetAddress = 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); + const asset = await createAsset(umi, {}) await addPlugin(umi, { - asset: assetAddress.publicKey, + asset: asset.publicKey, plugin: plugin('Freeze', [{ frozen: true }]), initAuthority: null }).sendAndConfirm(umi); - t.assert(await hasCollectAmount(umi, assetAddress.publicKey), 'Collect amount not found') + t.assert(await hasCollectAmount(umi, asset.publicKey), 'Collect amount not found') }); test('it can add remove asset plugin with collect amount', async (t) => { // Given a Umi instance and a new signer. const umi = await createUmi(); - const assetAddress = generateSigner(umi); + const asset = await createAsset(umi, { + plugins: [plugin('Freeze', [{ frozen: true }])] + }) - // When we create a new account. - await create(umi, { - dataState: DataState.AccountState, - asset: assetAddress, - name: 'Test Bread', - uri: 'https://example.com/bread', - plugins: [ - plugin('Freeze', [{ frozen: false }]) - ] - }).sendAndConfirm(umi); - t.assert(await hasCollectAmount(umi, assetAddress.publicKey), 'Collect amount not found') + t.assert(await hasCollectAmount(umi, asset.publicKey), 'Collect amount not found') await removePlugin(umi, { - asset: assetAddress.publicKey, + asset: asset.publicKey, pluginType: PluginType.Freeze, }).sendAndConfirm(umi); - t.assert(await hasCollectAmount(umi, assetAddress.publicKey), 'Collect amount not found') + t.assert(await hasCollectAmount(umi, asset.publicKey), 'Collect amount not found') }); \ No newline at end of file diff --git a/clients/js/test/compress.test.ts b/clients/js/test/compress.test.ts index 66e50e3e..384e91bd 100644 --- a/clients/js/test/compress.test.ts +++ b/clients/js/test/compress.test.ts @@ -13,49 +13,28 @@ import { HashedAssetSchema, updateAuthority, } from '../src'; -import { createUmi } from './_setup'; -// import bs58 from 'bs58'; +import { createAsset, createUmi } from './_setup'; test.skip('it can compress an asset without any plugins as the owner', async (t) => { // Given a Umi instance and a new signer. const umi = await createUmi(); - const assetAddress = 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 beforeAsset = await fetchAsset(umi, assetAddress.publicKey); - // console.log("Account State:", beforeAsset); - t.like(beforeAsset, { - publicKey: assetAddress.publicKey, - updateAuthority: updateAuthority('Address', [umi.identity.publicKey]), - owner: umi.identity.publicKey, - name: 'Test Bread', - uri: 'https://example.com/bread', - }); + const asset = await createAsset(umi, {}); // And when we compress the asset. await compress(umi, { - asset: assetAddress.publicKey, + asset: asset.publicKey, authority: umi.identity, logWrapper: publicKey('noopb9bkMVfRPU8AsbpTUg8AQkHtKwMYZiFUjNRtMmV'), }).sendAndConfirm(umi); // console.log('Compress signature: ', bs58.encode(tx.signature)); // And the asset is now compressed as a hashed asset. - const afterAsset = await fetchHashedAsset(umi, assetAddress.publicKey); + const afterAsset = await fetchHashedAsset(umi, asset.publicKey); // console.log("Account State:", afterAsset); // And the hash matches the expected value. const hashedAssetSchema: HashedAssetSchema = { - assetHash: hash(getAssetAccountDataSerializer().serialize(beforeAsset)), + assetHash: hash(getAssetAccountDataSerializer().serialize(asset)), pluginHashes: [], }; diff --git a/configs/kinobi.cjs b/configs/kinobi.cjs index 0a3da62e..5bc853f3 100755 --- a/configs/kinobi.cjs +++ b/configs/kinobi.cjs @@ -71,6 +71,13 @@ kinobi.update( defaultValue: k.noneValueNode() } } + }, + burn: { + arguments: { + compressionProof: { + defaultValue: k.noneValueNode() + } + } } }) );