pub struct StableSwap { /* private fields */ }
Expand description
The StableSwap invariant calculator.
This is primarily used to calculate two quantities:
D
, the swap invariant, andY
, 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
impl StableSwap
Sourcepub fn new_from_swap_info(info: &SwapInfo, current_ts: i64) -> StableSwap
pub fn new_from_swap_info(info: &SwapInfo, current_ts: i64) -> StableSwap
Constructs a new StableSwap from a SwapInfo.
Sourcepub fn new(
initial_amp_factor: u64,
target_amp_factor: u64,
current_ts: i64,
start_ramp_ts: i64,
stop_ramp_ts: i64,
) -> Self
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.
Sourcepub fn compute_amp_factor(&self) -> Option<u64>
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.
Sourcepub fn compute_d(&self, amount_a: u64, amount_b: u64) -> Option<U192>
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.
Sourcepub 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>
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.
Sourcepub fn compute_y_raw(&self, x: u64, d: U192) -> Option<U192>
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
Sourcepub fn compute_y(&self, x: u64, d: U192) -> Option<u64>
pub fn compute_y(&self, x: u64, d: U192) -> Option<u64>
Computes the swap amount y
in proportion to x
.
Sourcepub 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)>
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:
- Get current D
- Solve Eqn against
y_i
forD - _token_amount
Trait Implementations§
Source§impl Clone for StableSwap
impl Clone for StableSwap
Source§fn clone(&self) -> StableSwap
fn clone(&self) -> StableSwap
1.0.0 · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source
. Read moreSource§impl Debug for StableSwap
impl Debug for StableSwap
Source§impl From<&SaberSwap> for StableSwap
impl From<&SaberSwap> for StableSwap
Source§impl PartialEq for StableSwap
impl PartialEq for StableSwap
Source§impl TryFrom<&SwapInfo> for StableSwap
impl TryFrom<&SwapInfo> for StableSwap
Source§type Error = ProgramError
type Error = ProgramError
impl Copy for StableSwap
impl Eq for StableSwap
impl StructuralPartialEq for StableSwap
Auto Trait Implementations§
impl Freeze for StableSwap
impl RefUnwindSafe for StableSwap
impl Send for StableSwap
impl Sync for StableSwap
impl Unpin for StableSwap
impl UnwindSafe for StableSwap
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
Source§impl<Q, K> Equivalent<K> for Q
impl<Q, K> Equivalent<K> for Q
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