SwapTradeInfoCalculator

Struct SwapTradeInfoCalculator 

Source
pub struct SwapTradeInfoCalculator<'a> {
    pub is_inverted: bool,
    /* private fields */
}
Expand description

Computation engine for deriving market-oriented trade info from raw swap data.

This calculator translates DEX’s token0/token1 representation into standard trading terminology (base/quote, buy/sell) based on token priority.

§Token Priority and Inversion

The calculator determines which token is base vs quote by comparing token priorities. When the higher-priority token is token1 (not token0), the market is “inverted”:

§Precision Handling

For tokens with more than 16 decimals, quantities and prices are automatically scaled down to MAX_FLOAT_PRECISION (16) to ensure safe f64 conversion while maintaining reasonable precision for practical trading purposes.

Fields§

§is_inverted: bool

Whether the base/quote assignment differs from token0/token1 ordering.

  • true: token0=quote, token1=base (inverted)
  • false: token0=base, token1=quote (normal)

Implementations§

Source§

impl<'a> SwapTradeInfoCalculator<'a>

Source

pub fn new( token0: &'a Token, token1: &'a Token, raw_swap_data: RawSwapData, ) -> Self

Source

pub fn zero_for_one(&self) -> bool

Determines swap direction from amount signs.

Returns true if swapping token0 for token1 (zero_for_one).

Source

pub fn compute( &self, sqrt_price_x96_before: Option<U160>, ) -> Result<SwapTradeInfo>

Computes all trade information fields and returns a complete SwapTradeInfo.

Calculates order side, quantities, and prices from the raw swap data, applying token priority rules and decimal adjustments. If the price before the swap is provided, also computes price impact and slippage metrics.

§Arguments
  • sqrt_price_x96_before - Optional square root price before the swap (Q96 format). When provided, enables calculation of spot_price_before, price impact, and slippage.
§Errors

Returns an error if quantity or price calculations fail.

Source

pub fn order_side(&self) -> OrderSide

Determines the order side from the perspective of the determined base/quote tokens.

Uses market convention where base is the asset being traded and quote is the pricing currency.

§Returns
  • OrderSide::Buy when buying base token (selling quote for base)
  • OrderSide::Sell when selling base token (buying quote with base)
§Logic

The order side depends on:

  1. Which token is being bought/sold (from amount signs)
  2. Which token is base vs quote (from priority determination)
Source

pub fn quantity_base(&self) -> Result<Quantity>

Returns the quantity of the base token involved in the swap.

This is always the amount of the base asset being traded, regardless of whether it’s token0 or token1 in the pool.

§Returns

Absolute value of base token amount with proper decimals

§Errors

Returns an error if the amount cannot be converted to a valid Quantity.

Source

pub fn quantity_quote(&self) -> Result<Quantity>

Returns the quantity of the quote token involved in the swap.

This is always the amount of the quote (pricing) currency, regardless of whether it’s token0 or token1 in the pool.

§Returns

Absolute value of quote token amount with proper decimals

§Errors

Returns an error if the amount cannot be converted to a valid Quantity.

Trait Implementations§

Source§

impl<'a> Debug for SwapTradeInfoCalculator<'a>

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more

Auto Trait Implementations§

Blanket Implementations§

§

impl<T> Any for T
where T: 'static + ?Sized,

§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
§

impl<T> Borrow<T> for T
where T: ?Sized,

§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
§

impl<T> BorrowMut<T> for T
where T: ?Sized,

§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
§

impl<T> From<T> for T

§

fn from(t: T) -> T

Returns the argument unchanged.

§

impl<T> Instrument for T

§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided [Span], returning an Instrumented wrapper. Read more
§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
§

impl<T, U> Into<U> for T
where U: From<T>,

§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> IntoEither for T

Source§

fn into_either(self, into_left: bool) -> Either<Self, Self>

Converts self into a Left variant of Either<Self, Self> if into_left is true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
where F: FnOnce(&Self) -> bool,

Converts self into a Left variant of Either<Self, Self> if into_left(&self) returns true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

§

type Error = Infallible

The type returned in the event of a conversion error.
§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

§

fn vzip(self) -> V

§

impl<T> WithSubscriber for T

§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a [WithDispatch] wrapper. Read more
§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a [WithDispatch] wrapper. Read more
§

impl<T> Ungil for T
where T: Send,