Skip to main content

Crate hydra_amm

Crate hydra_amm 

Source
Expand description

§Hydra AMM

Universal AMM engine: build, configure, and operate any Automated Market Maker through a unified interface.

This crate provides domain types, core traits, configuration structures, and feature-gated pool implementations for six AMM families:

  • Constant Product (Uniswap v2 style) — constant-product feature
  • Concentrated Liquidity (Uniswap v3 style) — clmm feature
  • Hybrid / StableSwap (Curve style) — hybrid feature
  • Weighted Pools (Balancer style) — weighted feature
  • Dynamic / Proactive MM (DODO style) — dynamic feature
  • Order Book Hybrid (Phoenix style) — order-book feature

§Feature Flags

FeatureDefaultDescription
stdyesStandard library support
fixed-pointnoI80F48 fixed-point arithmetic
floatnof64 floating-point (implies std)
all-poolsyesEnables all six pool types

§Quick Start

Add to your Cargo.toml:

[dependencies]
hydra-amm = "0.1"

To use only specific pool types:

[dependencies]
hydra-amm = { version = "0.1", default-features = false, features = ["std", "constant-product"] }

§Create a pool and execute a swap

use hydra_amm::config::{AmmConfig, ConstantProductConfig};
use hydra_amm::domain::{
    Amount, BasisPoints, Decimals, FeeTier, SwapSpec,
    Token, TokenAddress, TokenPair,
};
use hydra_amm::factory::DefaultPoolFactory;
use hydra_amm::traits::SwapPool;

// 1. Define two tokens
let usdc = Token::new(
    TokenAddress::from_bytes([1u8; 32]),
    Decimals::new(6).expect("valid decimals"),
);
let weth = Token::new(
    TokenAddress::from_bytes([2u8; 32]),
    Decimals::new(18).expect("valid decimals"),
);

// 2. Build a Constant Product pool configuration
let pair = TokenPair::new(usdc, weth).expect("distinct tokens");
let fee  = FeeTier::new(BasisPoints::new(30)); // 0.30%
let config = AmmConfig::ConstantProduct(
    ConstantProductConfig::new(pair, fee, Amount::new(1_000_000), Amount::new(1_000_000))
        .expect("valid config"),
);

// 3. Create the pool via the factory
let mut pool = DefaultPoolFactory::create(&config).expect("pool created");

// 4. Execute a swap (sell 10 000 units of token A for token B)
let spec = SwapSpec::exact_in(Amount::new(10_000)).expect("non-zero");
let result = pool.swap(spec, usdc).expect("swap succeeded");

assert!(result.amount_out().get() > 0);
assert!(result.fee().get() > 0);

§Architecture

┌─────────────┐
│   Consumer   │  uses AmmConfig + DefaultPoolFactory
└──────┬──────┘
       │ create(&config)
       ▼
┌─────────────┐
│   Factory    │  validates config, dispatches to FromConfig
└──────┬──────┘
       │ PoolBox (enum dispatch)
       ▼
┌─────────────┐
│    Pools     │  ConstantProduct, CLMM, Hybrid, Weighted, Dynamic, OrderBook
└──────┬──────┘
       │ SwapPool + LiquidityPool traits
       ▼
┌─────────────┐
│   Domain     │  Amount, Price, Tick, Token, SwapResult, …
└─────────────┘

§Module Guide

ModulePurpose
domainNewtype value types: Amount, Price, Token, etc.
traitsCore abstractions: SwapPool, LiquidityPool, FromConfig
configDeclarative pool blueprints: AmmConfig and per-pool config structs
poolsFeature-gated pool implementations and PoolBox dispatch enum
factoryDefaultPoolFactory for config-driven pool construction
mathChecked arithmetic, precision backends, tick math
errorAmmError unified error enum
preludeConvenience re-exports for common types and traits

Modules§

config
Pool configuration enums and structs.
domain
Fundamental domain value types used throughout the AMM library.
error
Unified error types for the Hydra AMM library.
factory
Pool instantiation via the factory pattern.
math
Arithmetic and precision utilities for AMM calculations.
pools
Feature-gated pool implementations and the PoolBox dispatch enum.
prelude
Convenience re-exports for common types and traits.
traits
Core trait abstractions for AMM pool operations.