cfmm_common

package
v21.2.1 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jan 3, 2024 License: Apache-2.0 Imports: 5 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func BinarySearchSingleAssetJoin

func BinarySearchSingleAssetJoin(
	pool types.CFMMPoolI,
	tokenIn sdk.Coin,
	poolWithAddedLiquidityAndShares func(newLiquidity sdk.Coin, newShares osmomath.Int) types.CFMMPoolI,
) (numLPShares osmomath.Int, err error)

We binary search a number of LP shares, s.t. if we exited the pool with the updated liquidity, and swapped all the tokens back to the input denom, we'd get the same amount. (under 0 spread factor) Thanks to CFMM path-independence, we can estimate slippage with these swaps to be sure to get the right numbers here. (by path-independence, swap all of B -> A, and then swap all of C -> A will yield same amount of A, regardless of order and interleaving)

This implementation requires each of pool.GetTotalPoolLiquidity, pool.ExitPool, and pool.SwapExactAmountIn to not update or read from state, and instead only do updates based upon the pool struct.

func CalcExitPool

func CalcExitPool(ctx sdk.Context, pool types.CFMMPoolI, exitingShares osmomath.Int, exitFee osmomath.Dec) (sdk.Coins, error)

CalcExitPool returns how many tokens should come out, when exiting k LP shares against a "standard" CFMM

func MaximalExactRatioJoin

func MaximalExactRatioJoin(p types.CFMMPoolI, ctx sdk.Context, tokensIn sdk.Coins) (numShares osmomath.Int, remCoins sdk.Coins, err error)

MaximalExactRatioJoin calculates the maximal amount of tokens that can be joined whilst maintaining pool asset's ratio returning the number of shares that'd be and how many coins would be left over.

e.g) suppose we have a pool of 10 foo tokens and 10 bar tokens, with the total amount of 100 shares.
	 if `tokensIn` provided is 1 foo token and 2 bar tokens, `MaximalExactRatioJoin`
	 would be returning (10 shares, 1 bar token, nil)

This can be used when `tokensIn` are not guaranteed the same ratio as assets in the pool. Calculation for this is done in the following steps.

  1. iterate through all the tokens provided as an argument, calculate how much ratio it accounts for the asset in the pool
  2. get the minimal share ratio that would work as the benchmark for all tokens.
  3. calculate the number of shares that could be joined (total share * min share ratio), return the remaining coins

func SwapAllCoinsToSingleAsset

func SwapAllCoinsToSingleAsset(pool types.CFMMPoolI, ctx sdk.Context, inTokens sdk.Coins, swapToDenom string,
	spreadFactor osmomath.Dec,
) (osmomath.Int, error)

SwapAllCoinsToSingleAsset iterates through each token in the input set and trades it against the same pool sequentially

Types

This section is empty.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL