123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198 |
- /**
- * 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 {
- AccountRole,
- Address,
- Codec,
- Decoder,
- Encoder,
- IAccountMeta,
- IAccountSignerMeta,
- IInstruction,
- IInstructionWithAccounts,
- IInstructionWithData,
- ReadonlyAccount,
- ReadonlySignerAccount,
- TransactionSigner,
- WritableAccount,
- combineCodec,
- getStructDecoder,
- getStructEncoder,
- getU8Decoder,
- getU8Encoder,
- transformEncoder,
- } from '@solana/web3.js';
- import { TOKEN_PROGRAM_ADDRESS } from '../programs';
- import { ResolvedAccount, getAccountMetaFactory } from '../shared';
- export type CloseAccountInstruction<
- TProgram extends string = typeof TOKEN_PROGRAM_ADDRESS,
- TAccountAccount extends string | IAccountMeta<string> = string,
- TAccountDestination extends string | IAccountMeta<string> = string,
- TAccountOwner extends string | IAccountMeta<string> = string,
- TRemainingAccounts extends readonly IAccountMeta<string>[] = [],
- > = IInstruction<TProgram> &
- IInstructionWithData<Uint8Array> &
- IInstructionWithAccounts<
- [
- TAccountAccount extends string
- ? WritableAccount<TAccountAccount>
- : TAccountAccount,
- TAccountDestination extends string
- ? WritableAccount<TAccountDestination>
- : TAccountDestination,
- TAccountOwner extends string
- ? ReadonlyAccount<TAccountOwner>
- : TAccountOwner,
- ...TRemainingAccounts,
- ]
- >;
- export type CloseAccountInstructionData = { discriminator: number };
- export type CloseAccountInstructionDataArgs = {};
- export function getCloseAccountInstructionDataEncoder(): Encoder<CloseAccountInstructionDataArgs> {
- return transformEncoder(
- getStructEncoder([['discriminator', getU8Encoder()]]),
- (value) => ({ ...value, discriminator: 9 })
- );
- }
- export function getCloseAccountInstructionDataDecoder(): Decoder<CloseAccountInstructionData> {
- return getStructDecoder([['discriminator', getU8Decoder()]]);
- }
- export function getCloseAccountInstructionDataCodec(): Codec<
- CloseAccountInstructionDataArgs,
- CloseAccountInstructionData
- > {
- return combineCodec(
- getCloseAccountInstructionDataEncoder(),
- getCloseAccountInstructionDataDecoder()
- );
- }
- export type CloseAccountInput<
- TAccountAccount extends string = string,
- TAccountDestination extends string = string,
- TAccountOwner extends string = string,
- > = {
- /** The account to close. */
- account: Address<TAccountAccount>;
- /** The destination account. */
- destination: Address<TAccountDestination>;
- /** The account's owner or its multisignature account. */
- owner: Address<TAccountOwner> | TransactionSigner<TAccountOwner>;
- multiSigners?: Array<TransactionSigner>;
- };
- export function getCloseAccountInstruction<
- TAccountAccount extends string,
- TAccountDestination extends string,
- TAccountOwner extends string,
- >(
- input: CloseAccountInput<TAccountAccount, TAccountDestination, TAccountOwner>
- ): CloseAccountInstruction<
- typeof TOKEN_PROGRAM_ADDRESS,
- TAccountAccount,
- TAccountDestination,
- (typeof input)['owner'] extends TransactionSigner<TAccountOwner>
- ? ReadonlySignerAccount<TAccountOwner> & IAccountSignerMeta<TAccountOwner>
- : TAccountOwner
- > {
- // Program address.
- const programAddress = TOKEN_PROGRAM_ADDRESS;
- // Original accounts.
- const originalAccounts = {
- account: { value: input.account ?? null, isWritable: true },
- destination: { value: input.destination ?? null, isWritable: true },
- owner: { value: input.owner ?? null, isWritable: false },
- };
- const accounts = originalAccounts as Record<
- keyof typeof originalAccounts,
- ResolvedAccount
- >;
- // Remaining accounts.
- const remainingAccounts: IAccountMeta[] = (args.multiSigners ?? []).map(
- (signer) => ({
- address: signer.address,
- role: AccountRole.READONLY_SIGNER,
- signer,
- })
- );
- const getAccountMeta = getAccountMetaFactory(programAddress, 'programId');
- const instruction = {
- accounts: [
- getAccountMeta(accounts.account),
- getAccountMeta(accounts.destination),
- getAccountMeta(accounts.owner),
- ...remainingAccounts,
- ],
- programAddress,
- data: getCloseAccountInstructionDataEncoder().encode({}),
- } as CloseAccountInstruction<
- typeof TOKEN_PROGRAM_ADDRESS,
- TAccountAccount,
- TAccountDestination,
- (typeof input)['owner'] extends TransactionSigner<TAccountOwner>
- ? ReadonlySignerAccount<TAccountOwner> & IAccountSignerMeta<TAccountOwner>
- : TAccountOwner
- >;
- return instruction;
- }
- export type ParsedCloseAccountInstruction<
- TProgram extends string = typeof TOKEN_PROGRAM_ADDRESS,
- TAccountMetas extends readonly IAccountMeta[] = readonly IAccountMeta[],
- > = {
- programAddress: Address<TProgram>;
- accounts: {
- /** The account to close. */
- account: TAccountMetas[0];
- /** The destination account. */
- destination: TAccountMetas[1];
- /** The account's owner or its multisignature account. */
- owner: TAccountMetas[2];
- };
- data: CloseAccountInstructionData;
- };
- export function parseCloseAccountInstruction<
- TProgram extends string,
- TAccountMetas extends readonly IAccountMeta[],
- >(
- instruction: IInstruction<TProgram> &
- IInstructionWithAccounts<TAccountMetas> &
- IInstructionWithData<Uint8Array>
- ): ParsedCloseAccountInstruction<TProgram, TAccountMetas> {
- if (instruction.accounts.length < 3) {
- // TODO: Coded error.
- throw new Error('Not enough accounts');
- }
- let accountIndex = 0;
- const getNextAccount = () => {
- const accountMeta = instruction.accounts![accountIndex]!;
- accountIndex += 1;
- return accountMeta;
- };
- return {
- programAddress: instruction.programAddress,
- accounts: {
- account: getNextAccount(),
- destination: getNextAccount(),
- owner: getNextAccount(),
- },
- data: getCloseAccountInstructionDataDecoder().decode(instruction.data),
- };
- }
|