Crates.io | consta-pool |
lib.rs | consta-pool |
version | 0.1.0 |
source | src |
created_at | 2024-09-09 05:03:51.069562 |
updated_at | 2024-09-09 05:03:51.069562 |
description | A library implementing a constant product liquidity pool (a * b = k). |
homepage | |
repository | https://github.com/jbcaron/consta-pool |
max_upload_size | |
id | 1368799 |
size | 20,477 |
ConstaPool is a Rust library implementing a constant product liquidity pool model, based on the formula a * b = k
. This model is widely used in decentralized finance (DeFi) for automated market making, allowing for efficient and decentralized token swaps.
thiserror
crate, covering common pool errors such as insufficient funds, overflow, and invalid amounts.Here’s a basic example of how to use ConstaPool:
use consta_pool::{LiquidityPool, PoolError};
fn main() -> Result<(), PoolError> {
// Create a new liquidity pool with specified reserves
let mut pool = LiquidityPool::new(30 * 10u64.pow(9), 1_000_000_000 * 10u64.pow(6))?;
// Buy tokens from the pool
let token_amount = 1_000_000 * 10u64.pow(6); // Buying 1,000,000 tokens
let native_spent = pool.buy_tokens(token_amount, None)?;
println!("Spent {} native to buy {} tokens", native_spent, token_amount);
// Sell tokens back to the pool
let native_received = pool.sell_tokens(token_amount, None)?;
println!("Sold {} tokens to receive {} native", token_amount, native_received);
Ok(())
}
This project is licensed under the terms of the MIT License.