123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199 |
- /**
- * 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 {
- Address,
- Codec,
- Decoder,
- Encoder,
- IAccountMeta,
- IInstruction,
- IInstructionWithAccounts,
- IInstructionWithData,
- ReadonlyAccount,
- WritableAccount,
- combineCodec,
- getStructDecoder,
- getStructEncoder,
- getU8Decoder,
- getU8Encoder,
- transformEncoder,
- } from '@solana/web3.js';
- import { TOKEN_PROGRAM_ADDRESS } from '../programs';
- import { ResolvedAccount, getAccountMetaFactory } from '../shared';
- export type InitializeTokenInstruction<
- TProgram extends string = typeof TOKEN_PROGRAM_ADDRESS,
- TAccountAccount extends string | IAccountMeta<string> = string,
- TAccountMint extends string | IAccountMeta<string> = string,
- TAccountOwner extends string | IAccountMeta<string> = string,
- TAccountRent extends
- | string
- | IAccountMeta<string> = 'SysvarRent111111111111111111111111111111111',
- TRemainingAccounts extends readonly IAccountMeta<string>[] = [],
- > = IInstruction<TProgram> &
- IInstructionWithData<Uint8Array> &
- IInstructionWithAccounts<
- [
- TAccountAccount extends string
- ? WritableAccount<TAccountAccount>
- : TAccountAccount,
- TAccountMint extends string
- ? ReadonlyAccount<TAccountMint>
- : TAccountMint,
- TAccountOwner extends string
- ? ReadonlyAccount<TAccountOwner>
- : TAccountOwner,
- TAccountRent extends string
- ? ReadonlyAccount<TAccountRent>
- : TAccountRent,
- ...TRemainingAccounts,
- ]
- >;
- export type InitializeTokenInstructionData = { discriminator: number };
- export type InitializeTokenInstructionDataArgs = {};
- export function getInitializeTokenInstructionDataEncoder(): Encoder<InitializeTokenInstructionDataArgs> {
- return transformEncoder(
- getStructEncoder([['discriminator', getU8Encoder()]]),
- (value) => ({ ...value, discriminator: 1 })
- );
- }
- export function getInitializeTokenInstructionDataDecoder(): Decoder<InitializeTokenInstructionData> {
- return getStructDecoder([['discriminator', getU8Decoder()]]);
- }
- export function getInitializeTokenInstructionDataCodec(): Codec<
- InitializeTokenInstructionDataArgs,
- InitializeTokenInstructionData
- > {
- return combineCodec(
- getInitializeTokenInstructionDataEncoder(),
- getInitializeTokenInstructionDataDecoder()
- );
- }
- export type InitializeTokenInput<
- TAccountAccount extends string = string,
- TAccountMint extends string = string,
- TAccountOwner extends string = string,
- TAccountRent extends string = string,
- > = {
- account: Address<TAccountAccount>;
- mint: Address<TAccountMint>;
- owner: Address<TAccountOwner>;
- rent?: Address<TAccountRent>;
- };
- export function getInitializeTokenInstruction<
- TAccountAccount extends string,
- TAccountMint extends string,
- TAccountOwner extends string,
- TAccountRent extends string,
- >(
- input: InitializeTokenInput<
- TAccountAccount,
- TAccountMint,
- TAccountOwner,
- TAccountRent
- >
- ): InitializeTokenInstruction<
- typeof TOKEN_PROGRAM_ADDRESS,
- TAccountAccount,
- TAccountMint,
- TAccountOwner,
- TAccountRent
- > {
- // Program address.
- const programAddress = TOKEN_PROGRAM_ADDRESS;
- // Original accounts.
- const originalAccounts = {
- account: { value: input.account ?? null, isWritable: true },
- mint: { value: input.mint ?? null, isWritable: false },
- owner: { value: input.owner ?? null, isWritable: false },
- rent: { value: input.rent ?? null, isWritable: false },
- };
- const accounts = originalAccounts as Record<
- keyof typeof originalAccounts,
- ResolvedAccount
- >;
- // Resolve default values.
- if (!accounts.rent.value) {
- accounts.rent.value =
- 'SysvarRent111111111111111111111111111111111' as Address<'SysvarRent111111111111111111111111111111111'>;
- }
- const getAccountMeta = getAccountMetaFactory(programAddress, 'programId');
- const instruction = {
- accounts: [
- getAccountMeta(accounts.account),
- getAccountMeta(accounts.mint),
- getAccountMeta(accounts.owner),
- getAccountMeta(accounts.rent),
- ],
- programAddress,
- data: getInitializeTokenInstructionDataEncoder().encode({}),
- } as InitializeTokenInstruction<
- typeof TOKEN_PROGRAM_ADDRESS,
- TAccountAccount,
- TAccountMint,
- TAccountOwner,
- TAccountRent
- >;
- return instruction;
- }
- export type ParsedInitializeTokenInstruction<
- TProgram extends string = typeof TOKEN_PROGRAM_ADDRESS,
- TAccountMetas extends readonly IAccountMeta[] = readonly IAccountMeta[],
- > = {
- programAddress: Address<TProgram>;
- accounts: {
- account: TAccountMetas[0];
- mint: TAccountMetas[1];
- owner: TAccountMetas[2];
- rent: TAccountMetas[3];
- };
- data: InitializeTokenInstructionData;
- };
- export function parseInitializeTokenInstruction<
- TProgram extends string,
- TAccountMetas extends readonly IAccountMeta[],
- >(
- instruction: IInstruction<TProgram> &
- IInstructionWithAccounts<TAccountMetas> &
- IInstructionWithData<Uint8Array>
- ): ParsedInitializeTokenInstruction<TProgram, TAccountMetas> {
- if (instruction.accounts.length < 4) {
- // 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(),
- mint: getNextAccount(),
- owner: getNextAccount(),
- rent: getNextAccount(),
- },
- data: getInitializeTokenInstructionDataDecoder().decode(instruction.data),
- };
- }
|