/** * 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, getU64Decoder, getU64Encoder, getU8Decoder, getU8Encoder, transformEncoder, } from '@solana/web3.js'; import { TOKEN_PROGRAM_ADDRESS } from '../programs'; import { ResolvedAccount, getAccountMetaFactory } from '../shared'; export type ApproveCheckedInstruction< TProgram extends string = typeof TOKEN_PROGRAM_ADDRESS, TAccountSource extends string | IAccountMeta = string, TAccountMint extends string | IAccountMeta = string, TAccountDelegate extends string | IAccountMeta = string, TAccountOwner extends string | IAccountMeta = string, TRemainingAccounts extends readonly IAccountMeta[] = [], > = IInstruction & IInstructionWithData & IInstructionWithAccounts< [ TAccountSource extends string ? WritableAccount : TAccountSource, TAccountMint extends string ? ReadonlyAccount : TAccountMint, TAccountDelegate extends string ? ReadonlyAccount : TAccountDelegate, TAccountOwner extends string ? ReadonlyAccount : TAccountOwner, ...TRemainingAccounts, ] >; export type ApproveCheckedInstructionData = { discriminator: number; /** The amount of tokens the delegate is approved for. */ amount: bigint; /** Expected number of base 10 digits to the right of the decimal place. */ decimals: number; }; export type ApproveCheckedInstructionDataArgs = { /** The amount of tokens the delegate is approved for. */ amount: number | bigint; /** Expected number of base 10 digits to the right of the decimal place. */ decimals: number; }; export function getApproveCheckedInstructionDataEncoder(): Encoder { return transformEncoder( getStructEncoder([ ['discriminator', getU8Encoder()], ['amount', getU64Encoder()], ['decimals', getU8Encoder()], ]), (value) => ({ ...value, discriminator: 13 }) ); } export function getApproveCheckedInstructionDataDecoder(): Decoder { return getStructDecoder([ ['discriminator', getU8Decoder()], ['amount', getU64Decoder()], ['decimals', getU8Decoder()], ]); } export function getApproveCheckedInstructionDataCodec(): Codec< ApproveCheckedInstructionDataArgs, ApproveCheckedInstructionData > { return combineCodec( getApproveCheckedInstructionDataEncoder(), getApproveCheckedInstructionDataDecoder() ); } export type ApproveCheckedInput< TAccountSource extends string = string, TAccountMint extends string = string, TAccountDelegate extends string = string, TAccountOwner extends string = string, > = { /** The source account. */ source: Address; /** The token mint. */ mint: Address; /** The delegate. */ delegate: Address; /** The source account owner or its multisignature account. */ owner: Address | TransactionSigner; amount: ApproveCheckedInstructionDataArgs['amount']; decimals: ApproveCheckedInstructionDataArgs['decimals']; multiSigners?: Array; }; export function getApproveCheckedInstruction< TAccountSource extends string, TAccountMint extends string, TAccountDelegate extends string, TAccountOwner extends string, >( input: ApproveCheckedInput< TAccountSource, TAccountMint, TAccountDelegate, TAccountOwner > ): ApproveCheckedInstruction< typeof TOKEN_PROGRAM_ADDRESS, TAccountSource, TAccountMint, TAccountDelegate, (typeof input)['owner'] extends TransactionSigner ? ReadonlySignerAccount & IAccountSignerMeta : TAccountOwner > { // Program address. const programAddress = TOKEN_PROGRAM_ADDRESS; // Original accounts. const originalAccounts = { source: { value: input.source ?? null, isWritable: true }, mint: { value: input.mint ?? null, isWritable: false }, delegate: { value: input.delegate ?? null, isWritable: false }, owner: { value: input.owner ?? null, isWritable: false }, }; const accounts = originalAccounts as Record< keyof typeof originalAccounts, ResolvedAccount >; // Original args. const args = { ...input }; // 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.source), getAccountMeta(accounts.mint), getAccountMeta(accounts.delegate), getAccountMeta(accounts.owner), ...remainingAccounts, ], programAddress, data: getApproveCheckedInstructionDataEncoder().encode( args as ApproveCheckedInstructionDataArgs ), } as ApproveCheckedInstruction< typeof TOKEN_PROGRAM_ADDRESS, TAccountSource, TAccountMint, TAccountDelegate, (typeof input)['owner'] extends TransactionSigner ? ReadonlySignerAccount & IAccountSignerMeta : TAccountOwner >; return instruction; } export type ParsedApproveCheckedInstruction< TProgram extends string = typeof TOKEN_PROGRAM_ADDRESS, TAccountMetas extends readonly IAccountMeta[] = readonly IAccountMeta[], > = { programAddress: Address; accounts: { /** The source account. */ source: TAccountMetas[0]; /** The token mint. */ mint: TAccountMetas[1]; /** The delegate. */ delegate: TAccountMetas[2]; /** The source account owner or its multisignature account. */ owner: TAccountMetas[3]; }; data: ApproveCheckedInstructionData; }; export function parseApproveCheckedInstruction< TProgram extends string, TAccountMetas extends readonly IAccountMeta[], >( instruction: IInstruction & IInstructionWithAccounts & IInstructionWithData ): ParsedApproveCheckedInstruction { 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: { source: getNextAccount(), mint: getNextAccount(), delegate: getNextAccount(), owner: getNextAccount(), }, data: getApproveCheckedInstructionDataDecoder().decode(instruction.data), }; }