Skip to content

Commit

Permalink
default plugins to [], default create to {}, refactor update/transfer…
Browse files Browse the repository at this point in the history
… test
  • Loading branch information
nhanphan committed Mar 11, 2024
1 parent 1182ae1 commit 0da9781
Show file tree
Hide file tree
Showing 19 changed files with 102 additions and 193 deletions.
4 changes: 2 additions & 2 deletions clients/js/src/generated/instructions/create.ts
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ export type CreateInstructionDataArgs = {
dataState: DataStateArgs;
name: string;
uri: string;
plugins: Array<PluginAuthorityPairArgs>;
plugins?: Array<PluginAuthorityPairArgs>;
};

export function getCreateInstructionDataSerializer(): Serializer<
Expand All @@ -87,7 +87,7 @@ export function getCreateInstructionDataSerializer(): Serializer<
],
{ description: 'CreateInstructionData' }
),
(value) => ({ ...value, discriminator: 0 })
(value) => ({ ...value, discriminator: 0, plugins: value.plugins ?? [] })
) as Serializer<CreateInstructionDataArgs, CreateInstructionData>;
}

Expand Down
4 changes: 2 additions & 2 deletions clients/js/src/generated/instructions/createCollection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ export type CreateCollectionInstructionData = {
export type CreateCollectionInstructionDataArgs = {
name: string;
uri: string;
plugins: Array<PluginAuthorityPairArgs>;
plugins?: Array<PluginAuthorityPairArgs>;
};

export function getCreateCollectionInstructionDataSerializer(): Serializer<
Expand All @@ -77,7 +77,7 @@ export function getCreateCollectionInstructionDataSerializer(): Serializer<
],
{ description: 'CreateCollectionInstructionData' }
),
(value) => ({ ...value, discriminator: 1 })
(value) => ({ ...value, discriminator: 1, plugins: value.plugins ?? [] })
) as Serializer<
CreateCollectionInstructionDataArgs,
CreateCollectionInstructionData
Expand Down
2 changes: 1 addition & 1 deletion clients/js/src/hooked/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ export type BaseAuthority = {
address?: PublicKey;
};

export type PluginAuthorityType = Pick<Authority, '__kind'>['__kind'];
export type PluginAuthorityType = Authority['__kind'];

