diff --git a/CHANGELOG.md b/CHANGELOG.md index 370ac2774..b8bb02992 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -133,6 +133,7 @@ v0.47.5. * [#1614](https://github.com/NibiruChain/nibiru/pull/1614) - refactor(proto): Use explicit namespacing on proto imports for #1608 * [#1610](https://github.com/NibiruChain/nibiru/pull/1610) - refactor(app): Simplify app.go with less redundant imports using struct embedding. * [#1606](https://github.com/NibiruChain/nibiru/pull/1606) - fix(perp): emit `MarketUpdatedEvent` in the absence of index price +* [#1700](https://github.com/NibiruChain/nibiru/pull/1700) - fix(oracle): remove non deterministic sqrt calculation ### Dependencies diff --git a/x/oracle/types/ballot.go b/x/oracle/types/ballot.go index e0784ef21..7081caef1 100644 --- a/x/oracle/types/ballot.go +++ b/x/oracle/types/ballot.go @@ -2,13 +2,11 @@ package types import ( "encoding/json" - "fmt" - "math" "sort" - "strconv" sdk "github.com/cosmos/cosmos-sdk/types" + "github.com/NibiruChain/nibiru/x/common" "github.com/NibiruChain/nibiru/x/common/asset" ) @@ -149,9 +147,10 @@ func (pb ExchangeRateVotes) StandardDeviation(median sdk.Dec) (standardDeviation variance := sum.QuoInt64(int64(n)) - floatNum, _ := strconv.ParseFloat(variance.String(), 64) - floatNum = math.Sqrt(floatNum) - standardDeviation, _ = sdk.NewDecFromStr(fmt.Sprintf("%f", floatNum)) + standardDeviation, err := common.SqrtDec(variance) + if err != nil { + return sdk.ZeroDec() + } return } diff --git a/x/oracle/types/ballot_test.go b/x/oracle/types/ballot_test.go index 58de574bc..9a2e5fbb0 100644 --- a/x/oracle/types/ballot_test.go +++ b/x/oracle/types/ballot_test.go @@ -242,21 +242,21 @@ func TestPBStandardDeviation(t *testing.T) { []float64{1.0, 2.0, 10.0, 100000.0}, []int64{1, 1, 100, 1}, []bool{true, true, true, true}, - sdk.NewDecWithPrec(4999500036300, types.OracleDecPrecision), + sdk.MustNewDecFromStr("49995.000362536000000000"), }, { // Adding fake validator doesn't change outcome []float64{1.0, 2.0, 10.0, 100000.0, 10000000000}, []int64{1, 1, 100, 1, 10000}, []bool{true, true, true, true, false}, - sdk.NewDecWithPrec(447213595075100600, types.OracleDecPrecision), + sdk.MustNewDecFromStr("4472135950.751005519000000000"), }, { // Tie votes []float64{1.0, 2.0, 3.0, 4.0}, []int64{1, 100, 100, 1}, []bool{true, true, true, true}, - sdk.NewDecWithPrec(122474500, types.OracleDecPrecision), + sdk.MustNewDecFromStr("1.224744871000000000"), }, { // No votes @@ -270,7 +270,7 @@ func TestPBStandardDeviation(t *testing.T) { []float64{1.0, 2.0, 10.0, 100000.0, -99999999999.0, 0}, []int64{1, 1, 100, 1, 1, 1}, []bool{true, true, true, true, true, true}, - sdk.NewDecWithPrec(4999500036300, types.OracleDecPrecision), + sdk.MustNewDecFromStr("49995.000362536000000000"), }, }