Skip to content

Commit

Permalink
Adding create with delegates. Also updating burn to use new permissio…
Browse files Browse the repository at this point in the history
…n system.
  • Loading branch information
blockiosaurus committed Feb 26, 2024
1 parent bac0ce0 commit 211f0bb
Show file tree
Hide file tree
Showing 18 changed files with 185 additions and 39 deletions.
13 changes: 12 additions & 1 deletion clients/js/src/generated/instructions/create.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import {
} from '@metaplex-foundation/umi';
import {
Serializer,
array,
mapSerializer,
string,
struct,
Expand All @@ -26,7 +27,14 @@ import {
ResolvedAccountsWithIndices,
getAccountMetasAndSigners,
} from '../shared';
import { DataState, DataStateArgs, getDataStateSerializer } from '../types';
import {
DataState,
DataStateArgs,
Plugin,
PluginArgs,
getDataStateSerializer,
getPluginSerializer,
} from '../types';

// Accounts.
export type CreateInstructionAccounts = {
Expand All @@ -52,12 +60,14 @@ export type CreateInstructionData = {
dataState: DataState;
name: string;
uri: string;
plugins: Array<Plugin>;
};

export type CreateInstructionDataArgs = {
dataState: DataStateArgs;
name: string;
uri: string;
plugins: Array<PluginArgs>;
};

export function getCreateInstructionDataSerializer(): Serializer<
Expand All @@ -71,6 +81,7 @@ export function getCreateInstructionDataSerializer(): Serializer<
['dataState', getDataStateSerializer()],
['name', string()],
['uri', string()],
['plugins', array(getPluginSerializer())],
],
{ description: 'CreateInstructionData' }
),
Expand Down
1 change: 1 addition & 0 deletions clients/js/test/addAuthority.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ test('it can add an authority to a plugin', async (t) => {
assetAddress,
name: 'Test Bread',
uri: 'https://example.com/bread',
plugins: [],
}).sendAndConfirm(umi);

