Skip to content

Commit

Permalink
Merging in instruction changes.
Browse files Browse the repository at this point in the history
  • Loading branch information
blockiosaurus committed Mar 5, 2024
2 parents 7a1b140 + 716dacf commit 1f43ea8
Show file tree
Hide file tree
Showing 18 changed files with 650 additions and 27 deletions.
88 changes: 88 additions & 0 deletions clients/js/src/generated/instructions/collect.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
/**
* This code was AUTOGENERATED using the kinobi library.
* Please DO NOT EDIT THIS FILE, instead use visitors
* to add features, then rerun kinobi to update it.
*
* @see https://github.com/metaplex-foundation/kinobi
*/

import {
Context,
Pda,
PublicKey,
TransactionBuilder,
transactionBuilder,
} from '@metaplex-foundation/umi';
import {
Serializer,
mapSerializer,
struct,
u8,
} from '@metaplex-foundation/umi/serializers';
import {
ResolvedAccount,
ResolvedAccountsWithIndices,
getAccountMetasAndSigners,
} from '../shared';

// Accounts.
export type CollectInstructionAccounts = {
/** The address of the recipient */
recipient: PublicKey | Pda;
};

// Data.
export type CollectInstructionData = { discriminator: number };

export type CollectInstructionDataArgs = {};

export function getCollectInstructionDataSerializer(): Serializer<
CollectInstructionDataArgs,
CollectInstructionData
> {
return mapSerializer<CollectInstructionDataArgs, any, CollectInstructionData>(
struct<CollectInstructionData>([['discriminator', u8()]], {
description: 'CollectInstructionData',
}),
(value) => ({ ...value, discriminator: 19 })
) as Serializer<CollectInstructionDataArgs, CollectInstructionData>;
}

// Instruction.
export function collect(
context: Pick<Context, 'programs'>,
input: CollectInstructionAccounts
): TransactionBuilder {
// Program ID.
const programId = context.programs.getPublicKey(
'mplCore',
'CoREzp6dAdLVRKf3EM5tWrsXM2jQwRFeu5uhzsAyjYXL'
);

// Accounts.
const resolvedAccounts: ResolvedAccountsWithIndices = {
recipient: { index: 0, isWritable: true, value: input.recipient ?? null },
};

// Accounts in order.
const orderedAccounts: ResolvedAccount[] = Object.values(
resolvedAccounts
).sort((a, b) => a.index - b.index);

// Keys and Signers.
const [keys, signers] = getAccountMetasAndSigners(
orderedAccounts,
'programId',
programId
);

// Data.
const data = getCollectInstructionDataSerializer().serialize({});

// Bytes Created On Chain.
const bytesCreatedOnChain = 0;

return transactionBuilder([
{ instruction: { keys, programId, data }, signers, bytesCreatedOnChain },
]);
}
1 change: 1 addition & 0 deletions clients/js/src/generated/instructions/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ export * from './addPlugin';
export * from './addPluginAuthority';
export * from './burn';
export * from './burnCollection';
export * from './collect';
export * from './compress';
export * from './create';
export * from './createCollection';
Expand Down
99 changes: 99 additions & 0 deletions clients/js/test/collect.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
import { PublicKey, Umi, generateSigner, 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';

const hasCollectAmount = async (umi: Umi, address: PublicKey) => {
const account = await umi.rpc.getAccount(address);
if (account.exists) {
const rent = await umi.rpc.getRent(account.data.length)
const diff = account.lamports.basisPoints - rent.basisPoints
return diff === sol(0.0015).basisPoints
}
return false
}

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);

// 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')
});

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);

await addPlugin(umi, {
asset: assetAddress.publicKey,
addPluginArgs: { plugin: plugin('Freeze', [{ frozen: true }]) }
}).sendAndConfirm(umi);

t.assert(await hasCollectAmount(umi, assetAddress.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);

// 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')

await removePlugin(umi, {
asset: assetAddress.publicKey,
removePluginArgs: { pluginType: PluginType.Freeze, }
}).sendAndConfirm(umi);
t.assert(await hasCollectAmount(umi, assetAddress.publicKey), 'Collect amount not found')
});
2 changes: 1 addition & 1 deletion clients/js/test/plugins/asset/burn.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ test('it can burn an asset as the owner', async (t) => {
const afterAsset = await umi.rpc.getAccount(assetAddress.publicKey);
t.true(afterAsset.exists);
assertAccountExists(afterAsset);
t.deepEqual(afterAsset.lamports, sol(0.00089784));
t.deepEqual(afterAsset.lamports, sol(0.00089784 + 0.0015));
t.is(afterAsset.data.length, 1);
t.is(afterAsset.data[0], Key.Uninitialized);
});
Expand Down
Loading

0 comments on commit 1f43ea8

Please sign in to comment.