Struct StableSwap

Source
pub struct StableSwap { /* private fields */ }
Expand description

The StableSwap invariant calculator.

This is primarily used to calculate two quantities:

  • D, the swap invariant, and
  • Y, the amount of tokens swapped in an instruction.

This calculator also contains several helper utilities for computing swap, withdraw, and deposit amounts.

§Resources:

Implementations§

Source§

impl StableSwap

Source

pub fn new_from_swap_info(info: &SwapInfo, current_ts: i64) -> StableSwap

Constructs a new StableSwap from a SwapInfo.

Source

pub fn new( initial_amp_factor: u64, target_amp_factor: u64, current_ts: i64, start_ramp_ts: i64, stop_ramp_ts: i64, ) -> Self

Constructs a new StableSwap invariant calculator.

Source

pub fn compute_amp_factor(&self) -> Option<u64>

Compute the amplification coefficient (A).

The amplification coefficient is used to determine the slippage incurred when performing swaps. The lower it is, the closer the invariant is to the constant product1.

The amplication coefficient linearly increases with respect to time, based on the SwapInfo::start_ramp_ts and SwapInfo::stop_ramp_ts parameters.

Source

pub fn compute_d(&self, amount_a: u64, amount_b: u64) -> Option<U192>

Computes the Stable Swap invariant (D).

The invariant is defined as follows:

A * sum(x_i) * n**n + D = A * D * n**n + D**(n+1) / (n**n * prod(x_i))
§Arguments
  • amount_a - The amount of token A owned by the LP pool. (i.e. token A reserves)
  • amount_b - The amount of token B owned by the LP pool. (i.e. token B reserves)

For more info on reserves, see stable_swap_client::state::SwapTokenInfo::reserves.

Source

pub fn compute_mint_amount_for_deposit( &self, deposit_amount_a: u64, deposit_amount_b: u64, swap_amount_a: u64, swap_amount_b: u64, pool_token_supply: u64, fees: &Fees, ) -> Option<u64>

Computes the amount of pool tokens to mint after a deposit.

Source

pub fn compute_y_raw(&self, x: u64, d: U192) -> Option<U192>

Compute the swap amount y in proportion to x.

Solve for y:

y**2 + y * (sum' - (A*n**n - 1) * D / (A * n**n)) = D ** (n + 1) / (n ** (2 * n) * prod' * A)
y**2 + b*y = c
Source

pub fn compute_y(&self, x: u64, d: U192) -> Option<u64>

Computes the swap amount y in proportion to x.

Source

pub fn compute_withdraw_one( &self, pool_token_amount: u64, pool_token_supply: u64, swap_base_amount: u64, swap_quote_amount: u64, fees: &Fees, ) -> Option<(u64, u64)>

Calculates the withdrawal amount when withdrawing only one type of token.

Calculation:

  1. Get current D
  2. Solve Eqn against y_i for D - _token_amount
Source

pub fn swap_to( &self, source_amount: u64, swap_source_amount: u64, swap_destination_amount: u64, fees: &Fees, ) -> Option<SwapResult>

Compute SwapResult after an exchange

Trait Implementations§

Source§

impl Clone for StableSwap

Source§

fn clone(&self) -> StableSwap

Returns a duplicate of the value. Read more
1.0.0 · Source§

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

Performs copy-assignment from source. Read more
Source§

impl Debug for StableSwap

Source§

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

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

impl From<&SaberSwap> for StableSwap

Source§

fn from(swap: &SaberSwap) -> Self

Converts to this type from the input type.
Source§

impl PartialEq for StableSwap

Source§

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

Tests for self and other values to be equal, and is used by ==.
1.0.0 · 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 TryFrom<&SwapInfo> for StableSwap

Source§

type Error = ProgramError

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

fn try_from(info: &SwapInfo) -> Result<Self, ProgramError>

Performs the conversion.
Source§

impl Copy for StableSwap

Source§

impl Eq for StableSwap

Source§

impl StructuralPartialEq for StableSwap

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> AbiExample for T

Source§

default fn example() -> T

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<Q, K> Equivalent<K> for Q
where Q: Eq + ?Sized, K: Borrow<Q> + ?Sized,

Source§

fn equivalent(&self, key: &K) -> bool

Checks if this value is equivalent to the given key. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

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> Pointable for T

Source§

const ALIGN: usize

The alignment of pointer.
Source§

type Init = T

The type for initializers.
Source§

unsafe fn init(init: <T as Pointable>::Init) -> usize

Initializes a with the given initializer. Read more
Source§

unsafe fn deref<'a>(ptr: usize) -> &'a T

Dereferences the given pointer. Read more
Source§

unsafe fn deref_mut<'a>(ptr: usize) -> &'a mut T

Mutably dereferences the given pointer. Read more
Source§

unsafe fn drop(ptr: usize)

Drops the object pointed to by the given pointer. Read more
Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
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