UNPKG

@kamino-finance/kliquidity-sdk

Version:

Typescript SDK for interacting with the Kamino Liquidity (kliquidity) protocol

129 lines (110 loc) 16.8 kB
/** * 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, getI32Decoder, getI32Encoder, 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 ReadonlyUint8Array, type TransactionSigner, type WritableAccount, type WritableSignerAccount } from '@solana/kit'; import { AMM_V3_PROGRAM_ADDRESS } from '../programs'; import { getAccountMetaFactory, type ResolvedAccount } from '../shared'; export const OPEN_POSITION_DISCRIMINATOR = new Uint8Array([135, 128, 47, 77, 15, 152, 240, 49]); export function getOpenPositionDiscriminatorBytes() { return fixEncoderSize(getBytesEncoder(), 8).encode(OPEN_POSITION_DISCRIMINATOR); } export type OpenPositionInstruction<TProgram extends string = typeof AMM_V3_PROGRAM_ADDRESS, TAccountPayer extends string | AccountMeta<string> = string, TAccountPositionNftOwner extends string | AccountMeta<string> = string, TAccountPositionNftMint extends string | AccountMeta<string> = string, TAccountPositionNftAccount extends string | AccountMeta<string> = string, TAccountMetadataAccount extends string | AccountMeta<string> = string, TAccountPoolState extends string | AccountMeta<string> = string, TAccountProtocolPosition extends string | AccountMeta<string> = string, TAccountTickArrayLower extends string | AccountMeta<string> = string, TAccountTickArrayUpper extends string | AccountMeta<string> = string, TAccountPersonalPosition extends string | AccountMeta<string> = string, TAccountTokenAccount0 extends string | AccountMeta<string> = string, TAccountTokenAccount1 extends string | AccountMeta<string> = string, TAccountTokenVault0 extends string | AccountMeta<string> = string, TAccountTokenVault1 extends string | AccountMeta<string> = string, TAccountRent extends string | AccountMeta<string> = "SysvarRent111111111111111111111111111111111", TAccountSystemProgram extends string | AccountMeta<string> = "11111111111111111111111111111111", TAccountTokenProgram extends string | AccountMeta<string> = "TokenkegQfeZyiNwAJbNbGKPFXCWuBvf9Ss623VQ5DA", TAccountAssociatedTokenProgram extends string | AccountMeta<string> = string, TAccountMetadataProgram extends string | AccountMeta<string> = string, TRemainingAccounts extends readonly AccountMeta<string>[] = []> = Instruction<TProgram> & InstructionWithData<ReadonlyUint8Array> & InstructionWithAccounts<[TAccountPayer extends string ? WritableSignerAccount<TAccountPayer> & AccountSignerMeta<TAccountPayer> : TAccountPayer, TAccountPositionNftOwner extends string ? ReadonlyAccount<TAccountPositionNftOwner> : TAccountPositionNftOwner, TAccountPositionNftMint extends string ? WritableSignerAccount<TAccountPositionNftMint> & AccountSignerMeta<TAccountPositionNftMint> : TAccountPositionNftMint, TAccountPositionNftAccount extends string ? WritableAccount<TAccountPositionNftAccount> : TAccountPositionNftAccount, TAccountMetadataAccount extends string ? WritableAccount<TAccountMetadataAccount> : TAccountMetadataAccount, TAccountPoolState extends string ? WritableAccount<TAccountPoolState> : TAccountPoolState, TAccountProtocolPosition extends string ? WritableAccount<TAccountProtocolPosition> : TAccountProtocolPosition, TAccountTickArrayLower extends string ? WritableAccount<TAccountTickArrayLower> : TAccountTickArrayLower, TAccountTickArrayUpper extends string ? WritableAccount<TAccountTickArrayUpper> : TAccountTickArrayUpper, TAccountPersonalPosition extends string ? WritableAccount<TAccountPersonalPosition> : TAccountPersonalPosition, TAccountTokenAccount0 extends string ? WritableAccount<TAccountTokenAccount0> : TAccountTokenAccount0, TAccountTokenAccount1 extends string ? WritableAccount<TAccountTokenAccount1> : TAccountTokenAccount1, TAccountTokenVault0 extends string ? WritableAccount<TAccountTokenVault0> : TAccountTokenVault0, TAccountTokenVault1 extends string ? WritableAccount<TAccountTokenVault1> : TAccountTokenVault1, TAccountRent extends string ? ReadonlyAccount<TAccountRent> : TAccountRent, TAccountSystemProgram extends string ? ReadonlyAccount<TAccountSystemProgram> : TAccountSystemProgram, TAccountTokenProgram extends string ? ReadonlyAccount<TAccountTokenProgram> : TAccountTokenProgram, TAccountAssociatedTokenProgram extends string ? ReadonlyAccount<TAccountAssociatedTokenProgram> : TAccountAssociatedTokenProgram, TAccountMetadataProgram extends string ? ReadonlyAccount<TAccountMetadataProgram> : TAccountMetadataProgram, ...TRemainingAccounts]>; export type OpenPositionInstructionData = { discriminator: ReadonlyUint8Array; tickLowerIndex: number; tickUpperIndex: number; tickArrayLowerStartIndex: number; tickArrayUpperStartIndex: number; liquidity: bigint; amount0Max: bigint; amount1Max: bigint; }; export type OpenPositionInstructionDataArgs = { tickLowerIndex: number; tickUpperIndex: number; tickArrayLowerStartIndex: number; tickArrayUpperStartIndex: number; liquidity: number | bigint; amount0Max: number | bigint; amount1Max: number | bigint; }; export function getOpenPositionInstructionDataEncoder(): FixedSizeEncoder<OpenPositionInstructionDataArgs> { return transformEncoder(getStructEncoder([['discriminator', fixEncoderSize(getBytesEncoder(), 8)], ['tickLowerIndex', getI32Encoder()], ['tickUpperIndex', getI32Encoder()], ['tickArrayLowerStartIndex', getI32Encoder()], ['tickArrayUpperStartIndex', getI32Encoder()], ['liquidity', getU128Encoder()], ['amount0Max', getU64Encoder()], ['amount1Max', getU64Encoder()]]), (value) => ({ ...value, discriminator: OPEN_POSITION_DISCRIMINATOR })); } export function getOpenPositionInstructionDataDecoder(): FixedSizeDecoder<OpenPositionInstructionData> { return getStructDecoder([['discriminator', fixDecoderSize(getBytesDecoder(), 8)], ['tickLowerIndex', getI32Decoder()], ['tickUpperIndex', getI32Decoder()], ['tickArrayLowerStartIndex', getI32Decoder()], ['tickArrayUpperStartIndex', getI32Decoder()], ['liquidity', getU128Decoder()], ['amount0Max', getU64Decoder()], ['amount1Max', getU64Decoder()]]); } export function getOpenPositionInstructionDataCodec(): FixedSizeCodec<OpenPositionInstructionDataArgs, OpenPositionInstructionData> { return combineCodec(getOpenPositionInstructionDataEncoder(), getOpenPositionInstructionDataDecoder()); } export type OpenPositionInput<TAccountPayer extends string = string, TAccountPositionNftOwner extends string = string, TAccountPositionNftMint extends string = string, TAccountPositionNftAccount extends string = string, TAccountMetadataAccount extends string = string, TAccountPoolState extends string = string, TAccountProtocolPosition extends string = string, TAccountTickArrayLower extends string = string, TAccountTickArrayUpper extends string = string, TAccountPersonalPosition extends string = string, TAccountTokenAccount0 extends string = string, TAccountTokenAccount1 extends string = string, TAccountTokenVault0 extends string = string, TAccountTokenVault1 extends string = string, TAccountRent extends string = string, TAccountSystemProgram extends string = string, TAccountTokenProgram extends string = string, TAccountAssociatedTokenProgram extends string = string, TAccountMetadataProgram extends string = string> = { payer: TransactionSigner<TAccountPayer>; positionNftOwner: Address<TAccountPositionNftOwner>; positionNftMint: TransactionSigner<TAccountPositionNftMint>; positionNftAccount: Address<TAccountPositionNftAccount>; metadataAccount: Address<TAccountMetadataAccount>; poolState: Address<TAccountPoolState>; protocolPosition: Address<TAccountProtocolPosition>; tickArrayLower: Address<TAccountTickArrayLower>; tickArrayUpper: Address<TAccountTickArrayUpper>; personalPosition: Address<TAccountPersonalPosition>; tokenAccount0: Address<TAccountTokenAccount0>; tokenAccount1: Address<TAccountTokenAccount1>; tokenVault0: Address<TAccountTokenVault0>; tokenVault1: Address<TAccountTokenVault1>; rent?: Address<TAccountRent>; systemProgram?: Address<TAccountSystemProgram>; tokenProgram?: Address<TAccountTokenProgram>; associatedTokenProgram: Address<TAccountAssociatedTokenProgram>; metadataProgram: Address<TAccountMetadataProgram>; tickLowerIndex: OpenPositionInstructionDataArgs["tickLowerIndex"]; tickUpperIndex: OpenPositionInstructionDataArgs["tickUpperIndex"]; tickArrayLowerStartIndex: OpenPositionInstructionDataArgs["tickArrayLowerStartIndex"]; tickArrayUpperStartIndex: OpenPositionInstructionDataArgs["tickArrayUpperStartIndex"]; liquidity: OpenPositionInstructionDataArgs["liquidity"]; amount0Max: OpenPositionInstructionDataArgs["amount0Max"]; amount1Max: OpenPositionInstructionDataArgs["amount1Max"]; } export function getOpenPositionInstruction<TAccountPayer extends string, TAccountPositionNftOwner extends string, TAccountPositionNftMint extends string, TAccountPositionNftAccount extends string, TAccountMetadataAccount extends string, TAccountPoolState extends string, TAccountProtocolPosition extends string, TAccountTickArrayLower extends string, TAccountTickArrayUpper extends string, TAccountPersonalPosition extends string, TAccountTokenAccount0 extends string, TAccountTokenAccount1 extends string, TAccountTokenVault0 extends string, TAccountTokenVault1 extends string, TAccountRent extends string, TAccountSystemProgram extends string, TAccountTokenProgram extends string, TAccountAssociatedTokenProgram extends string, TAccountMetadataProgram extends string, TProgramAddress extends Address = typeof AMM_V3_PROGRAM_ADDRESS>(input: OpenPositionInput<TAccountPayer, TAccountPositionNftOwner, TAccountPositionNftMint, TAccountPositionNftAccount, TAccountMetadataAccount, TAccountPoolState, TAccountProtocolPosition, TAccountTickArrayLower, TAccountTickArrayUpper, TAccountPersonalPosition, TAccountTokenAccount0, TAccountTokenAccount1, TAccountTokenVault0, TAccountTokenVault1, TAccountRent, TAccountSystemProgram, TAccountTokenProgram, TAccountAssociatedTokenProgram, TAccountMetadataProgram>, config?: { programAddress?: TProgramAddress } ): OpenPositionInstruction<TProgramAddress, TAccountPayer, TAccountPositionNftOwner, TAccountPositionNftMint, TAccountPositionNftAccount, TAccountMetadataAccount, TAccountPoolState, TAccountProtocolPosition, TAccountTickArrayLower, TAccountTickArrayUpper, TAccountPersonalPosition, TAccountTokenAccount0, TAccountTokenAccount1, TAccountTokenVault0, TAccountTokenVault1, TAccountRent, TAccountSystemProgram, TAccountTokenProgram, TAccountAssociatedTokenProgram, TAccountMetadataProgram> { // Program address. const programAddress = config?.programAddress ?? AMM_V3_PROGRAM_ADDRESS; // Original accounts. const originalAccounts = { payer: { value: input.payer ?? null, isWritable: true }, positionNftOwner: { value: input.positionNftOwner ?? null, isWritable: false }, positionNftMint: { value: input.positionNftMint ?? null, isWritable: true }, positionNftAccount: { value: input.positionNftAccount ?? null, isWritable: true }, metadataAccount: { value: input.metadataAccount ?? null, isWritable: true }, poolState: { value: input.poolState ?? null, isWritable: true }, protocolPosition: { value: input.protocolPosition ?? null, isWritable: true }, tickArrayLower: { value: input.tickArrayLower ?? null, isWritable: true }, tickArrayUpper: { value: input.tickArrayUpper ?? null, isWritable: true }, personalPosition: { value: input.personalPosition ?? null, isWritable: true }, tokenAccount0: { value: input.tokenAccount0 ?? null, isWritable: true }, tokenAccount1: { value: input.tokenAccount1 ?? null, isWritable: true }, tokenVault0: { value: input.tokenVault0 ?? null, isWritable: true }, tokenVault1: { value: input.tokenVault1 ?? null, isWritable: true }, rent: { value: input.rent ?? null, isWritable: false }, systemProgram: { value: input.systemProgram ?? null, isWritable: false }, tokenProgram: { value: input.tokenProgram ?? null, isWritable: false }, associatedTokenProgram: { value: input.associatedTokenProgram ?? null, isWritable: false }, metadataProgram: { value: input.metadataProgram ?? null, isWritable: false } } const accounts = originalAccounts as Record<keyof typeof originalAccounts, ResolvedAccount>; // Original args. const args = { ...input, }; // Resolve default values. if (!accounts.rent.value) { accounts.rent.value = 'SysvarRent111111111111111111111111111111111' as Address<'SysvarRent111111111111111111111111111111111'>; } if (!accounts.systemProgram.value) { accounts.systemProgram.value = '11111111111111111111111111111111' as Address<'11111111111111111111111111111111'>; } if (!accounts.tokenProgram.value) { accounts.tokenProgram.value = 'TokenkegQfeZyiNwAJbNbGKPFXCWuBvf9Ss623VQ5DA' as Address<'TokenkegQfeZyiNwAJbNbGKPFXCWuBvf9Ss623VQ5DA'>; } const getAccountMeta = getAccountMetaFactory(programAddress, 'programId'); return Object.freeze({ accounts: [getAccountMeta(accounts.payer), getAccountMeta(accounts.positionNftOwner), getAccountMeta(accounts.positionNftMint), getAccountMeta(accounts.positionNftAccount), getAccountMeta(accounts.metadataAccount), getAccountMeta(accounts.poolState), getAccountMeta(accounts.protocolPosition), getAccountMeta(accounts.tickArrayLower), getAccountMeta(accounts.tickArrayUpper), getAccountMeta(accounts.personalPosition), getAccountMeta(accounts.tokenAccount0), getAccountMeta(accounts.tokenAccount1), getAccountMeta(accounts.tokenVault0), getAccountMeta(accounts.tokenVault1), getAccountMeta(accounts.rent), getAccountMeta(accounts.systemProgram), getAccountMeta(accounts.tokenProgram), getAccountMeta(accounts.associatedTokenProgram), getAccountMeta(accounts.metadataProgram)], data: getOpenPositionInstructionDataEncoder().encode(args as OpenPositionInstructionDataArgs), programAddress } as OpenPositionInstruction<TProgramAddress, TAccountPayer, TAccountPositionNftOwner, TAccountPositionNftMint, TAccountPositionNftAccount, TAccountMetadataAccount, TAccountPoolState, TAccountProtocolPosition, TAccountTickArrayLower, TAccountTickArrayUpper, TAccountPersonalPosition, TAccountTokenAccount0, TAccountTokenAccount1, TAccountTokenVault0, TAccountTokenVault1, TAccountRent, TAccountSystemProgram, TAccountTokenProgram, TAccountAssociatedTokenProgram, TAccountMetadataProgram>); } export type ParsedOpenPositionInstruction<TProgram extends string = typeof AMM_V3_PROGRAM_ADDRESS, TAccountMetas extends readonly AccountMeta[] = readonly AccountMeta[]> = { programAddress: Address<TProgram>; accounts: { payer: TAccountMetas[0]; positionNftOwner: TAccountMetas[1]; positionNftMint: TAccountMetas[2]; positionNftAccount: TAccountMetas[3]; metadataAccount: TAccountMetas[4]; poolState: TAccountMetas[5]; protocolPosition: TAccountMetas[6]; tickArrayLower: TAccountMetas[7]; tickArrayUpper: TAccountMetas[8]; personalPosition: TAccountMetas[9]; tokenAccount0: TAccountMetas[10]; tokenAccount1: TAccountMetas[11]; tokenVault0: TAccountMetas[12]; tokenVault1: TAccountMetas[13]; rent: TAccountMetas[14]; systemProgram: TAccountMetas[15]; tokenProgram: TAccountMetas[16]; associatedTokenProgram: TAccountMetas[17]; metadataProgram: TAccountMetas[18]; }; data: OpenPositionInstructionData; }; export function parseOpenPositionInstruction<TProgram extends string, TAccountMetas extends readonly AccountMeta[]>(instruction: Instruction<TProgram> & InstructionWithAccounts<TAccountMetas> & InstructionWithData<ReadonlyUint8Array>): ParsedOpenPositionInstruction<TProgram, TAccountMetas> { if (instruction.accounts.length < 19) { // 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: { payer: getNextAccount(), positionNftOwner: getNextAccount(), positionNftMint: getNextAccount(), positionNftAccount: getNextAccount(), metadataAccount: getNextAccount(), poolState: getNextAccount(), protocolPosition: getNextAccount(), tickArrayLower: getNextAccount(), tickArrayUpper: getNextAccount(), personalPosition: getNextAccount(), tokenAccount0: getNextAccount(), tokenAccount1: getNextAccount(), tokenVault0: getNextAccount(), tokenVault1: getNextAccount(), rent: getNextAccount(), systemProgram: getNextAccount(), tokenProgram: getNextAccount(), associatedTokenProgram: getNextAccount(), metadataProgram: getNextAccount() }, data: getOpenPositionInstructionDataDecoder().decode(instruction.data) }; }