/** * This code was AUTOGENERATED using the Codama library. * Please DO NOT EDIT THIS FILE, instead use visitors * to add features, then rerun Codama to update it. * * @see https://github.com/codama-idl/codama */ import { combineCodec, fixDecoderSize, fixEncoderSize, getBytesDecoder, getBytesEncoder, getStructDecoder, getStructEncoder, getU64Decoder, getU64Encoder, transformEncoder, type AccountMeta, type Address, type FixedSizeCodec, type FixedSizeDecoder, type FixedSizeEncoder, type Instruction, type InstructionWithAccounts, type InstructionWithData, type ReadonlyAccount, type ReadonlyUint8Array, } from "@solana/kit"; import { PERPETUALS_PROGRAM_ADDRESS } from "../programs"; import { getAccountMetaFactory, type ResolvedAccount } from "../shared"; export const GET_EXACT_OUT_SWAP_AMOUNT_AND_FEES_DISCRIMINATOR = new Uint8Array([ 196, 32, 218, 118, 151, 233, 43, 56, ]); export function getGetExactOutSwapAmountAndFeesDiscriminatorBytes() { return fixEncoderSize(getBytesEncoder(), 8).encode( GET_EXACT_OUT_SWAP_AMOUNT_AND_FEES_DISCRIMINATOR, ); } export type GetExactOutSwapAmountAndFeesInstruction< TProgram extends string = typeof PERPETUALS_PROGRAM_ADDRESS, TAccountPerpetuals extends string | AccountMeta = string, TAccountPool extends string | AccountMeta = string, TAccountReceivingCustody extends string | AccountMeta = string, TAccountReceivingCustodyOracleAccount extends string | AccountMeta = string, TAccountDispensingCustody extends string | AccountMeta = string, TAccountDispensingCustodyOracleAccount extends string | AccountMeta = string, TRemainingAccounts extends readonly AccountMeta[] = [], > = Instruction & InstructionWithData & InstructionWithAccounts< [ TAccountPerpetuals extends string ? ReadonlyAccount : TAccountPerpetuals, TAccountPool extends string ? ReadonlyAccount : TAccountPool, TAccountReceivingCustody extends string ? ReadonlyAccount : TAccountReceivingCustody, TAccountReceivingCustodyOracleAccount extends string ? ReadonlyAccount : TAccountReceivingCustodyOracleAccount, TAccountDispensingCustody extends string ? ReadonlyAccount : TAccountDispensingCustody, TAccountDispensingCustodyOracleAccount extends string ? ReadonlyAccount : TAccountDispensingCustodyOracleAccount, ...TRemainingAccounts, ] >; export type GetExactOutSwapAmountAndFeesInstructionData = { discriminator: ReadonlyUint8Array; amountOut: bigint; }; export type GetExactOutSwapAmountAndFeesInstructionDataArgs = { amountOut: number | bigint; }; export function getGetExactOutSwapAmountAndFeesInstructionDataEncoder(): FixedSizeEncoder { return transformEncoder( getStructEncoder([ ["discriminator", fixEncoderSize(getBytesEncoder(), 8)], ["amountOut", getU64Encoder()], ]), (value) => ({ ...value, discriminator: GET_EXACT_OUT_SWAP_AMOUNT_AND_FEES_DISCRIMINATOR, }), ); } export function getGetExactOutSwapAmountAndFeesInstructionDataDecoder(): FixedSizeDecoder { return getStructDecoder([ ["discriminator", fixDecoderSize(getBytesDecoder(), 8)], ["amountOut", getU64Decoder()], ]); } export function getGetExactOutSwapAmountAndFeesInstructionDataCodec(): FixedSizeCodec< GetExactOutSwapAmountAndFeesInstructionDataArgs, GetExactOutSwapAmountAndFeesInstructionData > { return combineCodec( getGetExactOutSwapAmountAndFeesInstructionDataEncoder(), getGetExactOutSwapAmountAndFeesInstructionDataDecoder(), ); } export type GetExactOutSwapAmountAndFeesInput< TAccountPerpetuals extends string = string, TAccountPool extends string = string, TAccountReceivingCustody extends string = string, TAccountReceivingCustodyOracleAccount extends string = string, TAccountDispensingCustody extends string = string, TAccountDispensingCustodyOracleAccount extends string = string, > = { perpetuals: Address; pool: Address; receivingCustody: Address; receivingCustodyOracleAccount: Address; dispensingCustody: Address; dispensingCustodyOracleAccount: Address; amountOut: GetExactOutSwapAmountAndFeesInstructionDataArgs["amountOut"]; }; export function getGetExactOutSwapAmountAndFeesInstruction< TAccountPerpetuals extends string, TAccountPool extends string, TAccountReceivingCustody extends string, TAccountReceivingCustodyOracleAccount extends string, TAccountDispensingCustody extends string, TAccountDispensingCustodyOracleAccount extends string, TProgramAddress extends Address = typeof PERPETUALS_PROGRAM_ADDRESS, >( input: GetExactOutSwapAmountAndFeesInput< TAccountPerpetuals, TAccountPool, TAccountReceivingCustody, TAccountReceivingCustodyOracleAccount, TAccountDispensingCustody, TAccountDispensingCustodyOracleAccount >, config?: { programAddress?: TProgramAddress }, ): GetExactOutSwapAmountAndFeesInstruction< TProgramAddress, TAccountPerpetuals, TAccountPool, TAccountReceivingCustody, TAccountReceivingCustodyOracleAccount, TAccountDispensingCustody, TAccountDispensingCustodyOracleAccount > { // Program address. const programAddress = config?.programAddress ?? PERPETUALS_PROGRAM_ADDRESS; // Original accounts. const originalAccounts = { perpetuals: { value: input.perpetuals ?? null, isWritable: false }, pool: { value: input.pool ?? null, isWritable: false }, receivingCustody: { value: input.receivingCustody ?? null, isWritable: false, }, receivingCustodyOracleAccount: { value: input.receivingCustodyOracleAccount ?? null, isWritable: false, }, dispensingCustody: { value: input.dispensingCustody ?? null, isWritable: false, }, dispensingCustodyOracleAccount: { value: input.dispensingCustodyOracleAccount ?? null, isWritable: false, }, }; const accounts = originalAccounts as Record< keyof typeof originalAccounts, ResolvedAccount >; // Original args. const args = { ...input }; const getAccountMeta = getAccountMetaFactory(programAddress, "programId"); return Object.freeze({ accounts: [ getAccountMeta(accounts.perpetuals), getAccountMeta(accounts.pool), getAccountMeta(accounts.receivingCustody), getAccountMeta(accounts.receivingCustodyOracleAccount), getAccountMeta(accounts.dispensingCustody), getAccountMeta(accounts.dispensingCustodyOracleAccount), ], data: getGetExactOutSwapAmountAndFeesInstructionDataEncoder().encode( args as GetExactOutSwapAmountAndFeesInstructionDataArgs, ), programAddress, } as GetExactOutSwapAmountAndFeesInstruction< TProgramAddress, TAccountPerpetuals, TAccountPool, TAccountReceivingCustody, TAccountReceivingCustodyOracleAccount, TAccountDispensingCustody, TAccountDispensingCustodyOracleAccount >); } export type ParsedGetExactOutSwapAmountAndFeesInstruction< TProgram extends string = typeof PERPETUALS_PROGRAM_ADDRESS, TAccountMetas extends readonly AccountMeta[] = readonly AccountMeta[], > = { programAddress: Address; accounts: { perpetuals: TAccountMetas[0]; pool: TAccountMetas[1]; receivingCustody: TAccountMetas[2]; receivingCustodyOracleAccount: TAccountMetas[3]; dispensingCustody: TAccountMetas[4]; dispensingCustodyOracleAccount: TAccountMetas[5]; }; data: GetExactOutSwapAmountAndFeesInstructionData; }; export function parseGetExactOutSwapAmountAndFeesInstruction< TProgram extends string, TAccountMetas extends readonly AccountMeta[], >( instruction: Instruction & InstructionWithAccounts & InstructionWithData, ): ParsedGetExactOutSwapAmountAndFeesInstruction { if (instruction.accounts.length < 6) { // TODO: Coded error. throw new Error("Not enough accounts"); } let accountIndex = 0; const getNextAccount = () => { const accountMeta = (instruction.accounts as TAccountMetas)[accountIndex]!; accountIndex += 1; return accountMeta; }; return { programAddress: instruction.programAddress, accounts: { perpetuals: getNextAccount(), pool: getNextAccount(), receivingCustody: getNextAccount(), receivingCustodyOracleAccount: getNextAccount(), dispensingCustody: getNextAccount(), dispensingCustodyOracleAccount: getNextAccount(), }, data: getGetExactOutSwapAmountAndFeesInstructionDataDecoder().decode( instruction.data, ), }; }