nautilus_model/defi/data/
swap_trade_info.rs

1// -------------------------------------------------------------------------------------------------
2//  Copyright (C) 2015-2025 Nautech Systems Pty Ltd. All rights reserved.
3//  https://nautechsystems.io
4//
5//  Licensed under the GNU Lesser General Public License Version 3.0 (the "License");
6//  You may not use this file except in compliance with the License.
7//  You may obtain a copy of the License at https://www.gnu.org/licenses/lgpl-3.0.en.html
8//
9//  Unless required by applicable law or agreed to in writing, software
10//  distributed under the License is distributed on an "AS IS" BASIS,
11//  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12//  See the License for the specific language governing permissions and
13//  limitations under the License.
14// -------------------------------------------------------------------------------------------------
15
16use alloy_primitives::{U160, U256};
17use rust_decimal::prelude::ToPrimitive;
18use rust_decimal_macros::dec;
19
20use crate::{
21    defi::{
22        Token,
23        data::swap::RawSwapData,
24        tick_map::{
25            full_math::FullMath, sqrt_price_math::decode_sqrt_price_x96_to_price_tokens_adjusted,
26        },
27    },
28    enums::OrderSide,
29    types::{Price, Quantity, fixed::FIXED_PRECISION},
30};
31
32/// Trade information derived from raw swap data, normalized to market conventions.
33///
34/// This structure represents a Uniswap V3 swap translated into standard trading terminology
35/// (base/quote, buy/sell) for consistency with traditional financial data systems.
36///
37/// # Base/Quote Token Convention
38///
39/// Tokens are assigned base/quote roles based on their priority:
40/// - Higher priority token → base (asset being traded)
41/// - Lower priority token → quote (pricing currency)
42///
43/// This may differ from the pool's token0/token1 ordering. When token priority differs
44/// from pool ordering, we say the market is "inverted":
45/// - NOT inverted: token0=base, token1=quote
46/// - Inverted: token0=quote, token1=base
47///
48/// # Prices
49///
50/// - `spot_price`: Instantaneous pool price after the swap (from sqrt_price_x96)
51/// - `execution_price`: Average realized price for this swap (from amount ratio)
52///
53/// Both prices are in quote/base direction (e.g., USDC per WETH) and adjusted for token decimals.
54#[derive(Debug, Clone, PartialEq, Eq)]
55pub struct SwapTradeInfo {
56    /// The direction of the trade from the base token perspective.
57    pub order_side: OrderSide,
58    /// The absolute quantity of the base token involved in the swap.
59    pub quantity_base: Quantity,
60    /// The absolute quantity of the quote token involved in the swap.
61    pub quantity_quote: Quantity,
62    /// The instantaneous pool price after the swap (quote per base).
63    pub spot_price: Price,
64    /// The average realized execution price for this swap (quote per base).
65    pub execution_price: Price,
66    /// Whether the base/quote assignment differs from token0/token1 ordering.
67    pub is_inverted: bool,
68    /// The pool price before that swap executed(optional).
69    pub spot_price_before: Option<Price>,
70}
71
72impl SwapTradeInfo {
73    /// Sets the spot price before the swap for price impact and slippage calculations.
74    pub fn set_spot_price_before(&mut self, price: Price) {
75        self.spot_price_before = Some(price);
76    }
77
78    /// Calculates price impact in basis points (requires token references for decimal adjustment).
79    ///
80    /// Price impact measures the market movement caused by the swap size,
81    /// excluding fees. This is the percentage change in spot price from
82    /// before to after the swap.
83    ///
84    /// # Returns
85    /// Price impact in basis points (10000 = 100%)
86    ///
87    /// # Errors
88    /// Returns error if price calculations fail
89    pub fn get_price_impact_bps(&self) -> anyhow::Result<u32> {
90        if let Some(spot_price_before) = self.spot_price_before {
91            let price_change = self.spot_price - spot_price_before;
92            let price_impact =
93                (price_change.as_decimal() / spot_price_before.as_decimal()).abs() * dec!(10_000);
94
95            Ok(price_impact.round().to_u32().unwrap_or(0))
96        } else {
97            anyhow::bail!("Cannot calculate price impact, the spot price before is not set");
98        }
99    }
100
101    /// Calculates slippage in basis points (requires token references for decimal adjustment).
102    ///
103    /// Slippage includes both price impact and fees, representing the total
104    /// deviation from the spot price before the swap. This measures the total
105    /// cost to the trader.
106    ///
107    /// # Returns
108    /// Total slippage in basis points (10000 = 100%)
109    ///
110    /// # Errors
111    /// Returns error if price calculations fail
112    pub fn get_slippage_bps(&self) -> anyhow::Result<u32> {
113        if let Some(spot_price_before) = self.spot_price_before {
114            let price_change = self.execution_price - spot_price_before;
115            let slippage =
116                (price_change.as_decimal() / spot_price_before.as_decimal()).abs() * dec!(10_000);
117
118            Ok(slippage.round().to_u32().unwrap_or(0))
119        } else {
120            anyhow::bail!("Cannot calculate slippage, the spot price before is not set")
121        }
122    }
123}
124
125/// Computation engine for deriving market-oriented trade info from raw swap data.
126///
127/// This calculator translates DEX's token0/token1 representation into standard
128/// trading terminology (base/quote, buy/sell) based on token priority.
129///
130/// # Token Priority and Inversion
131///
132/// The calculator determines which token is base vs quote by comparing token priorities.
133/// When the higher-priority token is token1 (not token0), the market is "inverted":
134///
135/// # Precision Handling
136///
137/// For tokens with more than 16 decimals, quantities and prices are automatically
138/// scaled down to MAX_FLOAT_PRECISION (16) to ensure safe f64 conversion while
139/// maintaining reasonable precision for practical trading purposes.
140#[derive(Debug)]
141pub struct SwapTradeInfoCalculator<'a> {
142    /// Reference to token0 from the pool.
143    token0: &'a Token,
144    /// Reference to token1 from the pool.
145    token1: &'a Token,
146    /// Whether the base/quote assignment differs from token0/token1 ordering.
147    ///
148    /// - `true`: token0=quote, token1=base (inverted)
149    /// - `false`: token0=base, token1=quote (normal)
150    pub is_inverted: bool,
151    /// Raw swap amounts and resulting sqrt price from the blockchain event.
152    raw_swap_data: RawSwapData,
153}
154
155impl<'a> SwapTradeInfoCalculator<'a> {
156    pub fn new(token0: &'a Token, token1: &'a Token, raw_swap_data: RawSwapData) -> Self {
157        let is_inverted = token0.get_token_priority() < token1.get_token_priority();
158        Self {
159            token0,
160            token1,
161            raw_swap_data,
162            is_inverted,
163        }
164    }
165
166    /// Determines swap direction from amount signs.
167    ///
168    /// Returns `true` if swapping token0 for token1 (zero_for_one).
169    pub fn zero_for_one(&self) -> bool {
170        self.raw_swap_data.amount0.is_positive()
171    }
172
173    /// Computes all trade information fields and returns a complete [`SwapTradeInfo`].
174    ///
175    /// Calculates order side, quantities, and prices from the raw swap data,
176    /// applying token priority rules and decimal adjustments. If the price before
177    /// the swap is provided, also computes price impact and slippage metrics.
178    ///
179    /// # Arguments
180    ///
181    /// * `sqrt_price_x96_before` - Optional square root price before the swap (Q96 format).
182    ///   When provided, enables calculation of `spot_price_before`, price impact, and slippage.
183    ///
184    /// # Errors
185    ///
186    /// Returns an error if quantity or price calculations fail.
187    pub fn compute(&self, sqrt_price_x96_before: Option<U160>) -> anyhow::Result<SwapTradeInfo> {
188        let spot_price_before = if let Some(sqrt_price_x96_before) = sqrt_price_x96_before {
189            Some(decode_sqrt_price_x96_to_price_tokens_adjusted(
190                sqrt_price_x96_before,
191                self.token0.decimals,
192                self.token1.decimals,
193                self.is_inverted,
194            )?)
195        } else {
196            None
197        };
198
199        Ok(SwapTradeInfo {
200            order_side: self.order_side(),
201            quantity_base: self.quantity_base()?,
202            quantity_quote: self.quantity_quote()?,
203            spot_price: self.spot_price()?,
204            execution_price: self.execution_price()?,
205            is_inverted: self.is_inverted,
206            spot_price_before,
207        })
208    }
209
210    /// Determines the order side from the perspective of the determined base/quote tokens.
211    ///
212    /// Uses market convention where base is the asset being traded and quote is the pricing currency.
213    ///
214    /// # Returns
215    /// - `OrderSide::Buy` when buying base token (selling quote for base)
216    /// - `OrderSide::Sell` when selling base token (buying quote with base)
217    ///
218    /// # Logic
219    ///
220    /// The order side depends on:
221    /// 1. Which token is being bought/sold (from amount signs)
222    /// 2. Which token is base vs quote (from priority determination)
223    pub fn order_side(&self) -> OrderSide {
224        let zero_for_one = self.zero_for_one();
225        if self.is_inverted {
226            // When inverted: token0=quote, token1=base
227            // - zero_for_one (sell token0/quote, buy token1/base) -> BUY base
228            // - one_for_zero (sell token1/base, buy token0/quote -> SELL base
229            if zero_for_one {
230                OrderSide::Buy
231            } else {
232                OrderSide::Sell
233            }
234        } else {
235            // When NOT inverted: token0=base, token1=quote
236            // - zero_for_one (sell token0/base, buy token1/quote) → SELL base
237            // - one_for_zero (sell token1/quote, buy token0/base) → BUY base
238            if zero_for_one {
239                OrderSide::Sell
240            } else {
241                OrderSide::Buy
242            }
243        }
244    }
245
246    /// Returns the quantity of the base token involved in the swap.
247    ///
248    /// This is always the amount of the base asset being traded,
249    /// regardless of whether it's token0 or token1 in the pool.
250    ///
251    /// # Returns
252    /// Absolute value of base token amount with proper decimals
253    ///
254    /// # Errors
255    ///
256    /// Returns an error if the amount cannot be converted to a valid `Quantity`.
257    pub fn quantity_base(&self) -> anyhow::Result<Quantity> {
258        let (amount, precision) = if self.is_inverted {
259            (
260                self.raw_swap_data.amount1.unsigned_abs(),
261                self.token1.decimals,
262            )
263        } else {
264            (
265                self.raw_swap_data.amount0.unsigned_abs(),
266                self.token0.decimals,
267            )
268        };
269
270        Quantity::from_u256(amount, precision)
271    }
272
273    /// Returns the quantity of the quote token involved in the swap.
274    ///
275    /// This is always the amount of the quote (pricing) currency,
276    /// regardless of whether it's token0 or token1 in the pool.
277    ///
278    /// # Returns
279    /// Absolute value of quote token amount with proper decimals
280    ///
281    /// # Errors
282    ///
283    /// Returns an error if the amount cannot be converted to a valid `Quantity`.
284    pub fn quantity_quote(&self) -> anyhow::Result<Quantity> {
285        let (amount, precision) = if self.is_inverted {
286            (
287                self.raw_swap_data.amount0.unsigned_abs(),
288                self.token0.decimals,
289            )
290        } else {
291            (
292                self.raw_swap_data.amount1.unsigned_abs(),
293                self.token1.decimals,
294            )
295        };
296
297        Quantity::from_u256(amount, precision)
298    }
299
300    /// Returns the human-readable spot price in base/quote (market) convention.
301    ///
302    /// This is the instantaneous market price after the swap, adjusted for token decimals
303    /// to provide a human-readable value. This price does NOT include fees or slippage.
304    ///
305    /// # Returns
306    /// Price adjusted for token decimals in quote/base direction (market convention).
307    ///
308    /// # Base/Quote Logic
309    /// - When is_inverted=false: token0=base, token1=quote → returns token1/token0 (quote/base)
310    /// - When is_inverted=true: token0=quote, token1=base → returns token0/token1 (quote/base)
311    ///
312    /// # Use Cases
313    /// - Displaying current market price to users
314    /// - Calculating price impact: `(spot_after - spot_before) / spot_before`
315    /// - Comparing market rate vs execution rate
316    /// - Real-time price feeds
317    fn spot_price(&self) -> anyhow::Result<Price> {
318        // Pool always stores token1/token0
319        // When is_inverted=false: token0=base, token1=quote → want token1/token0 (quote/base) → don't invert
320        // When is_inverted=true: token0=quote, token1=base → want token0/token1 (quote/base) → invert
321        decode_sqrt_price_x96_to_price_tokens_adjusted(
322            self.raw_swap_data.sqrt_price_x96,
323            self.token0.decimals,
324            self.token1.decimals,
325            self.is_inverted, // invert when base/quote differs from token0/token1
326        )
327    }
328
329    /// Calculates the average execution price for this swap (includes fees and slippage).
330    ///
331    /// This is the actual realized price paid/received in the swap, calculated from
332    /// the input and output amounts. This represents the true cost of the trade.
333    ///
334    /// # Returns
335    /// Price in quote/base direction (market convention), adjusted for token decimals.
336    ///
337    /// # Formula
338    /// ```text
339    /// price = (quote_amount / 10^quote_decimals) / (base_amount / 10^base_decimals)
340    ///       = (quote_amount * 10^base_decimals) / (base_amount * 10^quote_decimals)
341    /// ```
342    ///
343    /// To preserve precision in U256 arithmetic, we scale by 10^FIXED_PRECISION:
344    /// ```text
345    /// price_raw = (quote_amount * 10^base_decimals * 10^FIXED_PRECISION) / (base_amount * 10^quote_decimals)
346    /// ```
347    ///
348    /// # Base/Quote Logic
349    /// - When is_inverted=false: quote=token1, base=token0 → price = amount1/amount0
350    /// - When is_inverted=true: quote=token0, base=token1 → price = amount0/amount1
351    ///
352    /// # Use Cases
353    /// - Trade accounting and P&L calculation
354    /// - Comparing quoted vs executed prices
355    /// - Cost analysis (includes all fees and price impact)
356    /// - Performance reporting
357    fn execution_price(&self) -> anyhow::Result<Price> {
358        let amount0 = self.raw_swap_data.amount0.unsigned_abs();
359        let amount1 = self.raw_swap_data.amount1.unsigned_abs();
360
361        if amount0.is_zero() || amount1.is_zero() {
362            anyhow::bail!("Cannot calculate execution price with zero amounts");
363        }
364
365        // Determine base and quote amounts/decimals based on inversion
366        let (quote_amount, base_amount, quote_decimals, base_decimals) = if self.is_inverted {
367            // inverted: token0=quote, token1=base
368            (amount0, amount1, self.token0.decimals, self.token1.decimals)
369        } else {
370            // not inverted: token0=base, token1=quote
371            (amount1, amount0, self.token1.decimals, self.token0.decimals)
372        };
373
374        // Create decimal scalars
375        let base_decimals_scalar = U256::from(10u128.pow(base_decimals as u32));
376        let quote_decimals_scalar = U256::from(10u128.pow(quote_decimals as u32));
377        let fixed_scalar = U256::from(10u128.pow(FIXED_PRECISION as u32));
378
379        // Calculate: (quote_amount * 10^base_decimals * 10^FIXED_PRECISION) / (base_amount * 10^quote_decimals)
380        // Use FullMath::mul_div to handle large intermediate values safely
381
382        // Step 1: numerator = quote_amount * 10^base_decimals
383        let numerator_step1 = FullMath::mul_div(quote_amount, base_decimals_scalar, U256::from(1))?;
384
385        // Step 2: numerator = (quote_amount * 10^base_decimals) * 10^FIXED_PRECISION
386        let numerator_final = FullMath::mul_div(numerator_step1, fixed_scalar, U256::from(1))?;
387
388        // Step 3: denominator = base_amount * 10^quote_decimals
389        let denominator = FullMath::mul_div(base_amount, quote_decimals_scalar, U256::from(1))?;
390
391        // Step 4: Final division
392        let price_raw_u256 = FullMath::mul_div(numerator_final, U256::from(1), denominator)?;
393
394        // Convert to PriceRaw (i128)
395        anyhow::ensure!(
396            price_raw_u256 <= U256::from(i128::MAX as u128),
397            "Price overflow: {price_raw_u256} exceeds i128::MAX"
398        );
399
400        let price_raw = price_raw_u256.to::<i128>();
401
402        // price_raw is at FIXED_PRECISION scale, which is what Price expects
403        Ok(Price::from_raw(price_raw, FIXED_PRECISION))
404    }
405}
406
407#[cfg(test)]
408mod tests {
409    use std::str::FromStr;
410
411    use alloy_primitives::{I256, U160};
412    use rstest::rstest;
413    use rust_decimal_macros::dec;
414
415    use super::*;
416    use crate::defi::stubs::{usdc, weth};
417
418    #[rstest]
419    fn test_swap_trade_info_calculator_calculations_buy(weth: Token, usdc: Token) {
420        // Real Arbitrum transaction: https://arbiscan.io/tx/0xb9af1fd5eefe82650a5e0f8ff10b3a5e1c7f05f44f255e1335360df97bd1645a
421        let raw_data = RawSwapData::new(
422            I256::from_str("-466341596920355889").unwrap(),
423            I256::from_str("1656236893").unwrap(),
424            U160::from_str("4720799958938693700000000").unwrap(),
425        );
426
427        let calculator = SwapTradeInfoCalculator::new(&weth, &usdc, raw_data);
428        let result = calculator.compute(None).unwrap();
429        // Its not inverted first is WETH(base) and second USDC(quote) as stablecoin
430        assert!(!calculator.is_inverted);
431        // Its buy, as amount0(WETH) < 0 (we received WETH, pool outflow) and amount1 > 0 (USDC sent, pool inflow)
432        assert_eq!(result.order_side, OrderSide::Buy);
433        assert_eq!(
434            result.quantity_base.as_decimal(),
435            dec!(0.466341596920355889)
436        );
437        assert_eq!(result.quantity_quote.as_decimal(), dec!(1656.236893));
438        assert_eq!(result.spot_price.as_decimal(), dec!(3550.3570265047994091));
439        assert_eq!(
440            result.execution_price.as_decimal(),
441            dec!(3551.5529902061477063)
442        );
443    }
444
445    #[rstest]
446    fn test_swap_trade_info_calculator_calculations_sell(weth: Token, usdc: Token) {
447        //Real Arbitrum transaction: https://arbiscan.io/tx/0x1fbedacf4a1cc7f76174d905c93d2f56d42335cadb4a782e2d74e3019107286b
448        let raw_data = RawSwapData::new(
449            I256::from_str("193450074461093702").unwrap(),
450            I256::from_str("-691892530").unwrap(),
451            U160::from_str("4739235524363817533004858").unwrap(),
452        );
453
454        let calculator = SwapTradeInfoCalculator::new(&weth, &usdc, raw_data);
455        let result = calculator.compute(None).unwrap();
456        // Its sell as amount0(WETH) > 0 (we send WETH, pool inflow) and amount1 <0 (USDC received, pool outflow)
457        assert_eq!(result.order_side, OrderSide::Sell);
458        assert_eq!(
459            result.quantity_base.as_decimal(),
460            dec!(0.193450074461093702)
461        );
462        assert_eq!(result.quantity_quote.as_decimal(), dec!(691.89253));
463        assert_eq!(result.spot_price.as_decimal(), dec!(3578.1407251651610105));
464        assert_eq!(
465            result.execution_price.as_decimal(),
466            dec!(3576.5947980503469024)
467        );
468    }
469}