stableswap_calculator

exception pactsdk.stableswap_calculator.ConvergenceError

Bases: pactsdk.exceptions.PactSdkError

class pactsdk.stableswap_calculator.StableswapParams(fee_bps, pact_fee_bps, initial_a, initial_a_time, future_a, future_a_time, precision)

Bases: object

fee_bps: int
pact_fee_bps: int
initial_a: int
initial_a_time: int
future_a: int
future_a_time: int
precision: int
pactsdk.stableswap_calculator.get_tx_fee(invariant_iterations, extra_margin)

To calculate the pool invariant, a Newton-Raphson method is used in both - the SDK and the smart contract.

Algorand has a limit of the number of operations available in a single app call. To increase the limit, an additional empty inner transaction have to be created. Each extra tx increases tx fee. This functions calculates the fee needed for a swap transaction.

Parameters
  • invariant_iterations (int) – Number of iterations of Newton-Raphson interpolation.

  • extra_margin (int) – Number of extra inner transactions needed in case of slippage.

Return type

int

Returns

The required fee.

pactsdk.stableswap_calculator.get_stableswap_minted_liquidity_tokens(added_primary, added_secondary, total_primary, total_secondary, total_liquidity, amplifier, precision, fee_bps)

Returns a tuple of minted tokens and total Newton-Raphson iterations (needed for tx fee calculations).

Return type

tuple[int, int]

pactsdk.stableswap_calculator.get_add_liquidity_bonus_pct(added_primary, added_secondary, total_primary, total_secondary, fee_bps, amplifier, precision)
Return type

float

pactsdk.stableswap_calculator.get_add_liquidity_fees(initial_totals, updated_totals, fee_bps, amplifier, precision)
Return type

tuple[tuple[int, int], int, int]

pactsdk.stableswap_calculator.get_swap_gross_amount_received(liq_a, liq_b, amount_deposited, amplifier, precision)
Return type

tuple[int, int]

pactsdk.stableswap_calculator.get_swap_amount_deposited(liq_a, liq_b, gross_amount_received, amplifier, precision)
Return type

tuple[int, int]

pactsdk.stableswap_calculator.get_invariant(liq_a, liq_b, amp, precision)

Uses a Newton-Raphson method to calculate the pool invariant.

Return type

tuple[int, int]

Returns

A tuple of invariant and number of iterations required to calculate the invariant.

pactsdk.stableswap_calculator.get_new_liq(liq_other, amplifier, inv, precision)
Return type

int

pactsdk.stableswap_calculator.get_amplifier(timestamp, initial_a, initial_a_time, future_a, future_a_time)

Calculates the amplifier at specified timestamp using linear interpolation.

Return type

int

class pactsdk.stableswap_calculator.StableswapCalculator(pool)

Bases: object

An implementation of a math behind stableswap pools.

swap_invariant_iterations = 0

Keeps the amount of iteration used to calculate invariant in the last call to getSwapGrossAmountReceived or getSwapAmountDeposited. Needed to calculate transaction fee.

mint_tokens_invariant_iterations = 0

The same as swap_invariant_iterations but for adding liquidity.

property stableswap_params: pactsdk.stableswap_calculator.StableswapParams
Return type

StableswapParams

get_amplifier()
get_price(liq_a, liq_b)

May return zero for highly unbalanced pools.

Return type

float

_get_price(liq_a, liq_b, retries)

Price is calculated by simulating a swap for 10**6 of micro values. This price is highly inaccurate for low liquidity pools. In case of ConvergenceError we try to simulate a swap using a different swap amount. Returns zero if all retries will fail.

Return type

float

get_swap_gross_amount_received(liq_a, liq_b, amount_deposited, save_iterations=True)
Return type

int

get_swap_amount_deposited(liq_a, liq_b, gross_amount_received, save_iterations=True)
Return type

int

get_minted_liquidity_tokens(added_liq_a, added_liq_b)
Return type

int