@kamino-finance/kliquidity-sdk
Version:
Typescript SDK for interacting with the Kamino Liquidity (kliquidity) protocol
127 lines (108 loc) • 12.4 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, getArrayDecoder, getArrayEncoder, getBytesDecoder, getBytesEncoder, getI32Decoder, getI32Encoder, getStructDecoder, getStructEncoder, getU64Decoder, getU64Encoder, transformEncoder, type AccountMeta, type AccountSignerMeta, type Address, type Codec, type Decoder, type Encoder, type Instruction, type InstructionWithAccounts, type InstructionWithData, type ReadonlyAccount, type ReadonlySignerAccount, type ReadonlyUint8Array, type TransactionSigner, type WritableAccount } from '@solana/kit';
import { LB_CLMM_PROGRAM_ADDRESS } from '../programs';
import { getAccountMetaFactory, type ResolvedAccount } from '../shared';
import { getBinLiquidityDistributionByWeightDecoder, getBinLiquidityDistributionByWeightEncoder, type BinLiquidityDistributionByWeight, type BinLiquidityDistributionByWeightArgs } from '../types';
export const ADD_LIQUIDITY_ONE_SIDE_DISCRIMINATOR = new Uint8Array([94, 155, 103, 151, 70, 95, 220, 165]);
export function getAddLiquidityOneSideDiscriminatorBytes() { return fixEncoderSize(getBytesEncoder(), 8).encode(ADD_LIQUIDITY_ONE_SIDE_DISCRIMINATOR); }
export type AddLiquidityOneSideInstruction<TProgram extends string = typeof LB_CLMM_PROGRAM_ADDRESS, TAccountPosition extends string | AccountMeta<string> = string, TAccountLbPair extends string | AccountMeta<string> = string, TAccountBinArrayBitmapExtension extends string | AccountMeta<string> = string, TAccountUserToken extends string | AccountMeta<string> = string, TAccountReserve extends string | AccountMeta<string> = string, TAccountTokenMint extends string | AccountMeta<string> = string, TAccountBinArrayLower extends string | AccountMeta<string> = string, TAccountBinArrayUpper extends string | AccountMeta<string> = string, TAccountSender extends string | AccountMeta<string> = string, TAccountTokenProgram extends string | AccountMeta<string> = "TokenkegQfeZyiNwAJbNbGKPFXCWuBvf9Ss623VQ5DA", TAccountEventAuthority extends string | AccountMeta<string> = string, TAccountProgram extends string | AccountMeta<string> = string, TRemainingAccounts extends readonly AccountMeta<string>[] = []> =
Instruction<TProgram> & InstructionWithData<ReadonlyUint8Array> & InstructionWithAccounts<[TAccountPosition extends string ? WritableAccount<TAccountPosition> : TAccountPosition, TAccountLbPair extends string ? WritableAccount<TAccountLbPair> : TAccountLbPair, TAccountBinArrayBitmapExtension extends string ? WritableAccount<TAccountBinArrayBitmapExtension> : TAccountBinArrayBitmapExtension, TAccountUserToken extends string ? WritableAccount<TAccountUserToken> : TAccountUserToken, TAccountReserve extends string ? WritableAccount<TAccountReserve> : TAccountReserve, TAccountTokenMint extends string ? ReadonlyAccount<TAccountTokenMint> : TAccountTokenMint, TAccountBinArrayLower extends string ? WritableAccount<TAccountBinArrayLower> : TAccountBinArrayLower, TAccountBinArrayUpper extends string ? WritableAccount<TAccountBinArrayUpper> : TAccountBinArrayUpper, TAccountSender extends string ? ReadonlySignerAccount<TAccountSender> & AccountSignerMeta<TAccountSender> : TAccountSender, TAccountTokenProgram extends string ? ReadonlyAccount<TAccountTokenProgram> : TAccountTokenProgram, TAccountEventAuthority extends string ? ReadonlyAccount<TAccountEventAuthority> : TAccountEventAuthority, TAccountProgram extends string ? ReadonlyAccount<TAccountProgram> : TAccountProgram, ...TRemainingAccounts]>;
export type AddLiquidityOneSideInstructionData = { discriminator: ReadonlyUint8Array;
/** Amount of X token or Y token to deposit */
amount: bigint;
/** Active bin that integrator observe off-chain */
activeId: number;
/** max active bin slippage allowed */
maxActiveBinSlippage: number;
/** Liquidity distribution to each bins */
binLiquidityDist: Array<BinLiquidityDistributionByWeight>; };
export type AddLiquidityOneSideInstructionDataArgs = {
/** Amount of X token or Y token to deposit */
amount: number | bigint;
/** Active bin that integrator observe off-chain */
activeId: number;
/** max active bin slippage allowed */
maxActiveBinSlippage: number;
/** Liquidity distribution to each bins */
binLiquidityDist: Array<BinLiquidityDistributionByWeightArgs>; };
export function getAddLiquidityOneSideInstructionDataEncoder(): Encoder<AddLiquidityOneSideInstructionDataArgs> {
return transformEncoder(getStructEncoder([['discriminator', fixEncoderSize(getBytesEncoder(), 8)], ['amount', getU64Encoder()], ['activeId', getI32Encoder()], ['maxActiveBinSlippage', getI32Encoder()], ['binLiquidityDist', getArrayEncoder(getBinLiquidityDistributionByWeightEncoder())]]), (value) => ({ ...value, discriminator: ADD_LIQUIDITY_ONE_SIDE_DISCRIMINATOR }));
}
export function getAddLiquidityOneSideInstructionDataDecoder(): Decoder<AddLiquidityOneSideInstructionData> {
return getStructDecoder([['discriminator', fixDecoderSize(getBytesDecoder(), 8)], ['amount', getU64Decoder()], ['activeId', getI32Decoder()], ['maxActiveBinSlippage', getI32Decoder()], ['binLiquidityDist', getArrayDecoder(getBinLiquidityDistributionByWeightDecoder())]]);
}
export function getAddLiquidityOneSideInstructionDataCodec(): Codec<AddLiquidityOneSideInstructionDataArgs, AddLiquidityOneSideInstructionData> {
return combineCodec(getAddLiquidityOneSideInstructionDataEncoder(), getAddLiquidityOneSideInstructionDataDecoder());
}
export type AddLiquidityOneSideInput<TAccountPosition extends string = string, TAccountLbPair extends string = string, TAccountBinArrayBitmapExtension extends string = string, TAccountUserToken extends string = string, TAccountReserve extends string = string, TAccountTokenMint extends string = string, TAccountBinArrayLower extends string = string, TAccountBinArrayUpper extends string = string, TAccountSender extends string = string, TAccountTokenProgram extends string = string, TAccountEventAuthority extends string = string, TAccountProgram extends string = string> = {
position: Address<TAccountPosition>;
lbPair: Address<TAccountLbPair>;
binArrayBitmapExtension?: Address<TAccountBinArrayBitmapExtension>;
userToken: Address<TAccountUserToken>;
reserve: Address<TAccountReserve>;
tokenMint: Address<TAccountTokenMint>;
binArrayLower: Address<TAccountBinArrayLower>;
binArrayUpper: Address<TAccountBinArrayUpper>;
sender: TransactionSigner<TAccountSender>;
tokenProgram?: Address<TAccountTokenProgram>;
eventAuthority: Address<TAccountEventAuthority>;
program: Address<TAccountProgram>;
amount: AddLiquidityOneSideInstructionDataArgs["amount"];
activeId: AddLiquidityOneSideInstructionDataArgs["activeId"];
maxActiveBinSlippage: AddLiquidityOneSideInstructionDataArgs["maxActiveBinSlippage"];
binLiquidityDist: AddLiquidityOneSideInstructionDataArgs["binLiquidityDist"];
}
export function getAddLiquidityOneSideInstruction<TAccountPosition extends string, TAccountLbPair extends string, TAccountBinArrayBitmapExtension extends string, TAccountUserToken extends string, TAccountReserve extends string, TAccountTokenMint extends string, TAccountBinArrayLower extends string, TAccountBinArrayUpper extends string, TAccountSender extends string, TAccountTokenProgram extends string, TAccountEventAuthority extends string, TAccountProgram extends string, TProgramAddress extends Address = typeof LB_CLMM_PROGRAM_ADDRESS>(input: AddLiquidityOneSideInput<TAccountPosition, TAccountLbPair, TAccountBinArrayBitmapExtension, TAccountUserToken, TAccountReserve, TAccountTokenMint, TAccountBinArrayLower, TAccountBinArrayUpper, TAccountSender, TAccountTokenProgram, TAccountEventAuthority, TAccountProgram>, config?: { programAddress?: TProgramAddress } ): AddLiquidityOneSideInstruction<TProgramAddress, TAccountPosition, TAccountLbPair, TAccountBinArrayBitmapExtension, TAccountUserToken, TAccountReserve, TAccountTokenMint, TAccountBinArrayLower, TAccountBinArrayUpper, TAccountSender, TAccountTokenProgram, TAccountEventAuthority, TAccountProgram> {
// Program address.
const programAddress = config?.programAddress ?? LB_CLMM_PROGRAM_ADDRESS;
// Original accounts.
const originalAccounts = { position: { value: input.position ?? null, isWritable: true }, lbPair: { value: input.lbPair ?? null, isWritable: true }, binArrayBitmapExtension: { value: input.binArrayBitmapExtension ?? null, isWritable: true }, userToken: { value: input.userToken ?? null, isWritable: true }, reserve: { value: input.reserve ?? null, isWritable: true }, tokenMint: { value: input.tokenMint ?? null, isWritable: false }, binArrayLower: { value: input.binArrayLower ?? null, isWritable: true }, binArrayUpper: { value: input.binArrayUpper ?? null, isWritable: true }, sender: { value: input.sender ?? null, isWritable: false }, tokenProgram: { value: input.tokenProgram ?? null, isWritable: false }, eventAuthority: { value: input.eventAuthority ?? null, isWritable: false }, program: { value: input.program ?? 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.position), getAccountMeta(accounts.lbPair), getAccountMeta(accounts.binArrayBitmapExtension), getAccountMeta(accounts.userToken), getAccountMeta(accounts.reserve), getAccountMeta(accounts.tokenMint), getAccountMeta(accounts.binArrayLower), getAccountMeta(accounts.binArrayUpper), getAccountMeta(accounts.sender), getAccountMeta(accounts.tokenProgram), getAccountMeta(accounts.eventAuthority), getAccountMeta(accounts.program)], data: getAddLiquidityOneSideInstructionDataEncoder().encode(args as AddLiquidityOneSideInstructionDataArgs), programAddress } as AddLiquidityOneSideInstruction<TProgramAddress, TAccountPosition, TAccountLbPair, TAccountBinArrayBitmapExtension, TAccountUserToken, TAccountReserve, TAccountTokenMint, TAccountBinArrayLower, TAccountBinArrayUpper, TAccountSender, TAccountTokenProgram, TAccountEventAuthority, TAccountProgram>);
}
export type ParsedAddLiquidityOneSideInstruction<TProgram extends string = typeof LB_CLMM_PROGRAM_ADDRESS, TAccountMetas extends readonly AccountMeta[] = readonly AccountMeta[]> = { programAddress: Address<TProgram>;
accounts: {
position: TAccountMetas[0];
lbPair: TAccountMetas[1];
binArrayBitmapExtension?: TAccountMetas[2] | undefined;
userToken: TAccountMetas[3];
reserve: TAccountMetas[4];
tokenMint: TAccountMetas[5];
binArrayLower: TAccountMetas[6];
binArrayUpper: TAccountMetas[7];
sender: TAccountMetas[8];
tokenProgram: TAccountMetas[9];
eventAuthority: TAccountMetas[10];
program: TAccountMetas[11];
};
data: AddLiquidityOneSideInstructionData; };
export function parseAddLiquidityOneSideInstruction<TProgram extends string, TAccountMetas extends readonly AccountMeta[]>(instruction: Instruction<TProgram> & InstructionWithAccounts<TAccountMetas> & InstructionWithData<ReadonlyUint8Array>): ParsedAddLiquidityOneSideInstruction<TProgram, TAccountMetas> {
if (instruction.accounts.length < 12) {
// 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;
}
const getNextOptionalAccount = () => {
const accountMeta = getNextAccount();
return accountMeta.address === LB_CLMM_PROGRAM_ADDRESS ? undefined : accountMeta;
};
return { programAddress: instruction.programAddress, accounts: { position: getNextAccount(), lbPair: getNextAccount(), binArrayBitmapExtension: getNextOptionalAccount(), userToken: getNextAccount(), reserve: getNextAccount(), tokenMint: getNextAccount(), binArrayLower: getNextAccount(), binArrayUpper: getNextAccount(), sender: getNextAccount(), tokenProgram: getNextAccount(), eventAuthority: getNextAccount(), program: getNextAccount() }, data: getAddLiquidityOneSideInstructionDataDecoder().decode(instruction.data) };
}