Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add collect #5

Merged
merged 1 commit into from
Mar 5, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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: 12 })
) 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 @@ -9,6 +9,7 @@
export * from './addAuthority';
export * from './addPlugin';
export * from './burn';
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,
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,
assetAddress,
name: 'Test Bread',
uri: 'https://example.com/bread',
plugins: []
}).sendAndConfirm(umi);

await addPlugin(umi, {
assetAddress: assetAddress.publicKey,
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,
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, {
assetAddress: assetAddress.publicKey,
pluginType: PluginType.Freeze,
}).sendAndConfirm(umi);
t.assert(await hasCollectAmount(umi, assetAddress.publicKey), 'Collect amount not found')
});
Loading
Loading