export type BasePlugin = {
authority: BaseAuthority;
Expand Down
2 changes: 1 addition & 1 deletion clients/js/test/_setup.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ export const DEFAULT_COLLECTION = {
uri: 'https://example.com/collection',
};

export const createAsset = async (umi: Umi, input: CreateAssetHelperArgs) => {
export const createAsset = async (umi: Umi, input: CreateAssetHelperArgs = {}) => {
const payer = input.payer || umi.identity;
const owner = publicKey(input.owner || input.payer || umi.identity);
const asset = input.asset || generateSigner(umi);
Expand Down
4 changes: 2 additions & 2 deletions clients/js/test/addPlugin.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ test('it can add a plugin to an asset', async (t) => {
// Given a Umi instance and a new signer.
const umi = await createUmi();

const asset = await createAsset(umi, {});
const asset = await createAsset(umi);

// Then an account was created with the correct data.
await assertAsset(t, umi, {
Expand Down Expand Up @@ -57,7 +57,7 @@ test('it can add a plugin to an asset with a different authority than the defaul
const umi = await createUmi();
const delegateAddress = generateSigner(umi);

const asset = await createAsset(umi, {});
const asset = await createAsset(umi);

await assertAsset(t, umi, {
...DEFAULT_ASSET,
Expand Down
2 changes: 1 addition & 1 deletion clients/js/test/approveAuthority.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ test('it can add an authority to a plugin', async (t) => {
const umi = await createUmi();
const delegateAddress = generateSigner(umi);

const asset = await createAsset(umi, {});
const asset = await createAsset(umi);

await assertAsset(t, umi, {
asset: asset.publicKey,
Expand Down
4 changes: 2 additions & 2 deletions clients/js/test/burn.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ 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 asset = await createAsset(umi, {});
const asset = await createAsset(umi);
await assertAsset(t, umi, {
...DEFAULT_ASSET,
asset: asset.publicKey,
Expand All @@ -37,7 +37,7 @@ test('it cannot burn an asset if not the owner', async (t) => {
const umi = await createUmi();
const attacker = generateSigner(umi);

const asset = await createAsset(umi, {});
const asset = await createAsset(umi);
await assertAsset(t, umi, {
...DEFAULT_ASSET,
asset: asset.publicKey,
Expand Down
4 changes: 2 additions & 2 deletions clients/js/test/collect.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ 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 asset = await createAsset(umi, {});
const asset = await createAsset(umi);

t.assert(
await hasCollectAmount(umi, asset.publicKey),
Expand All @@ -28,7 +28,7 @@ test('it can create a new asset with collect amount', async (t) => {
test('it can add asset plugin with collect amount', async (t) => {
// Given a Umi instance and a new signer.
const umi = await createUmi();
const asset = await createAsset(umi, {});
const asset = await createAsset(umi);

await addPlugin(umi, {
asset: asset.publicKey,
Expand Down
4 changes: 1 addition & 3 deletions clients/js/test/compress.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ 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 asset = await createAsset(umi, {});
const asset = await createAsset(umi);

// And when we compress the asset.
await compress(umi, {
Expand Down Expand Up @@ -56,7 +56,6 @@ test.skip('it cannot compress an asset if not the owner', async (t) => {
asset: assetAddress,
name: 'Test Bread',
uri: 'https://example.com/bread',
plugins: [],
}).sendAndConfirm(umi);

// Then an account was created with the correct data.
Expand Down Expand Up @@ -99,7 +98,6 @@ test('it cannot compress an asset because it is not available', async (t) => {
asset: assetAddress,
name: 'Test Bread',
uri: 'https://example.com/bread',
plugins: [],
}).sendAndConfirm(umi);

// Then an account was created with the correct data.
Expand Down
4 changes: 0 additions & 4 deletions clients/js/test/create.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,6 @@ test('it can create a new asset with a different payer', async (t) => {
dataState: DataState.AccountState,
asset: assetAddress,
payer,
plugins: [],
}).sendAndConfirm(umi);

const asset = await createAsset(umi, {
Expand Down Expand Up @@ -69,7 +68,6 @@ test.skip('it can create a new asset in ledger state', async (t) => {
name: 'Test Bread',
uri: 'https://example.com/bread',
logWrapper: publicKey('noopb9bkMVfRPU8AsbpTUg8AQkHtKwMYZiFUjNRtMmV'),
plugins: [],
}).sendAndConfirm(umi);

// Then an account was created with the correct data.
Expand Down Expand Up @@ -107,7 +105,6 @@ test('it cannot create a new asset in ledger state because it is not available',
name: 'Test Bread',
uri: 'https://example.com/bread',
logWrapper: publicKey('noopb9bkMVfRPU8AsbpTUg8AQkHtKwMYZiFUjNRtMmV'),
plugins: [],
}).sendAndConfirm(umi);

await t.throwsAsync(result, { name: 'NotAvailable' });
Expand Down Expand Up @@ -208,7 +205,6 @@ test('it cannot create a new asset if the address is already in use', async (t)
asset: assetAddress,
name: DEFAULT_ASSET.name,
uri: DEFAULT_ASSET.uri,
plugins: [],
}).sendAndConfirm(umi);

await t.throwsAsync(result, { name: 'InvalidSystemProgram' });
Expand Down
1 change: 0 additions & 1 deletion clients/js/test/decompress.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@ test.skip('it can decompress a previously compressed asset as the owner', async
asset: assetAddress,
name: 'Test Bread',
uri: 'https://example.com/bread',
plugins: [],
}).sendAndConfirm(umi);

// Then an account was created with the correct data.
Expand Down
2 changes: 0 additions & 2 deletions clients/js/test/info.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ test('fetch account info for account state', async (t) => {
asset: assetAddress,
name: 'Test Bread',
uri: 'https://example.com/bread',
plugins: [],
}).sendAndConfirm(umi);

// Print the size of the account.
Expand Down Expand Up @@ -42,7 +41,6 @@ test.skip('fetch account info for ledger state', async (t) => {
name: 'Test Bread',
uri: 'https://example.com/bread',
logWrapper: publicKey('noopb9bkMVfRPU8AsbpTUg8AQkHtKwMYZiFUjNRtMmV'),
plugins: [],
}).sendAndConfirm(umi);

// Print the size of the account.
Expand Down
2 changes: 1 addition & 1 deletion clients/js/test/plugins/asset/freeze.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ test('it can freeze and unfreeze an asset', async (t) => {
// Given a Umi instance and a new signer.
const umi = await createUmi();

const asset = await createAsset(umi, {});
const asset = await createAsset(umi);

await addPlugin(umi, {
asset: asset.publicKey,
Expand Down
10 changes: 5 additions & 5 deletions clients/js/test/plugins/asset/royalties.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ test('it can transfer an asset with royalties', async (t) => {
});

// Here we're creating a new owner that's program owned, so we're just going to use another asset.
const programOwned = await createAsset(umi, {});
const programOwned = await createAsset(umi);

// Then an account was created with the correct data.
await assertAsset(t, umi, {
Expand Down Expand Up @@ -87,7 +87,7 @@ test('it can transfer an asset with royalties to an allowlisted program address'
});

// Here we're creating a new owner that's program owned, so we're just going to use another asset.
const programOwned = await createAsset(umi, {});
const programOwned = await createAsset(umi);

// Then an account was created with the correct data.
await assertAsset(t, umi, {
Expand Down Expand Up @@ -128,7 +128,7 @@ test('it cannot transfer an asset with royalties to a program address not on the
});

// Create a second one because allowlist needs both to be off the allowlist.
const programOwned2 = await createAsset(umi, {});
const programOwned2 = await createAsset(umi);

// Creating a new asset to transfer.
const asset = await createAsset(umi, {
Expand Down Expand Up @@ -172,7 +172,7 @@ test('it can transfer an asset with royalties to a program address not on the de
const umi = await createUmi();

// Here we're creating a new owner that's program owned, so we're just going to use another asset.
const programOwned = await createAsset(umi, {});
const programOwned = await createAsset(umi);

// Creating a new asset to transfer.
const asset = await createAsset(umi, {
Expand Down Expand Up @@ -214,7 +214,7 @@ test('it cannot transfer an asset with royalties to a denylisted program', async
const umi = await createUmi();

// Here we're creating a new owner that's program owned, so we're just going to use another asset.
const programOwned = await createAsset(umi, {});
const programOwned = await createAsset(umi);

// Creating a new asset to transfer.
const asset = await createAsset(umi, {
Expand Down
77 changes: 15 additions & 62 deletions clients/js/test/transfer.test.ts
Original file line number Diff line number Diff line change
@@ -1,102 +1,55 @@
import { generateSigner } from '@metaplex-foundation/umi';
import test from 'ava';
// import { base58 } from '@metaplex-foundation/umi/serializers';

import {
Asset,
DataState,
create,
fetchAsset,
transfer,
updateAuthority,
} from '../src';
import { createUmi } from './_setup';
import { assertAsset, createAsset, createUmi } from './_setup';

test('it can transfer an asset as the owner', async (t) => {
// Given a Umi instance and a new signer.
const umi = await createUmi();
const assetAddress = generateSigner(umi);
const newOwner = 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, {
asset: asset.publicKey,
owner: umi.identity.publicKey,
name: 'Test Bread',
uri: 'https://example.com/bread',
updateAuthority: updateAuthority('Address', [umi.identity.publicKey]),
});

await transfer(umi, {
asset: assetAddress.publicKey,
asset: asset.publicKey,
newOwner: newOwner.publicKey,
compressionProof: null,
}).sendAndConfirm(umi);

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, {
asset: asset.publicKey,
owner: newOwner.publicKey,
name: 'Test Bread',
uri: 'https://example.com/bread',
updateAuthority: updateAuthority('Address', [umi.identity.publicKey]),
});
});

test('it cannot transfer 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 newOwner = 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]),
owner: umi.identity.publicKey,
name: 'Test Bread',
uri: 'https://example.com/bread',
});
const asset = await createAsset(umi)

const result = transfer(umi, {
asset: assetAddress.publicKey,
asset: asset.publicKey,
newOwner: newOwner.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: updateAuthority('Address', [umi.identity.publicKey]),
await assertAsset(t, umi, {
asset: asset.publicKey,
owner: umi.identity.publicKey,
name: 'Test Bread',
uri: 'https://example.com/bread',
updateAuthority: updateAuthority('Address', [umi.identity.publicKey]),
});
});
Loading

0 comments on commit 0da9781

Please sign in to comment.