Liquidity Pool Module
Module Infoβ
- Package Name:
AtmosSwap
- Address:
atmos_swap=0xa4a4a31116e114bf3c4f4728914e6b43db73279a4421b0768993e07248fe2234
- Module:
atmos_swap::liquidity_pool
- Description: Core module for managing liquidity pools in the Atmos Swap Hyper Amm. Supports both Weighted and Stable pool types with various pool operations including swaps, liquidity provision, and fee management. This module handles the core pool logic and mathematical calculations for AMM operations.
Constantsβ
Constant | Value | Description |
---|---|---|
MAX_STABLE_TOKENS | 4 | Maximum allowed tokens in a stable pool |
MAX_WEIGHTED_TOKENS | 4 | Maximum allowed tokens in a weighted pool |
POOL_TYPE_STABLE | 100 | Stable pool type identifier |
POOL_TYPE_WEIGHTED | 101 | Weighted pool type identifier |
BPS_BASE | 10000 | Base points denominator (100%) |
MIN_AMP_FACTOR | 0 | Minimum AMP factor for stable pools |
MAX_AMP_FACTOR | 10000 | Maximum AMP factor for stable pools |
MAX_SWAP_FEE | 1000 | Maximum swap fee in basis points |
Public Functionsβ
Pool Creationβ
Create Stable Poolβ
public fun create_pool_stable(
initial_assets: vector<FungibleAsset>,
swap_fee_bps: u64,
amp_factor: u64
) : (Object<Pool>, FungibleAsset)
Creates a new stable pool with specified parameters and initial liquidity.
Function Arguments
Argument | Type | Description |
---|---|---|
initial_assets | vector<FungibleAsset> | Initial assets to deposit |
swap_fee_bps | u64 | Swap fee in basis points |
amp_factor | u64 | Amplification coefficient |
Returns
Type | Description |
---|---|
Object<Pool> | Pool object |
FungibleAsset | LP tokens |
Aborts
- If swap fee is invalid
- If assets validation fails
- If pool already exists
- If amp factor is out of valid range
- If any initial amount is zero
Create Weighted Poolβ
public fun create_pool_weighted(
initial_assets: vector<FungibleAsset>,
pool_weights: vector<u64>,
swap_fee_bps: u64
) : (Object<Pool>, FungibleAsset)
Creates a new weighted pool with specified parameters and initial liquidity.
Function Arguments
Argument | Type | Description |
---|---|---|
initial_assets | vector<FungibleAsset> | Initial assets to deposit |
pool_weights | vector<u64> | Token weights (must sum to 100) |
swap_fee_bps | u64 | Swap fee in basis points |
Returns
Type | Description |
---|---|
Object<Pool> | Pool object |
FungibleAsset | LP tokens |
Aborts
- If swap fee is invalid
- If assets validation fails
- If pool already exists
- If any weight is zero
- If weights donβt sum to 100
- If any initial amount is zero
Liquidity Managementβ
Add Liquidity (Stable)β
public fun add_liquidity_stable(
pool: Object<Pool>,
assets_to_add: vector<FungibleAsset>
) : FungibleAsset
Adds liquidity to a stable pool.
Function Arguments
Argument | Type | Description |
---|---|---|
pool | Object<Pool> | Pool to add liquidity to |
assets_to_add | vector<FungibleAsset> | Assets to add as liquidity |
Returns
Type | Description |
---|---|
FungibleAsset | Minted LP tokens |
Aborts
- If number of assets doesnβt match pool metadata
- If validation checks fail
Add Liquidity (Weighted)β
public fun add_liquidity_weighted(
pool: Object<Pool>,
assets_to_add: vector<FungibleAsset>
) : (FungibleAsset, vector<FungibleAsset>)
Adds liquidity to a weighted pool with automatic refund handling.
Function Arguments
Argument | Type | Description |
---|---|---|
pool | Object<Pool> | Pool to add liquidity to |
assets_to_add | vector<FungibleAsset> | Assets to add as liquidity |
Returns
Type | Description |
---|---|
FungibleAsset | Minted LP tokens |
vector<FungibleAsset> | Refunded assets |
Aborts
- If number of assets doesnβt match pool metadata
- If validation checks fail
Remove Liquidityβ
public fun remove_liquidity(
pool: Object<Pool>,
lp_tokens: FungibleAsset
) : vector<FungibleAsset>
Removes liquidity from a pool by burning LP tokens.
Function Arguments
Argument | Type | Description |
---|---|---|
pool | Object<Pool> | Pool object |
lp_tokens | FungibleAsset | LP tokens to burn |
Returns
Type | Description |
---|---|
vector<FungibleAsset> | Withdrawn assets |
Aborts
- If validation fails
- If vector lengths donβt match
Swap Functionsβ
Exact Input Swapsβ
Stable Pool Swap (Exact In)β
public fun swap_exact_in_stable(
trader: &signer,
pool: Object<Pool>,
tokens_in: FungibleAsset,
token_out: Object<Metadata>
) : FungibleAsset
Performs exact input swap in a stable pool.
Function Arguments
Argument | Type | Description |
---|---|---|
trader | &signer | Trader account |
pool | Object<Pool> | Pool to swap in |
tokens_in | FungibleAsset | Input tokens |
token_out | Object<Metadata> | Output token metadata |
Returns
Type | Description |
---|---|
FungibleAsset | Output tokens |
Aborts
- If input amount is zero
- If simulate validation fails
Weighted Pool Swap (Exact In)β
public fun swap_exact_in_weighted(
trader: &signer,
pool: Object<Pool>,
input_tokens: FungibleAsset,
token_out: Object<Metadata>
) : FungibleAsset
Performs exact input swap in a weighted pool.
Function Arguments
Argument | Type | Description |
---|---|---|
trader | &signer | Trader account |
pool | Object<Pool> | Pool to swap in |
input_tokens | FungibleAsset | Input tokens |
token_out | Object<Metadata> | Output token metadata |
Returns
Type | Description |
---|---|
FungibleAsset | Output tokens |
Aborts
- If input amount is zero
- If simulate validation fails
Exact Output Swapsβ
Stable Pool Swap (Exact Out)β
public fun swap_exact_out_stable(
trader: &signer,
pool: Object<Pool>,
input_tokens: FungibleAsset,
token_out: Object<Metadata>,
exact_amount_out: u64
) : (FungibleAsset, FungibleAsset)
Performs exact output swap in a stable pool.
Function Arguments
Argument | Type | Description |
---|---|---|
trader | &signer | Trader account |
pool | Object<Pool> | Pool to swap in |
input_tokens | FungibleAsset | Input tokens |
token_out | Object<Metadata> | Output token metadata |
exact_amount_out | u64 | Exact amount of output tokens |
Returns
Type | Description |
---|---|
FungibleAsset | Refund tokens |
FungibleAsset | Output tokens |
Aborts
- If exact_amount_out is zero
- If input amount is insufficient
Weighted Pool Swap (Exact Out)β
public fun swap_exact_out_weighted(
trader: &signer,
pool: Object<Pool>,
input_tokens: FungibleAsset,
token_out: Object<Metadata>,
exact_amount_out: u64
) : (FungibleAsset, FungibleAsset)
Performs exact output swap in a weighted pool.
Function Arguments
Argument | Type | Description |
---|---|---|
trader | &signer | Trader account |
pool | Object<Pool> | Pool to swap in |
input_tokens | FungibleAsset | Input tokens |
token_out | Object<Metadata> | Output token metadata |
exact_amount_out | u64 | Exact amount of output tokens |
Returns
Type | Description |
---|---|
FungibleAsset | Refund tokens |
FungibleAsset | Output tokens |
Aborts
- If exact_amount_out is zero
- If input amount is insufficient
View Functionsβ
Pool Informationβ
Pool Assets Metadataβ
#[view]
public fun pool_assets_metadata(pool: Object<Pool>) : vector<Object<Metadata>>
Returns metadata of pool assets.
Pool Balancesβ
#[view]
public fun pool_balances(pool: Object<Pool>) : vector<u64>
Returns current balances of pool assets.
Pool Sizeβ
#[view]
public fun pool_size(pool: Object<Pool>) : u64
Returns number of assets in a pool.
Pool Typeβ
#[view]
public fun pool_type(pool: Object<Pool>) : u8
Returns pool type (100 for stable, 101 for weighted).
Pool Locked Statusβ
#[view]
public fun pool_locked(pool: Object<Pool>) : bool
Returns if pool is locked.
Pool Swap Feeβ
#[view]
public fun pool_swap_fee_bps(pool: Object<Pool>) : u64
Returns swap fee in basis points.
Stable Pool Specificβ
Pool Amplification Factorβ
#[view]
public fun pool_amp_factor(pool: Object<Pool>) : u64
Returns amplification factor of a stable pool.
Pool Precision Multipliersβ
#[view]
public fun pool_precision_multipliers(pool: Object<Pool>) : vector<u64>
Returns precision multipliers for stable pool tokens.
Pool Invariantβ
public fun pool_invariant(pool: Object<Pool>) : u256
Returns current pool invariant for a stable pool.
Weighted Pool Specificβ
Pool Weightsβ
#[view]
public fun pool_weights(pool: Object<Pool>) : vector<u64>
Returns weights for weighted pool tokens.
LP Token Informationβ
LP Token Metadataβ
#[view]
public fun pool_lp_token_metadata(pool: Object<Pool>) : Object<Metadata>
Returns LP token metadata of a pool.
LP Token Supplyβ
#[view]
public fun pool_lp_token_supply(pool: Object<Pool>) : u64
Returns LP token supply of a pool.
Pool Existence Checksβ
Stable Pool Existsβ
#[view]
public fun stable_pool_exists(
token_metadata: vector<Object<Metadata>>,
swap_fee_bps: u64
) : bool
Checks if a stable pool exists with given parameters.
Weighted Pool Existsβ
#[view]
public fun weighted_pool_exists(
token_metadata: vector<Object<Metadata>>,
weights: vector<u64>,
swap_fee_bps: u64
) : bool
Checks if a weighted pool exists with given parameters.
Simulation Functionsβ
Response Structsβ
SwapSimulateβ
struct SwapSimulate has drop {
amount_in: u64,
amount_in_post_fee: u64,
amount_out: u64,
amount_normalized_in: u128,
amount_normalized_out: u128,
total_fee_amount: u64,
protocol_fee_amount: u64,
idx_in: u64,
idx_out: u64,
swap_fee_bps: u64,
}
Simulate data for swap operations including fees and normalized amounts.
AddLiquiditySimulateβ
struct AddLiquiditySimulate has drop {
minted_lp_token_amount: u64,
refund_amounts: vector<u64>,
}
Simulate data for liquidity addition including LP tokens to mint and refund amounts.
RemoveLiquiditySimulateβ
struct RemoveLiquiditySimulate has drop {
withdrawn_amounts: vector<u64>,
}
Simulate data for liquidity removal showing withdrawn token amounts.
Simulate Add Liquidity (Stable)β
#[view]
public fun simulate_add_liquidity_stable_pool(
pool: Object<Pool>,
token_metadata_addr: vector<address>,
deposit_amounts: vector<u64>
) : AddLiquiditySimulate
Simulates adding liquidity to a stable pool.
Simulate Add Liquidity (Weighted)β
#[view]
public fun simulate_add_liquidity_weighted_pool(
pool: Object<Pool>,
token_metadata_addr: vector<address>,
deposit_amounts: vector<u64>
) : AddLiquiditySimulate
Simulates adding liquidity to a weighted pool.