Repository: osmosis-labs/osmosis
Pull request #3058 (2022-10-19 20:20:28)

CL: basic approx log
Basic approx log from https://github.com/osmosis-labs/osmosis/pull/2788


Patch:
diff --git a/osmomath/decimal.go b/osmomath/decimal.go
index 265f6ada0..761238fd5 100644
--- a/osmomath/decimal.go
+++ b/osmomath/decimal.go
@@ -4,6 +4,7 @@ import (
 	"encoding/json"
 	"errors"
 	"fmt"
+	"math"
 	"math/big"
 	"strconv"
 	"strings"
@@ -39,6 +40,8 @@ var (
 	zeroInt              = big.NewInt(0)
 	oneInt               = big.NewInt(1)
 	tenInt               = big.NewInt(10)
+
+	log2LookupTable map[uint16]BigDec
 )
 
 // Decimal errors
@@ -54,6 +57,28 @@ func init() {
 	for i := 0; i <= Precision; i++ {
 		precisionMultipliers[i] = calcPrecisionMultiplier(int64(i))
 	}
+
+	log2LookupTable = buildLog2LookupTable()
+}
+
+// buildLog2LookupTable returns a lookup table for log values
+// ranging from [1, 2)
+// the keys are multiplied by 10 to simplify the rounding logic
+// in the log function
+func buildLog2LookupTable() map[uint16]BigDec {
+	return map[uint16]BigDec{
+		10000: ZeroDec(),
+		10001: MustNewDecFromStr("0.000144262291094554178391070900057479"),
+		11000: MustNewDecFromStr("0.137503523749934908329043617236402782"),
+		12000: MustNewDecFromStr("0.263034405833793833583419514458426332"),
+		13000: MustNewDecFromStr("0.378511623253729812526493224767304557"),
+		14000: MustNewDecFromStr("0.485426827170241759571649887742440632"),
+		15000: MustNewDecFromStr("0.584962500721156181453738943947816508"),
+		16000: MustNewDecFromStr("0.678071905112637652129680570510609824"),
+		17000: MustNewDecFromStr("0.765534746362977060383746581321014178"),
+		18000: MustNewDecFromStr("0.847996906554950015037158458406242841"),
+		19000: MustNewDecFromStr("0.925999418556223145923199993417444246"),
+	}
 }
 
 func precisionInt() *big.Int {
@@ -211,7 +236,8 @@ func (d BigDec) GTE(d2 BigDec) bool   { return (d.i).Cmp(d2.i) >= 0 }          /
 func (d BigDec) LT(d2 BigDec) bool    { return (d.i).Cmp(d2.i) < 0 }           // less than
 func (d BigDec) LTE(d2 BigDec) bool   { return (d.i).Cmp(d2.i) <= 0 }          // less than or equal
 func (d BigDec) Neg() BigDec          { return BigDec{new(big.Int).Neg(d.i)} } // reverse the decimal sign
-func (d BigDec) Abs() BigDec          { return BigDec{new(big.Int).Abs(d.i)} } // absolute value
+// nolint: stylecheck
+func (d BigDec) Abs() BigDec { return BigDec{new(big.Int).Abs(d.i)} } // absolute value
 
 // BigInt returns a copy of the underlying big.Int.
 func (d BigDec) BigInt() *big.Int {
@@ -856,3 +882,55 @@ func DecApproxEq(t *testing.T, d1 BigDec, d2 BigDec, tol BigDec) (*testing.T, bo
 	diff := d1.Sub(d2).Abs()
 	return t, diff.LTE(tol), "expected |d1 - d2| <:\t%v\ngot |d1 - d2| = \t\t%v", tol.String(), diff.String()
 }
+
+// ApproxLog2 returns the approximation of log_2 {x}.
+// Rounds down by truncating and right shifting during
+// calculations.
+func (x BigDec) ApproxLog2() BigDec {
+	if x.LT(OneDec()) {
+		panic(fmt.Sprintf("only supporting values >= 1, given (%s)", x))
+	}
+
+	// Normalize x to be 1 <= x < 2
+
+	// y is the exponent that results in a whole multiple of 2.
+	y := int64(0)
+
+	// invariant: x >= 1
+	// while x < 1
+	for x.LT(OneDec()) {
+		x.i = x.i.Lsh(x.i, 1)
+		y = y - 1
+	}
+
+	// invariant: x < 2
+	// while x >= 2
+	twoDec := NewBigDec(2)
+	for x.GTE(twoDec) {
+		x.i = x.i.Rsh(x.i, 1)
+		y = y + 1
+	}
+
+	// exponentiate to simplify truncation necessary for
+	// looking up values in the table.
+	lookupKey := x.MulInt64(10000).TruncateInt()
+	if lookupKey.Equal(NewInt(10001)) {
+		tableValue, found := log2LookupTable[uint16(lookupKey.Int64())]
+		if !found {
+			panic(fmt.Sprintf("no matching value for key (%s) in the lookup table", lookupKey))
+		}
+		return NewBigDec(y).Add(tableValue)
+	}
+	lookupKeyInt := lookupKey.ToDec().MustFloat64()
+	roundedLookup := math.Round(lookupKeyInt/1000) * 1000
+	lookupKey, _ = NewIntFromString(fmt.Sprintf("%v", roundedLookup))
+	if lookupKey.GTE(NewInt(20000)) || lookupKey.LT(NewInt(10000)) {
+		panic(fmt.Sprintf("invalid lookup key (%s), must be 10 <= lookup key < 2", lookupKey))
+	}
+
+	tableValue, found := log2LookupTable[uint16(lookupKey.Int64())]
+	if !found {
+		panic(fmt.Sprintf("no matching value for key (%s) in the lookup table", lookupKey))
+	}
+	return NewBigDec(y).Add(tableValue)
+}
diff --git a/x/concentrated-liquidity/pool.go b/x/concentrated-liquidity/pool.go
index 52d6d6ee2..0094986a4 100644
--- a/x/concentrated-liquidity/pool.go
+++ b/x/concentrated-liquidity/pool.go
@@ -32,7 +32,7 @@ func liquidity0(amount int64, pa, pb sdk.Dec) sdk.Dec {
 	if pa.GT(pb) {
 		pa, pb = pb, pa
 	}
-	product := pa.Mul(pb).Quo(sdk.NewDec(10).Power(6))
+	product := pa.Mul(pb)
 	diff := pb.Sub(pa)
 	amt := sdk.NewDec(amount)
 	return amt.Mul(product.Quo(diff))
@@ -47,7 +47,7 @@ func liquidity1(amount int64, pa, pb sdk.Dec) sdk.Dec {
 	}
 	diff := pb.Sub(pa)
 	amt := sdk.NewDec(amount)
-	return amt.Quo(diff).Mul(sdk.NewDec(10).Power(6))
+	return amt.Quo(diff)
 }
 
 // calcAmount0 takes the asset with the smaller liqudity in the pool as well as the sqrtpCur and the nextPrice and calculates the amount of asset 0
@@ -58,7 +58,7 @@ func calcAmount0(liq, pa, pb sdk.Dec) sdk.Dec {
 		pa, pb = pb, pa
 	}
 	diff := pb.Sub(pa)
-	mult := liq.Mul(sdk.NewDec(10).Power(6))
+	mult := liq
 	return (mult.Mul(diff)).Quo(pb).Quo(pa)
 }
 
@@ -70,7 +70,7 @@ func calcAmount1(liq, pa, pb sdk.Dec) sdk.Dec {
 		pa, pb = pb, pa
 	}
 	diff := pb.Sub(pa)
-	return liq.Mul(diff).Quo(sdk.NewDec(10).Power(6))
+	return liq.Mul(diff)
 }
 
 func (p Pool) GetAddress() sdk.AccAddress {
@@ -113,9 +113,9 @@ func (p Pool) CalcOutAmtGivenIn(ctx sdk.Context, tokensIn sdk.Coins, tokenOutDen
 	tokenAmountInAfterFee := tokenIn.Amount.ToDec().Mul(sdk.OneDec().Sub(swapFee))
 
 	// TODO: Replace with spot price
-	priceLower := sdk.NewDec(4500000000)
-	priceCur := sdk.NewDec(5000000000)
-	priceUpper := sdk.NewDec(5500000000)
+	priceLower := sdk.NewDec(4500)
+	priceCur := sdk.NewDec(5000)
+	priceUpper := sdk.NewDec(5500)
 
 	sqrtPLowerTick, _ := priceLower.ApproxSqrt()
 	sqrtPCurTick, _ := priceCur.ApproxSqrt()
@@ -137,13 +137,12 @@ func (p Pool) CalcOutAmtGivenIn(ctx sdk.Context, tokensIn sdk.Coins, tokenOutDen
 		amountOut := calcAmount0(liq, priceNext, sqrtPCurTick)
 		return sdk.NewCoin(tokenOutDenom, amountOut.TruncateInt()), nil
 	} else if tokenIn.Denom == asset0 {
-		priceNextTop := liq.Mul(sdk.NewDec(10).Power(6).Mul(sqrtPCurTick))
-		priceNextBot := liq.Mul(sdk.NewDec(10).Power(6)).Add(tokenAmountInAfterFee.Mul(sqrtPCurTick))
+		priceNextTop := liq.Mul(sqrtPCurTick)
+		priceNextBot := liq.Add(tokenAmountInAfterFee.Mul(sqrtPCurTick))
 		priceNext := priceNextTop.Quo(priceNextBot)
 		// new amount in, will be needed later
 		//amountIn = calcAmount1(liq, priceNext, sqrtPCurTick)
 		amountOut := calcAmount1(liq, priceNext, sqrtPCurTick)
-		amountOut = amountOut.Mul(sdk.NewDec(10).Power(6))
 		return sdk.NewCoin(tokenOutDenom, amountOut.TruncateInt()), nil
 	}
 	return sdk.Coin{}, fmt.Errorf("tokenIn does not match any asset in pool")
@@ -160,9 +159,9 @@ func (p Pool) CalcInAmtGivenOut(ctx sdk.Context, tokensOut sdk.Coins, tokenInDen
 	asset1 := "usdc"
 
 	// TODO: Replace with spot price
-	priceLower := sdk.NewDec(4500000000)
-	priceCur := sdk.NewDec(5000000000)
-	priceUpper := sdk.NewDec(5500000000)
+	priceLower := sdk.NewDec(4500)
+	priceCur := sdk.NewDec(5000)
+	priceUpper := sdk.NewDec(5500)
 
 	sqrtPLowerTick, _ := priceLower.ApproxSqrt()
 	sqrtPCurTick, _ := priceCur.ApproxSqrt()
@@ -188,14 +187,13 @@ func (p Pool) CalcInAmtGivenOut(ctx sdk.Context, tokensOut sdk.Coins, tokenInDen
 		amountIn = amountIn.Quo(sdk.OneDec().Sub(swapFee))
 		return sdk.NewCoin(tokenInDenom, amountIn.TruncateInt()), nil
 	} else if tokenOut.Denom == asset0 {
-		priceNextTop := liq.Mul(sdk.NewDec(10).Power(6).Mul(sqrtPCurTick))
-		priceNextBot := liq.Mul(sdk.NewDec(10).Power(6)).Add(tokenOutAmt.Mul(sqrtPCurTick))
+		priceNextTop := liq.Mul(sqrtPCurTick)
+		priceNextBot := liq.Add(tokenOutAmt.Mul(sqrtPCurTick))
 		priceNext := priceNextTop.Quo(priceNextBot)
 
 		// new amount in, will be needed later
 		// amountIn = calcAmount1(liq, priceNext, sqrtPCurTick)
 		amountIn := calcAmount1(liq, priceNext, sqrtPCurTick)
-		amountIn = amountIn.Mul(sdk.NewDec(10).Power(6))
 		// fee logic
 		amountIn = amountIn.Quo(sdk.OneDec().Sub(swapFee))
 		return sdk.NewCoin(tokenInDenom, amountIn.TruncateInt()), nil


Test patch:
diff --git a/osmomath/decimal_test.go b/osmomath/decimal_test.go
index 1650186dd..92b886d1d 100644
--- a/osmomath/decimal_test.go
+++ b/osmomath/decimal_test.go
@@ -11,6 +11,8 @@ import (
 	"github.com/stretchr/testify/require"
 	"github.com/stretchr/testify/suite"
 	"gopkg.in/yaml.v2"
+
+	"github.com/osmosis-labs/osmosis/v12/app/apptesting/osmoassert"
 )
 
 type decimalTestSuite struct {
@@ -152,8 +154,8 @@ func (s *decimalTestSuite) TestDecFloat64() {
 
 func (s *decimalTestSuite) TestSdkDec() {
 	tests := []struct {
-		d    BigDec
-		want sdk.Dec
+		d        BigDec
+		want     sdk.Dec
 		expPanic bool
 	}{
 		{NewBigDec(0), sdk.MustNewDecFromStr("0.000000000000000000"), false},
@@ -177,8 +179,8 @@ func (s *decimalTestSuite) TestSdkDec() {
 
 func (s *decimalTestSuite) TestBigDecFromSdkDec() {
 	tests := []struct {
-		d    sdk.Dec
-		want BigDec
+		d        sdk.Dec
+		want     BigDec
 		expPanic bool
 	}{
 		{sdk.MustNewDecFromStr("0.000000000000000000"), NewBigDec(0), false},
@@ -441,14 +443,14 @@ func (s *decimalTestSuite) TestDecCeil() {
 		input    BigDec
 		expected BigDec
 	}{
-		{MustNewDecFromStr("0.001"), NewBigDec(1)}, // 0.001 => 1.0
-		{MustNewDecFromStr("-0.001"), ZeroDec()},   // -0.001 => 0.0
-		{ZeroDec(), ZeroDec()}, // 0.0 => 0.0
-		{MustNewDecFromStr("0.9"), NewBigDec(1)},    // 0.9 => 1.0
+		{MustNewDecFromStr("0.001"), NewBigDec(1)},   // 0.001 => 1.0
+		{MustNewDecFromStr("-0.001"), ZeroDec()},     // -0.001 => 0.0
+		{ZeroDec(), ZeroDec()},                       // 0.0 => 0.0
+		{MustNewDecFromStr("0.9"), NewBigDec(1)},     // 0.9 => 1.0
 		{MustNewDecFromStr("4.001"), NewBigDec(5)},   // 4.001 => 5.0
 		{MustNewDecFromStr("-4.001"), NewBigDec(-4)}, // -4.001 => -4.0
-		{MustNewDecFromStr("4.7"), NewBigDec(5)},   // 4.7 => 5.0
-		{MustNewDecFromStr("-4.7"), NewBigDec(-4)}, // -4.7 => -4.0
+		{MustNewDecFromStr("4.7"), NewBigDec(5)},     // 4.7 => 5.0
+		{MustNewDecFromStr("-4.7"), NewBigDec(-4)},   // -4.7 => -4.0
 	}
 
 	for i, tc := range testCases {
@@ -463,11 +465,11 @@ func (s *decimalTestSuite) TestPower() {
 		power    uint64
 		expected BigDec
 	}{
-		{OneDec(), 10, OneDec()},                                               // 1.0 ^ (10) => 1.0
-		{NewDecWithPrec(5, 1), 2, NewDecWithPrec(25, 2)},                       // 0.5 ^ 2 => 0.25
-		{NewDecWithPrec(2, 1), 2, NewDecWithPrec(4, 2)},                        // 0.2 ^ 2 => 0.04
-		{NewDecFromInt(NewInt(3)), 3, NewDecFromInt(NewInt(27))},               // 3 ^ 3 => 27
-		{NewDecFromInt(NewInt(-3)), 4, NewDecFromInt(NewInt(81))},              // -3 ^ 4 = 81
+		{OneDec(), 10, OneDec()},                                                                   // 1.0 ^ (10) => 1.0
+		{NewDecWithPrec(5, 1), 2, NewDecWithPrec(25, 2)},                                           // 0.5 ^ 2 => 0.25
+		{NewDecWithPrec(2, 1), 2, NewDecWithPrec(4, 2)},                                            // 0.2 ^ 2 => 0.04
+		{NewDecFromInt(NewInt(3)), 3, NewDecFromInt(NewInt(27))},                                   // 3 ^ 3 => 27
+		{NewDecFromInt(NewInt(-3)), 4, NewDecFromInt(NewInt(81))},                                  // -3 ^ 4 = 81
 		{MustNewDecFromStr("1.414213562373095048801688724209698079"), 2, NewDecFromInt(NewInt(2))}, // 1.414213562373095048801688724209698079 ^ 2 = 2
 	}
 
@@ -483,14 +485,14 @@ func (s *decimalTestSuite) TestApproxRoot() {
 		root     uint64
 		expected BigDec
 	}{
-		{OneDec(), 10, OneDec()},                                                       // 1.0 ^ (0.1) => 1.0
-		{NewDecWithPrec(25, 2), 2, NewDecWithPrec(5, 1)},                               // 0.25 ^ (0.5) => 0.5
-		{NewDecWithPrec(4, 2), 2, NewDecWithPrec(2, 1)},                                // 0.04 ^ (0.5) => 0.2
-		{NewDecFromInt(NewInt(27)), 3, NewDecFromInt(NewInt(3))},                       // 27 ^ (1/3) => 3
-		{NewDecFromInt(NewInt(-81)), 4, NewDecFromInt(NewInt(-3))},                     // -81 ^ (0.25) => -3
-		{NewDecFromInt(NewInt(2)), 2, MustNewDecFromStr("1.414213562373095048801688724209698079")},         // 2 ^ (0.5) => 1.414213562373095048801688724209698079
+		{OneDec(), 10, OneDec()},                                                                         // 1.0 ^ (0.1) => 1.0
+		{NewDecWithPrec(25, 2), 2, NewDecWithPrec(5, 1)},                                                 // 0.25 ^ (0.5) => 0.5
+		{NewDecWithPrec(4, 2), 2, NewDecWithPrec(2, 1)},                                                  // 0.04 ^ (0.5) => 0.2
+		{NewDecFromInt(NewInt(27)), 3, NewDecFromInt(NewInt(3))},                                         // 27 ^ (1/3) => 3
+		{NewDecFromInt(NewInt(-81)), 4, NewDecFromInt(NewInt(-3))},                                       // -81 ^ (0.25) => -3
+		{NewDecFromInt(NewInt(2)), 2, MustNewDecFromStr("1.414213562373095048801688724209698079")},       // 2 ^ (0.5) => 1.414213562373095048801688724209698079
 		{NewDecWithPrec(1005, 3), 31536000, MustNewDecFromStr("1.000000000158153903837946258002096839")}, // 1.005 ^ (1/31536000) ≈ 1.000000000158153903837946258002096839
-		{SmallestDec(), 2, NewDecWithPrec(1, 18)},                                       // 1e-36 ^ (0.5) => 1e-18
+		{SmallestDec(), 2, NewDecWithPrec(1, 18)},                                                        // 1e-36 ^ (0.5) => 1e-18
 		{SmallestDec(), 3, MustNewDecFromStr("0.000000000001000000000000000002431786")},                  // 1e-36 ^ (1/3) => 1e-12
 		{NewDecWithPrec(1, 8), 3, MustNewDecFromStr("0.002154434690031883721759293566519280")},           // 1e-8 ^ (1/3) ≈ 0.002154434690031883721759293566519
 	}
@@ -511,11 +513,11 @@ func (s *decimalTestSuite) TestApproxSqrt() {
 		input    BigDec
 		expected BigDec
 	}{
-		{OneDec(), OneDec()},                                                // 1.0 => 1.0
-		{NewDecWithPrec(25, 2), NewDecWithPrec(5, 1)},                       // 0.25 => 0.5
-		{NewDecWithPrec(4, 2), NewDecWithPrec(2, 1)},                        // 0.09 => 0.3
-		{NewDecFromInt(NewInt(9)), NewDecFromInt(NewInt(3))},                // 9 => 3
-		{NewDecFromInt(NewInt(-9)), NewDecFromInt(NewInt(-3))},              // -9 => -3
+		{OneDec(), OneDec()},                                                                    // 1.0 => 1.0
+		{NewDecWithPrec(25, 2), NewDecWithPrec(5, 1)},                                           // 0.25 => 0.5
+		{NewDecWithPrec(4, 2), NewDecWithPrec(2, 1)},                                            // 0.09 => 0.3
+		{NewDecFromInt(NewInt(9)), NewDecFromInt(NewInt(3))},                                    // 9 => 3
+		{NewDecFromInt(NewInt(-9)), NewDecFromInt(NewInt(-3))},                                  // -9 => -3
 		{NewDecFromInt(NewInt(2)), MustNewDecFromStr("1.414213562373095048801688724209698079")}, // 2 => 1.414213562373095048801688724209698079
 	}
 
@@ -650,3 +652,62 @@ func BenchmarkMarshalTo(b *testing.B) {
 		}
 	}
 }
+
+func (s *decimalTestSuite) TestLog2() {
+
+	tests := map[string]struct {
+		initialValue BigDec
+		expected     BigDec
+
+		expectedPanic bool
+	}{
+		"log_2{0.99}; not supported; panic": {
+			initialValue: NewDecWithPrec(99, 2),
+
+			expectedPanic: true,
+		},
+		"log_2{1} = 0": {
+			initialValue: NewBigDec(1),
+			expected:     NewBigDec(0),
+		},
+		"log_2{2} = 1": {
+			initialValue: NewBigDec(2),
+			expected:     NewBigDec(1),
+		},
+		"log_2{512} = 9": {
+			initialValue: NewBigDec(512),
+			expected:     NewBigDec(9),
+		},
+		"log_2{600} = 9": {
+			initialValue: NewBigDec(580),
+			// TODO: true value is: 9.179909090014934468590092754117374938
+			// Need better lookup table.
+			expected: MustNewDecFromStr("9.137503523749934908329043617236402782"),
+		},
+		"log_2{1024} = 10": {
+			initialValue: NewBigDec(1024),
+			expected:     NewBigDec(10),
+		},
+		"log_2{1024.987654321} = 10": {
+			initialValue: NewDecWithPrec(1024987654321, 9),
+			// TODO: true value is: 10.001390817654141324352719749259888355
+			// Need better lookup table
+			expected: MustNewDecFromStr("10.000000000000000000000000000000000000"),
+		},
+		"log_2{912648174127941279170121098210.92821920190204131121} = 99.525973560175362367047484597337715868": {
+			initialValue: MustNewDecFromStr("912648174127941279170121098210.92821920190204131121"),
+			// TODO: true value is: 99.525973560175362367047484597337715868
+			// Need better lookup table
+			expected: MustNewDecFromStr("99.485426827170241759571649887742440632"),
+		},
+	}
+
+	for name, tc := range tests {
+		s.Run(name, func() {
+			osmoassert.ConditionalPanic(s.T(), tc.expectedPanic, func() {
+				res := tc.initialValue.ApproxLog2()
+				s.Require().Equal(tc.expected, res)
+			})
+		})
+	}
+}
diff --git a/osmomath/log2_bench_test.go b/osmomath/log2_bench_test.go
new file mode 100644
index 000000000..1a723be16
--- /dev/null
+++ b/osmomath/log2_bench_test.go
@@ -0,0 +1,34 @@
+package osmomath
+
+import (
+	"testing"
+)
+
+func BenchmarkLog2(b *testing.B) {
+	tests := []struct {
+		value BigDec
+	}{
+		// TODO: Choose selection here more robustly
+		{
+			value: MustNewDecFromStr("1.2"),
+		},
+		{
+			value: MustNewDecFromStr("1.234"),
+		},
+		{
+			value: MustNewDecFromStr("1024"),
+		},
+		{
+			value: NewBigDec(2048 * 2048 * 2048 * 2048 * 2048),
+		},
+		{
+			value: MustNewDecFromStr("999999999999999999999999999999999999999999999999999999.9122181273612911"),
+		},
+	}
+
+	for i := 0; i < b.N; i++ {
+		for _, test := range tests {
+			test.value.ApproxLog2()
+		}
+	}
+}
diff --git a/x/concentrated-liquidity/pool_test.go b/x/concentrated-liquidity/pool_test.go
index 87cc69964..50b656595 100644
--- a/x/concentrated-liquidity/pool_test.go
+++ b/x/concentrated-liquidity/pool_test.go
@@ -19,7 +19,7 @@ func TestCalcOutAmtGivenIn(t *testing.T) {
 	swapFee := sdk.NewDec(0)
 
 	amountOut, _ := pool.CalcOutAmtGivenIn(ctx, tokensIn, tokenOutDenom, swapFee)
-	require.Equal(t, sdk.NewDec(664366278), amountOut.Amount.ToDec())
+	require.Equal(t, sdk.NewDec(663944647).String(), amountOut.Amount.ToDec().String())
 
 	// test asset b to a logic
 	tokensIn = sdk.NewCoins(sdk.NewCoin("usdc", sdk.NewInt(4199999999)))
@@ -27,7 +27,7 @@ func TestCalcOutAmtGivenIn(t *testing.T) {
 	swapFee = sdk.NewDec(0)
 
 	amountOut, _ = pool.CalcOutAmtGivenIn(ctx, tokensIn, tokenOutDenom, swapFee)
-	require.Equal(t, sdk.NewDec(808398), amountOut.Amount.ToDec())
+	require.Equal(t, sdk.NewDec(805287), amountOut.Amount.ToDec())
 
 	// test with swap fee
 	tokensIn = sdk.NewCoins(sdk.NewCoin("usdc", sdk.NewInt(4199999999)))
@@ -35,7 +35,7 @@ func TestCalcOutAmtGivenIn(t *testing.T) {
 	swapFee = sdk.NewDecWithPrec(2, 2)
 
 	amountOut, _ = pool.CalcOutAmtGivenIn(ctx, tokensIn, tokenOutDenom, swapFee)
-	require.Equal(t, sdk.NewDec(792827), amountOut.Amount.ToDec())
+	require.Equal(t, sdk.NewDec(789834), amountOut.Amount.ToDec())
 }
 
 func TestCalcInAmtGivenOut(t *testing.T) {
@@ -49,7 +49,7 @@ func TestCalcInAmtGivenOut(t *testing.T) {
 
 	amountIn, err := pool.CalcInAmtGivenOut(ctx, tokensOut, tokenInDenom, swapFee)
 	require.NoError(t, err)
-	require.Equal(t, sdk.NewDec(808398), amountIn.Amount.ToDec())
+	require.Equal(t, sdk.NewDec(805287), amountIn.Amount.ToDec())
 
 	// test asset b to a logic
 	tokensOut = sdk.NewCoins(sdk.NewCoin("eth", sdk.NewInt(133700)))
@@ -58,7 +58,7 @@ func TestCalcInAmtGivenOut(t *testing.T) {
 
 	amountIn, err = pool.CalcInAmtGivenOut(ctx, tokensOut, tokenInDenom, swapFee)
 	require.NoError(t, err)
-	require.Equal(t, sdk.NewDec(664366278), amountIn.Amount.ToDec())
+	require.Equal(t, sdk.NewDec(663944647), amountIn.Amount.ToDec())
 
 	// test asset a to b logic
 	tokensOut = sdk.NewCoins(sdk.NewCoin("usdc", sdk.NewInt(4199999999)))
@@ -66,5 +66,5 @@ func TestCalcInAmtGivenOut(t *testing.T) {
 	swapFee = sdk.NewDecWithPrec(2, 2)
 
 	amountIn, _ = pool.CalcInAmtGivenOut(ctx, tokensOut, tokenInDenom, swapFee)
-	require.Equal(t, sdk.NewDec(824896), amountIn.Amount.ToDec())
+	require.Equal(t, sdk.NewDec(821721), amountIn.Amount.ToDec())
 }