Skip to main content

SwapConstraint

Enum SwapConstraint 

Source
pub enum SwapConstraint {
#[non_exhaustive]
TradeLimitPrice { limit: Price, tolerance: f64, min_amount_in: Option<BigUint>, max_amount_in: Option<BigUint>, },
#[non_exhaustive]
PoolTargetPrice { target: Price, tolerance: f64, min_amount_in: Option<BigUint>, max_amount_in: Option<BigUint>, }, }
Expand description

Defines constraints for advanced quote calculations.

These constraints allow sophisticated trading strategies by limiting swaps based on price thresholds or targeting specific pool states.

Variants§

§

#[non_exhaustive]
TradeLimitPrice

This mode will calculate the maximum trade that this pool can execute while respecting a trade limit price.

Fields

This variant is marked as non-exhaustive
Non-exhaustive enum variants could have additional fields added in future. Therefore, non-exhaustive enum variants cannot be constructed in external crates and cannot be matched against.
§limit: Price

The minimum acceptable price for the resulting trade, as a Price struct. The resulting amount_out / amount_in must be >= trade_limit_price

§tolerance: f64

The tolerance as a fraction to be applied on top of (increasing) the trade limit price, raising the acceptance threshold. This is used to loosen the acceptance criteria for implementations of this method, but will never allow violating the trade limit price itself.

§min_amount_in: Option<BigUint>

The minimum amount of token_in that must be used for this trade.

§max_amount_in: Option<BigUint>

The maximum amount of token_in that can be used for this trade.

§

#[non_exhaustive]
PoolTargetPrice

This mode will calculate the amount of token_in required to move the pool’s marginal price down to a target price, and the amount of token_out received.

§Edge Cases and Limitations

Computing the exact amount to move a pool’s marginal price to a target has several challenges:

  • The definition of marginal price varies between protocols. It is usually not an attribute of the pool but a consequence of its liquidity distribution and current state.
  • For protocols with concentrated liquidity, the marginal price is discrete, meaning we can’t always find an exact trade amount to reach the target price.
  • Not all protocols support analytical solutions for this problem, requiring numerical methods.

Fields

This variant is marked as non-exhaustive
Non-exhaustive enum variants could have additional fields added in future. Therefore, non-exhaustive enum variants cannot be constructed in external crates and cannot be matched against.
§target: Price

The marginal price we want the pool to be after the trade, as a Price struct. The pool’s price will move down to this level as token_in is sold into it

§tolerance: f64

The tolerance as a fraction of the resulting pool marginal price. After trading, the pool’s price will decrease to the interval [target, target * (1 + tolerance)].

§min_amount_in: Option<BigUint>

The lower bound for searching algorithms.

§max_amount_in: Option<BigUint>

The upper bound for searching algorithms.

Implementations§

Source§

impl SwapConstraint

Source

pub fn trade_limit_price(limit: Price, tolerance: f64) -> Self

Creates a trade limit price constraint.

This constraint finds the maximum trade size while respecting a minimum price threshold. See SwapConstraint::TradeLimitPrice for details.

§Arguments
  • limit - The minimum acceptable price for the trade
  • tolerance - Additional tolerance as a fraction to loosen the constraint
Source

pub fn pool_target_price(target: Price, tolerance: f64) -> Self

Creates a pool target price constraint.

This constraint calculates the trade needed to move the pool’s price to a target level. See SwapConstraint::PoolTargetPrice for details.`

§Arguments
  • target - The desired final marginal price of the pool
  • tolerance - Acceptable variance from the target price as a fraction
Source

pub fn with_lower_bound(self, lower: BigUint) -> SimulationResult<Self>

Adds a lower bound to the constraint’s search range.

§Arguments
  • lower - The minimum amount_in to consider
§Returns

The modified constraint with the lower bound applied.

Source

pub fn with_upper_bound(self, upper: BigUint) -> SimulationResult<Self>

Adds an upper bound to the constraint’s search range.

§Arguments
  • upper - The maximum amount_in to consider
§Returns

The modified constraint with the upper bound applied.

Trait Implementations§

Source§

impl Clone for SwapConstraint

Source§

fn clone(&self) -> SwapConstraint

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for SwapConstraint

Source§

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

Formats the value using the given formatter. Read more
Source§

impl PartialEq for SwapConstraint

Source§

fn eq(&self, other: &SwapConstraint) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 (const: unstable) · Source§

fn ne(&self, other: &Rhs) -> bool

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl StructuralPartialEq for SwapConstraint

Auto Trait Implementations§

Blanket Implementations§

Source§

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

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

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

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

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

Source§

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

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T> Instrument for T

Source§

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

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

fn in_current_span(self) -> Instrumented<Self>

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

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

Source§

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> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

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

Source§

type Error = Infallible

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

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

Performs the conversion.
Source§

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

Source§

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

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

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

Performs the conversion.
Source§

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

Source§

fn vzip(self) -> V

Source§

impl<T> WithSubscriber for T

Source§

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
Source§

fn with_current_subscriber(self) -> WithDispatch<Self>

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