Struct stable_swap::curve::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, 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
sourceimpl 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
) -> StableSwap
pub fn new(
initial_amp_factor: u64,
target_amp_factor: u64,
current_ts: i64,
start_ramp_ts: i64,
stop_ramp_ts: i64
) -> StableSwap
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
sourceimpl Clone for StableSwap
impl Clone for StableSwap
sourcepub fn clone(&self) -> StableSwap
pub fn clone(&self) -> StableSwap
Returns a copy of the value. Read more
1.0.0 · sourcefn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
Performs copy-assignment from source
. Read more
sourceimpl Debug for StableSwap
impl Debug for StableSwap
sourceimpl<'_> From<&'_ SaberSwap> for StableSwap
impl<'_> From<&'_ SaberSwap> for StableSwap
sourcepub fn from(swap: &SaberSwap) -> StableSwap
pub fn from(swap: &SaberSwap) -> StableSwap
Performs the conversion.
sourceimpl PartialEq<StableSwap> for StableSwap
impl PartialEq<StableSwap> for StableSwap
sourcepub fn eq(&self, other: &StableSwap) -> bool
pub fn eq(&self, other: &StableSwap) -> bool
This method tests for self
and other
values to be equal, and is used
by ==
. Read more
sourcepub fn ne(&self, other: &StableSwap) -> bool
pub fn ne(&self, other: &StableSwap) -> bool
This method tests for !=
.
sourceimpl<'_> TryFrom<&'_ SwapInfo> for StableSwap
impl<'_> TryFrom<&'_ SwapInfo> for StableSwap
type Error = ProgramError
type Error = ProgramError
The type returned in the event of a conversion error.
sourcepub fn try_from(info: &SwapInfo) -> Result<StableSwap, ProgramError>
pub fn try_from(info: &SwapInfo) -> Result<StableSwap, ProgramError>
Performs the conversion.
impl Copy for StableSwap
impl Eq for StableSwap
impl StructuralEq for StableSwap
impl StructuralPartialEq for StableSwap
Auto Trait Implementations
impl RefUnwindSafe for StableSwap
impl Send for StableSwap
impl Sync for StableSwap
impl Unpin for StableSwap
impl UnwindSafe for StableSwap
Blanket Implementations
impl<T> AbiExample for T
impl<T> AbiExample for T
pub default fn example() -> T
sourceimpl<T> BorrowMut<T> for T where
T: ?Sized,
impl<T> BorrowMut<T> for T where
T: ?Sized,
const: unstable · sourcepub fn borrow_mut(&mut self) -> &mut T
pub fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more
sourceimpl<T> ToOwned for T where
T: Clone,
impl<T> ToOwned for T where
T: Clone,
type Owned = T
type Owned = T
The resulting type after obtaining ownership.
sourcepub fn to_owned(&self) -> T
pub fn to_owned(&self) -> T
Creates owned data from borrowed data, usually by cloning. Read more
sourcepub fn clone_into(&self, target: &mut T)
pub fn clone_into(&self, target: &mut T)
toowned_clone_into
)Uses borrowed data to replace owned data, usually by cloning. Read more