UNPKG

@kamino-finance/kliquidity-sdk

Version:

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

107 lines (88 loc) 10.6 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, getU8Decoder, getU8Encoder, 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 { WHIRLPOOL_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 WHIRLPOOL_PROGRAM_ADDRESS, TAccountFunder extends string | AccountMeta<string> = string, TAccountOwner extends string | AccountMeta<string> = string, TAccountPosition extends string | AccountMeta<string> = string, TAccountPositionMint extends string | AccountMeta<string> = string, TAccountPositionTokenAccount extends string | AccountMeta<string> = string, TAccountWhirlpool extends string | AccountMeta<string> = string, TAccountTokenProgram extends string | AccountMeta<string> = "TokenkegQfeZyiNwAJbNbGKPFXCWuBvf9Ss623VQ5DA", TAccountSystemProgram extends string | AccountMeta<string> = "11111111111111111111111111111111", TAccountRent extends string | AccountMeta<string> = "SysvarRent111111111111111111111111111111111", TAccountAssociatedTokenProgram extends string | AccountMeta<string> = string, TRemainingAccounts extends readonly AccountMeta<string>[] = []> = Instruction<TProgram> & InstructionWithData<ReadonlyUint8Array> & InstructionWithAccounts<[TAccountFunder extends string ? WritableSignerAccount<TAccountFunder> & AccountSignerMeta<TAccountFunder> : TAccountFunder, TAccountOwner extends string ? ReadonlyAccount<TAccountOwner> : TAccountOwner, TAccountPosition extends string ? WritableAccount<TAccountPosition> : TAccountPosition, TAccountPositionMint extends string ? WritableSignerAccount<TAccountPositionMint> & AccountSignerMeta<TAccountPositionMint> : TAccountPositionMint, TAccountPositionTokenAccount extends string ? WritableAccount<TAccountPositionTokenAccount> : TAccountPositionTokenAccount, TAccountWhirlpool extends string ? ReadonlyAccount<TAccountWhirlpool> : TAccountWhirlpool, TAccountTokenProgram extends string ? ReadonlyAccount<TAccountTokenProgram> : TAccountTokenProgram, TAccountSystemProgram extends string ? ReadonlyAccount<TAccountSystemProgram> : TAccountSystemProgram, TAccountRent extends string ? ReadonlyAccount<TAccountRent> : TAccountRent, TAccountAssociatedTokenProgram extends string ? ReadonlyAccount<TAccountAssociatedTokenProgram> : TAccountAssociatedTokenProgram, ...TRemainingAccounts]>; export type OpenPositionInstructionData = { discriminator: ReadonlyUint8Array; positionBump: number; tickLowerIndex: number; tickUpperIndex: number; }; export type OpenPositionInstructionDataArgs = { positionBump: number; tickLowerIndex: number; tickUpperIndex: number; }; export function getOpenPositionInstructionDataEncoder(): FixedSizeEncoder<OpenPositionInstructionDataArgs> { return transformEncoder(getStructEncoder([['discriminator', fixEncoderSize(getBytesEncoder(), 8)], ['positionBump', getU8Encoder()], ['tickLowerIndex', getI32Encoder()], ['tickUpperIndex', getI32Encoder()]]), (value) => ({ ...value, discriminator: OPEN_POSITION_DISCRIMINATOR })); } export function getOpenPositionInstructionDataDecoder(): FixedSizeDecoder<OpenPositionInstructionData> { return getStructDecoder([['discriminator', fixDecoderSize(getBytesDecoder(), 8)], ['positionBump', getU8Decoder()], ['tickLowerIndex', getI32Decoder()], ['tickUpperIndex', getI32Decoder()]]); } export function getOpenPositionInstructionDataCodec(): FixedSizeCodec<OpenPositionInstructionDataArgs, OpenPositionInstructionData> { return combineCodec(getOpenPositionInstructionDataEncoder(), getOpenPositionInstructionDataDecoder()); } export type OpenPositionInput<TAccountFunder extends string = string, TAccountOwner extends string = string, TAccountPosition extends string = string, TAccountPositionMint extends string = string, TAccountPositionTokenAccount extends string = string, TAccountWhirlpool extends string = string, TAccountTokenProgram extends string = string, TAccountSystemProgram extends string = string, TAccountRent extends string = string, TAccountAssociatedTokenProgram extends string = string> = { funder: TransactionSigner<TAccountFunder>; owner: Address<TAccountOwner>; position: Address<TAccountPosition>; positionMint: TransactionSigner<TAccountPositionMint>; positionTokenAccount: Address<TAccountPositionTokenAccount>; whirlpool: Address<TAccountWhirlpool>; tokenProgram?: Address<TAccountTokenProgram>; systemProgram?: Address<TAccountSystemProgram>; rent?: Address<TAccountRent>; associatedTokenProgram: Address<TAccountAssociatedTokenProgram>; positionBump: OpenPositionInstructionDataArgs["positionBump"]; tickLowerIndex: OpenPositionInstructionDataArgs["tickLowerIndex"]; tickUpperIndex: OpenPositionInstructionDataArgs["tickUpperIndex"]; } export function getOpenPositionInstruction<TAccountFunder extends string, TAccountOwner extends string, TAccountPosition extends string, TAccountPositionMint extends string, TAccountPositionTokenAccount extends string, TAccountWhirlpool extends string, TAccountTokenProgram extends string, TAccountSystemProgram extends string, TAccountRent extends string, TAccountAssociatedTokenProgram extends string, TProgramAddress extends Address = typeof WHIRLPOOL_PROGRAM_ADDRESS>(input: OpenPositionInput<TAccountFunder, TAccountOwner, TAccountPosition, TAccountPositionMint, TAccountPositionTokenAccount, TAccountWhirlpool, TAccountTokenProgram, TAccountSystemProgram, TAccountRent, TAccountAssociatedTokenProgram>, config?: { programAddress?: TProgramAddress } ): OpenPositionInstruction<TProgramAddress, TAccountFunder, TAccountOwner, TAccountPosition, TAccountPositionMint, TAccountPositionTokenAccount, TAccountWhirlpool, TAccountTokenProgram, TAccountSystemProgram, TAccountRent, TAccountAssociatedTokenProgram> { // Program address. const programAddress = config?.programAddress ?? WHIRLPOOL_PROGRAM_ADDRESS; // Original accounts. const originalAccounts = { funder: { value: input.funder ?? null, isWritable: true }, owner: { value: input.owner ?? null, isWritable: false }, position: { value: input.position ?? null, isWritable: true }, positionMint: { value: input.positionMint ?? null, isWritable: true }, positionTokenAccount: { value: input.positionTokenAccount ?? null, isWritable: true }, whirlpool: { value: input.whirlpool ?? null, isWritable: false }, tokenProgram: { value: input.tokenProgram ?? null, isWritable: false }, systemProgram: { value: input.systemProgram ?? null, isWritable: false }, rent: { value: input.rent ?? null, isWritable: false }, associatedTokenProgram: { value: input.associatedTokenProgram ?? 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'>; } if (!accounts.systemProgram.value) { accounts.systemProgram.value = '11111111111111111111111111111111' as Address<'11111111111111111111111111111111'>; } if (!accounts.rent.value) { accounts.rent.value = 'SysvarRent111111111111111111111111111111111' as Address<'SysvarRent111111111111111111111111111111111'>; } const getAccountMeta = getAccountMetaFactory(programAddress, 'programId'); return Object.freeze({ accounts: [getAccountMeta(accounts.funder), getAccountMeta(accounts.owner), getAccountMeta(accounts.position), getAccountMeta(accounts.positionMint), getAccountMeta(accounts.positionTokenAccount), getAccountMeta(accounts.whirlpool), getAccountMeta(accounts.tokenProgram), getAccountMeta(accounts.systemProgram), getAccountMeta(accounts.rent), getAccountMeta(accounts.associatedTokenProgram)], data: getOpenPositionInstructionDataEncoder().encode(args as OpenPositionInstructionDataArgs), programAddress } as OpenPositionInstruction<TProgramAddress, TAccountFunder, TAccountOwner, TAccountPosition, TAccountPositionMint, TAccountPositionTokenAccount, TAccountWhirlpool, TAccountTokenProgram, TAccountSystemProgram, TAccountRent, TAccountAssociatedTokenProgram>); } export type ParsedOpenPositionInstruction<TProgram extends string = typeof WHIRLPOOL_PROGRAM_ADDRESS, TAccountMetas extends readonly AccountMeta[] = readonly AccountMeta[]> = { programAddress: Address<TProgram>; accounts: { funder: TAccountMetas[0]; owner: TAccountMetas[1]; position: TAccountMetas[2]; positionMint: TAccountMetas[3]; positionTokenAccount: TAccountMetas[4]; whirlpool: TAccountMetas[5]; tokenProgram: TAccountMetas[6]; systemProgram: TAccountMetas[7]; rent: TAccountMetas[8]; associatedTokenProgram: TAccountMetas[9]; }; 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 < 10) { // 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: { funder: getNextAccount(), owner: getNextAccount(), position: getNextAccount(), positionMint: getNextAccount(), positionTokenAccount: getNextAccount(), whirlpool: getNextAccount(), tokenProgram: getNextAccount(), systemProgram: getNextAccount(), rent: getNextAccount(), associatedTokenProgram: getNextAccount() }, data: getOpenPositionInstructionDataDecoder().decode(instruction.data) }; }