@kamino-finance/kliquidity-sdk
Version:
Typescript SDK for interacting with the Kamino Liquidity (kliquidity) protocol
125 lines (106 loc) • 17.2 kB
text/typescript
/**
* 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, getBooleanDecoder, getBooleanEncoder, getBytesDecoder, getBytesEncoder, getStructDecoder, getStructEncoder, getU128Decoder, getU128Encoder, getU64Decoder, getU64Encoder, transformEncoder, type AccountMeta, type AccountSignerMeta, type Address, type FixedSizeCodec, type FixedSizeDecoder, type FixedSizeEncoder, type Instruction, type InstructionWithAccounts, type InstructionWithData, type ReadonlyAccount, type ReadonlySignerAccount, type ReadonlyUint8Array, type TransactionSigner, type WritableAccount } from '@solana/kit';
import { WHIRLPOOL_PROGRAM_ADDRESS } from '../programs';
import { getAccountMetaFactory, type ResolvedAccount } from '../shared';
export const TWO_HOP_SWAP_DISCRIMINATOR = new Uint8Array([195, 96, 237, 108, 68, 162, 219, 230]);
export function getTwoHopSwapDiscriminatorBytes() { return fixEncoderSize(getBytesEncoder(), 8).encode(TWO_HOP_SWAP_DISCRIMINATOR); }
export type TwoHopSwapInstruction<TProgram extends string = typeof WHIRLPOOL_PROGRAM_ADDRESS, TAccountTokenProgram extends string | AccountMeta<string> = "TokenkegQfeZyiNwAJbNbGKPFXCWuBvf9Ss623VQ5DA", TAccountTokenAuthority extends string | AccountMeta<string> = string, TAccountWhirlpoolOne extends string | AccountMeta<string> = string, TAccountWhirlpoolTwo extends string | AccountMeta<string> = string, TAccountTokenOwnerAccountOneA extends string | AccountMeta<string> = string, TAccountTokenVaultOneA extends string | AccountMeta<string> = string, TAccountTokenOwnerAccountOneB extends string | AccountMeta<string> = string, TAccountTokenVaultOneB extends string | AccountMeta<string> = string, TAccountTokenOwnerAccountTwoA extends string | AccountMeta<string> = string, TAccountTokenVaultTwoA extends string | AccountMeta<string> = string, TAccountTokenOwnerAccountTwoB extends string | AccountMeta<string> = string, TAccountTokenVaultTwoB extends string | AccountMeta<string> = string, TAccountTickArrayOne0 extends string | AccountMeta<string> = string, TAccountTickArrayOne1 extends string | AccountMeta<string> = string, TAccountTickArrayOne2 extends string | AccountMeta<string> = string, TAccountTickArrayTwo0 extends string | AccountMeta<string> = string, TAccountTickArrayTwo1 extends string | AccountMeta<string> = string, TAccountTickArrayTwo2 extends string | AccountMeta<string> = string, TAccountOracleOne extends string | AccountMeta<string> = string, TAccountOracleTwo extends string | AccountMeta<string> = string, TRemainingAccounts extends readonly AccountMeta<string>[] = []> =
Instruction<TProgram> & InstructionWithData<ReadonlyUint8Array> & InstructionWithAccounts<[TAccountTokenProgram extends string ? ReadonlyAccount<TAccountTokenProgram> : TAccountTokenProgram, TAccountTokenAuthority extends string ? ReadonlySignerAccount<TAccountTokenAuthority> & AccountSignerMeta<TAccountTokenAuthority> : TAccountTokenAuthority, TAccountWhirlpoolOne extends string ? WritableAccount<TAccountWhirlpoolOne> : TAccountWhirlpoolOne, TAccountWhirlpoolTwo extends string ? WritableAccount<TAccountWhirlpoolTwo> : TAccountWhirlpoolTwo, TAccountTokenOwnerAccountOneA extends string ? WritableAccount<TAccountTokenOwnerAccountOneA> : TAccountTokenOwnerAccountOneA, TAccountTokenVaultOneA extends string ? WritableAccount<TAccountTokenVaultOneA> : TAccountTokenVaultOneA, TAccountTokenOwnerAccountOneB extends string ? WritableAccount<TAccountTokenOwnerAccountOneB> : TAccountTokenOwnerAccountOneB, TAccountTokenVaultOneB extends string ? WritableAccount<TAccountTokenVaultOneB> : TAccountTokenVaultOneB, TAccountTokenOwnerAccountTwoA extends string ? WritableAccount<TAccountTokenOwnerAccountTwoA> : TAccountTokenOwnerAccountTwoA, TAccountTokenVaultTwoA extends string ? WritableAccount<TAccountTokenVaultTwoA> : TAccountTokenVaultTwoA, TAccountTokenOwnerAccountTwoB extends string ? WritableAccount<TAccountTokenOwnerAccountTwoB> : TAccountTokenOwnerAccountTwoB, TAccountTokenVaultTwoB extends string ? WritableAccount<TAccountTokenVaultTwoB> : TAccountTokenVaultTwoB, TAccountTickArrayOne0 extends string ? WritableAccount<TAccountTickArrayOne0> : TAccountTickArrayOne0, TAccountTickArrayOne1 extends string ? WritableAccount<TAccountTickArrayOne1> : TAccountTickArrayOne1, TAccountTickArrayOne2 extends string ? WritableAccount<TAccountTickArrayOne2> : TAccountTickArrayOne2, TAccountTickArrayTwo0 extends string ? WritableAccount<TAccountTickArrayTwo0> : TAccountTickArrayTwo0, TAccountTickArrayTwo1 extends string ? WritableAccount<TAccountTickArrayTwo1> : TAccountTickArrayTwo1, TAccountTickArrayTwo2 extends string ? WritableAccount<TAccountTickArrayTwo2> : TAccountTickArrayTwo2, TAccountOracleOne extends string ? ReadonlyAccount<TAccountOracleOne> : TAccountOracleOne, TAccountOracleTwo extends string ? ReadonlyAccount<TAccountOracleTwo> : TAccountOracleTwo, ...TRemainingAccounts]>;
export type TwoHopSwapInstructionData = { discriminator: ReadonlyUint8Array; amount: bigint; otherAmountThreshold: bigint; amountSpecifiedIsInput: boolean; aToBOne: boolean; aToBTwo: boolean; sqrtPriceLimitOne: bigint; sqrtPriceLimitTwo: bigint; };
export type TwoHopSwapInstructionDataArgs = { amount: number | bigint; otherAmountThreshold: number | bigint; amountSpecifiedIsInput: boolean; aToBOne: boolean; aToBTwo: boolean; sqrtPriceLimitOne: number | bigint; sqrtPriceLimitTwo: number | bigint; };
export function getTwoHopSwapInstructionDataEncoder(): FixedSizeEncoder<TwoHopSwapInstructionDataArgs> {
return transformEncoder(getStructEncoder([['discriminator', fixEncoderSize(getBytesEncoder(), 8)], ['amount', getU64Encoder()], ['otherAmountThreshold', getU64Encoder()], ['amountSpecifiedIsInput', getBooleanEncoder()], ['aToBOne', getBooleanEncoder()], ['aToBTwo', getBooleanEncoder()], ['sqrtPriceLimitOne', getU128Encoder()], ['sqrtPriceLimitTwo', getU128Encoder()]]), (value) => ({ ...value, discriminator: TWO_HOP_SWAP_DISCRIMINATOR }));
}
export function getTwoHopSwapInstructionDataDecoder(): FixedSizeDecoder<TwoHopSwapInstructionData> {
return getStructDecoder([['discriminator', fixDecoderSize(getBytesDecoder(), 8)], ['amount', getU64Decoder()], ['otherAmountThreshold', getU64Decoder()], ['amountSpecifiedIsInput', getBooleanDecoder()], ['aToBOne', getBooleanDecoder()], ['aToBTwo', getBooleanDecoder()], ['sqrtPriceLimitOne', getU128Decoder()], ['sqrtPriceLimitTwo', getU128Decoder()]]);
}
export function getTwoHopSwapInstructionDataCodec(): FixedSizeCodec<TwoHopSwapInstructionDataArgs, TwoHopSwapInstructionData> {
return combineCodec(getTwoHopSwapInstructionDataEncoder(), getTwoHopSwapInstructionDataDecoder());
}
export type TwoHopSwapInput<TAccountTokenProgram extends string = string, TAccountTokenAuthority extends string = string, TAccountWhirlpoolOne extends string = string, TAccountWhirlpoolTwo extends string = string, TAccountTokenOwnerAccountOneA extends string = string, TAccountTokenVaultOneA extends string = string, TAccountTokenOwnerAccountOneB extends string = string, TAccountTokenVaultOneB extends string = string, TAccountTokenOwnerAccountTwoA extends string = string, TAccountTokenVaultTwoA extends string = string, TAccountTokenOwnerAccountTwoB extends string = string, TAccountTokenVaultTwoB extends string = string, TAccountTickArrayOne0 extends string = string, TAccountTickArrayOne1 extends string = string, TAccountTickArrayOne2 extends string = string, TAccountTickArrayTwo0 extends string = string, TAccountTickArrayTwo1 extends string = string, TAccountTickArrayTwo2 extends string = string, TAccountOracleOne extends string = string, TAccountOracleTwo extends string = string> = {
tokenProgram?: Address<TAccountTokenProgram>;
tokenAuthority: TransactionSigner<TAccountTokenAuthority>;
whirlpoolOne: Address<TAccountWhirlpoolOne>;
whirlpoolTwo: Address<TAccountWhirlpoolTwo>;
tokenOwnerAccountOneA: Address<TAccountTokenOwnerAccountOneA>;
tokenVaultOneA: Address<TAccountTokenVaultOneA>;
tokenOwnerAccountOneB: Address<TAccountTokenOwnerAccountOneB>;
tokenVaultOneB: Address<TAccountTokenVaultOneB>;
tokenOwnerAccountTwoA: Address<TAccountTokenOwnerAccountTwoA>;
tokenVaultTwoA: Address<TAccountTokenVaultTwoA>;
tokenOwnerAccountTwoB: Address<TAccountTokenOwnerAccountTwoB>;
tokenVaultTwoB: Address<TAccountTokenVaultTwoB>;
tickArrayOne0: Address<TAccountTickArrayOne0>;
tickArrayOne1: Address<TAccountTickArrayOne1>;
tickArrayOne2: Address<TAccountTickArrayOne2>;
tickArrayTwo0: Address<TAccountTickArrayTwo0>;
tickArrayTwo1: Address<TAccountTickArrayTwo1>;
tickArrayTwo2: Address<TAccountTickArrayTwo2>;
oracleOne: Address<TAccountOracleOne>;
oracleTwo: Address<TAccountOracleTwo>;
amount: TwoHopSwapInstructionDataArgs["amount"];
otherAmountThreshold: TwoHopSwapInstructionDataArgs["otherAmountThreshold"];
amountSpecifiedIsInput: TwoHopSwapInstructionDataArgs["amountSpecifiedIsInput"];
aToBOne: TwoHopSwapInstructionDataArgs["aToBOne"];
aToBTwo: TwoHopSwapInstructionDataArgs["aToBTwo"];
sqrtPriceLimitOne: TwoHopSwapInstructionDataArgs["sqrtPriceLimitOne"];
sqrtPriceLimitTwo: TwoHopSwapInstructionDataArgs["sqrtPriceLimitTwo"];
}
export function getTwoHopSwapInstruction<TAccountTokenProgram extends string, TAccountTokenAuthority extends string, TAccountWhirlpoolOne extends string, TAccountWhirlpoolTwo extends string, TAccountTokenOwnerAccountOneA extends string, TAccountTokenVaultOneA extends string, TAccountTokenOwnerAccountOneB extends string, TAccountTokenVaultOneB extends string, TAccountTokenOwnerAccountTwoA extends string, TAccountTokenVaultTwoA extends string, TAccountTokenOwnerAccountTwoB extends string, TAccountTokenVaultTwoB extends string, TAccountTickArrayOne0 extends string, TAccountTickArrayOne1 extends string, TAccountTickArrayOne2 extends string, TAccountTickArrayTwo0 extends string, TAccountTickArrayTwo1 extends string, TAccountTickArrayTwo2 extends string, TAccountOracleOne extends string, TAccountOracleTwo extends string, TProgramAddress extends Address = typeof WHIRLPOOL_PROGRAM_ADDRESS>(input: TwoHopSwapInput<TAccountTokenProgram, TAccountTokenAuthority, TAccountWhirlpoolOne, TAccountWhirlpoolTwo, TAccountTokenOwnerAccountOneA, TAccountTokenVaultOneA, TAccountTokenOwnerAccountOneB, TAccountTokenVaultOneB, TAccountTokenOwnerAccountTwoA, TAccountTokenVaultTwoA, TAccountTokenOwnerAccountTwoB, TAccountTokenVaultTwoB, TAccountTickArrayOne0, TAccountTickArrayOne1, TAccountTickArrayOne2, TAccountTickArrayTwo0, TAccountTickArrayTwo1, TAccountTickArrayTwo2, TAccountOracleOne, TAccountOracleTwo>, config?: { programAddress?: TProgramAddress } ): TwoHopSwapInstruction<TProgramAddress, TAccountTokenProgram, TAccountTokenAuthority, TAccountWhirlpoolOne, TAccountWhirlpoolTwo, TAccountTokenOwnerAccountOneA, TAccountTokenVaultOneA, TAccountTokenOwnerAccountOneB, TAccountTokenVaultOneB, TAccountTokenOwnerAccountTwoA, TAccountTokenVaultTwoA, TAccountTokenOwnerAccountTwoB, TAccountTokenVaultTwoB, TAccountTickArrayOne0, TAccountTickArrayOne1, TAccountTickArrayOne2, TAccountTickArrayTwo0, TAccountTickArrayTwo1, TAccountTickArrayTwo2, TAccountOracleOne, TAccountOracleTwo> {
// 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 }, whirlpoolOne: { value: input.whirlpoolOne ?? null, isWritable: true }, whirlpoolTwo: { value: input.whirlpoolTwo ?? null, isWritable: true }, tokenOwnerAccountOneA: { value: input.tokenOwnerAccountOneA ?? null, isWritable: true }, tokenVaultOneA: { value: input.tokenVaultOneA ?? null, isWritable: true }, tokenOwnerAccountOneB: { value: input.tokenOwnerAccountOneB ?? null, isWritable: true }, tokenVaultOneB: { value: input.tokenVaultOneB ?? null, isWritable: true }, tokenOwnerAccountTwoA: { value: input.tokenOwnerAccountTwoA ?? null, isWritable: true }, tokenVaultTwoA: { value: input.tokenVaultTwoA ?? null, isWritable: true }, tokenOwnerAccountTwoB: { value: input.tokenOwnerAccountTwoB ?? null, isWritable: true }, tokenVaultTwoB: { value: input.tokenVaultTwoB ?? null, isWritable: true }, tickArrayOne0: { value: input.tickArrayOne0 ?? null, isWritable: true }, tickArrayOne1: { value: input.tickArrayOne1 ?? null, isWritable: true }, tickArrayOne2: { value: input.tickArrayOne2 ?? null, isWritable: true }, tickArrayTwo0: { value: input.tickArrayTwo0 ?? null, isWritable: true }, tickArrayTwo1: { value: input.tickArrayTwo1 ?? null, isWritable: true }, tickArrayTwo2: { value: input.tickArrayTwo2 ?? null, isWritable: true }, oracleOne: { value: input.oracleOne ?? null, isWritable: false }, oracleTwo: { value: input.oracleTwo ?? null, isWritable: false } }
const accounts = originalAccounts as Record<keyof typeof originalAccounts, ResolvedAccount>;
// 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(accounts.tokenProgram), getAccountMeta(accounts.tokenAuthority), getAccountMeta(accounts.whirlpoolOne), getAccountMeta(accounts.whirlpoolTwo), getAccountMeta(accounts.tokenOwnerAccountOneA), getAccountMeta(accounts.tokenVaultOneA), getAccountMeta(accounts.tokenOwnerAccountOneB), getAccountMeta(accounts.tokenVaultOneB), getAccountMeta(accounts.tokenOwnerAccountTwoA), getAccountMeta(accounts.tokenVaultTwoA), getAccountMeta(accounts.tokenOwnerAccountTwoB), getAccountMeta(accounts.tokenVaultTwoB), getAccountMeta(accounts.tickArrayOne0), getAccountMeta(accounts.tickArrayOne1), getAccountMeta(accounts.tickArrayOne2), getAccountMeta(accounts.tickArrayTwo0), getAccountMeta(accounts.tickArrayTwo1), getAccountMeta(accounts.tickArrayTwo2), getAccountMeta(accounts.oracleOne), getAccountMeta(accounts.oracleTwo)], data: getTwoHopSwapInstructionDataEncoder().encode(args as TwoHopSwapInstructionDataArgs), programAddress } as TwoHopSwapInstruction<TProgramAddress, TAccountTokenProgram, TAccountTokenAuthority, TAccountWhirlpoolOne, TAccountWhirlpoolTwo, TAccountTokenOwnerAccountOneA, TAccountTokenVaultOneA, TAccountTokenOwnerAccountOneB, TAccountTokenVaultOneB, TAccountTokenOwnerAccountTwoA, TAccountTokenVaultTwoA, TAccountTokenOwnerAccountTwoB, TAccountTokenVaultTwoB, TAccountTickArrayOne0, TAccountTickArrayOne1, TAccountTickArrayOne2, TAccountTickArrayTwo0, TAccountTickArrayTwo1, TAccountTickArrayTwo2, TAccountOracleOne, TAccountOracleTwo>);
}
export type ParsedTwoHopSwapInstruction<TProgram extends string = typeof WHIRLPOOL_PROGRAM_ADDRESS, TAccountMetas extends readonly AccountMeta[] = readonly AccountMeta[]> = { programAddress: Address<TProgram>;
accounts: {
tokenProgram: TAccountMetas[0];
tokenAuthority: TAccountMetas[1];
whirlpoolOne: TAccountMetas[2];
whirlpoolTwo: TAccountMetas[3];
tokenOwnerAccountOneA: TAccountMetas[4];
tokenVaultOneA: TAccountMetas[5];
tokenOwnerAccountOneB: TAccountMetas[6];
tokenVaultOneB: TAccountMetas[7];
tokenOwnerAccountTwoA: TAccountMetas[8];
tokenVaultTwoA: TAccountMetas[9];
tokenOwnerAccountTwoB: TAccountMetas[10];
tokenVaultTwoB: TAccountMetas[11];
tickArrayOne0: TAccountMetas[12];
tickArrayOne1: TAccountMetas[13];
tickArrayOne2: TAccountMetas[14];
tickArrayTwo0: TAccountMetas[15];
tickArrayTwo1: TAccountMetas[16];
tickArrayTwo2: TAccountMetas[17];
oracleOne: TAccountMetas[18];
oracleTwo: TAccountMetas[19];
};
data: TwoHopSwapInstructionData; };
export function parseTwoHopSwapInstruction<TProgram extends string, TAccountMetas extends readonly AccountMeta[]>(instruction: Instruction<TProgram> & InstructionWithAccounts<TAccountMetas> & InstructionWithData<ReadonlyUint8Array>): ParsedTwoHopSwapInstruction<TProgram, TAccountMetas> {
if (instruction.accounts.length < 20) {
// 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: { tokenProgram: getNextAccount(), tokenAuthority: getNextAccount(), whirlpoolOne: getNextAccount(), whirlpoolTwo: getNextAccount(), tokenOwnerAccountOneA: getNextAccount(), tokenVaultOneA: getNextAccount(), tokenOwnerAccountOneB: getNextAccount(), tokenVaultOneB: getNextAccount(), tokenOwnerAccountTwoA: getNextAccount(), tokenVaultTwoA: getNextAccount(), tokenOwnerAccountTwoB: getNextAccount(), tokenVaultTwoB: getNextAccount(), tickArrayOne0: getNextAccount(), tickArrayOne1: getNextAccount(), tickArrayOne2: getNextAccount(), tickArrayTwo0: getNextAccount(), tickArrayTwo1: getNextAccount(), tickArrayTwo2: getNextAccount(), oracleOne: getNextAccount(), oracleTwo: getNextAccount() }, data: getTwoHopSwapInstructionDataDecoder().decode(instruction.data) };
}