@firefly-exchange/library-sui
Version:
Sui library housing helper methods, classes to interact with Bluefin protocol(s) deployed on Sui
185 lines (184 loc) • 7.5 kB
TypeScript
/**
* This file incorporates code from cetus-clmm-sui-sdk by CetusProtocol,
* licensed under the Apache License 2.0 (http://www.apache.org/licenses/LICENSE-2.0)
* which can be found at https://github.com/CetusProtocol/cetus-clmm-sui-sdk/blob/main/LICENSE
*/
import BN from "bn.js";
export type SwapStepResult = {
amountIn: BN;
amountOut: BN;
nextSqrtPrice: BN;
feeAmount: BN;
};
export type SwapResult = {
amountIn: BN;
amountOut: BN;
feeAmount: BN;
refAmount: BN;
nextSqrtPrice: BN;
crossTickNum: number;
};
export type CoinAmounts = {
coinA: BN;
coinB: BN;
};
export declare function toCoinAmount(a: number, b: number): CoinAmounts;
/**
* Represents input data for adding liquidity to a pool.
*/
export type LiquidityInput = {
coinAmount: BN;
coinAmountA: BN;
coinAmountB: BN;
tokenMaxA: BN;
tokenMaxB: BN;
liquidityAmount: BN;
fix_amount_a: boolean;
};
/**
* Calculates the change in amount A between two prices based on a given amount of liquidity.
* The formula is `delta_a = (liquidity * delta_sqrt_price) / (sqrt_price_upper * sqrt_price_lower)`
*
* @param sqrtPrice0 - The first sqrt price
* @param sqrtPrice1 - The second sqrt price
* @param liquidity - The available liquidity to use
* @param roundUp - Flag to indicate whether to round the result up or down
* @returns
*/
export declare function getDeltaA(sqrtPrice0: BN, sqrtPrice1: BN, liquidity: BN, roundUp: boolean): BN;
/**
* Computes the change in amount B between two prices for a given liquidity amount.
* The formula used is `delta_a = (liquidity * delta_sqrt_price) / (sqrt_price_upper * sqrt_price_lower)`
*
* @param sqrtPrice0 - The first sqrt price
* @param sqrtPrice1 - The second sqrt price
* @param liquidity - The amount of available liquidity
* @param roundUp - Determines if the result should be rounded up or down
* @returns
*/
export declare function getDeltaB(sqrtPrice0: BN, sqrtPrice1: BN, liquidity: BN, roundUp: boolean): BN;
/**
* Calculates the next sqrt price based on a delta of token_a.
* The formula is `new_sqrt_price = (sqrt_price * liquidity) / (liquidity +/- amount * sqrt_price)`
*
* @param sqrtPrice - The initial sqrt price
* @param liquidity - The available liquidity
* @param amount - The amount of token_a involved
* @param byAmountIn - Determines whether the input is fixed
*/
export declare function getNextSqrtPriceAUp(sqrtPrice: BN, liquidity: BN, amount: BN, byAmountIn: boolean): BN;
/**
* Calculates the next sqrt price based on a delta of token_b.
* The formula is `new_sqrt_price = (sqrt_price + (delta_b / liquidity))`
*
* @param sqrtPrice - The initial sqrt price
* @param liquidity - The available liquidity
* @param amount - The amount of token_b involved
* @param byAmountIn - Indicates whether the input is fixed
*/
export declare function getNextSqrtPriceBDown(sqrtPrice: BN, liquidity: BN, amount: BN, byAmountIn: boolean): BN;
/**
* Calculates the next sqrt price based on the provided parameters.
*
* @param sqrtPrice - The current sqrt price
* @param liquidity - The available liquidity
* @param amount - The token amount involved
* @param aToB - A flag indicating if the calculation is from token_a to token_b
* @returns
*/
export declare function getNextSqrtPriceFromInput(sqrtPrice: BN, liquidity: BN, amount: BN, aToB: boolean): BN;
/**
* Calculates the next sqrt price based on the output parameters.
*
* @param sqrtPrice - The current sqrt price
* @param liquidity - The available liquidity
* @param amount - The token amount involved
* @param a2b - A flag indicating if the operation is from token_a to token_b
* @returns
*/
export declare function getNextSqrtPriceFromOutput(sqrtPrice: BN, liquidity: BN, amount: BN, a2b: boolean): BN;
/**
* Calculates the amount of delta_a or delta_b based on the input parameters, rounding the result up.
*
* @param currentSqrtPrice - The current sqrt price
* @param targetSqrtPrice - The target sqrt price
* @param liquidity - The available liquidity
* @param a2b - A flag indicating if the calculation is from token_a to token_b
* @returns
*/
export declare function getDeltaUpFromInput(currentSqrtPrice: BN, targetSqrtPrice: BN, liquidity: BN, a2b: boolean): BN;
/**
* Calculates the amount of delta_a or delta_b based on the output parameters, rounding the result down.
*
* @param currentSqrtPrice - The current sqrt price
* @param targetSqrtPrice - The target sqrt price
* @param liquidity - The available liquidity
* @param a2b - A flag indicating if the operation is from token_a to token_b
* @returns
*/
export declare function getDeltaDownFromOutput(currentSqrtPrice: BN, targetSqrtPrice: BN, liquidity: BN, a2b: boolean): BN;
/**
* Simulates each step of a swap for every tick.
*
* @param currentSqrtPrice - The current sqrt price
* @param targetSqrtPrice - The target sqrt price
* @param liquidity - The available liquidity
* @param amount - The token amount involved
* @param feeRate - The applied fee rate for the swap
* @param byAmountIn - Indicates whether the input amount is fixed
* @returns
*/
export declare function computeSwapStep(currentSqrtPrice: BN, targetSqrtPrice: BN, liquidity: BN, amount: BN, feeRate: BN, byAmountIn: boolean): SwapStepResult;
/**
* Estimates the liquidity for coin A.
*
* @param sqrtPriceX - The sqrt price of coin A
* @param sqrtPriceY - The sqrt price of coin B
* @param coinAmount - The amount of tokens involved
* @returns
*/
export declare function estimateLiquidityForCoinA(sqrtPriceX: BN, sqrtPriceY: BN, coinAmount: BN): BN;
/**
* Estimates the liquidity for coin B.
*
* @param sqrtPriceX - The sqrt price of coin A
* @param sqrtPriceY - The sqrt price of coin B
* @param coinAmount - The amount of tokens involved
* @returns
*/
export declare function estimateLiquidityForCoinB(sqrtPriceX: BN, sqrtPriceY: BN, coinAmount: BN): BN;
export declare class ClmmPoolUtil {
/**
* Calculates the token amount from liquidity.
*
* @param liquidity - The available liquidity
* @param curSqrtPrice - The current sqrt price of the pool
* @param lowerSqrtPrice - The lower sqrt price of the position
* @param upperSqrtPrice - The upper sqrt price of the position
* @param roundUp - Specifies whether to round the result up
* @returns
*/
static getCoinAmountFromLiquidity(liquidity: BN, curSqrtPrice: BN, lowerSqrtPrice: BN, upperSqrtPrice: BN, roundUp: boolean): CoinAmounts;
/**
* Estimates liquidity based on token amounts.
*
* @param curSqrtPrice - The current sqrt price
* @param lowerTick - The lower tick
* @param upperTick - The upper tick
* @param tokenAmount - The amount of tokens
* @returns
*/
static estimateLiquidityFromCoinAmounts(curSqrtPrice: BN, lowerTick: number, upperTick: number, tokenAmount: CoinAmounts): BN;
/**
* Estimate liquidity and token amount from one amounts
* @param lowerTick - lower tick
* @param upperTick - upper tick
* @param coinAmount - token amount
* @param isCoinA - is token A
* @param roundUp - is round up
* @param slippage - slippage percentage
* @param curSqrtPrice - current sqrt price.
* @return IncreaseLiquidityInput
*/
static estLiquidityAndCoinAmountFromOneAmounts(lowerTick: number, upperTick: number, coinAmount: BN, isCoinA: boolean, roundUp: boolean, slippage: number, curSqrtPrice: BN): LiquidityInput;
}