/** * 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_SWAP_AMOUNT_AND_FEES_DISCRIMINATOR = new Uint8Array([ 247, 121, 40, 99, 35, 82, 100, 32, ]); export function getGetSwapAmountAndFeesDiscriminatorBytes() { return fixEncoderSize(getBytesEncoder(), 8).encode( GET_SWAP_AMOUNT_AND_FEES_DISCRIMINATOR, ); } export type GetSwapAmountAndFeesInstruction< 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 GetSwapAmountAndFeesInstructionData = { discriminator: ReadonlyUint8Array; amountIn: bigint; }; export type GetSwapAmountAndFeesInstructionDataArgs = { amountIn: number | bigint; }; export function getGetSwapAmountAndFeesInstructionDataEncoder(): FixedSizeEncoder { return transformEncoder( getStructEncoder([ ["discriminator", fixEncoderSize(getBytesEncoder(), 8)], ["amountIn", getU64Encoder()], ]), (value) => ({ ...value, discriminator: GET_SWAP_AMOUNT_AND_FEES_DISCRIMINATOR, }), ); } export function getGetSwapAmountAndFeesInstructionDataDecoder(): FixedSizeDecoder { return getStructDecoder([ ["discriminator", fixDecoderSize(getBytesDecoder(), 8)], ["amountIn", getU64Decoder()], ]); } export function getGetSwapAmountAndFeesInstructionDataCodec(): FixedSizeCodec< GetSwapAmountAndFeesInstructionDataArgs, GetSwapAmountAndFeesInstructionData > { return combineCodec( getGetSwapAmountAndFeesInstructionDataEncoder(), getGetSwapAmountAndFeesInstructionDataDecoder(), ); } export type GetSwapAmountAndFeesInput< 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; amountIn: GetSwapAmountAndFeesInstructionDataArgs["amountIn"]; }; export function getGetSwapAmountAndFeesInstruction< 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: GetSwapAmountAndFeesInput< TAccountPerpetuals, TAccountPool, TAccountReceivingCustody, TAccountReceivingCustodyOracleAccount, TAccountDispensingCustody, TAccountDispensingCustodyOracleAccount >, config?: { programAddress?: TProgramAddress }, ): GetSwapAmountAndFeesInstruction< 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: getGetSwapAmountAndFeesInstructionDataEncoder().encode( args as GetSwapAmountAndFeesInstructionDataArgs, ), programAddress, } as GetSwapAmountAndFeesInstruction< TProgramAddress, TAccountPerpetuals, TAccountPool, TAccountReceivingCustody, TAccountReceivingCustodyOracleAccount, TAccountDispensingCustody, TAccountDispensingCustodyOracleAccount >); } export type ParsedGetSwapAmountAndFeesInstruction< 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: GetSwapAmountAndFeesInstructionData; }; export function parseGetSwapAmountAndFeesInstruction< TProgram extends string, TAccountMetas extends readonly AccountMeta[], >( instruction: Instruction & InstructionWithAccounts & InstructionWithData, ): ParsedGetSwapAmountAndFeesInstruction { 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: getGetSwapAmountAndFeesInstructionDataDecoder().decode( instruction.data, ), }; }