Skip to main content

pump_rust_client/math/
mod.rs

1//! Bonding-curve and AMM quote math (`u128` intermediates, `u64` at the boundary).
2
3pub mod amm;
4pub mod bonding_curve;
5pub mod fees;
6pub mod utils;
7
8pub use bonding_curve::TOKEN_SUPPLY;
9
10/// Quote failure modes (empty reserves, bad inputs, fee overflow, etc.).
11#[derive(Debug, Clone, Copy, PartialEq, Eq)]
12pub enum QuoteError {
13    /// `base_reserve` or `quote_reserve` is zero, so the pool cannot price
14    /// any trade.
15    EmptyReserves,
16    /// Caller asked for `base_out` >= `base_reserve` on a buy. Equivalent
17    /// to draining the pool; the constant-product denominator would be
18    /// `<= 0`.
19    BaseOutExceedsReserve,
20    /// Sum of LP, protocol, and coin-creator fees exceeds the raw quote
21    /// output — only reachable with a degenerate fee table whose total bps
22    /// > 10_000.
23    FeesExceedOutput,
24    /// Bonding curve degeneracy where `real_token_reserves ==
25    /// virtual_token_reserves`, which would zero the constant-product
26    /// denominator on a token-out buy.
27    DepletedBondingCurve,
28    /// A `u128` intermediate overflowed, or a subtraction would underflow
29    /// (e.g. `sol_out >= virtual_sol_reserves` on the `sell_token_quote_with_sol`
30    /// inverse). Surfaced from the fee-less primitive quote helpers so callers
31    /// don't need to wrap primitive arithmetic.
32    MathOverflow,
33    /// Observed market cap fell outside the caller's `target ± slippage_bps`
34    /// envelope. Returned by `validate_market_cap` on both quote paths.
35    SlippageExceeded,
36}
37
38impl std::fmt::Display for QuoteError {
39    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
40        match self {
41            Self::EmptyReserves => write!(f, "pool reserves are zero"),
42            Self::BaseOutExceedsReserve => {
43                write!(f, "base_out exceeds the pool's base reserve")
44            }
45            Self::FeesExceedOutput => write!(f, "fees exceed the pool's quote output"),
46            Self::DepletedBondingCurve => {
47                write!(f, "bonding curve is depleted (real == virtual reserves)")
48            }
49            Self::MathOverflow => write!(f, "checked arithmetic overflowed"),
50            Self::SlippageExceeded => {
51                write!(f, "market cap fell outside the slippage envelope")
52            }
53        }
54    }
55}
56
57impl std::error::Error for QuoteError {}
58
59pub type QuoteResult<T> = std::result::Result<T, QuoteError>;