/** * 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, Codec, Decoder, Encoder, Instruction, InstructionWithAccounts, InstructionWithData, Option, OptionOrNullable, ReadonlyAccount, ReadonlySignerAccount, ReadonlyUint8Array, TransactionSigner, WritableAccount, } from "@solana/kit"; import type { ResolvedInstructionAccount } from "@solana/program-client-core"; import type { RemainingAccountsInfo, RemainingAccountsInfoArgs, } from "../types/index.js"; import { combineCodec, fixDecoderSize, fixEncoderSize, getBooleanDecoder, getBooleanEncoder, getBytesDecoder, getBytesEncoder, getOptionDecoder, getOptionEncoder, 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"; import { getRemainingAccountsInfoDecoder, getRemainingAccountsInfoEncoder, } from "../types/index.js"; export const SWAP_V2_DISCRIMINATOR: ReadonlyUint8Array = new Uint8Array([ 43, 4, 237, 11, 26, 201, 30, 98, ]); export function getSwapV2DiscriminatorBytes(): ReadonlyUint8Array { return fixEncoderSize(getBytesEncoder(), 8).encode(SWAP_V2_DISCRIMINATOR); } export type SwapV2Instruction< TProgram extends string = typeof WHIRLPOOL_PROGRAM_ADDRESS, TAccountTokenProgramA extends string | AccountMeta = string, TAccountTokenProgramB extends string | AccountMeta = string, TAccountMemoProgram extends | string | AccountMeta = "MemoSq4gqABAXKb96qnH8TysNcWxMyWCqXgDLGmfcHr", TAccountTokenAuthority extends string | AccountMeta = string, TAccountWhirlpool extends string | AccountMeta = string, TAccountTokenMintA extends string | AccountMeta = string, TAccountTokenMintB 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< [ TAccountTokenProgramA extends string ? ReadonlyAccount : TAccountTokenProgramA, TAccountTokenProgramB extends string ? ReadonlyAccount : TAccountTokenProgramB, TAccountMemoProgram extends string ? ReadonlyAccount : TAccountMemoProgram, TAccountTokenAuthority extends string ? ReadonlySignerAccount & AccountSignerMeta : TAccountTokenAuthority, TAccountWhirlpool extends string ? WritableAccount : TAccountWhirlpool, TAccountTokenMintA extends string ? ReadonlyAccount : TAccountTokenMintA, TAccountTokenMintB extends string ? ReadonlyAccount : TAccountTokenMintB, 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 ? WritableAccount : TAccountOracle, ...TRemainingAccounts, ] >; export interface SwapV2InstructionData { discriminator: ReadonlyUint8Array; amount: bigint; otherAmountThreshold: bigint; sqrtPriceLimit: bigint; amountSpecifiedIsInput: boolean; aToB: boolean; remainingAccountsInfo: Option; } export interface SwapV2InstructionDataArgs { amount: number | bigint; otherAmountThreshold: number | bigint; sqrtPriceLimit: number | bigint; amountSpecifiedIsInput: boolean; aToB: boolean; remainingAccountsInfo: OptionOrNullable; } export function getSwapV2InstructionDataEncoder(): Encoder { return transformEncoder( getStructEncoder([ ["discriminator", fixEncoderSize(getBytesEncoder(), 8)], ["amount", getU64Encoder()], ["otherAmountThreshold", getU64Encoder()], ["sqrtPriceLimit", getU128Encoder()], ["amountSpecifiedIsInput", getBooleanEncoder()], ["aToB", getBooleanEncoder()], [ "remainingAccountsInfo", getOptionEncoder(getRemainingAccountsInfoEncoder()), ], ]), (value) => ({ ...value, discriminator: SWAP_V2_DISCRIMINATOR }), ); } export function getSwapV2InstructionDataDecoder(): Decoder { return getStructDecoder([ ["discriminator", fixDecoderSize(getBytesDecoder(), 8)], ["amount", getU64Decoder()], ["otherAmountThreshold", getU64Decoder()], ["sqrtPriceLimit", getU128Decoder()], ["amountSpecifiedIsInput", getBooleanDecoder()], ["aToB", getBooleanDecoder()], [ "remainingAccountsInfo", getOptionDecoder(getRemainingAccountsInfoDecoder()), ], ]); } export function getSwapV2InstructionDataCodec(): Codec< SwapV2InstructionDataArgs, SwapV2InstructionData > { return combineCodec( getSwapV2InstructionDataEncoder(), getSwapV2InstructionDataDecoder(), ); } export interface SwapV2Input< TAccountTokenProgramA extends string = string, TAccountTokenProgramB extends string = string, TAccountMemoProgram extends string = string, TAccountTokenAuthority extends string = string, TAccountWhirlpool extends string = string, TAccountTokenMintA extends string = string, TAccountTokenMintB 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, > { tokenProgramA: Address; tokenProgramB: Address; memoProgram?: Address; tokenAuthority: TransactionSigner; whirlpool: Address; tokenMintA: Address; tokenMintB: Address; tokenOwnerAccountA: Address; tokenVaultA: Address; tokenOwnerAccountB: Address; tokenVaultB: Address; tickArray0: Address; tickArray1: Address; tickArray2: Address; oracle: Address; amount: SwapV2InstructionDataArgs["amount"]; otherAmountThreshold: SwapV2InstructionDataArgs["otherAmountThreshold"]; sqrtPriceLimit: SwapV2InstructionDataArgs["sqrtPriceLimit"]; amountSpecifiedIsInput: SwapV2InstructionDataArgs["amountSpecifiedIsInput"]; aToB: SwapV2InstructionDataArgs["aToB"]; remainingAccountsInfo: SwapV2InstructionDataArgs["remainingAccountsInfo"]; } export function getSwapV2Instruction< TAccountTokenProgramA extends string, TAccountTokenProgramB extends string, TAccountMemoProgram extends string, TAccountTokenAuthority extends string, TAccountWhirlpool extends string, TAccountTokenMintA extends string, TAccountTokenMintB 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: SwapV2Input< TAccountTokenProgramA, TAccountTokenProgramB, TAccountMemoProgram, TAccountTokenAuthority, TAccountWhirlpool, TAccountTokenMintA, TAccountTokenMintB, TAccountTokenOwnerAccountA, TAccountTokenVaultA, TAccountTokenOwnerAccountB, TAccountTokenVaultB, TAccountTickArray0, TAccountTickArray1, TAccountTickArray2, TAccountOracle >, config?: { programAddress?: TProgramAddress }, ): SwapV2Instruction< TProgramAddress, TAccountTokenProgramA, TAccountTokenProgramB, TAccountMemoProgram, TAccountTokenAuthority, TAccountWhirlpool, TAccountTokenMintA, TAccountTokenMintB, TAccountTokenOwnerAccountA, TAccountTokenVaultA, TAccountTokenOwnerAccountB, TAccountTokenVaultB, TAccountTickArray0, TAccountTickArray1, TAccountTickArray2, TAccountOracle > { // Program address. const programAddress = config?.programAddress ?? WHIRLPOOL_PROGRAM_ADDRESS; // Original accounts. const originalAccounts = { tokenProgramA: { value: input.tokenProgramA ?? null, isWritable: false }, tokenProgramB: { value: input.tokenProgramB ?? null, isWritable: false }, memoProgram: { value: input.memoProgram ?? null, isWritable: false }, tokenAuthority: { value: input.tokenAuthority ?? null, isWritable: false }, whirlpool: { value: input.whirlpool ?? null, isWritable: true }, tokenMintA: { value: input.tokenMintA ?? null, isWritable: false }, tokenMintB: { value: input.tokenMintB ?? null, isWritable: false }, 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: true }, }; const accounts = originalAccounts as Record< keyof typeof originalAccounts, ResolvedInstructionAccount >; // Original args. const args = { ...input }; // Resolve default values. if (!accounts.memoProgram.value) { accounts.memoProgram.value = "MemoSq4gqABAXKb96qnH8TysNcWxMyWCqXgDLGmfcHr" as Address<"MemoSq4gqABAXKb96qnH8TysNcWxMyWCqXgDLGmfcHr">; } const getAccountMeta = getAccountMetaFactory(programAddress, "programId"); return Object.freeze({ accounts: [ getAccountMeta("tokenProgramA", accounts.tokenProgramA), getAccountMeta("tokenProgramB", accounts.tokenProgramB), getAccountMeta("memoProgram", accounts.memoProgram), getAccountMeta("tokenAuthority", accounts.tokenAuthority), getAccountMeta("whirlpool", accounts.whirlpool), getAccountMeta("tokenMintA", accounts.tokenMintA), getAccountMeta("tokenMintB", accounts.tokenMintB), 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: getSwapV2InstructionDataEncoder().encode( args as SwapV2InstructionDataArgs, ), programAddress, } as SwapV2Instruction< TProgramAddress, TAccountTokenProgramA, TAccountTokenProgramB, TAccountMemoProgram, TAccountTokenAuthority, TAccountWhirlpool, TAccountTokenMintA, TAccountTokenMintB, TAccountTokenOwnerAccountA, TAccountTokenVaultA, TAccountTokenOwnerAccountB, TAccountTokenVaultB, TAccountTickArray0, TAccountTickArray1, TAccountTickArray2, TAccountOracle >); } export interface ParsedSwapV2Instruction< TProgram extends string = typeof WHIRLPOOL_PROGRAM_ADDRESS, TAccountMetas extends readonly AccountMeta[] = readonly AccountMeta[], > { programAddress: Address; accounts: { tokenProgramA: TAccountMetas[0]; tokenProgramB: TAccountMetas[1]; memoProgram: TAccountMetas[2]; tokenAuthority: TAccountMetas[3]; whirlpool: TAccountMetas[4]; tokenMintA: TAccountMetas[5]; tokenMintB: TAccountMetas[6]; tokenOwnerAccountA: TAccountMetas[7]; tokenVaultA: TAccountMetas[8]; tokenOwnerAccountB: TAccountMetas[9]; tokenVaultB: TAccountMetas[10]; tickArray0: TAccountMetas[11]; tickArray1: TAccountMetas[12]; tickArray2: TAccountMetas[13]; oracle: TAccountMetas[14]; }; data: SwapV2InstructionData; } export function parseSwapV2Instruction< TProgram extends string, TAccountMetas extends readonly AccountMeta[], >( instruction: Instruction & InstructionWithAccounts & InstructionWithData, ): ParsedSwapV2Instruction { if (instruction.accounts.length < 15) { throw new SolanaError( SOLANA_ERROR__PROGRAM_CLIENTS__INSUFFICIENT_ACCOUNT_METAS, { actualAccountMetas: instruction.accounts.length, expectedAccountMetas: 15, }, ); } let accountIndex = 0; const getNextAccount = () => { const accountMeta = (instruction.accounts as TAccountMetas)[accountIndex]!; accountIndex += 1; return accountMeta; }; return { programAddress: instruction.programAddress, accounts: { tokenProgramA: getNextAccount(), tokenProgramB: getNextAccount(), memoProgram: getNextAccount(), tokenAuthority: getNextAccount(), whirlpool: getNextAccount(), tokenMintA: getNextAccount(), tokenMintB: getNextAccount(), tokenOwnerAccountA: getNextAccount(), tokenVaultA: getNextAccount(), tokenOwnerAccountB: getNextAccount(), tokenVaultB: getNextAccount(), tickArray0: getNextAccount(), tickArray1: getNextAccount(), tickArray2: getNextAccount(), oracle: getNextAccount(), }, data: getSwapV2InstructionDataDecoder().decode(instruction.data), }; }