nautechsystems/nautilus_trader · error · anyhow::Error
Failed to calculate price impact: {e}
Error message
Failed to calculate price impact: {e} What it means
This public `get_price_impact_bps` wraps the inner `SwapTradeInfo::get_price_impact_bps` call and, when trade-info initialization fails (i.e. `trade_info` is None), re-raises the underlying error with the message `Failed to calculate price impact: {e}`. It is an error-context wrapper: the root cause is the uninitialized trade info reported by `check_if_trade_info_initialized`.
Source
Thrown at crates/model/src/defi/pool_analysis/quote.rs:219
self.amount1.unsigned_abs()
}
}
/// Calculates price impact in basis points (requires token references for decimal adjustment).
///
/// Price impact measures the market movement caused by the swap size,
/// excluding fees. This is the percentage change in spot price from
/// before to after the swap.
///
/// # Returns
/// Price impact in basis points (10000 = 100%)
///
/// # Errors
/// Returns error if price calculations fail
pub fn get_price_impact_bps(&mut self) -> anyhow::Result<u32> {
match self.check_if_trade_info_initialized() {
Ok(trade_info) => trade_info.get_price_impact_bps(),
Err(e) => anyhow::bail!("Failed to calculate price impact: {e}"),
}
}
/// Calculates slippage in basis points (requires token references for decimal adjustment).
///
/// Slippage includes both price impact and fees, representing the total
/// deviation from the spot price before the swap. This measures the total
/// cost to the trader.
///
/// # Returns
/// Total slippage in basis points (10000 = 100%)
///
/// # Errors
/// Returns error if price calculations fail
pub fn get_slippage_bps(&mut self) -> anyhow::Result<u32> {
match self.check_if_trade_info_initialized() {
Ok(trade_info) => trade_info.get_slippage_bps(),
Err(e) => anyhow::bail!("Failed to calculate slippage: {e}"),View on GitHub (pinned to 18893faf8b)
Solutions
- Call `calculate_trade_info()` on the quote before requesting price impact.
- Read the wrapped inner message to confirm the root cause is uninitialized trade info, not a math failure.
- Guard with a helper that checks/initializes trade info once per quote.
- If trade info can legitimately be absent, handle the Result instead of `?`-propagating into caller code.
Example fix
// before let mut quote = Quote::new(pool, amount_in); let impact_bps = quote.get_price_impact_bps()?; // after let mut quote = Quote::new(pool, amount_in); quote.calculate_trade_info()?; let impact_bps = quote.get_price_impact_bps()?;
Defensive patterns
Strategy: try-catch
Validate before calling
quote.calculate_trade_info()?; // avoids the wrapper error entirely
Try / catch
let impact_bps = match quote.get_price_impact_bps() {
Ok(v) => v,
Err(e) if e.to_string().contains("Failed to calculate price impact") => {
quote.calculate_trade_info()?;
quote.get_price_impact_bps()?
},
Err(e) => return Err(e),
}; Prevention
- Read the wrapped inner message to identify the root cause (uninitialized trade info).
- Initialize trade info eagerly at quote construction to make the wrapper a non-issue.
- Wrap quote usage in a small facade that guarantees calculate_trade_info() was called.
- Log full error chains, not just the outer message, when diagnosing.
When it happens
Trigger: Calling `get_price_impact_bps()` on a quote whose `trade_info` was never initialized via `calculate_trade_info()`. Observed in tests as `test_metric_errors_propagate_from_trade_info`.
Common situations: Forgetting the `calculate_trade_info()` step before reading price impact; early-return paths that skip initialization; copying example code that initialized slippage but not price impact (shared init means either order fails identically).
Understand the failure class
Background: "Invalid state transition" errors: "status must be X, actually Y", "already rejected/charging/uninstalled", "cannot ... while running" — what they mean when a library rejects your call — this error's family across 31 libraries.
Related errors
- Trade info is not initialized. Please call calculate_trade_i
- Failed to calculate slippage: {e}
- Cannot quote swap with zero amount
- Slippage {actual_slippage} bps exceeds tolerance {max_slippa
- Insufficient liquidity: requested {amount_out_requested}, av
AI-assisted analysis of nautechsystems/nautilus_trader@18893faf8b (2026-09-08).
Data as JSON: /api/errors/a85ace93f8169ef3.
Report an issue: GitHub.