/** * 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 type { AccountMeta, AccountSignerMeta, Address, FixedSizeCodec, FixedSizeDecoder, FixedSizeEncoder, Instruction, InstructionWithAccounts, InstructionWithData, ReadonlyAccount, ReadonlySignerAccount, ReadonlyUint8Array, TransactionSigner, WritableAccount, } from "@solana/kit"; import type { ResolvedInstructionAccount } from "@solana/program-client-core"; import { combineCodec, fixDecoderSize, fixEncoderSize, getBooleanDecoder, getBooleanEncoder, getBytesDecoder, getBytesEncoder, getStructDecoder, getStructEncoder, getU64Decoder, getU64Encoder, getU128Decoder, getU128Encoder, SOLANA_ERROR__PROGRAM_CLIENTS__INSUFFICIENT_ACCOUNT_METAS, SolanaError, transformEncoder, } from "@solana/kit"; import { getAccountMetaFactory } from "@solana/program-client-core"; import { WHIRLPOOL_PROGRAM_ADDRESS } from "../programs/index.js"; export const SWAP_DISCRIMINATOR: ReadonlyUint8Array = new Uint8Array([ 248, 198, 158, 145, 225, 117, 135, 200, ]); export function getSwapDiscriminatorBytes(): ReadonlyUint8Array { return fixEncoderSize(getBytesEncoder(), 8).encode(SWAP_DISCRIMINATOR); } export type SwapInstruction< TProgram extends string = typeof WHIRLPOOL_PROGRAM_ADDRESS, TAccountTokenProgram extends | string | AccountMeta = "TokenkegQfeZyiNwAJbNbGKPFXCWuBvf9Ss623VQ5DA", TAccountTokenAuthority extends string | AccountMeta = string, TAccountWhirlpool extends string | AccountMeta = string, TAccountTokenOwnerAccountA extends string | AccountMeta = string, TAccountTokenVaultA extends string | AccountMeta = string, TAccountTokenOwnerAccountB extends string | AccountMeta = string, TAccountTokenVaultB extends string | AccountMeta = string, TAccountTickArray0 extends string | AccountMeta = string, TAccountTickArray1 extends string | AccountMeta = string, TAccountTickArray2 extends string | AccountMeta = string, TAccountOracle extends string | AccountMeta = string, TRemainingAccounts extends readonly AccountMeta[] = [], > = Instruction & InstructionWithData & InstructionWithAccounts< [ TAccountTokenProgram extends string ? ReadonlyAccount : TAccountTokenProgram, TAccountTokenAuthority extends string ? ReadonlySignerAccount & AccountSignerMeta : TAccountTokenAuthority, TAccountWhirlpool extends string ? WritableAccount : TAccountWhirlpool, TAccountTokenOwnerAccountA extends string ? WritableAccount : TAccountTokenOwnerAccountA, TAccountTokenVaultA extends string ? WritableAccount : TAccountTokenVaultA, TAccountTokenOwnerAccountB extends string ? WritableAccount : TAccountTokenOwnerAccountB, TAccountTokenVaultB extends string ? WritableAccount : TAccountTokenVaultB, TAccountTickArray0 extends string ? WritableAccount : TAccountTickArray0, TAccountTickArray1 extends string ? WritableAccount : TAccountTickArray1, TAccountTickArray2 extends string ? WritableAccount : TAccountTickArray2, TAccountOracle extends string ? ReadonlyAccount : TAccountOracle, ...TRemainingAccounts, ] >; export interface SwapInstructionData { discriminator: ReadonlyUint8Array; amount: bigint; otherAmountThreshold: bigint; sqrtPriceLimit: bigint; amountSpecifiedIsInput: boolean; aToB: boolean; } export interface SwapInstructionDataArgs { amount: number | bigint; otherAmountThreshold: number | bigint; sqrtPriceLimit: number | bigint; amountSpecifiedIsInput: boolean; aToB: boolean; } export function getSwapInstructionDataEncoder(): FixedSizeEncoder { return transformEncoder( getStructEncoder([ ["discriminator", fixEncoderSize(getBytesEncoder(), 8)], ["amount", getU64Encoder()], ["otherAmountThreshold", getU64Encoder()], ["sqrtPriceLimit", getU128Encoder()], ["amountSpecifiedIsInput", getBooleanEncoder()], ["aToB", getBooleanEncoder()], ]), (value) => ({ ...value, discriminator: SWAP_DISCRIMINATOR }), ); } export function getSwapInstructionDataDecoder(): FixedSizeDecoder { return getStructDecoder([ ["discriminator", fixDecoderSize(getBytesDecoder(), 8)], ["amount", getU64Decoder()], ["otherAmountThreshold", getU64Decoder()], ["sqrtPriceLimit", getU128Decoder()], ["amountSpecifiedIsInput", getBooleanDecoder()], ["aToB", getBooleanDecoder()], ]); } export function getSwapInstructionDataCodec(): FixedSizeCodec< SwapInstructionDataArgs, SwapInstructionData > { return combineCodec( getSwapInstructionDataEncoder(), getSwapInstructionDataDecoder(), ); } export interface SwapInput< TAccountTokenProgram extends string = string, TAccountTokenAuthority extends string = string, TAccountWhirlpool extends string = string, TAccountTokenOwnerAccountA extends string = string, TAccountTokenVaultA extends string = string, TAccountTokenOwnerAccountB extends string = string, TAccountTokenVaultB extends string = string, TAccountTickArray0 extends string = string, TAccountTickArray1 extends string = string, TAccountTickArray2 extends string = string, TAccountOracle extends string = string, > { tokenProgram?: Address; tokenAuthority: TransactionSigner; whirlpool: Address; tokenOwnerAccountA: Address; tokenVaultA: Address; tokenOwnerAccountB: Address; tokenVaultB: Address; tickArray0: Address; tickArray1: Address; tickArray2: Address; oracle: Address; amount: SwapInstructionDataArgs["amount"]; otherAmountThreshold: SwapInstructionDataArgs["otherAmountThreshold"]; sqrtPriceLimit: SwapInstructionDataArgs["sqrtPriceLimit"]; amountSpecifiedIsInput: SwapInstructionDataArgs["amountSpecifiedIsInput"]; aToB: SwapInstructionDataArgs["aToB"]; } export function getSwapInstruction< TAccountTokenProgram extends string, TAccountTokenAuthority extends string, TAccountWhirlpool extends string, TAccountTokenOwnerAccountA extends string, TAccountTokenVaultA extends string, TAccountTokenOwnerAccountB extends string, TAccountTokenVaultB extends string, TAccountTickArray0 extends string, TAccountTickArray1 extends string, TAccountTickArray2 extends string, TAccountOracle extends string, TProgramAddress extends Address = typeof WHIRLPOOL_PROGRAM_ADDRESS, >( input: SwapInput< TAccountTokenProgram, TAccountTokenAuthority, TAccountWhirlpool, TAccountTokenOwnerAccountA, TAccountTokenVaultA, TAccountTokenOwnerAccountB, TAccountTokenVaultB, TAccountTickArray0, TAccountTickArray1, TAccountTickArray2, TAccountOracle >, config?: { programAddress?: TProgramAddress }, ): SwapInstruction< TProgramAddress, TAccountTokenProgram, TAccountTokenAuthority, TAccountWhirlpool, TAccountTokenOwnerAccountA, TAccountTokenVaultA, TAccountTokenOwnerAccountB, TAccountTokenVaultB, TAccountTickArray0, TAccountTickArray1, TAccountTickArray2, TAccountOracle > { // Program address. const programAddress = config?.programAddress ?? WHIRLPOOL_PROGRAM_ADDRESS; // Original accounts. const originalAccounts = { tokenProgram: { value: input.tokenProgram ?? null, isWritable: false }, tokenAuthority: { value: input.tokenAuthority ?? null, isWritable: false }, whirlpool: { value: input.whirlpool ?? null, isWritable: true }, tokenOwnerAccountA: { value: input.tokenOwnerAccountA ?? null, isWritable: true, }, tokenVaultA: { value: input.tokenVaultA ?? null, isWritable: true }, tokenOwnerAccountB: { value: input.tokenOwnerAccountB ?? null, isWritable: true, }, tokenVaultB: { value: input.tokenVaultB ?? null, isWritable: true }, tickArray0: { value: input.tickArray0 ?? null, isWritable: true }, tickArray1: { value: input.tickArray1 ?? null, isWritable: true }, tickArray2: { value: input.tickArray2 ?? null, isWritable: true }, oracle: { value: input.oracle ?? null, isWritable: false }, }; const accounts = originalAccounts as Record< keyof typeof originalAccounts, ResolvedInstructionAccount >; // Original args. const args = { ...input }; // Resolve default values. if (!accounts.tokenProgram.value) { accounts.tokenProgram.value = "TokenkegQfeZyiNwAJbNbGKPFXCWuBvf9Ss623VQ5DA" as Address<"TokenkegQfeZyiNwAJbNbGKPFXCWuBvf9Ss623VQ5DA">; } const getAccountMeta = getAccountMetaFactory(programAddress, "programId"); return Object.freeze({ accounts: [ getAccountMeta("tokenProgram", accounts.tokenProgram), getAccountMeta("tokenAuthority", accounts.tokenAuthority), getAccountMeta("whirlpool", accounts.whirlpool), getAccountMeta("tokenOwnerAccountA", accounts.tokenOwnerAccountA), getAccountMeta("tokenVaultA", accounts.tokenVaultA), getAccountMeta("tokenOwnerAccountB", accounts.tokenOwnerAccountB), getAccountMeta("tokenVaultB", accounts.tokenVaultB), getAccountMeta("tickArray0", accounts.tickArray0), getAccountMeta("tickArray1", accounts.tickArray1), getAccountMeta("tickArray2", accounts.tickArray2), getAccountMeta("oracle", accounts.oracle), ], data: getSwapInstructionDataEncoder().encode( args as SwapInstructionDataArgs, ), programAddress, } as SwapInstruction< TProgramAddress, TAccountTokenProgram, TAccountTokenAuthority, TAccountWhirlpool, TAccountTokenOwnerAccountA, TAccountTokenVaultA, TAccountTokenOwnerAccountB, TAccountTokenVaultB, TAccountTickArray0, TAccountTickArray1, TAccountTickArray2, TAccountOracle >); } export interface ParsedSwapInstruction< TProgram extends string = typeof WHIRLPOOL_PROGRAM_ADDRESS, TAccountMetas extends readonly AccountMeta[] = readonly AccountMeta[], > { programAddress: Address; accounts: { tokenProgram: TAccountMetas[0]; tokenAuthority: TAccountMetas[1]; whirlpool: TAccountMetas[2]; tokenOwnerAccountA: TAccountMetas[3]; tokenVaultA: TAccountMetas[4]; tokenOwnerAccountB: TAccountMetas[5]; tokenVaultB: TAccountMetas[6]; tickArray0: TAccountMetas[7]; tickArray1: TAccountMetas[8]; tickArray2: TAccountMetas[9]; oracle: TAccountMetas[10]; }; data: SwapInstructionData; } export function parseSwapInstruction< TProgram extends string, TAccountMetas extends readonly AccountMeta[], >( instruction: Instruction & InstructionWithAccounts & InstructionWithData, ): ParsedSwapInstruction { if (instruction.accounts.length < 11) { throw new SolanaError( SOLANA_ERROR__PROGRAM_CLIENTS__INSUFFICIENT_ACCOUNT_METAS, { actualAccountMetas: instruction.accounts.length, expectedAccountMetas: 11, }, ); } let accountIndex = 0; const getNextAccount = () => { const accountMeta = (instruction.accounts as TAccountMetas)[accountIndex]!; accountIndex += 1; return accountMeta; }; return { programAddress: instruction.programAddress, accounts: { tokenProgram: getNextAccount(), tokenAuthority: getNextAccount(), whirlpool: getNextAccount(), tokenOwnerAccountA: getNextAccount(), tokenVaultA: getNextAccount(), tokenOwnerAccountB: getNextAccount(), tokenVaultB: getNextAccount(), tickArray0: getNextAccount(), tickArray1: getNextAccount(), tickArray2: getNextAccount(), oracle: getNextAccount(), }, data: getSwapInstructionDataDecoder().decode(instruction.data), }; }