Documentation Index
Fetch the complete documentation index at: https://sapi-7869507e.mintlify.app/llms.txt
Use this file to discover all available pages before exploring further.
Overview
This guide provides everything you need to integrate SAPI’s Dynamic Liquidity Market Maker (DLMM) into your protocol on Aptos. DLMM offers advanced liquidity management with bin-based architecture and dynamic fees.Quick Start Example
See a complete integration example with pool creation, liquidity provision,
and swapping.
Core Functions
Initialization
initialize(user: &signer, max_protocol_fee: u128)
Initializes the protocol. Must be called once by the deployer/admin.
Parameters:
user: Signer accountmax_protocol_fee: Maximum allowed protocol fee (u128)
Pool Management
register_pool(...) -> Object
Creates a new pool between two tokens with specific configuration.
Parameters:
token_x,token_y: Object references for the token pairactive_bin_id: u32 - Starting active bin identifierbin_step: u32 - Price difference between consecutive binsbase_factor: u128 - Base fee multiplierprotocol_share: u128 - Protocol fee percentagefilter_period: u64 - Time filter for volatility calculationsdecay_period: u64 - Volatility decay time periodreduction_factor: u128 - Fee reduction factorvariable_fee_control: u128 - Dynamic fee control parametermax_volatility_accum: u128 - Maximum volatility accumulator value
Liquidity Operations
add_liquidity(...) -> (Object, FungibleAsset, FungibleAsset)
Adds liquidity across selected bins and returns an LP NFT and refunds.
Features:
- Handles slippage and bin adjustments automatically
- Returns LP position NFT for tracking
- Provides refunds for unused tokens
- LP position NFT object
- Refunded token X
- Refunded token Y
update_liquidity(...) -> (FungibleAsset, FungibleAsset)
Updates an existing position by adding more funds or adjusting bin allocations.
Returns:
- Refunded token X
- Refunded token Y
withdraw_liquidity(...) -> (FungibleAsset, FungibleAsset)
Withdraws a portion of liquidity from a position based on percentages.
Returns:
- Withdrawn token X
- Withdrawn token Y
remove_liquidity(...) -> (FungibleAsset, FungibleAsset)
Closes the position completely and withdraws all liquidity.
Returns:
- All remaining token X
- All remaining token Y
Fee Management
claim_fees(...) -> (FungibleAsset, FungibleAsset)
Claims accumulated fees from a single LP position.
Returns:
- Claimed fees in token X
- Claimed fees in token Y
claim_fees_multiple(...)
Batch version for claiming fees from multiple LP NFT positions simultaneously.
Trading Functions
swap_exact_x_for_y(...) -> FungibleAsset
Performs a swap from token X to token Y with exact input amount.
Features:
- Asserts minimum output amount
- Handles multi-bin swaps automatically
- Applies dynamic fees
swap_exact_y_for_x(...) -> FungibleAsset
Performs a swap from token Y to token X with exact input amount.
Returns: Output token X amount
Error Codes
Understanding common error codes for debugging:| Code | Error | Description |
|---|---|---|
| 0 | EEXCEED_MAX_BIN_ID | Bin ID exceeds maximum allowed value |
| 1 | EACTIVE_BIN_OUT_OF_SLIPPAGE | Active bin moved outside slippage tolerance |
| 2 | ELIQ_BELOW_DESIRED | Liquidity amount below desired minimum |
| 3 | EBIN_ID_TOO_SMALL_FOR_DIFF | Bin ID too small for the requested difference |
| 4 | ENOT_POSITION_OWNER | Caller is not the owner of the LP position |
| 5 | EINSUFFICIENT_OUTPUT_AMOUNT | Swap output below minimum required |
| 6 | EINVALID_LENGTH | Invalid array length in batch operations |
Example Flow
Here’s a complete example of integrating DLMM into your protocol:Step 1: Register a Pool
Step 2: Add Liquidity
Step 3: Perform Swaps
Step 4: Claim Fees
Integration Best Practices
Pool Configuration
Choosing Bin Steps
- Stable pairs (USDC/USDT): Use smaller bin steps (1-10 basis points)
- Volatile pairs (APT/ETH): Use larger bin steps (25-100 basis points)
- Meme tokens: Consider larger bin steps (50-200 basis points)
Fee Parameters
- Base factor: Start with 5000-10000 for most pairs
- Protocol share: Typically 5-20% of total fees
- Variable fee control: Higher values (40000+) for more dynamic fees
Liquidity Management
Monitoring Positions
- Track active bin movement relative to your positions
- Monitor impermanent loss vs fee earnings
- Consider rebalancing when price moves significantly
Gas Optimization
- Use batch operations (
claim_fees_multiple) when possible - Combine liquidity updates with fee claims
- Plan bin selections to minimize cross-bin complexity
Error Handling
Router Integration
SAPI provides router integration for seamless multi-hop swaps and advanced routing:Testing Your Integration
Local Testing
- Deploy DLMM contracts to local testnet
- Create test pools with various configurations
- Test all liquidity and swap operations
- Verify fee calculations and distributions
Mainnet Preparation
- Audit all integration code
- Test with small amounts first
- Monitor gas costs and optimize
- Implement proper error handling and recovery
Support
Contact Integration Team
Get direct support for your integration project.
Next Steps
After completing your integration:- Deploy to testnet for thorough testing
- Security audit checklist before mainnet deployment
- Monitor and optimize your integrated pools
- Join developer community for ongoing support