Skip to content
Open
Show file tree
Hide file tree
Changes from all 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
7 changes: 7 additions & 0 deletions .changeset/utils-todecimal-jsonnumber.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
---
"chainlink": patch
---

Teach `utils.ToDecimal` to convert `json.Number` so pipeline tasks that consume HTTP JSON payloads (ethabiencode, fluxmonitor, OCR data sources) no longer fail with "type json.Number cannot be converted to decimal.Decimal".

#bugfix
9 changes: 9 additions & 0 deletions core/utils/decimal.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package utils

import (
"encoding/json"
"math"
"math/big"

Expand All @@ -14,6 +15,14 @@ import (
// https://github.com/smartcontractkit/chainlink/pull/14841
func ToDecimal(input any) (decimal.Decimal, error) {
switch v := input.(type) {
case json.Number:
// json.Number is what encoding/json returns when Decoder.UseNumber()
// has been set, which is how the pipeline parses HTTP / JSON payloads
// containing numeric values. Without this case any downstream task
// that receives a json.Number (e.g. ethabiencode with a uint256[]
// argument, fluxmonitor, OCR data sources) fails the conversion with
// "type json.Number cannot be converted to decimal.Decimal".
return decimal.NewFromString(string(v))
case string:
return decimal.NewFromString(v)
case int:
Expand Down
7 changes: 7 additions & 0 deletions core/utils/decimal_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package utils

import (
"encoding/json"
"math"
"math/big"
"testing"
Expand Down Expand Up @@ -46,6 +47,12 @@ func TestDecimal(t *testing.T) {
{math.NaN(), true},
{float32(math.NaN()), true},
{true, true},
// json.Number is produced by Decoder.UseNumber, which the pipeline
// uses when decoding JSON HTTP responses. Both integer and floating
// point forms have to be accepted; see #8504.
{json.Number("123"), false},
{json.Number("-1.1"), false},
{json.Number("not-a-number"), true},
}
for _, tc := range tt {
_, err := ToDecimal(tc.v)
Expand Down