pub struct AmmPool {
pub currency_a: String,
pub currency_b: String,
pub reserve_a: u64,
pub reserve_b: u64,
pub total_shares: u64,
pub fee_bps: u64,
/* private fields */
}Expand description
AMM constant-product liquidity pool (x * y = k).
Supports add/remove liquidity and swaps with a configurable fee
(default 30 basis points = 0.3%). All arithmetic uses u128
intermediates to prevent overflow.
Fields§
§currency_a: String§currency_b: String§reserve_a: u64§reserve_b: u64§fee_bps: u64Fee in basis points (100 bps = 1%). Default 30 (0.3%).
Implementations§
Source§impl AmmPool
impl AmmPool
Sourcepub const DEFAULT_FEE_BPS: u64 = 30
pub const DEFAULT_FEE_BPS: u64 = 30
Default fee: 30 bps (0.3%), matching Uniswap V2.
pub fn new(currency_a: &str, currency_b: &str, fee_bps: u64) -> Self
Sourcepub fn add_liquidity(
&mut self,
provider: &str,
amount_a: u64,
amount_b: u64,
ledger: &mut WebLedger,
) -> Result<u64, PaymentError>
pub fn add_liquidity( &mut self, provider: &str, amount_a: u64, amount_b: u64, ledger: &mut WebLedger, ) -> Result<u64, PaymentError>
Add liquidity to the pool.
The first provider sets the initial ratio. Subsequent providers must provide amounts in the same ratio as the current reserves (within integer rounding). Returns the number of LP shares issued.
Sourcepub fn remove_liquidity(
&mut self,
provider: &str,
shares: u64,
ledger: &mut WebLedger,
) -> Result<(u64, u64), PaymentError>
pub fn remove_liquidity( &mut self, provider: &str, shares: u64, ledger: &mut WebLedger, ) -> Result<(u64, u64), PaymentError>
Remove liquidity from the pool.
Withdraws proportional amounts of both currencies based on the
share count. Returns (amount_a, amount_b) credited back.
Sourcepub fn swap(
&mut self,
trader: &str,
from_currency: &str,
amount_in: u64,
ledger: &mut WebLedger,
) -> Result<SwapResult, PaymentError>
pub fn swap( &mut self, trader: &str, from_currency: &str, amount_in: u64, ledger: &mut WebLedger, ) -> Result<SwapResult, PaymentError>
Execute a constant-product swap.
Formula (with fee):
amount_out = (reserve_out * amount_in * (10000 - fee_bps))
/ (reserve_in * 10000 + amount_in * (10000 - fee_bps))All intermediate arithmetic uses u128 to prevent overflow.