// Then an account was created with the correct data.
Expand Down
1 change: 1 addition & 0 deletions clients/js/test/addPlugin.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ test('it can add a plugin to an asset', async (t) => {
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: 2 additions & 0 deletions clients/js/test/burn.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ test('it can burn an asset as the owner', async (t) => {
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 @@ -58,6 +59,7 @@ test('it cannot burn an asset if not the owner', async (t) => {
assetAddress,
name: 'Test Bread',
uri: 'https://example.com/bread',
plugins: [],
}).sendAndConfirm(umi);

// Then an account was created with the correct data.
Expand Down
16 changes: 9 additions & 7 deletions clients/js/test/compress.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,11 @@ import {
fetchHashedAsset,
getAssetAccountDataSerializer,
getHashedAssetSchemaSerializer,
hash,
HashedAssetSchema,
} from '../src';
import { createUmi } from './_setup';
//import bs58 from 'bs58';
import { hash } from '../src';
// import bs58 from 'bs58';

test('it can compress an asset without any plugins as the owner', async (t) => {
// Given a Umi instance and a new signer.
Expand All @@ -26,11 +26,12 @@ test('it can compress an asset without any plugins as the owner', async (t) => {
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);
// console.log("Account State:", beforeAsset);
t.like(beforeAsset, <Asset>{
publicKey: assetAddress.publicKey,
updateAuthority: umi.identity.publicKey,
Expand All @@ -44,19 +45,19 @@ test('it can compress an asset without any plugins as the owner', async (t) => {
assetAddress: assetAddress.publicKey,
owner: umi.identity,
}).sendAndConfirm(umi);
//console.log('Compress signature: ', bs58.encode(tx.signature));
// 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);
//console.log("Account State:", afterAsset);
// console.log("Account State:", afterAsset);

// And the hash matches the expected value.
let hashedAssetSchema: HashedAssetSchema = {
const hashedAssetSchema: HashedAssetSchema = {
assetHash: hash(getAssetAccountDataSerializer().serialize(beforeAsset)),
pluginHashes: [],
};

let hashedAsset = hash(
const hashedAsset = hash(
getHashedAssetSchemaSerializer().serialize(hashedAssetSchema)
);
t.deepEqual(afterAsset.hash, hashedAsset);
Expand All @@ -74,6 +75,7 @@ test('it cannot compress an asset if not the owner', async (t) => {
assetAddress,
name: 'Test Bread',
uri: 'https://example.com/bread',
plugins: [],
}).sendAndConfirm(umi);

// Then an account was created with the correct data.
Expand Down
55 changes: 55 additions & 0 deletions clients/js/test/create.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,11 @@ import test from 'ava';
// import { base58 } from '@metaplex-foundation/umi/serializers';
import {
Asset,
AssetWithPlugins,
DataState,
create,
fetchAsset,
fetchAssetWithPlugins,
fetchHashedAsset,
getAssetAccountDataSerializer,
} from '../src';
Expand All @@ -22,6 +24,7 @@ test('it can create a new asset in account state', async (t) => {
assetAddress,
name: 'Test Bread',
uri: 'https://example.com/bread',
plugins: []
}).sendAndConfirm(umi);

// Then an account was created with the correct data.
Expand All @@ -48,6 +51,7 @@ test('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 All @@ -72,3 +76,54 @@ test('it can create a new asset in ledger state', async (t) => {
});
}
});

test('it can create a new asset with plugins', 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',
plugins: [{ __kind: 'Freeze', fields: [{ frozen: false }] }]
}).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: umi.identity.publicKey,
owner: umi.identity.publicKey,
name: 'Test Bread',
uri: 'https://example.com/bread',
pluginHeader: {
key: 3,
pluginRegistryOffset: BigInt(119),
},
pluginRegistry: {
key: 4,
registry: [
{
pluginType: 2,
data: {
offset: BigInt(117),
authorities: [{ __kind: 'Owner' }],
},
},
],
},
plugins: [
{
authorities: [{ __kind: 'Owner' }],
plugin: {
__kind: 'Freeze',
fields: [{ frozen: false }],
},
},
],
});
});
9 changes: 5 additions & 4 deletions clients/js/test/decompress.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,11 @@ import {
fetchHashedAsset,
getAssetAccountDataSerializer,
getHashedAssetSchemaSerializer,
hash,
HashedAssetSchema,
Key,
} from '../src';
import { createUmi } from './_setup';
import { hash } from '../src';

test('it can decompress a previously compressed asset as the owner', async (t) => {
// Given a Umi instance and a new signer.
Expand All @@ -27,11 +27,12 @@ test('it can decompress a previously compressed asset as the owner', async (t) =
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);
// console.log("Account State:", beforeAsset);
t.like(beforeAsset, <Asset>{
publicKey: assetAddress.publicKey,
updateAuthority: umi.identity.publicKey,
Expand All @@ -53,12 +54,12 @@ test('it can decompress a previously compressed asset as the owner', async (t) =
);

// And the hash matches the expected value.
let hashedAssetSchema: HashedAssetSchema = {
const hashedAssetSchema: HashedAssetSchema = {
assetHash: hash(getAssetAccountDataSerializer().serialize(beforeAsset)),
pluginHashes: [],
};

let hashedAsset = hash(
const hashedAsset = hash(
getHashedAssetSchemaSerializer().serialize(hashedAssetSchema)
);
t.deepEqual(afterCompressedAsset.hash, hashedAsset);
Expand Down
2 changes: 2 additions & 0 deletions clients/js/test/delegate.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ test('it can delegate a new authority', async (t) => {
assetAddress,
name: 'Test Bread',
uri: 'https://example.com/bread',
plugins: [],
}).sendAndConfirm(umi);

await addPlugin(umi, {
Expand Down Expand Up @@ -98,6 +99,7 @@ test('a delegate can freeze the token', async (t) => {
assetAddress,
name: 'Test Bread',
uri: 'https://example.com/bread',
plugins: [],
}).sendAndConfirm(umi);

await addPlugin(umi, {
Expand Down
1 change: 1 addition & 0 deletions clients/js/test/delegateTransfer.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ test('a delegate can transfer the asset', async (t) => {
assetAddress,
name: 'Test Bread',
uri: 'https://example.com/bread',
plugins: [],
}).sendAndConfirm(umi);

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

// Print the size of the account.
Expand Down Expand Up @@ -41,6 +42,7 @@ test('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: 2 additions & 0 deletions clients/js/test/removeAuthority.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ test('it can remove an authority from a plugin', async (t) => {
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 @@ -156,6 +157,7 @@ test('it can remove the default authority from a plugin to make it immutable', a
assetAddress,
name: 'Test Bread',
uri: 'https://example.com/bread',
plugins: [],
}).sendAndConfirm(umi);

// Then an account was created with the correct data.
Expand Down
1 change: 1 addition & 0 deletions clients/js/test/removePlugin.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ test('it can remove a plugin from an asset', async (t) => {
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: 2 additions & 0 deletions clients/js/test/transfer.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ test('it can transfer an asset as the owner', async (t) => {
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 @@ -59,6 +60,7 @@ test('it cannot transfer an asset if not the owner', async (t) => {
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: 4 additions & 0 deletions clients/js/test/update.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ test('it can update an asset to be larger', async (t) => {
assetAddress,
name: 'Test Bread',
uri: 'https://example.com/bread',
plugins: [],
}).sendAndConfirm(umi);

await update(umi, {
Expand Down Expand Up @@ -53,6 +54,7 @@ test('it can update an asset to be smaller', async (t) => {
assetAddress,
name: 'Test Bread',
uri: 'https://example.com/bread',
plugins: [],
}).sendAndConfirm(umi);

await update(umi, {
Expand Down Expand Up @@ -83,6 +85,7 @@ test('it can update an asset with plugins to be larger', async (t) => {
assetAddress,
name: 'Test Bread',
uri: 'https://example.com/bread',
plugins: [],
}).sendAndConfirm(umi);

await addPlugin(umi, {
Expand Down Expand Up @@ -147,6 +150,7 @@ test('it can update an asset with plugins to be smaller', async (t) => {
assetAddress,
name: 'Test Bread',
uri: 'https://example.com/bread',
plugins: [],
}).sendAndConfirm(umi);

await addPlugin(umi, {
Expand Down
Loading

0 comments on commit 211f0bb

Please sign in to comment.