- System: Mac or Linux
- Rust
- Solana, v1.18.12 or later
- Anchor Install Anchor version manager (avm), then Anchor 0.29.0
./build.sh
./tests.sh
anchor build
cd sdk
npm i
npm run build
cargo test
npm i
./tests.sh
Computes the max tick for given tick spacing.
pub fn get_max_tick(tick_spacing: u16) -> i32
Computes the min tick for given tick spacing.
pub fn get_min_tick(tick_spacing: u16) -> i32
This function allows to change discrete values from tick (
pub fn calculate_price_sqrt(tick_index: i32) -> Price
Calculates the token amounts of X needed to change the price between points A and B, corresponding to their respective price roots and the liquidity between these points. The order of prices at points A and B can be ignored since the formula uses absolute values.
pub fn get_delta_x(
sqrt_price_a: Price,
sqrt_price_b: Price,
liquidity: Liquidity,
up: bool) -> Option<TokenAmount>
Calculates the token amounts of Y needed to change the price between points A and B, corresponding to their respective price roots and the liquidity between these points. The order of prices at points A and B can be ignored since the formula uses absolute values.
pub fn get_delta_y(
sqrt_price_a: Price,
sqrt_price_b: Price,
liquidity: Liquidity,
up: bool) -> Option<TokenAmount>
Calculate the required amounts of token X and Y when adding or removing liquidity from the pool within a specified price range and liquidity delta. The price range is determined by lower and upper ticks, and the liquidity direction is indicated by the liquidity sign. Additionally, the token ratio is determined by the current square root of the price.
pub fn calculate_amount_delta(
current_sqrt_price: Price,
liquidity_delta: Liquidity,
liquidity_sign: bool,
current_tick_index: i32,
lower_tick: i32,
upper_tick: i32,
) -> Result<(TokenAmount, TokenAmount), String>
Calculate the amount of liquidity provided based on the amount of token x provided based on the current price and the prices of the liquidity range from lower to upper sqrt_price. Additionally returns the amount of token y required for that liquidity change to happen.
pub fn get_liquidity_by_x_sqrt_price(
x: TokenAmount,
lower_sqrt_price: Price,
upper_sqrt_price: Price,
current_sqrt_price: Price,
rounding_up: bool,
) -> TrackableResult<SingleTokenLiquidity>
pub struct SingleTokenLiquidity {
pub l: Liquidity,
pub amount: TokenAmount,
}
Calculate the amount of liquidity provided based on the amount of token y provided the current price and the prices of the liquidity range from lower to upper sqrt_price. Additionally returns the amount of token x required for that liquidity change to happen.
pub fn get_liquidity_by_y_sqrt_price(
y: TokenAmount,
lower_sqrt_price: Price,
upper_sqrt_price: Price,
current_sqrt_price: Price,
rounding_up: bool,
) -> TrackableResult<SingleTokenLiquidity>
pub struct SingleTokenLiquidity {
pub l: Liquidity,
pub amount: TokenAmount,
}
Converts from liquidity delta to the amount of tokens.
pub fn liquidity_to_lp_token_amount(
lp_token_supply: TokenAmount,
current_liquidity: Liquidity,
liquidity_delta: Liquidity,
rounding_up: bool,
) -> TrackableResult<TokenAmount>
const
Converts from the amount of tokens to liquidity delta.
pub fn lp_token_amount_to_liquidity(
lp_token_supply: TokenAmount,
current_liquidity: Liquidity,
lp_token_amount_delta: TokenAmount,
) -> TrackableResult<Liquidity>
const
Calculates max liquidity achievable given the provided token amounts.
pub fn get_max_liquidity(
x: TokenAmount,
y: TokenAmount,
lower_tick: i32,
upper_tick: i32,
current_sqrt_price: Price,
rounding_up: bool,
) -> TrackableResult<LiquidityResult>
pub struct LiquidityResult {
pub x: TokenAmount,
pub y: TokenAmount,
pub l: Liquidity,
}
Computation is performed as follows: The function evaluates the maximum liquidity achievable given the tokens provided on the full liquidity range (min to max tick for the provided tick spacing).
- Next the amount of tokens is calculated from the liquidity, to get the actual amount that will be used.
- If the liquidity delta argument is not zero then the amount required to change the position according to the given liquidity delta is computed. Otherwise the function will return early with the position calculated from the initial amounts.
- Any tokens that would not fit into the initial position are returned separately.
- Additionally the amount of Lp tokens to be burned or minted are returned, in case when liquidity is provided tokens are rounded down, if it’s created they’re rounded up.
fn compute_lp_share_change(
provide_liquidity: bool,
lp_token_supply: TokenAmount,
liquidity_delta: Liquidity,
x_before: TokenAmount,
y_before: TokenAmount,
tick_spacing: u16,
current_tick_index: i32,
current_sqrt_price: Price,
) -> TrackableResult<LiquidityChangeResult>
pub struct PositionDetails {
pub lower_tick: i32,
pub upper_tick: i32,
pub liquidity: Liquidity,
}
pub struct LiquidityChangeResult {
positions_details: PositionDetails,
transferred_amounts: (TokenAmount, TokenAmount),
lp_token_change: Option<TokenAmount>,
leftover_amounts: (TokenAmount, TokenAmount),
}