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

Constructs a new StableSwap from a SwapInfo.

Constructs a new StableSwap invariant calculator.

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.

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.

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

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

Computes the swap amount y in proportion to x.

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

Compute SwapResult after an exchange

Trait Implementations

Returns a copy of the value. Read more

Performs copy-assignment from source. Read more

Formats the value using the given formatter. Read more

Performs the conversion.

This method tests for self and other values to be equal, and is used by ==. Read more

This method tests for !=.

The type returned in the event of a conversion error.

Performs the conversion.

Auto Trait Implementations

Blanket Implementations

Gets the TypeId of self. Read more

Immutably borrows from an owned value. Read more

Mutably borrows from an owned value. Read more

Performs the conversion.

Performs the conversion.

Should always be Self

The resulting type after obtaining ownership.

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

🔬 This is a nightly-only experimental API. (toowned_clone_into)

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

The type returned in the event of a conversion error.

Performs the conversion.

The type returned in the event of a conversion error.

Performs the conversion.