Skip to content

Commit

Permalink
refactor burn, collect tests
Browse files Browse the repository at this point in the history
  • Loading branch information
nhanphan committed Mar 10, 2024
1 parent 0c83ef4 commit 5347a32
Show file tree
Hide file tree
Showing 5 changed files with 78 additions and 173 deletions.
9 changes: 7 additions & 2 deletions clients/js/src/generated/instructions/burn.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import {
PublicKey,
Signer,
TransactionBuilder,
none,
transactionBuilder,
} from '@metaplex-foundation/umi';
import {
Expand Down Expand Up @@ -57,7 +58,7 @@ export type BurnInstructionData = {
};

export type BurnInstructionDataArgs = {
compressionProof: OptionOrNullable<CompressionProofArgs>;
compressionProof?: OptionOrNullable<CompressionProofArgs>;
};

export function getBurnInstructionDataSerializer(): Serializer<
Expand All @@ -72,7 +73,11 @@ export function getBurnInstructionDataSerializer(): Serializer<
],
{ description: 'BurnInstructionData' }
),
(value) => ({ ...value, discriminator: 12 })
(value) => ({
...value,
discriminator: 12,
compressionProof: value.compressionProof ?? none(),
})
) as Serializer<BurnInstructionDataArgs, BurnInstructionData>;
}

Expand Down
136 changes: 46 additions & 90 deletions clients/js/test/burn.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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, <Asset>{
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));
Expand All @@ -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, <Asset>{
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, <Asset>{
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, <Asset>{
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, <Asset>{
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,
},
});
});
68 changes: 13 additions & 55 deletions clients/js/test/collect.test.ts
Original file line number Diff line number Diff line change
@@ -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);
Expand All @@ -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, <AssetWithPlugins>{
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')
});
31 changes: 5 additions & 26 deletions clients/js/test/compress.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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, <Asset>{
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: [],
};

Expand Down
7 changes: 7 additions & 0 deletions configs/kinobi.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,13 @@ kinobi.update(
defaultValue: k.noneValueNode()
}
}
},
burn: {
arguments: {
compressionProof: {
defaultValue: k.noneValueNode()
}
}
}
})
);
Expand Down

0 comments on commit 5347a32

Please sign in to comment.