Pool Module
The Pool module is the core component of the DAMM protocol, defining trading pairs and handling all operations related to trading, liquidity management, and advanced fee mechanisms.
Overview
The Pool module provides:
Core swap functionality with dynamic and scheduled fees
Concentrated liquidity management
Flash swap and flash loan capabilities
Position and fee management
Reward distribution integration
Whitelist and activation controls
Data Structures
Pool Struct
struct Pool<phantom CoinTypeA, phantom CoinTypeB> has key, store {
id: UID,
creator: address,
parameters: PairParameters,
coin_a: Balance<CoinTypeA>,
coin_b: Balance<CoinTypeB>,
liquidity: u128,
whitelisted: VecSet<address>,
collect_fee_mode: u8,
is_quote_y: bool,
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:
creator
address
Pool creator address
parameters
PairParameters
Fee scheduler and dynamic fee configuration
coin_a, coin_b
Balance
Token balances in the pool
liquidity
u128
Current active liquidity
whitelisted
VecSet<address>
Pre-activation whitelist
collect_fee_mode
u8
Fee collection mode
is_quote_y
bool
Whether token Y is quote
fee_growth_global_a/b
u128
Global fee growth trackers
fee_protocol_coin_a/b
u64
Accumulated protocol fees
Receipt Structs
FlashSwapReceipt
AddLiquidityReceipt
FlashLoanReceipt
CalculatedSwapResult
Core Functions
1. Position Management
Open Position
Creates a new liquidity position within the specified tick range.
Parameters:
tick_lower_idx
Lower tick index (as u32, converted to I32 internally)
tick_upper_idx
Upper tick index
Close Position
Closes an empty position and removes it from the registry.
Lock Position
Locks a position until a specified timestamp, preventing liquidity removal.
2. Liquidity Management
Add Liquidity
Adds liquidity to an existing position by specifying liquidity amount.
Add Liquidity with Fixed Amount
Adds liquidity by fixing one token amount.
Repay Add Liquidity
Completes liquidity addition by providing required tokens.
Remove Liquidity
Removes liquidity from a position and returns token balances.
3. Trading
Flash Swap
Executes a flash swap operation.
Parameters:
a2b
true if swapping from CoinA to CoinB
by_amount_in
true if specifying input amount
amount
Token amount to swap
sqrt_price_limit
Price limit for slippage protection
Repay Flash Swap
Completes flash swap by providing input tokens.
Calculate Swap Result
Simulates a swap without executing, useful for quotes.
4. Flash Loans
Create Flash Loan
Borrows tokens for atomic operations.
Repay Flash Loan
Repays flash loan with fee.
5. Fee and Reward Collection
Collect Position Fees
Collects accumulated trading fees for a position.
Collect Rewards
Collects accumulated rewards for a position.
Collect Protocol Fees
Collects protocol fees (admin only).
6. Fee Configuration
Update Fee Rate
Updates the base fee rate (admin only).
Enable/Disable Fee Scheduler
Enable/Disable Dynamic Fee
7. Whitelist Management
Add Whitelist
Adds addresses to pre-activation whitelist.
Remove Whitelist
Removes addresses from whitelist.
8. Pool Management
Pause/Unpause Pool
Query Functions
Pool Information
Position Information
Calculate and Update Functions
Events
SwapEvent
AddLiquidityEvent
RemoveLiquidityEvent
OpenPositionEvent
LockPositionEvent
FlashLoanEvent
Error Codes
400
E_INVALID_AMOUNT
Invalid token amount
401
E_LIQUIDITY_OVERFLOW
Liquidity calculation overflow
403
E_LIQUIDITY_ZERO
Zero liquidity provided
405
E_INSUFFICIENT_AMOUNT
Insufficient token amount
406
E_SWAP_AMOUNT_IN_OVERFLOW
Swap input amount overflow
407
E_SWAP_AMOUNT_OUT_OVERFLOW
Swap output amount overflow
408
E_SWAP_FEE_AMOUNT_OVERFLOW
Fee amount overflow
410
E_POOL_TICK_OUT_OF_RANGE
Tick outside valid range
412
E_POOL_ID_MISMATCH
Pool ID mismatch
413
E_POOL_PAUSED
Pool is paused
417
E_REWARDER_NOT_FOUND
Rewarder not initialized
418
E_SWAP_ZERO_OUTPUT
Swap resulted in zero output
419
E_POSITION_POOL_MISMATCH
Position doesn't belong to pool
423
E_INVALID_LOCK_TIME
Invalid lock duration
424
E_POSITION_LOCKED
Position is currently locked
430
E_FLASH_LOAN_PAUSED
Flash loans are disabled
431
E_PENDING_ADD_LIQUIDITY
Pending liquidity operation
432
E_NOT_WHITELISTED
Address not whitelisted for pre-activation
433
E_MAX_TOTAL_FEE_EXCEEDED
Total fee exceeds maximum
434
E_NO_PERMISSION
Insufficient permissions
435
E_CLIFF_FEE_BELOW_BASE_FEE
Cliff fee must be >= base fee
Fee Collection Modes
COLLECT_FEE_ON_BOTH (0)
Fees are always collected from the input token.
COLLECT_FEE_ON_QUOTE (1)
Fees are collected from the quote token:
If
is_quote_y = trueand swapping A→B: fee from output (B)If
is_quote_y = trueand swapping B→A: fee from input (B)If
is_quote_y = falseand swapping A→B: fee from input (A)If
is_quote_y = falseand swapping B→A: fee from output (A)
Usage Examples
Complete Swap Flow
Adding Liquidity
Security Considerations
Slippage Protection: Always use
sqrt_price_limitwhen swappingReceipt Validation: Carefully validate receipts before repaying
Permission Checks: Only admins can pause/unpause pools
Lock Period: Positions can be locked to prevent premature withdrawal
Whitelist Check: Pre-activation swaps require whitelist membership
Last updated