Skip to content

Commit

Permalink
Add burn tests
Browse files Browse the repository at this point in the history
  • Loading branch information
danenbm committed Feb 21, 2024
1 parent c8ec5ff commit 1a56b2a
Showing 1 changed file with 87 additions and 0 deletions.
87 changes: 87 additions & 0 deletions clients/js/test/burn.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
import { assertAccountExists, generateSigner, sol} from '@metaplex-foundation/umi';
import test from 'ava';
// import { base58 } from '@metaplex-foundation/umi/serializers';
import { Asset, DataState, create, fetchAsset, burn, Key } from '../src';
import { 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,
assetAddress,
name: 'Test Bread',
uri: 'https://example.com/bread',
}).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: umi.identity.publicKey,
owner: umi.identity.publicKey,
name: 'Test Bread',
uri: 'https://example.com/bread',
});

await burn(umi, {
assetAddress: assetAddress.publicKey,
compressionProof: null
}).sendAndConfirm(umi);

// And the asset address still exists but was resized to 1.
const afterAsset = await umi.rpc.getAccount(assetAddress.publicKey);
t.true(afterAsset.exists);
assertAccountExists(afterAsset);
t.deepEqual(afterAsset.lamports, sol(0.00089784));
t.is(afterAsset.data.length, 1);
t.is(afterAsset.data[0], Key.Uninitialized);
});

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,
assetAddress,
name: 'Test Bread',
uri: 'https://example.com/bread',
}).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: umi.identity.publicKey,
owner: umi.identity.publicKey,
name: 'Test Bread',
uri: 'https://example.com/bread',
});

const result = burn(umi, {
assetAddress: assetAddress.publicKey,
compressionProof: null,
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: umi.identity.publicKey,
owner: umi.identity.publicKey,
name: 'Test Bread',
uri: 'https://example.com/bread',
});
});

0 comments on commit 1a56b2a

Please sign in to comment.