Pool Module
The pool module is the core component of the CLMM protocol, defining trading pairs and handling all operations related to trading and liquidity management.
Data Structures
Pool Struct
struct Pool<phantom CoinTypeA, phantom CoinTypeB> has key, store {
id: UID,
coin_a: Balance<CoinTypeA>,
coin_b: Balance<CoinTypeB>,
tick_spacing: u32,
fee_rate: u64,
liquidity: u128,
current_sqrt_price: u128,
current_tick_index: I32,
fee_growth_global_a: u128,
fee_growth_global_b: u128,
fee_protocol_coin_a: u64,
fee_protocol_coin_b: u64,
tick_manager: TickManager,
rewarder_manager: RewarderManager,
position_manager: PositionManager,
is_pause: bool,
index: u64,
url: String,
}Key Fields:
coin_a,coin_b: Balance of both token types in the pooltick_spacing: Distance between ticksfee_rate: Trading fee rateliquidity: Current total liquiditycurrent_sqrt_price: Square root of the current pricecurrent_tick_index: Current tick index
Receipt Structs
FlashSwapReceipt
AddLiquidityReceipt
FlashLoanReceipt
Core Functions
1. Position Management
Open Position
Description: Creates a new liquidity position within the specified tick range.
Parameters:
tick_lower_idx: Lower tick indextick_upper_idx: Upper tick indexlock_until: Position lock timestamp
Close Position
2. Liquidity Management
Add Liquidity
Add Liquidity with Fixed Amount
Remove Liquidity
3. Trading
Flash Swap
Parameters:
a2b: True if swapping from CoinA to CoinBby_amount_in: True if specifying input amountamount: Token amountsqrt_price_limit: Price limit protection
Calculate Swap Result
4. Flash Loans
Create Flash Loan
Repay Flash Loan
5. Fee and Reward Collection
Collect Position Fees
Collect Rewards
Collect Protocol Fees
Events
OpenPositionEvent
SwapEvent
AddLiquidityEvent
Utility Functions
Pool Information Queries
Position Information Queries
Rewarder Management
Initialize New Rewarder
Update Emission Rate
Pool Management
Pause Pool
Unpause Pool
Update Fee Rate
Important Notes
Security Considerations
Slippage Protection: Always use
sqrt_price_limitwhen swapping to protect against slippageReceipt Validation: Carefully validate receipts before repaying
Permission Checks: Only admins can pause/unpause pools
Lock Period: Positions can be locked for specified time periods
Gas Optimization
Batch Operations: Group multiple operations in a single transaction
Efficient Calculations: Module uses optimized math libraries
Event Emission: Events are optimized to save gas
Error Codes
0: Insufficient balance or invalid amount3: Invalid liquidity amount4: No tick available for swap5: Insufficient amount for subtraction9: Fee rate exceeds maximum11: Invalid sqrt price limit13: Pool is paused17: Rewarder not found18: No amount out from swap19: Pool ID mismatch20: Flash loan pool mismatch
Last updated