Manages the discrete price points and liquidity distribution across the price curve.
Core Data Structures
TickManager
The main controller that organizes all ticks for a pool:
structTickManager{tick_spacing:u32,// Spacing between valid ticksticks:SkipList<Tick>// Ordered collection of ticks}
Tick
Individual tick containing all relevant price point data:
structTick{index:I32,// Tick index (price level)sqrt_price:u128,// Square root price at this tickliquidity_net:I128,// Net liquidity change when crossingliquidity_gross:u128,// Total liquidity at this tickfee_growth_outside_a:u128,// Fee growth outside for token Afee_growth_outside_b:u128,// Fee growth outside for token Bpoints_growth_outside:u128,// Points growth outsiderewards_growth_outside:vector<u128>// Reward growth outside (per token)}
Key Properties
Liquidity Net vs Gross
liquidity_net: Signed change in active liquidity when crossing this tick
Positive when tick is the lower bound of positions
Negative when tick is the upper bound of positions
liquidity_gross: Total amount of liquidity referencing this tick
Always positive
Used to determine if tick can be removed
Growth Outside Pattern
The "growth outside" pattern enables efficient fee and reward calculations:
Tracks accumulated growth outside the current active range
Allows O(1) calculation of growth within any range
Automatically updated when ticks are crossed during swaps
public fun first_score_for_swap(
tick_manager: &TickManager,
tick_idx: I32,
a2b: bool
) : OptionU64
public fun borrow_tick_for_swap(
tick_manager: &TickManager,
node_id: u64,
a2b: bool
) : (&Tick, OptionU64)
public fun get_fee_in_range(
current_tick: I32,
fee_growth_global_a: u128,
fee_growth_global_b: u128,
tick_lower_opt: Option<Tick>,
tick_upper_opt: Option<Tick>
) : (u128, u128)
fee_inside = fee_global - fee_below - fee_above
public fun get_points_in_range(
current_tick: I32,
points_growth_global: u128,
tick_lower_opt: Option<Tick>,
tick_upper_opt: Option<Tick>
) : u128
public fun get_rewards_in_range(
current_tick: I32,
rewards_growth_global: vector<u128>,
tick_lower_opt: Option<Tick>,
tick_upper_opt: Option<Tick>
) : vector<u128>
public fun fetch_ticks(
tick_manager: &TickManager,
start: vector<u32>,
limit: u64
) : vector<Tick>
public fun borrow_tick(tick_manager: &TickManager, tick_idx: I32) : &Tick
public fun try_borrow_tick(tick_manager: &TickManager, tick_idx: I32) : Option<Tick>
public fun index(tick: &Tick) : I32
public fun sqrt_price(tick: &Tick) : u128
public fun liquidity_gross(tick: &Tick) : u128
public fun liquidity_net(tick: &Tick) : I128
public fun fee_growth_outside(tick: &Tick) : (u128, u128)
public fun points_growth_outside(tick: &Tick) : u128
public fun rewards_growth_outside(tick: &Tick) : &vector<u128>
public fun tick_spacing(tick_manager: &TickManager) : u32
public fun tick_count(tick_manager: &TickManager) : u64
public fun get_reward_growth_outside(tick: &Tick, index: u64) : u128