UNPKG

@kamino-finance/kliquidity-sdk

Version:

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

86 lines (81 loc) 2.6 kB
/* eslint-disable @typescript-eslint/no-unused-vars */ import { Address, isSome, IAccountMeta, IAccountSignerMeta, IInstruction, Option, TransactionSigner, } from "@solana/kit" /* eslint-enable @typescript-eslint/no-unused-vars */ import BN from "bn.js" // eslint-disable-line @typescript-eslint/no-unused-vars import * as borsh from "@coral-xyz/borsh" // eslint-disable-line @typescript-eslint/no-unused-vars import { borshAddress } from "../utils" // eslint-disable-line @typescript-eslint/no-unused-vars import * as types from "../types" // eslint-disable-line @typescript-eslint/no-unused-vars import { PROGRAM_ID } from "../programId" export interface SwapArgs { amount: BN otherAmountThreshold: BN sqrtPriceLimit: BN amountSpecifiedIsInput: boolean aToB: boolean } export interface SwapAccounts { tokenProgram: Address tokenAuthority: TransactionSigner whirlpool: Address tokenOwnerAccountA: Address tokenVaultA: Address tokenOwnerAccountB: Address tokenVaultB: Address tickArray0: Address tickArray1: Address tickArray2: Address oracle: Address } export const layout = borsh.struct([ borsh.u64("amount"), borsh.u64("otherAmountThreshold"), borsh.u128("sqrtPriceLimit"), borsh.bool("amountSpecifiedIsInput"), borsh.bool("aToB"), ]) export function swap( args: SwapArgs, accounts: SwapAccounts, programAddress: Address = PROGRAM_ID ) { const keys: Array<IAccountMeta | IAccountSignerMeta> = [ { address: accounts.tokenProgram, role: 0 }, { address: accounts.tokenAuthority.address, role: 2, signer: accounts.tokenAuthority, }, { address: accounts.whirlpool, role: 1 }, { address: accounts.tokenOwnerAccountA, role: 1 }, { address: accounts.tokenVaultA, role: 1 }, { address: accounts.tokenOwnerAccountB, role: 1 }, { address: accounts.tokenVaultB, role: 1 }, { address: accounts.tickArray0, role: 1 }, { address: accounts.tickArray1, role: 1 }, { address: accounts.tickArray2, role: 1 }, { address: accounts.oracle, role: 0 }, ] const identifier = Buffer.from([248, 198, 158, 145, 225, 117, 135, 200]) const buffer = Buffer.alloc(1000) const len = layout.encode( { amount: args.amount, otherAmountThreshold: args.otherAmountThreshold, sqrtPriceLimit: args.sqrtPriceLimit, amountSpecifiedIsInput: args.amountSpecifiedIsInput, aToB: args.aToB, }, buffer ) const data = Buffer.concat([identifier, buffer]).slice(0, 8 + len) const ix: IInstruction = { accounts: keys, programAddress, data } return ix }