Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Handle Hex String in EA Telemetry #14827

Merged
merged 24 commits into from
Oct 24, 2024
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/lemon-ads-fix.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"chainlink": patch
---

#added Handle Hex String in Utils.ToDecimal
20 changes: 19 additions & 1 deletion core/utils/decimal.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import (
"math"
"math/big"
"strings"

"github.com/pkg/errors"
"github.com/shopspring/decimal"
Expand All @@ -12,7 +13,17 @@
func ToDecimal(input interface{}) (decimal.Decimal, error) {
switch v := input.(type) {
case string:
return decimal.NewFromString(v)
answer, err := decimal.NewFromString(v)
if err == nil {
return answer, nil
} else {
hexAnswer, hexErr := hexStringToDecimal(v)
if hexErr {
return answer, err
} else {

Check failure on line 23 in core/utils/decimal.go

View workflow job for this annotation

GitHub Actions / lint

indent-error-flow: if block ends with a return statement, so drop this else and outdent its block (revive)
return hexAnswer, nil
}
}
case int:
return decimal.New(int64(v), 0), nil
case int8:
Expand Down Expand Up @@ -56,6 +67,13 @@
}
}

func hexStringToDecimal(hexString string) (decimal.Decimal, bool) {
hexString = strings.TrimPrefix(hexString, "0x")
n := new(big.Int)
_, err := n.SetString(hexString, 16)
return decimal.NewFromBigInt(n, 0), err
}

func validFloat(f float64) bool {
return !math.IsNaN(f) && !math.IsInf(f, 0)
}
4 changes: 4 additions & 0 deletions core/utils/decimal_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,12 +39,16 @@ func TestDecimal(t *testing.T) {
{&dec, false},
{big, false},
{*big, false},
{"0x1abc", false},
{"1abc", false},
{math.Inf(1), true},
{math.Inf(-1), true},
{float32(math.Inf(-1)), true},
{float32(math.Inf(1)), true},
{math.NaN(), true},
{float32(math.NaN()), true},
{"0x1akbc", true},
{"1akbc", true},
{true, true},
}
for _, tc := range tt {
Expand Down
Loading