stableswap

package
v1.3.3 Latest Latest
Warning

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

Go to latest
Published: Mar 17, 2023 License: MIT Imports: 3 Imported by: 0

Documentation

Overview

stableswap is the reference implementation of stable swap for https://aux.exchange.

The idea is to generate test cases and implementation details in a more capable languange and verify the mathematics. It doesn't involve onchain logics or other.

Index

Examples

Constants

View Source
const NDecimals = 18

Variables

View Source
var ThreeCoinTestCases [][]string = [][]string{}/* 125 elements not displayed */

Mathematica Test Cases for 3 coins

ds[x1_,x2_,x3_,A_]:=Module[{d,r,k},r=NSolve[A*27*(x1+x2+x3)+d-A*27*d-d^(4)/27/x1/x2/x3,d,Reals,WorkingPrecision->100];k=IntegerPart[d/.r];Print[{x1,x2,x3,Select[k,#>0&][[1]]}]] dd=Table[ds[i1*10^21,i2*10^21,i3*10^21,15],{i1,1,5},{i2,1,5},{i3,1,5}]

View Source
var TwoCoinTestCases = [][]string{
	{"1000000000000000000000", "1000000000000000000000", "2000000000000000000000"},
	{"1000000000000000000000", "2000000000000000000000", "2993987324342594819162"},
	{"1000000000000000000000", "3000000000000000000000", "3978828876840973560802"},
	{"1000000000000000000000", "4000000000000000000000", "4955810397915579285744"},
	{"1000000000000000000000", "5000000000000000000000", "5925388362748303567980"},
	{"1000000000000000000000", "6000000000000000000000", "6887867795041488135767"},
	{"1000000000000000000000", "7000000000000000000000", "7843501099357207543234"},
	{"1000000000000000000000", "8000000000000000000000", "8792513639835515740306"},
	{"1000000000000000000000", "9000000000000000000000", "9735112883796791085400"},
	{"1000000000000000000000", "10000000000000000000000", "10671492539695429511451"},
	{"2000000000000000000000", "1000000000000000000000", "2993987324342594819162"},
	{"2000000000000000000000", "2000000000000000000000", "4000000000000000000000"},
	{"2000000000000000000000", "3000000000000000000000", "4996646432824630184465"},
	{"2000000000000000000000", "4000000000000000000000", "5987974648685189638325"},
	{"2000000000000000000000", "5000000000000000000000", "6974865077314815159986"},
	{"2000000000000000000000", "6000000000000000000000", "7957657753681947121605"},
	{"2000000000000000000000", "7000000000000000000000", "8936535524851711934567"},
	{"2000000000000000000000", "8000000000000000000000", "9911620795831158571488"},
	{"2000000000000000000000", "9000000000000000000000", "10883008008267593357850"},
	{"2000000000000000000000", "10000000000000000000000", "11850776725496607135961"},
	{"3000000000000000000000", "1000000000000000000000", "3978828876840973560802"},
	{"3000000000000000000000", "2000000000000000000000", "4996646432824630184465"},
	{"3000000000000000000000", "3000000000000000000000", "6000000000000000000000"},
	{"3000000000000000000000", "4000000000000000000000", "6997650179274309702766"},
	{"3000000000000000000000", "5000000000000000000000", "7991425036417606251576"},
	{"3000000000000000000000", "6000000000000000000000", "8981961973027784457488"},
	{"3000000000000000000000", "7000000000000000000000", "9969553313965636362846"},
	{"3000000000000000000000", "8000000000000000000000", "10954360603545046811764"},
	{"3000000000000000000000", "9000000000000000000000", "11936486630522920682408"},
	{"3000000000000000000000", "10000000000000000000000", "12916004407207853098264"},
	{"4000000000000000000000", "1000000000000000000000", "4955810397915579285744"},
	{"4000000000000000000000", "2000000000000000000000", "5987974648685189638325"},
	{"4000000000000000000000", "3000000000000000000000", "6997650179274309702766"},
	{"4000000000000000000000", "4000000000000000000000", "8000000000000000000000"},
	{"4000000000000000000000", "5000000000000000000000", "8998186562807776230738"},
	{"4000000000000000000000", "6000000000000000000000", "9993292865649260368930"},
	{"4000000000000000000000", "7000000000000000000000", "10985797352784921592990"},
	{"4000000000000000000000", "8000000000000000000000", "11975949297370379276650"},
	{"4000000000000000000000", "9000000000000000000000", "12963895280304759421395"},
	{"4000000000000000000000", "10000000000000000000000", "13949730154629630319972"},
	{"5000000000000000000000", "1000000000000000000000", "5925388362748303567980"},
	{"5000000000000000000000", "2000000000000000000000", "6974865077314815159986"},
	{"5000000000000000000000", "3000000000000000000000", "7991425036417606251576"},
	{"5000000000000000000000", "4000000000000000000000", "8998186562807776230738"},
	{"5000000000000000000000", "5000000000000000000000", "10000000000000000000000"},
	{"5000000000000000000000", "6000000000000000000000", "10998522091619534205600"},
	{"5000000000000000000000", "7000000000000000000000", "11994477554332926716575"},
	{"5000000000000000000000", "8000000000000000000000", "12988237117259130537377"},
	{"5000000000000000000000", "9000000000000000000000", "13980012779215461970321"},
	{"5000000000000000000000", "10000000000000000000000", "14969936621712974095813"},
	{"6000000000000000000000", "1000000000000000000000", "6887867795041488135767"},
	{"6000000000000000000000", "2000000000000000000000", "7957657753681947121605"},
	{"6000000000000000000000", "3000000000000000000000", "8981961973027784457488"},
	{"6000000000000000000000", "4000000000000000000000", "9993292865649260368930"},
	{"6000000000000000000000", "5000000000000000000000", "10998522091619534205600"},
	{"6000000000000000000000", "6000000000000000000000", "12000000000000000000000"},
	{"6000000000000000000000", "7000000000000000000000", "12998752273662148280448"},
	{"6000000000000000000000", "8000000000000000000000", "13995300358548619405533"},
	{"6000000000000000000000", "9000000000000000000000", "14989939298473890553395"},
	{"6000000000000000000000", "10000000000000000000000", "15982850072835212503153"},
	{"7000000000000000000000", "1000000000000000000000", "7843501099357207543234"},
	{"7000000000000000000000", "2000000000000000000000", "8936535524851711934567"},
	{"7000000000000000000000", "3000000000000000000000", "9969553313965636362846"},
	{"7000000000000000000000", "4000000000000000000000", "10985797352784921592990"},
	{"7000000000000000000000", "5000000000000000000000", "11994477554332926716575"},
	{"7000000000000000000000", "6000000000000000000000", "12998752273662148280448"},
	{"7000000000000000000000", "7000000000000000000000", "14000000000000000000000"},
	{"7000000000000000000000", "8000000000000000000000", "14998920160357702452125"},
	{"7000000000000000000000", "9000000000000000000000", "15995906830127569462179"},
	{"7000000000000000000000", "10000000000000000000000", "16991200094974791444669"},
	{"8000000000000000000000", "1000000000000000000000", "8792513639835515740306"},
	{"8000000000000000000000", "2000000000000000000000", "9911620795831158571488"},
	{"8000000000000000000000", "3000000000000000000000", "10954360603545046811764"},
	{"8000000000000000000000", "4000000000000000000000", "11975949297370379276650"},
	{"8000000000000000000000", "5000000000000000000000", "12988237117259130537377"},
	{"8000000000000000000000", "6000000000000000000000", "13995300358548619405533"},
	{"8000000000000000000000", "7000000000000000000000", "14998920160357702452125"},
	{"8000000000000000000000", "8000000000000000000000", "16000000000000000000000"},
	{"8000000000000000000000", "9000000000000000000000", "16999048096410141658304"},
	{"8000000000000000000000", "10000000000000000000000", "17996373125615552461477"},
	{"9000000000000000000000", "1000000000000000000000", "9735112883796791085400"},
	{"9000000000000000000000", "2000000000000000000000", "10883008008267593357850"},
	{"9000000000000000000000", "3000000000000000000000", "11936486630522920682408"},
	{"9000000000000000000000", "4000000000000000000000", "12963895280304759421395"},
	{"9000000000000000000000", "5000000000000000000000", "13980012779215461970321"},
	{"9000000000000000000000", "6000000000000000000000", "14989939298473890553395"},
	{"9000000000000000000000", "7000000000000000000000", "15995906830127569462179"},
	{"9000000000000000000000", "8000000000000000000000", "16999048096410141658304"},
	{"9000000000000000000000", "9000000000000000000000", "18000000000000000000000"},
	{"9000000000000000000000", "10000000000000000000000", "18999148858070482878872"},
	{"10000000000000000000000", "1000000000000000000000", "10671492539695429511451"},
	{"10000000000000000000000", "2000000000000000000000", "11850776725496607135961"},
	{"10000000000000000000000", "3000000000000000000000", "12916004407207853098264"},
	{"10000000000000000000000", "4000000000000000000000", "13949730154629630319972"},
	{"10000000000000000000000", "5000000000000000000000", "14969936621712974095813"},
	{"10000000000000000000000", "6000000000000000000000", "15982850072835212503153"},
	{"10000000000000000000000", "7000000000000000000000", "16991200094974791444669"},
	{"10000000000000000000000", "8000000000000000000000", "17996373125615552461477"},
	{"10000000000000000000000", "9000000000000000000000", "18999148858070482878872"},
	{"10000000000000000000000", "10000000000000000000000", "20000000000000000000000"},
}

Mathematica generated test cases for two coins

ds[x1_,x2_,A_]:=Module[{d,r,k},r=NSolve[A*4*(x1+x2)+d-A*4*d-d^(3)/4/x1/x2,d,Reals,WorkingPrecision->100];k=IntegerPart[d/.r];Print[{x1,x2,Select[k,#>0&][[1]]}]] dd=Table[ds[i1*10^21,i2*10^21,15],{i1,1,10},{i2,1,10}]

Functions

func CalculateD

func CalculateD(amp *big.Int, nCoins int, xi ...*big.Int) (*big.Int, error)

CalculateD calculates D for a series of coin balances.

See stable swap doc for details.

Example
a := big.NewInt(300)
test := TwoCoinTestCases[0]
x0, _ := big.NewInt(0).SetString(test[0], 10)
x1 := big.NewInt(1_000_000_000)

d, err := CalculateD(a, 2, x0, x1)
if err != nil {
	panic(err)
}

v := big.NewInt(0).Div(x0, d)

fmt.Printf("x0: %s, x1: %s, d: %s, x0/d: %s\n", x0.String(), x1.String(), d.String(), v.String())
Output:

x0: 1000000000000000000000, x1: 1000000000, d: 1685917616243366874, x0/d: 593

func CalculateXi

func CalculateXi(amp *big.Int, nCoins int, d *big.Int, xj ...*big.Int) (*big.Int, error)

func CalculateXiWithI

func CalculateXiWithI(amp *big.Int, nCoins int, d *big.Int, i int, xj ...*big.Int) (*big.Int, error)

Types

type PoolGen added in v1.3.3

type PoolGen struct {
	// N is number of coins.
	N int
	// LastIndex is number of coins - 1
	LastIndex int
	Xis       []*poolCoinI

	AllPerms [][]*poolCoinI

	Precision     uint64
	NPrecision    int
	BalanceScaler uint64

	NameTake int

	Decimals []uint64

	CoinTypeList string

	MaxU64 uint64

	MaxDecimal uint64

	GenPermutation bool

	FeeDenominator  uint64
	MaxFeeNumerator uint64
	MinFeeNumerator uint64

	MaxAmp uint64

	WarningForManualEdit string

	UseQuoter bool

	IsQuoter   bool
	ModuleName string

	StructParamName string
}

func NewPoolGen added in v1.3.3

func NewPoolGen(n int, isQuoter bool) *PoolGen

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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