Skip to content

Commit

Permalink
Suggestion to allow better precision for exchange rates (#47)
Browse files Browse the repository at this point in the history
* Allow more decimals in exchange rate

* Fix import

* Fix truncation

* Fix math

* Update oracle.go
  • Loading branch information
StrathCole authored Oct 8, 2024
1 parent 8c605e6 commit be13684
Showing 1 changed file with 23 additions and 3 deletions.
26 changes: 23 additions & 3 deletions feeder/priceposter/oracle.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@ package priceposter
import (
"context"
"crypto/rand"
"fmt"
"strconv"
"strings"
"math/big"

oracletypes "github.com/NibiruChain/nibiru/x/oracle/types"
Expand Down Expand Up @@ -121,6 +122,25 @@ func newPrevote(prices []types.Price, validator sdk.ValAddress, feeder sdk.AccAd
}

func float64ToDec(price float64) sdk.Dec {
// TODO(mercilex): precision for numbers with a lot of decimal digits
return sdk.MustNewDecFromStr(fmt.Sprintf("%f", price))
formattedPrice := strconv.FormatFloat(price, 'f', -1, 64)

parts := strings.Split(formattedPrice, ".")
intPart := parts[0]
decPart := ""

if len(parts) > 1 {
decPart = parts[1]
}

if len(decPart) > 18 {
decPart = decPart[:18]
}

if decPart == "" {
formattedPrice = intPart
} else {
formattedPrice = intPart + "." + decPart
}

return sdk.MustNewDecFromStr(formattedPrice)
}

0 comments on commit be13684

Please sign in to comment.