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: boolWhether 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>
impl<'a> SwapTradeInfoCalculator<'a>
pub fn new( token0: &'a Token, token1: &'a Token, raw_swap_data: RawSwapData, ) -> Self
Sourcepub fn zero_for_one(&self) -> bool
pub fn zero_for_one(&self) -> bool
Determines swap direction from amount signs.
Returns true if swapping token0 for token1 (zero_for_one).
Sourcepub fn compute(
&self,
sqrt_price_x96_before: Option<U160>,
) -> Result<SwapTradeInfo>
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 ofspot_price_before, price impact, and slippage.
§Errors
Returns an error if quantity or price calculations fail.
Sourcepub fn order_side(&self) -> OrderSide
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::Buywhen buying base token (selling quote for base)OrderSide::Sellwhen selling base token (buying quote with base)
§Logic
The order side depends on:
- Which token is being bought/sold (from amount signs)
- Which token is base vs quote (from priority determination)
Sourcepub fn quantity_base(&self) -> Result<Quantity>
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.
Sourcepub fn quantity_quote(&self) -> Result<Quantity>
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§
Auto Trait Implementations§
impl<'a> Freeze for SwapTradeInfoCalculator<'a>
impl<'a> RefUnwindSafe for SwapTradeInfoCalculator<'a>
impl<'a> Send for SwapTradeInfoCalculator<'a>
impl<'a> Sync for SwapTradeInfoCalculator<'a>
impl<'a> Unpin for SwapTradeInfoCalculator<'a>
impl<'a> UnwindSafe for SwapTradeInfoCalculator<'a>
Blanket Implementations§
§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
§impl<T> Instrument for T
impl<T> Instrument for T
§fn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
§fn in_current_span(self) -> Instrumented<Self>
fn in_current_span(self) -> Instrumented<Self>
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
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 moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
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