import { BN, IdlAccounts, ProgramAccount, IdlTypes, web3 } from '@coral-xyz/anchor'; import { PublicKey, TransactionInstruction, Connection, Cluster } from '@solana/web3.js'; type LbClmm = { version: '0.1.0'; name: 'lb_clmm'; constants: [ { name: 'MAX_DELTA_IDS'; type: { defined: 'usize'; }; value: '50'; }, { name: 'BASIS_POINT_MAX'; type: 'i32'; value: '10000'; }, { name: 'MAX_BIN_PER_ARRAY'; type: { defined: 'usize'; }; value: '200'; }, { name: 'MIN_BIN_ID'; type: 'i32'; value: '- 887272'; }, { name: 'MAX_BIN_ID'; type: 'i32'; value: '887272'; }, { name: 'BIN_ARRAY'; type: 'bytes'; value: '[98, 105, 110, 95, 97, 114, 114, 97, 121]'; }, { name: 'FEE'; type: 'bytes'; value: '[102, 101, 101]'; } ]; instructions: [ { name: 'initializeLbPair'; accounts: [ { name: 'lbPair'; isMut: true; isSigner: false; }, { name: 'tokenMintX'; isMut: false; isSigner: false; }, { name: 'tokenMintY'; isMut: false; isSigner: false; }, { name: 'reserveX'; isMut: true; isSigner: false; }, { name: 'reserveY'; isMut: true; isSigner: false; }, { name: 'protocolReserveX'; isMut: true; isSigner: false; }, { name: 'protocolReserveY'; isMut: true; isSigner: false; }, { name: 'funder'; isMut: true; isSigner: true; }, { name: 'feeOwner'; isMut: false; isSigner: false; }, { name: 'tokenProgram'; isMut: false; isSigner: false; }, { name: 'systemProgram'; isMut: false; isSigner: false; }, { name: 'rent'; isMut: false; isSigner: false; } ]; args: [ { name: 'activeId'; type: 'i32'; }, { name: 'binStep'; type: 'u16'; } ]; }, { name: 'initializeBinArray'; accounts: [ { name: 'lbPair'; isMut: false; isSigner: false; }, { name: 'binArray'; isMut: true; isSigner: false; }, { name: 'funder'; isMut: true; isSigner: true; }, { name: 'systemProgram'; isMut: false; isSigner: false; } ]; args: [ { name: 'index'; type: 'i64'; } ]; }, { name: 'addLiquidity'; accounts: [ { name: 'position'; isMut: true; isSigner: false; }, { name: 'lbPair'; isMut: true; isSigner: false; }, { name: 'userTokenX'; isMut: true; isSigner: false; }, { name: 'userTokenY'; isMut: true; isSigner: false; }, { name: 'reserveX'; isMut: true; isSigner: false; }, { name: 'reserveY'; isMut: true; isSigner: false; }, { name: 'tokenXMint'; isMut: false; isSigner: false; }, { name: 'tokenYMint'; isMut: false; isSigner: false; }, { name: 'binArrayLower'; isMut: true; isSigner: false; }, { name: 'binArrayUpper'; isMut: true; isSigner: false; }, { name: 'owner'; isMut: true; isSigner: true; }, { name: 'tokenXProgram'; isMut: false; isSigner: false; }, { name: 'tokenYProgram'; isMut: false; isSigner: false; } ]; args: [ { name: 'liquidityParameter'; type: { defined: 'LiquidityParameter'; }; } ]; }, { name: 'removeLiquidity'; accounts: [ { name: 'position'; isMut: true; isSigner: false; }, { name: 'lbPair'; isMut: true; isSigner: false; }, { name: 'userTokenX'; isMut: true; isSigner: false; }, { name: 'userTokenY'; isMut: true; isSigner: false; }, { name: 'reserveX'; isMut: true; isSigner: false; }, { name: 'reserveY'; isMut: true; isSigner: false; }, { name: 'tokenXMint'; isMut: false; isSigner: false; }, { name: 'tokenYMint'; isMut: false; isSigner: false; }, { name: 'binArrayLower'; isMut: true; isSigner: false; }, { name: 'binArrayUpper'; isMut: true; isSigner: false; }, { name: 'owner'; isMut: true; isSigner: true; }, { name: 'tokenXProgram'; isMut: false; isSigner: false; }, { name: 'tokenYProgram'; isMut: false; isSigner: false; } ]; args: [ { name: 'binIds'; type: { vec: 'i32'; }; }, { name: 'liquiditiesToRemove'; type: { vec: { array: ['u64', 4]; }; }; } ]; }, { name: 'initializePosition'; accounts: [ { name: 'payer'; isMut: true; isSigner: true; }, { name: 'position'; isMut: true; isSigner: true; }, { name: 'lbPair'; isMut: false; isSigner: false; }, { name: 'owner'; isMut: false; isSigner: true; }, { name: 'systemProgram'; isMut: false; isSigner: false; }, { name: 'rent'; isMut: false; isSigner: false; } ]; args: [ { name: 'positionBound'; type: { defined: 'PositionBound'; }; } ]; }, { name: 'swap'; accounts: [ { name: 'lbPair'; isMut: true; isSigner: false; }, { name: 'reserveX'; isMut: true; isSigner: false; }, { name: 'reserveY'; isMut: true; isSigner: false; }, { name: 'userTokenIn'; isMut: true; isSigner: false; }, { name: 'userTokenOut'; isMut: true; isSigner: false; }, { name: 'binArray0'; isMut: true; isSigner: false; }, { name: 'binArray1'; isMut: true; isSigner: false; }, { name: 'binArray2'; isMut: true; isSigner: false; }, { name: 'tokenXMint'; isMut: false; isSigner: false; }, { name: 'tokenYMint'; isMut: false; isSigner: false; }, { name: 'user'; isMut: true; isSigner: true; }, { name: 'tokenXProgram'; isMut: false; isSigner: false; }, { name: 'tokenYProgram'; isMut: false; isSigner: false; } ]; args: [ { name: 'amountIn'; type: 'u64'; }, { name: 'minAmountOut'; type: 'u64'; } ]; } ]; accounts: [ { name: 'binArray'; docs: [ 'An account to contain a range of bin. For example: Bin 100 <-> 200.', 'For example:', 'BinArray index: 0 contains bin 0 <-> 599', 'index: 2 contains bin 600 <-> 1199, ...' ]; type: { kind: 'struct'; fields: [ { name: 'index'; type: 'i64'; }, { name: 'lbPair'; type: 'publicKey'; }, { name: 'bins'; type: { array: [ { defined: 'Bin'; }, 200 ]; }; } ]; }; }, { name: 'lbPair'; type: { kind: 'struct'; fields: [ { name: 'parameters'; type: { defined: 'StaticParameters'; }; }, { name: 'vParameters'; type: { defined: 'VariableParameters'; }; }, { name: 'bumpSeed'; docs: ['Bump signer seed']; type: { array: ['u8', 1]; }; }, { name: 'binStepSeed'; docs: ['Bin step signer seed']; type: { array: ['u8', 2]; }; }, { name: 'activeId'; docs: ['Active bin id']; type: 'i32'; }, { name: 'binStep'; docs: ['Bin step. Represent the price increment / decrement.']; type: 'u16'; }, { name: 'tokenXMint'; docs: ['Token X mint']; type: 'publicKey'; }, { name: 'tokenYMint'; docs: ['Token Y mint']; type: 'publicKey'; }, { name: 'reserveX'; docs: ['LB token X vault']; type: 'publicKey'; }, { name: 'reserveY'; docs: ['LB token Y vault']; type: 'publicKey'; }, { name: 'protocolReserveX'; docs: ['Protocol token X fee vault']; type: 'publicKey'; }, { name: 'protocolReserveY'; docs: ['Protocol token Y fee vault']; type: 'publicKey'; }, { name: 'protocolFee'; docs: ['Uncollected protocol fee']; type: { defined: 'ProtocolFee'; }; } ]; }; }, { name: 'position'; type: { kind: 'struct'; fields: [ { name: 'lbPair'; docs: ['The LB pair of this position']; type: 'publicKey'; }, { name: 'owner'; docs: ['Owner of the position']; type: 'publicKey'; }, { name: 'lowerBinId'; docs: ['Lower bin ID']; type: 'i32'; }, { name: 'upperBinId'; docs: ['Upper bin ID']; type: 'i32'; }, { name: 'liquidityShares'; docs: [ 'Liquidity shares of this position in bins (lower_bin_id <-> upper_bin_id). This is the same as LP concept.' ]; type: { vec: { array: ['u64', 4]; }; }; } ]; }; } ]; types: [ { name: 'LiquidityParameter'; type: { kind: 'struct'; fields: [ { name: 'amountX'; docs: ['Amount of X token to deposit']; type: 'u64'; }, { name: 'amountY'; docs: ['Amount of Y token to deposit']; type: 'u64'; }, { name: 'amountXMin'; type: 'u64'; }, { name: 'amountYMin'; type: 'u64'; }, { name: 'activeIdDesired'; type: 'i32'; }, { name: 'idSlippage'; type: 'i32'; }, { name: 'deltaIds'; docs: [ 'Define which bin liquidity will be added relative to activeId. Negative will contains only token Y. Positive will contains only token X. 0 may contains both token X and Y.' ]; type: { vec: 'i32'; }; }, { name: 'distributionX'; docs: [ 'DistributionX (or distributionY) is the percentages of amountX (or amountY) you want to add to each bin.' ]; type: { vec: 'u16'; }; }, { name: 'distributionY'; docs: [ 'DistributionX (or distributionY) is the percentages of amountX (or amountY) you want to add to each bin.' ]; type: { vec: 'u16'; }; } ]; }; }, { name: 'PositionBound'; type: { kind: 'struct'; fields: [ { name: 'lowerBinId'; type: 'i32'; }, { name: 'upperBinId'; type: 'i32'; } ]; }; }, { name: 'Bin'; type: { kind: 'struct'; fields: [ { name: 'amountX'; docs: ['Amount of token X in the bin. This already excluded protocol fees.']; type: 'u64'; }, { name: 'amountY'; docs: ['Amount of token Y in the bin. This already excluded protocol fees.']; type: 'u64'; }, { name: 'liquiditySupply'; docs: ['Liquidities of the bin. This is the same as LP mint supply.']; type: { array: ['u64', 4]; }; } ]; }; }, { name: 'ProtocolFee'; type: { kind: 'struct'; fields: [ { name: 'amountX'; type: 'u64'; }, { name: 'amountY'; type: 'u64'; } ]; }; }, { name: 'StaticParameters'; docs: ['Parameter that set by the protocol']; type: { kind: 'struct'; fields: [ { name: 'baseFactor'; docs: ['Used for base fee calculation. base_fee_rate = base_factor * bin_step']; type: 'u16'; }, { name: 'filterPeriod'; docs: ['Filter period determine high frequency trading time window.']; type: 'u16'; }, { name: 'decayPeriod'; docs: ['Decay period determine when the volatile fee start decay / decrease.']; type: 'u16'; }, { name: 'reductionFactor'; docs: ['Reduction factor controls the volatile fee rate decrement rate.']; type: 'u16'; }, { name: 'variableFeeControl'; docs: ['Used to scale the variable fee component depending on the dynamic of the market']; type: 'u32'; }, { name: 'protocolShare'; docs: [ 'Portion of swap fees retained by the protocol by controlling protocol_share parameter. protocol_swap_fee = protocol_share * total_swap_fee' ]; type: 'u16'; }, { name: 'maxVolatilityAccumulator'; docs: ['Maximum number of bin crossed can be accumulated. Used to cap volatile fee rate.']; type: 'u32'; } ]; }; }, { name: 'VariableParameters'; docs: ['Parameters that changes based on dynamic of the market']; type: { kind: 'struct'; fields: [ { name: 'volatilityAccumulator'; docs: [ 'Volatility accumulator measure the number of bin crossed since reference bin ID. Normally (without filter period taken into consideration), reference bin ID is the active bin of last swap.', 'It affects the variable fee rate' ]; type: 'u32'; }, { name: 'volatilityReference'; docs: ['Volatility reference is decayed volatility accumulator. It is always <= volatility_accumulator']; type: 'u32'; }, { name: 'indexReference'; docs: ['Active bin id of last swap.']; type: 'i32'; }, { name: 'lastUpdateTimestamp'; type: 'i64'; } ]; }; }, { name: 'Rounding'; type: { kind: 'enum'; variants: [ { name: 'Up'; }, { name: 'Down'; } ]; }; } ]; events: [ { name: 'CompositionFee'; fields: [ { name: 'from'; type: 'publicKey'; index: false; }, { name: 'binId'; type: 'i16'; index: false; }, { name: 'tokenXFeeAmount'; type: 'u64'; index: false; }, { name: 'tokenYFeeAmount'; type: 'u64'; index: false; }, { name: 'protocolTokenXFeeAmount'; type: 'u64'; index: false; }, { name: 'protocolTokenYFeeAmount'; type: 'u64'; index: false; } ]; }, { name: 'AddLiquidity'; fields: [ { name: 'from'; type: 'publicKey'; index: false; }, { name: 'binIds'; type: { vec: 'i32'; }; index: false; }, { name: 'amounts'; type: { vec: { array: ['u64', 2]; }; }; index: false; } ]; }, { name: 'RemoveLiquidity'; fields: [ { name: 'from'; type: 'publicKey'; index: false; }, { name: 'binIds'; type: { vec: 'i32'; }; index: false; }, { name: 'amounts'; type: { vec: { array: ['u64', 2]; }; }; index: false; } ]; }, { name: 'Swap'; fields: [ { name: 'from'; type: 'publicKey'; index: false; }, { name: 'binId'; type: 'i32'; index: false; }, { name: 'amountIn'; type: 'u64'; index: false; }, { name: 'amountOut'; type: 'u64'; index: false; }, { name: 'swapForY'; type: 'bool'; index: false; }, { name: 'fee'; type: 'u64'; index: false; }, { name: 'protocolFee'; type: 'u64'; index: false; } ]; } ]; errors: [ { code: 6000; name: 'InvalidStartBinIndex'; msg: 'Invalid start bin index'; }, { code: 6001; name: 'InvalidBinId'; msg: 'Invalid bin id'; }, { code: 6002; name: 'InvalidInput'; msg: 'Invalid input data'; }, { code: 6003; name: 'ExceededIdSlippageTolerance'; msg: 'Exceeded id slippage tolerance'; }, { code: 6004; name: 'ExceededAmountSlippageTolerance'; msg: 'Exceeded amount slippage tolerance'; }, { code: 6005; name: 'CompositionFactorFlawed'; msg: 'Composition factor flawed'; }, { code: 6006; name: 'NonPresetBinStep'; msg: 'Non preset bin step'; }, { code: 6007; name: 'ZeroLiquidity'; msg: 'Zero liquidity'; }, { code: 6008; name: 'InvalidPosition'; msg: 'Invalid position'; }, { code: 6009; name: 'BinArrayNotFound'; msg: 'Bin array not found'; }, { code: 6010; name: 'InvalidTokenMint'; msg: 'Invalid token mint'; }, { code: 6011; name: 'BinInsufficientLiquidity'; msg: 'Bin insufficient liquidity'; }, { code: 6012; name: 'InvalidFeeOwner'; msg: 'Invalid fee owner'; } ]; }; interface LiquidityParameter { amountX: BN; amountY: BN; amountXMin: BN; amountYMin: BN; activeIdDesired: number; idSlippage: number; deltaIds: number[]; distributionX: number[]; distributionY: number[]; } interface BinAndAmount { binId: number; xAmountBpsOfTotal: BN; yAmountBpsOfTotal: BN; } type LbPairAccount = IdlAccounts['lbPair']; type LbPairAccountsStruct = ProgramAccount['lbPair']>; type Bin = IdlTypes['Bin']; type BinArray = Omit['binArray'], ''>; interface TInitializePositionAndAddLiquidityParams { lbPairPubKey: PublicKey; positionPubKey: PublicKey; totalXAmount: BN; totalYAmount: BN; amountSlipageBps: BN; maxBinSlippage: number; activeBinId: number; xYAmountDistribution: BinAndAmount[]; user: PublicKey; userTokenXAccount?: PublicKey; userTokenYAccount?: PublicKey; } interface BinLiquidity { binId: number; xAmount: BN; yAmount: BN; supply: BN; price: string; } interface PositionBinData { binId: number; price: string; binXAmount: string; binYAmount: string; binLiquidity: string; positionLiquidity: string; positionXAmount: string; positionYAmount: string; } interface PositionData { totalXAmount: string; totalYAmount: string; positionBinData: PositionBinData[]; } interface SwapParams { /** * mint of in token */ inToken: PublicKey; /** * mint of out token */ outToken: PublicKey; /** * in token amount */ inAmount: BN; /** * minimum out with slippage */ minOutAmount: BN; /** * desired lbPair to swap against */ lbPair: PublicKey; /** * user */ user: PublicKey; /** * Optional. If not passed in, will use ATA */ outTokenAccount?: PublicKey; /** * Optional. If not passed in, will use ATA */ inTokenAccount?: PublicKey; } interface GetOrCreateATAResponse { ataPubKey: PublicKey; ix?: TransactionInstruction; } declare const LBCLMM_PROGRAM_IDS: { devnet: string; localhost: string; }; declare const ADMIN: { devnet: string; localhost: string; }; declare enum Network { MAINNET = "mainnet-beta", TESTNET = "testnet", DEVNET = "devnet", LOCAL = "localhost" } declare const BASIS_POINT_MAX = 10000; declare const MAX_BIN_ARRAY_SIZE: any; declare function u64ArraysToBN(values: BN[]): BN; declare function BNTou64Arrays(value: BN): BN[]; declare function sortTokenMints(tokenX: PublicKey, tokenY: PublicKey): PublicKey[]; declare function deriveLbPair(tokenX: PublicKey, tokenY: PublicKey, binStep: BN, programId: PublicKey): [PublicKey, number]; declare function derivePosition(mint: PublicKey, programId: PublicKey): [PublicKey, number]; declare function binIdToBinArrayIndex(binId: BN): any; declare function deriveBinArray(lbPair: PublicKey, index: BN, programId: PublicKey): [PublicKey, number]; declare function deriveReserve(token: PublicKey, lbPair: PublicKey, programId: PublicKey): [PublicKey, number]; declare function deriveFee(token: PublicKey, lbPair: PublicKey, programId: PublicKey): [PublicKey, number]; declare function getBinArrayLowerUpperBinId(binArrayIndex: BN): any[]; declare function calculateDistribution(activeBin: number, binIds: number[]): { binId: number; xAmountBpsOfTotal: BN; yAmountBpsOfTotal: BN; }[]; declare function getTokenDecimals(conn: Connection, mint: PublicKey): Promise; declare const getOrCreateATAInstruction: (connection: Connection, tokenMint: PublicKey, owner: PublicKey, payer?: PublicKey, allowOwnerOffCurve?: boolean) => Promise; declare function getTokenBalance(conn: Connection, tokenAccount: PublicKey): Promise; declare class LBCLMM { private readonly connection; private program; private programAdmin; constructor(connection: Connection, cluster?: Cluster | 'localhost'); getLbPairs(): Promise; getLbPair(lbPairPubKey: PublicKey): Promise; getReserves(lbPairPubKey: PublicKey): Promise<{ tokenX: { publicKey: web3.PublicKey; reserve: web3.PublicKey; amount: bigint; }; tokenY: { publicKey: web3.PublicKey; reserve: web3.PublicKey; amount: bigint; }; }>; getBinsAroundActiveBin(lbPairPubKey: PublicKey, numberOfBinsToTheLeft: number, numberOfBinsToTheRight: number): Promise<{ activeBin: number; bins: BinLiquidity[]; }>; getBinsBetweenMinAndMaxPrice(lbPairPubKey: PublicKey, minPrice: number, maxPrice: number): Promise<{ activeBin: number; bins: BinLiquidity[]; }>; getBinsBetweenLowerAndUpperBound(lbPairPubKey: PublicKey, lowerBinId: number, upperBinId: number): Promise<{ activeBin: number; bins: BinLiquidity[]; }>; private getBins; getCurrentPrice(lbPairPubKey: PublicKey): Promise; getPriceOfBinByLbPair(lbPairPubKey: PublicKey, binId: number): Promise; getPriceOfBinByBinStep(binStep: number, binId: number): string; getBinIdFromPrice(binStep: number, price: number, min: boolean): number; getPositionsByUser(userPubKey: PublicKey): Promise<{ publicKey: web3.PublicKey; positionData: PositionData; }[]>; getPositionsByUserAndLbPair(userPubKey: PublicKey, lbPairPubKey: PublicKey): Promise<{ publicKey: PublicKey; positionData: PositionData; }[]>; getPosition(positionPubKey: PublicKey): Promise; private processPosition; initializePositionAndAddLiquidity({ lbPairPubKey, positionPubKey, totalXAmount, totalYAmount, amountSlipageBps, maxBinSlippage, activeBinId, xYAmountDistribution, user, userTokenXAccount, userTokenYAccount, }: TInitializePositionAndAddLiquidityParams): Promise; private processXYAmountDistribution; createBinArraysIfNeeded(lbPair: PublicKey, binArrayIndexes: BN[], funder: PublicKey): Promise; removeLiquidity({ user, position, binIds, liquiditiesBpsToRemove, tokenXAccount, tokenYAccount, }: { user: PublicKey; position: PublicKey; binIds: number[]; liquiditiesBpsToRemove: BN[]; tokenXAccount?: PublicKey; tokenYAccount?: PublicKey; }): Promise; /** * Returns an equal weightage amount of the opposing token based on ratio. * @param {number} lbPair - The pubkey of the LB Pair * @param {BN} tokenXAmount - Optional. The amount of token X to deposit. Returns equal weightage amount of token Y. * @param {BN} tokenYAmount - Optional. The amount of token Y to deposit. Returns equal weightage amount of token X. * @returns {BN} The equal weightage of the opposing token. If tokenXAmount was passed in, token Y amount is returned else if tokenYAmount was passed in, token X amount is returned */ getEqualWeightageAmount(lbPairPubKey: PublicKey, tokenXAmount?: BN, tokenYAmount?: BN): Promise; /** * Returns a transaction to be signed and sent by user performing swap. * @param {SwapParams} {inToken, outToken, inAmount, minOutAmount, lbPair, user, outTokenAccount, inTokenAccount} * @returns {Transaction} */ swap({ inToken, outToken, inAmount, minOutAmount, lbPair, user, outTokenAccount, inTokenAccount, }: SwapParams): Promise; private getBinArraysForSwap; createLbPair(funder: PublicKey, tokenX: PublicKey, tokenY: PublicKey, activeId: BN, binStep: BN): Promise; findLbPairs({ tokenX, tokenY, }: { tokenX: PublicKey; tokenY: PublicKey; }): Promise<{ lbPairs: LbPairAccountsStruct[]; inversedLbPairs: LbPairAccountsStruct[]; }>; } export { ADMIN, BASIS_POINT_MAX, BNTou64Arrays, Bin, BinAndAmount, BinArray, BinLiquidity, GetOrCreateATAResponse, LBCLMM, LBCLMM_PROGRAM_IDS, LbPairAccount, LbPairAccountsStruct, LiquidityParameter, MAX_BIN_ARRAY_SIZE, Network, PositionBinData, PositionData, SwapParams, TInitializePositionAndAddLiquidityParams, binIdToBinArrayIndex, calculateDistribution, deriveBinArray, deriveFee, deriveLbPair, derivePosition, deriveReserve, getBinArrayLowerUpperBinId, getOrCreateATAInstruction, getTokenBalance, getTokenDecimals, sortTokenMints, u64ArraysToBN };