Skip to content

Commit 0491627

Browse files
99adarshavkr003
andauthored
Move from LegacyDec to osmomath BigDec for internal calculations (#1231)
* replacing price by osmosmath BigDec * further fixes * amm: move from LegacyDec to osmomath BigDec * commitment: move from LegacyDec to osmomath BigDec * estaking: move from LegacyDec to osmomath BigDec * leveragelp: move LegacyDec to osmoBigDec * masterchef: move LegacyDec to osmoBigDec * perpetual: move LegacyDec to osmoBigDec * stablestake: move LegacyDec to osmoBigDec * tier: move LegacyDec to osmoBigDec * tradeshield: move LegacyDec to osmoBigDec * more changes * fix interest rate hour usd calculation * test fix * fixes * refactoring * optimizing pow 10 function * optimizing power functions * fix test case * optimizing further GetDenomPrice * refactor * add comment for using MaxBigdec * adding constants * reordering multiplication * reordering multiplication * removing redundant function * simplify addition --------- Co-authored-by: avkr003 <abhinav@elys.network>
1 parent 4522f84 commit 0491627

File tree

254 files changed

+3983
-2191
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

254 files changed

+3983
-2191
lines changed

go.mod

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ toolchain go1.23.1
77
require (
88
cosmossdk.io/api v0.7.6
99
cosmossdk.io/client/v2 v2.0.0-beta.7
10-
cosmossdk.io/core v0.11.1
10+
cosmossdk.io/core v0.12.0
1111
cosmossdk.io/errors v1.0.1
1212
cosmossdk.io/log v1.5.0
1313
cosmossdk.io/math v1.4.0
@@ -29,6 +29,7 @@ require (
2929
github.com/cosmos/ibc-go/v8 v8.7.0
3030
github.com/cosmos/interchain-security/v6 v6.3.0
3131
github.com/gorilla/mux v1.8.1
32+
github.com/osmosis-labs/osmosis/osmomath v0.0.17
3233
github.com/prometheus/client_golang v1.20.5 // indirect
3334
github.com/spf13/cast v1.7.1
3435
github.com/spf13/cobra v1.8.1
@@ -143,13 +144,13 @@ require (
143144
github.com/gofrs/uuid/v5 v5.1.0 // indirect
144145
github.com/gogo/googleapis v1.4.1 // indirect
145146
github.com/gogo/protobuf v1.3.3 // indirect
146-
github.com/golang/glog v1.2.3 // indirect
147+
github.com/golang/glog v1.2.4 // indirect
147148
github.com/golang/groupcache v0.0.0-20210331224755-41bb18bfe9da // indirect
148149
github.com/golang/mock v1.6.0 // indirect
149150
github.com/golang/snappy v0.0.5-0.20220116011046-fa5810519dcb // indirect
150151
github.com/google/btree v1.1.3 // indirect
151152
github.com/google/cel-go v0.20.1 // indirect
152-
github.com/google/flatbuffers v2.0.8+incompatible // indirect
153+
github.com/google/flatbuffers v23.5.26+incompatible // indirect
153154
github.com/google/go-cmp v0.6.0 // indirect
154155
github.com/google/go-containerregistry v0.19.1 // indirect
155156
github.com/google/gofuzz v1.2.0 // indirect
@@ -183,7 +184,7 @@ require (
183184
github.com/jmespath/go-jmespath v0.4.0 // indirect
184185
github.com/jmhodges/levigo v1.0.0 // indirect
185186
github.com/klauspost/compress v1.17.9 // indirect
186-
github.com/klauspost/cpuid/v2 v2.0.9 // indirect
187+
github.com/klauspost/cpuid/v2 v2.2.5 // indirect
187188
github.com/klauspost/pgzip v1.2.6 // indirect
188189
github.com/kr/pretty v0.3.1 // indirect
189190
github.com/kr/text v0.2.0 // indirect
@@ -271,6 +272,7 @@ require (
271272
)
272273

273274
replace (
275+
cosmossdk.io/core => cosmossdk.io/core v0.11.1
274276
github.com/99designs/keyring => github.com/cosmos/keyring v1.2.0
275277
github.com/bandprotocol/bandchain-packet => github.com/elys-network/bandchain-packet v0.0.3-sdk47
276278

go.sum

Lines changed: 1275 additions & 39 deletions
Large diffs are not rendered by default.

utils/constants.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
package utils
2+
3+
import "github.com/osmosis-labs/osmosis/osmomath"
4+
5+
var (
6+
HoursInYear = osmomath.NewBigDec(365 * 24)
7+
)

utils/math.go

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
package utils
2+
3+
import (
4+
sdkmath "cosmossdk.io/math"
5+
"github.com/osmosis-labs/osmosis/osmomath"
6+
)
7+
8+
var pow10Int64Cache = [...]int64{
9+
1,
10+
10,
11+
100,
12+
1000,
13+
10000,
14+
100000,
15+
1000000,
16+
10000000,
17+
100000000,
18+
1000000000,
19+
10000000000,
20+
100000000000,
21+
1000000000000,
22+
10000000000000,
23+
100000000000000,
24+
1000000000000000,
25+
10000000000000000,
26+
100000000000000000,
27+
1000000000000000000,
28+
}
29+
30+
func Pow10(decimal uint64) osmomath.BigDec {
31+
if decimal <= 18 {
32+
return osmomath.NewBigDec(pow10Int64Cache[decimal])
33+
}
34+
35+
// This case less likely to happen
36+
value := osmomath.NewBigDec(1)
37+
for i := 0; i < int(decimal); i++ {
38+
value = value.MulInt64(10)
39+
}
40+
return value
41+
}
42+
43+
func Pow10Int64(decimal uint64) int64 {
44+
if decimal <= 18 {
45+
return pow10Int64Cache[decimal]
46+
} else {
47+
panic("cannot do more than 10^18 for int64")
48+
}
49+
}
50+
51+
// AbsDifferenceWithSign returns | a - b |, (a - b).sign()
52+
// a is mutated and returned.
53+
func AbsDifferenceWithSign(a, b sdkmath.LegacyDec) (sdkmath.LegacyDec, bool) {
54+
if a.GTE(b) {
55+
return a.SubMut(b), false
56+
} else {
57+
return a.NegMut().AddMut(b), true
58+
}
59+
}

utils/math_test.go

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
package utils
2+
3+
import (
4+
"testing"
5+
6+
sdkmath "cosmossdk.io/math"
7+
)
8+
9+
func TestAbsDifferenceWithSign(t *testing.T) {
10+
tests := []struct {
11+
a sdkmath.LegacyDec
12+
b sdkmath.LegacyDec
13+
expected sdkmath.LegacyDec
14+
sign bool
15+
}{
16+
{sdkmath.LegacyNewDec(5), sdkmath.LegacyNewDec(3), sdkmath.LegacyNewDec(2), false},
17+
{sdkmath.LegacyNewDec(3), sdkmath.LegacyNewDec(5), sdkmath.LegacyNewDec(2), true},
18+
{sdkmath.LegacyNewDec(0), sdkmath.LegacyNewDec(0), sdkmath.LegacyNewDec(0), false},
19+
}
20+
21+
for _, tt := range tests {
22+
result, sign := AbsDifferenceWithSign(tt.a, tt.b)
23+
if !result.Equal(tt.expected) || sign != tt.sign {
24+
t.Errorf("AbsDifferenceWithSign(%s, %s) = (%s, %v); want (%s, %v)", tt.a, tt.b, result, sign, tt.expected, tt.sign)
25+
}
26+
}
27+
}
Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,30 @@
1-
package types
1+
package utils
22

33
import (
44
sdkmath "cosmossdk.io/math"
55
"fmt"
6+
7+
"github.com/osmosis-labs/osmosis/osmomath"
8+
)
9+
10+
var (
11+
oneHalf = sdkmath.LegacyMustNewDecFromStr("0.5")
12+
twoDec = sdkmath.LegacyMustNewDecFromStr("2")
13+
ln2 = sdkmath.LegacyMustNewDecFromStr("0.693147180559945309")
14+
inverseLn2 = sdkmath.LegacyMustNewDecFromStr("1.442695040888963407")
15+
euler = sdkmath.LegacyMustNewDecFromStr("2.718281828459045235")
16+
powIterationLimit = int64(150_000)
17+
18+
// PowPrecision Don't EVER change after initializing
19+
// TODO: Analyze choice here.
20+
powPrecision = sdkmath.LegacyMustNewDecFromStr("0.00000001")
621
)
722

823
// Pow computes base^(exp)
924
// However since the exponent is not an integer, we must do an approximation algorithm.
1025
// TODO: In the future, lets add some optimized routines for common exponents, e.g. for common wIn / wOut ratios
1126
// Many simple exponents like 2:1 pools.
12-
func Pow(base sdkmath.LegacyDec, exp sdkmath.LegacyDec) sdkmath.LegacyDec {
27+
func Pow(base osmomath.BigDec, exp osmomath.BigDec) osmomath.BigDec {
1328
// Exponentiation of a negative base with an arbitrary real exponent is not closed within the reals.
1429
// You can see this by recalling that `i = (-1)^(.5)`. We have to go to complex numbers to define this.
1530
// (And would have to implement complex logarithms)
@@ -24,16 +39,16 @@ func Pow(base sdkmath.LegacyDec, exp sdkmath.LegacyDec) sdkmath.LegacyDec {
2439
integer := exp.TruncateDec()
2540
fractional := exp.Sub(integer)
2641

27-
integerPow := base.Power(uint64(integer.TruncateInt64()))
42+
integerPow := base.PowerInteger(uint64(integer.TruncateInt64()))
2843

2944
if fractional.IsZero() {
3045
return integerPow
3146
}
3247

33-
fractionalPow, err := powerApproximation(base, fractional)
48+
fractionalPow, err := powerApproximation(base.Dec(), fractional.Dec())
3449
if err != nil {
3550
panic(err)
3651
}
3752

38-
return integerPow.Mul(fractionalPow)
53+
return integerPow.Mul(osmomath.BigDecFromDec(fractionalPow))
3954
}
Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
1-
package types
1+
package utils
22

33
import (
4-
sdkmath "cosmossdk.io/math"
54
"errors"
65
"fmt"
6+
"github.com/osmosis-labs/osmosis/osmomath"
77
"math"
88
"strconv"
9+
10+
sdkmath "cosmossdk.io/math"
911
)
1012

1113
func computeExp(x sdkmath.LegacyDec) (sdkmath.LegacyDec, error) {
@@ -16,13 +18,13 @@ func computeExp(x sdkmath.LegacyDec) (sdkmath.LegacyDec, error) {
1618
return euler, nil
1719
}
1820
// exp(-42) is approx 5.7 x 10^-19, smallest dec possible is 10^-18
19-
if x.LTE(sdkmath.LegacyNewDecFromInt(sdkmath.NewInt(-42))) {
21+
if x.LTE(sdkmath.LegacyNewDec(-42)) {
2022
return sdkmath.LegacyZeroDec(), nil
2123
}
2224

2325
// Range reduction: x = k * ln(2) + y
2426
k := x.Mul(inverseLn2).TruncateInt64()
25-
y := x.Sub(sdkmath.LegacyNewDecFromInt(sdkmath.NewInt(k)).Mul(ln2))
27+
y := x.Sub(sdkmath.LegacyNewDec(k).Mul(ln2))
2628

2729
expY := sdkmath.LegacyOneDec()
2830
term := sdkmath.LegacyOneDec()
@@ -104,6 +106,7 @@ func computeLn(x sdkmath.LegacyDec) (result sdkmath.LegacyDec, err error) {
104106
}
105107

106108
// powerApproximation Check exponentialLogarithmicMethod and maclaurinSeriesApproximation to understand the limits of this function
109+
// Power calculation is currently being used to calculate fees, having precision more than 18 decimal places is not that useful as it's as good as 0
107110
func powerApproximation(base sdkmath.LegacyDec, exp sdkmath.LegacyDec) (sdkmath.LegacyDec, error) {
108111
if !base.IsPositive() {
109112
return sdkmath.LegacyDec{}, errors.New("base must be greater than 0")
@@ -133,7 +136,8 @@ func powerApproximation(base sdkmath.LegacyDec, exp sdkmath.LegacyDec) (sdkmath.
133136
}
134137

135138
if exp.GT(sdkmath.LegacyOneDec()) {
136-
return Pow(base, exp), nil
139+
result := Pow(osmomath.BigDecFromDec(base), osmomath.BigDecFromDec(exp))
140+
return result.Dec(), nil
137141
}
138142

139143
if base.GTE(oneHalf) && base.LT(twoDec) {
Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
1-
package types
1+
package utils
22

33
import (
44
sdkmath "cosmossdk.io/math"
55
"fmt"
6-
"github.com/stretchr/testify/require"
76
"testing"
7+
8+
"github.com/stretchr/testify/require"
89
)
910

1011
func ConditionalPanic(t *testing.T, expectPanic bool, sut func()) {

utils/pow_test.go

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
package utils_test
2+
3+
import (
4+
"github.com/elys-network/elys/utils"
5+
"testing"
6+
7+
"github.com/osmosis-labs/osmosis/osmomath"
8+
"github.com/stretchr/testify/require"
9+
)
10+
11+
func TestPow(t *testing.T) {
12+
pow := utils.Pow(osmomath.NewBigDec(2), osmomath.NewBigDecWithPrec(25, 1)) // 2^2.5
13+
require.Equal(t, pow.String(), "5.656854249492380196000000000000000000")
14+
pow = utils.Pow(osmomath.NewBigDec(10), osmomath.NewBigDecWithPrec(25, 1)) // 10^2.5
15+
require.Equal(t, pow.String(), "316.227766016837933200000000000000000000")
16+
}

x/amm/keeper/abci.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,17 +5,17 @@ import (
55
"strings"
66
"time"
77

8-
sdkmath "cosmossdk.io/math"
98
"github.com/cosmos/cosmos-sdk/telemetry"
109
sdk "github.com/cosmos/cosmos-sdk/types"
10+
"github.com/osmosis-labs/osmosis/osmomath"
1111

1212
"github.com/elys-network/elys/x/amm/types"
1313
)
1414

15-
func (k Keeper) GetStackedSlippage(ctx sdk.Context, poolId uint64) sdkmath.LegacyDec {
15+
func (k Keeper) GetStackedSlippage(ctx sdk.Context, poolId uint64) osmomath.BigDec {
1616
pool, found := k.GetPool(ctx, poolId)
1717
if !found {
18-
return sdkmath.LegacyZeroDec()
18+
return osmomath.ZeroBigDec()
1919
}
2020
snapshot := k.GetAccountedPoolSnapshotOrSet(ctx, pool)
2121
return pool.StackedRatioFromSnapshot(ctx, k.oracleKeeper, &snapshot)

0 commit comments

Comments
 (0)