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 pool

  • tick_spacing: Distance between ticks

  • fee_rate: Trading fee rate

  • liquidity: Current total liquidity

  • current_sqrt_price: Square root of the current price

  • current_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 index

  • tick_upper_idx: Upper tick index

  • lock_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 CoinB

  • by_amount_in: True if specifying input amount

  • amount: Token amount

  • sqrt_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

  1. Slippage Protection: Always use sqrt_price_limit when swapping to protect against slippage

  2. Receipt Validation: Carefully validate receipts before repaying

  3. Permission Checks: Only admins can pause/unpause pools

  4. Lock Period: Positions can be locked for specified time periods

Gas Optimization

  1. Batch Operations: Group multiple operations in a single transaction

  2. Efficient Calculations: Module uses optimized math libraries

  3. Event Emission: Events are optimized to save gas

Error Codes

  • 0: Insufficient balance or invalid amount

  • 3: Invalid liquidity amount

  • 4: No tick available for swap

  • 5: Insufficient amount for subtraction

  • 9: Fee rate exceeds maximum

  • 11: Invalid sqrt price limit

  • 13: Pool is paused

  • 17: Rewarder not found

  • 18: No amount out from swap

  • 19: Pool ID mismatch

  • 20: Flash loan pool mismatch

Last updated