diff --git a/pkg/liquidity-source/syncswapv2/aqua/math.go b/pkg/liquidity-source/syncswapv2/aqua/math.go index c345e0cb3..8b6e8a398 100644 --- a/pkg/liquidity-source/syncswapv2/aqua/math.go +++ b/pkg/liquidity-source/syncswapv2/aqua/math.go @@ -139,58 +139,60 @@ func newtonD(ANN *uint256.Int, gamma *uint256.Int, xUnsorted []*uint256.Int) (*u } else { _g1k0 = new(uint256.Int).Add(new(uint256.Int).Sub(K0, _g1k0), constant.One) } - var mul1 = new(uint256.Int).Div( - new(uint256.Int).Mul( - new(uint256.Int).Mul( - new(uint256.Int).Div( - new(uint256.Int).Mul( - new(uint256.Int).Div(new(uint256.Int).Mul(constant.BONE, D), gamma), _g1k0, - ), gamma, - ), - _g1k0, - ), - AMultiplier, - ), ANN, - ) - var mul2 = new(uint256.Int).Div( - new(uint256.Int).Mul( - new(uint256.Int).Mul( - new(uint256.Int).Mul(constant.Two, constant.BONE), nCoinsBi, - ), K0, - ), _g1k0, - ) - var negFprime = new(uint256.Int).Sub( - new(uint256.Int).Add( - new(uint256.Int).Add(S, new(uint256.Int).Div(new(uint256.Int).Mul(S, mul2), constant.BONE)), - new(uint256.Int).Div(new(uint256.Int).Mul(mul1, nCoinsBi), K0), - ), - new(uint256.Int).Div(new(uint256.Int).Mul(mul2, D), constant.BONE), + // var mul1 = new(uint256.Int).Div( + // new(uint256.Int).Mul( + // new(uint256.Int).Mul( + // new(uint256.Int).Div( + // new(uint256.Int).Mul( + // new(uint256.Int).Div(new(uint256.Int).Mul(constant.BONE, D), gamma), _g1k0, + // ), gamma, + // ), + // _g1k0, + // ), + // AMultiplier, + // ), ANN, + // ) + var mul1 = new(uint256.Int) + mul1.Mul(constant.BONE, D).Div(mul1, gamma).Mul(mul1, _g1k0).Div(mul1, gamma).Mul(mul1, _g1k0).Mul(mul1, AMultiplier).Div(mul1, ANN) + // var mul2 = new(uint256.Int).Div( + // new(uint256.Int).Mul( + // new(uint256.Int).Mul( + // new(uint256.Int).Mul(constant.Two, constant.BONE), nCoinsBi, + // ), K0, + // ), _g1k0, + // ) + var mul2 = new(uint256.Int) + mul2.Mul(constant.Two, constant.BONE).Mul(mul2, nCoinsBi).Mul(mul2, K0).Div(mul2, _g1k0) + // var negFprime = new(uint256.Int).Sub( + // new(uint256.Int).Add( + // new(uint256.Int).Add(S, new(uint256.Int).Div(new(uint256.Int).Mul(S, mul2), constant.BONE)), + // new(uint256.Int).Div(new(uint256.Int).Mul(mul1, nCoinsBi), K0), + // ), + // new(uint256.Int).Div(new(uint256.Int).Mul(mul2, D), constant.BONE), + // ) + var negFprime = new(uint256.Int) + negFprime.Mul(S, mul2).Div(negFprime, constant.BONE).Add(S, negFprime).Add( + new(uint256.Int).Set(negFprime), + negFprime.Mul(mul1, nCoinsBi).Div(negFprime, K0), + ).Sub( + new(uint256.Int).Set(negFprime), + negFprime.Mul(mul2, D).Div(negFprime, constant.BONE), ) - var DPlus = new(uint256.Int).Div(new(uint256.Int).Mul(D, new(uint256.Int).Add(negFprime, S)), negFprime) - var DMinus = new(uint256.Int).Div(new(uint256.Int).Mul(D, D), negFprime) + // var DPlus = new(uint256.Int).Div(new(uint256.Int).Mul(D, new(uint256.Int).Add(negFprime, S)), negFprime) + var DPlus = new(uint256.Int) + DPlus.Add(negFprime, S).Mul(D, DPlus).Div(DPlus, negFprime) + // var DMinus = new(uint256.Int).Div(new(uint256.Int).Mul(D, D), negFprime) + var DMinus = new(uint256.Int) + DMinus.Mul(D, D).Div(DMinus, negFprime) if constant.BONE.Cmp(K0) > 0 { - DMinus = new(uint256.Int).Add( - DMinus, - new(uint256.Int).Div( - new(uint256.Int).Mul( - new(uint256.Int).Div( - new(uint256.Int).Mul(D, new(uint256.Int).Div(mul1, negFprime)), constant.BONE, - ), new(uint256.Int).Sub(constant.BONE, K0), - ), - K0, - ), + DMinus.Add(new( + uint256.Int).Set(DMinus), + DMinus.Div(mul1, negFprime).Mul(D, DMinus).Div(DMinus, constant.BONE).Mul(DMinus, new(uint256.Int).Sub(constant.BONE, K0)).Div(DMinus, K0), ) } else { - DMinus = new(uint256.Int).Sub( - DMinus, - new(uint256.Int).Div( - new(uint256.Int).Mul( - new(uint256.Int).Div( - new(uint256.Int).Mul(D, new(uint256.Int).Div(mul1, negFprime)), constant.BONE, - ), new(uint256.Int).Sub(K0, constant.BONE), - ), - K0, - ), + DMinus.Add(new( + uint256.Int).Set(DMinus), + DMinus.Div(mul1, negFprime).Mul(D, DMinus).Div(DMinus, constant.BONE).Mul(DMinus, new(uint256.Int).Sub(K0, constant.BONE)).Div(DMinus, K0), ) } if DPlus.Cmp(DMinus) > 0 { @@ -267,24 +269,28 @@ func newtonY(ann *uint256.Int, gamma *uint256.Int, x []*uint256.Int, D *uint256. } else { _g1k0 = new(uint256.Int).Add(new(uint256.Int).Sub(K0, _g1k0), constant.One) } - var mul1 = new(uint256.Int).Div( - new(uint256.Int).Mul( - new(uint256.Int).Div( - new(uint256.Int).Mul( - new(uint256.Int).Div(new(uint256.Int).Mul(constant.BONE, D), gamma), - _g1k0, - ), gamma, - ), - new(uint256.Int).Mul(_g1k0, AMultiplier), - ), ann, - ) - var mul2 = new(uint256.Int).Add( - new(uint256.Int).Div( - new(uint256.Int).Mul( - new(uint256.Int).Mul(constant.Two, constant.BONE), K0, - ), _g1k0, - ), constant.BONE, - ) + // var mul1 = new(uint256.Int).Div( + // new(uint256.Int).Mul( + // new(uint256.Int).Div( + // new(uint256.Int).Mul( + // new(uint256.Int).Div(new(uint256.Int).Mul(constant.BONE, D), gamma), + // _g1k0, + // ), gamma, + // ), + // new(uint256.Int).Mul(_g1k0, AMultiplier), + // ), ann, + // ) + var mul1 = new(uint256.Int) + mul1.Mul(constant.BONE, D).Div(mul1, gamma).Mul(mul1, _g1k0).Div(mul1, gamma).Mul(mul1, _g1k0).Mul(mul1, AMultiplier).Div(mul1, ann) + // var mul2 = new(uint256.Int).Add( + // new(uint256.Int).Div( + // new(uint256.Int).Mul( + // new(uint256.Int).Mul(constant.Two, constant.BONE), K0, + // ), _g1k0, + // ), constant.BONE, + // ) + var mul2 = new(uint256.Int) + mul2.Mul(constant.Two, constant.BONE).Mul(mul2, K0).Div(mul2, _g1k0).Add(mul2, constant.BONE) var yfprime = new(uint256.Int).Add( new(uint256.Int).Add(new(uint256.Int).Mul(constant.BONE, y), new(uint256.Int).Mul(S, mul2)), mul1, ) @@ -685,50 +691,58 @@ func getCryptoFee(minFee, maxFee, gamma, xp0, xp1 *uint256.Int) *uint256.Int { // gamma.add(ETHER).sub(ETHER.mul(4).mul(xp0).div(f).mul(xp1).div(f)) // ); - f = new(uint256.Int).Div( - new(uint256.Int).Mul( - gamma, - constant.BONE, - ), - new(uint256.Int).Sub( - new(uint256.Int).Add( - gamma, - constant.BONE, - ), - new(uint256.Int).Div( - new(uint256.Int).Mul( - new(uint256.Int).Div( - new(uint256.Int).Mul( - new(uint256.Int).Mul( - constant.BONE, - constant.Four, - ), - xp0, - ), - f, - ), - xp1, - ), - f, - ), - ), + // f = new(uint256.Int).Div( + // new(uint256.Int).Mul( + // gamma, + // constant.BONE, + // ), + // new(uint256.Int).Sub( + // new(uint256.Int).Add( + // gamma, + // constant.BONE, + // ), + // new(uint256.Int).Div( + // new(uint256.Int).Mul( + // new(uint256.Int).Div( + // new(uint256.Int).Mul( + // new(uint256.Int).Mul( + // constant.BONE, + // constant.Four, + // ), + // xp0, + // ), + // f, + // ), + // xp1, + // ), + // f, + // ), + // ), + // ) + f1 := new(uint256.Int).Set(f) + f = f.Mul(constant.BONE, constant.Four).Mul(f, xp0).Div(f, f1).Mul(f, xp1).Div(f, f1).Sub( + new(uint256.Int).Add(gamma, constant.BONE), f, + ).Div( + new(uint256.Int).Mul(gamma, constant.BONE), f, ) // const fee = minFee.mul(f).add(maxFee.mul(ETHER.sub(f))).div(ETHER); - fee := new(uint256.Int).Div( - new(uint256.Int).Add( - new(uint256.Int).Mul( - minFee, f, - ), - new(uint256.Int).Mul( - maxFee, - new(uint256.Int).Sub( - constant.BONE, f, - ), - ), - ), - constant.BONE, - ) + // fee := new(uint256.Int).Div( + // new(uint256.Int).Add( + // new(uint256.Int).Mul( + // minFee, f, + // ), + // new(uint256.Int).Mul( + // maxFee, + // new(uint256.Int).Sub( + // constant.BONE, f, + // ), + // ), + // ), + // constant.BONE, + // ) + var fee = new(uint256.Int) + fee.Sub(constant.BONE, f).Mul(maxFee, fee).Add(fee, new(uint256.Int).Mul(minFee, f)).Div(fee, constant.BONE) return fee } diff --git a/pkg/liquidity-source/syncswapv2/aqua/math_test.go b/pkg/liquidity-source/syncswapv2/aqua/math_test.go new file mode 100644 index 000000000..eea952450 --- /dev/null +++ b/pkg/liquidity-source/syncswapv2/aqua/math_test.go @@ -0,0 +1,170 @@ +package syncswapv2aqua + +import ( + "testing" + + constant "github.com/KyberNetwork/kyberswap-dex-lib/pkg/util/big256" + "github.com/holiman/uint256" + "github.com/stretchr/testify/assert" +) + +func TestUint256Refactor(t *testing.T) { + func(D, gamma, _g1k0, ANN, AMultiplier *uint256.Int) { + var mul1 = new(uint256.Int) + assert.Equal(t, new(uint256.Int).Div( + new(uint256.Int).Mul( + new(uint256.Int).Mul( + new(uint256.Int).Div( + new(uint256.Int).Mul( + new(uint256.Int).Div(new(uint256.Int).Mul(constant.BONE, D), gamma), _g1k0, + ), gamma, + ), + _g1k0, + ), + AMultiplier, + ), ANN, + ), mul1.Mul(constant.BONE, D).Div(mul1, gamma).Mul(mul1, _g1k0).Div(mul1, gamma).Mul(mul1, _g1k0).Mul(mul1, AMultiplier).Div(mul1, ANN), "fail") + }(uint256.NewInt(2), uint256.NewInt(3), uint256.NewInt(4), uint256.NewInt(5), uint256.NewInt(6)) + + func(nCoinsBi, K0, _g1k0 *uint256.Int) { + var mul2 = new(uint256.Int) + assert.Equal(t, new(uint256.Int).Div( + new(uint256.Int).Mul( + new(uint256.Int).Mul( + new(uint256.Int).Mul(constant.Two, constant.BONE), nCoinsBi, + ), K0, + ), _g1k0, + ), mul2.Mul(constant.Two, constant.BONE).Mul(mul2, nCoinsBi).Mul(mul2, K0).Div(mul2, _g1k0), "fail") + }(uint256.NewInt(2), uint256.NewInt(3), uint256.NewInt(4)) + + func(S, mul2, mul1, nCoinsBi, K0, D *uint256.Int) { + var negFprime = new(uint256.Int) + assert.Equal(t, new(uint256.Int).Sub( + new(uint256.Int).Add( + new(uint256.Int).Add(S, new(uint256.Int).Div(new(uint256.Int).Mul(S, mul2), constant.BONE)), + new(uint256.Int).Div(new(uint256.Int).Mul(mul1, nCoinsBi), K0), + ), + new(uint256.Int).Div(new(uint256.Int).Mul(mul2, D), constant.BONE), + ), negFprime.Mul(S, mul2).Div(negFprime, constant.BONE).Add(S, negFprime).Add( + new(uint256.Int).Set(negFprime), + negFprime.Mul(mul1, nCoinsBi).Div(negFprime, K0), + ).Sub( + new(uint256.Int).Set(negFprime), + negFprime.Mul(mul2, D).Div(negFprime, constant.BONE), + ), "fail") + }(uint256.NewInt(2), uint256.NewInt(3), uint256.NewInt(4), uint256.NewInt(5), uint256.NewInt(6), uint256.NewInt(7)) + + func(D, negFprime, S *uint256.Int) { + var DPlus = new(uint256.Int) + assert.Equal(t, new(uint256.Int).Div(new(uint256.Int).Mul(D, new(uint256.Int).Add(negFprime, S)), negFprime), + DPlus.Add(negFprime, S).Mul(D, DPlus).Div(DPlus, negFprime), "fail") + }(uint256.NewInt(2), uint256.NewInt(3), uint256.NewInt(4)) + + func(D, negFprime *uint256.Int) { + var DMinus = new(uint256.Int) + assert.Equal(t, new(uint256.Int).Div(new(uint256.Int).Mul(D, D), negFprime), + DMinus.Mul(D, D).Div(DMinus, negFprime), "fail") + }(uint256.NewInt(2), uint256.NewInt(3)) + + func(D, mul1, negFprime, K0, DMinus *uint256.Int) { + assert.Equal(t, new(uint256.Int).Add( + DMinus, + new(uint256.Int).Div( + new(uint256.Int).Mul( + new(uint256.Int).Div( + new(uint256.Int).Mul(D, new(uint256.Int).Div(mul1, negFprime)), constant.BONE, + ), new(uint256.Int).Sub(constant.BONE, K0), + ), + K0, + ), + ), DMinus.Add(new( + uint256.Int).Set(DMinus), + DMinus.Div(mul1, negFprime).Mul(D, DMinus).Div(DMinus, constant.BONE).Mul(DMinus, new(uint256.Int).Sub(constant.BONE, K0)).Div(DMinus, K0), + ), "fail") + }(uint256.NewInt(2), uint256.NewInt(3), uint256.NewInt(4), uint256.NewInt(5), uint256.NewInt(6)) + + func(D, mul1, negFprime, K0, DMinus *uint256.Int) { + assert.Equal(t, new(uint256.Int).Sub( + DMinus, + new(uint256.Int).Div( + new(uint256.Int).Mul( + new(uint256.Int).Div( + new(uint256.Int).Mul(D, new(uint256.Int).Div(mul1, negFprime)), constant.BONE, + ), new(uint256.Int).Sub(K0, constant.BONE), + ), + K0, + ), + ), DMinus.Add(new( + uint256.Int).Set(DMinus), + DMinus.Div(mul1, negFprime).Mul(D, DMinus).Div(DMinus, constant.BONE).Mul(DMinus, new(uint256.Int).Sub(K0, constant.BONE)).Div(DMinus, K0), + ), "fail") + }(uint256.NewInt(2), uint256.NewInt(3), uint256.NewInt(4), uint256.NewInt(5), uint256.NewInt(6)) + + func(K0, _g1k0 *uint256.Int) { + var mul2 = new(uint256.Int) + assert.Equal(t, new(uint256.Int).Add( + new(uint256.Int).Div( + new(uint256.Int).Mul( + new(uint256.Int).Mul(constant.Two, constant.BONE), K0, + ), _g1k0, + ), constant.BONE, + ), mul2.Mul(constant.Two, constant.BONE).Mul(mul2, K0).Div(mul2, _g1k0).Add(mul2, constant.BONE), "fail") + }(uint256.NewInt(2), uint256.NewInt(3)) + + func(gamma, ETHER, xp0, xp1, f *uint256.Int) { + // f = gamma.mul(ETHER).div( + // gamma.add(ETHER).sub(ETHER.mul(4).mul(xp0).div(f).mul(xp1).div(f)) + // ); + f1 := new(uint256.Int).Set(f) + assert.Equal(t, new(uint256.Int).Div( + new(uint256.Int).Mul( + gamma, + constant.BONE, + ), + new(uint256.Int).Sub( + new(uint256.Int).Add( + gamma, + constant.BONE, + ), + new(uint256.Int).Div( + new(uint256.Int).Mul( + new(uint256.Int).Div( + new(uint256.Int).Mul( + new(uint256.Int).Mul( + constant.BONE, + constant.Four, + ), + xp0, + ), + f, + ), + xp1, + ), + f, + ), + ), + ), f.Mul(constant.BONE, constant.Four).Mul(f, xp0).Div(f, f1).Mul(f, xp1).Div(f, f1).Sub( + new(uint256.Int).Add(gamma, constant.BONE), f, + ).Div( + new(uint256.Int).Mul(gamma, constant.BONE), f, + ), "fail") + }(uint256.NewInt(2), uint256.NewInt(3), uint256.NewInt(4), uint256.NewInt(5), uint256.NewInt(6)) + + func(minFee, maxFee, f *uint256.Int) { + var fee = new(uint256.Int) + assert.Equal(t, new(uint256.Int).Div( + new(uint256.Int).Add( + new(uint256.Int).Mul( + minFee, f, + ), + new(uint256.Int).Mul( + maxFee, + new(uint256.Int).Sub( + constant.BONE, f, + ), + ), + ), + constant.BONE, + ), fee.Sub(constant.BONE, f).Mul(maxFee, fee).Add(fee, new(uint256.Int).Mul(minFee, f)).Div(fee, constant.BONE), "fail") + }(uint256.NewInt(2), uint256.NewInt(3), uint256.NewInt(4)) +} diff --git a/pkg/liquidity-source/syncswapv2/stable/math.go b/pkg/liquidity-source/syncswapv2/stable/math.go index e6797e0dd..6c67ad2ba 100644 --- a/pkg/liquidity-source/syncswapv2/stable/math.go +++ b/pkg/liquidity-source/syncswapv2/stable/math.go @@ -56,7 +56,7 @@ func _getAmountIn( // amountIn = MAX_FEE * (x - adjustedReserveIn) / (MAX_FEE - swapFee) + 1; // amountIn /= tokenInPrecisionMultiplier; amountIn := new(uint256.Int) - amountIn.Set(x).Sub(amountIn, adjustedReserveIn).Mul(amountIn, MaxFee).Div( + amountIn.Sub(x, adjustedReserveIn).Mul(amountIn, MaxFee).Div( amountIn, new(uint256.Int).Sub(MaxFee, swapFee), ).Add(amountIn, constant.One).Div(amountIn, tokenInPrecisionMultiplier) @@ -126,18 +126,16 @@ func computeDFromAdjustedBalances(xp0, xp1 *uint256.Int, A *uint256.Int) *uint25 //d = (((2000 * s) + 2 * dP) * d) / ((2000 - 1) * d + 3 * dP); num := new(uint256.Int) den := new(uint256.Int) - d = num.Div( - num.Mul( - num.Add( - num.Set(A).Mul(num, s), - new(uint256.Int).Mul(constant.Two, dp), - ), d), - den.Add( - den.Mul(den.Set(A).Sub(den, constant.One), d), + d = num.Mul(A, s).Add( + num, + new(uint256.Int).Mul(constant.Two, dp), + ).Mul(num, d).Div( + num, + den.Sub(A, constant.One).Mul(den, d).Add( + den, new(uint256.Int).Mul(constant.Three, dp), ), ) - if within1(d, prevD) { break } diff --git a/pkg/liquidity-source/syncswapv2/stable/math_test.go b/pkg/liquidity-source/syncswapv2/stable/math_test.go new file mode 100644 index 000000000..20140f9d6 --- /dev/null +++ b/pkg/liquidity-source/syncswapv2/stable/math_test.go @@ -0,0 +1,64 @@ +package syncswapv2stable + +import ( + "testing" + + constant "github.com/KyberNetwork/kyberswap-dex-lib/pkg/util/big256" + "github.com/holiman/uint256" + "github.com/stretchr/testify/assert" +) + +func TestUint256Refactor(t *testing.T) { + func(A, s, dp, d *uint256.Int) { + num := new(uint256.Int) + den := new(uint256.Int) + assert.Equal(t, new(uint256.Int).Div( + new(uint256.Int).Mul( + new(uint256.Int).Add( + new(uint256.Int).Mul(A, s), + new(uint256.Int).Mul(uint256.NewInt(2), dp), + ), d), + new(uint256.Int).Add( + new(uint256.Int).Mul(new(uint256.Int).Sub(A, uint256.NewInt(1)), d), + new(uint256.Int).Mul(uint256.NewInt(3), dp), + ), + ), num.Mul(A, s).Add( + num, + new(uint256.Int).Mul(constant.Two, dp), + ).Mul(num, d).Div( + num, + den.Sub(A, constant.One).Mul(den, d).Add( + den, + new(uint256.Int).Mul(constant.Three, dp), + ), + ), "fail") + }(uint256.NewInt(2), uint256.NewInt(3), uint256.NewInt(4), uint256.NewInt(5)) + + func(x, adjustedReserveIn, MaxFee, swapFee, tokenInPrecisionMultiplier *uint256.Int) { + amountIn := new(uint256.Int) + assert.Equal(t, new(uint256.Int).Div(new(uint256.Int).Add( + new(uint256.Int).Div( + new(uint256.Int).Mul( + MaxFee, + new(uint256.Int).Sub(x, adjustedReserveIn), + ), + new(uint256.Int).Sub(MaxFee, swapFee), + ), + uint256.NewInt(1), + ), tokenInPrecisionMultiplier), + amountIn.Sub(x, adjustedReserveIn).Mul(amountIn, MaxFee).Div( + amountIn, + new(uint256.Int).Sub(MaxFee, swapFee), + ).Add(amountIn, constant.One).Div(amountIn, tokenInPrecisionMultiplier), "fail") + }(uint256.NewInt(2), uint256.NewInt(3), uint256.NewInt(4), uint256.NewInt(5), uint256.NewInt(6)) + + func(d, xp0, xp1 *uint256.Int) { + var dp = new(uint256.Int) + assert.Equal(t, new(uint256.Int).Div( + new(uint256.Int).Div( + new(uint256.Int).Mul( + new(uint256.Int).Div( + new(uint256.Int).Mul(d, d), xp0), d), xp1), uint256.NewInt(4)), + dp.Set(d).Mul(dp, d).Div(dp, xp0).Mul(dp, d).Div(dp, xp1).Div(dp, constant.Four), "fail") + }(uint256.NewInt(2), uint256.NewInt(3), uint256.NewInt(4)) +}