-
Notifications
You must be signed in to change notification settings - Fork 60
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
offchain - move price getter under internal/pricegetter (#143)
- Loading branch information
Showing
10 changed files
with
185 additions
and
82 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
44 changes: 44 additions & 0 deletions
44
core/services/ocr2/plugins/ccip/internal/parseutil/bigint.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
package parseutil | ||
|
||
import ( | ||
"math/big" | ||
|
||
"github.com/pkg/errors" | ||
"github.com/shopspring/decimal" | ||
) | ||
|
||
func ParseBigIntFromAny(val any) (*big.Int, error) { | ||
if val == nil { | ||
return nil, errors.Errorf("nil value passed") | ||
} | ||
|
||
switch v := val.(type) { | ||
case decimal.Decimal: | ||
return ParseBigIntFromString(v.String()) | ||
case *decimal.Decimal: | ||
return ParseBigIntFromString(v.String()) | ||
case *big.Int: | ||
return v, nil | ||
case string: | ||
return ParseBigIntFromString(v) | ||
case int: | ||
return big.NewInt(int64(v)), nil | ||
case int64: | ||
return big.NewInt(v), nil | ||
case float64: | ||
i := new(big.Int) | ||
big.NewFloat(v).Int(i) | ||
return i, nil | ||
default: | ||
return nil, errors.Errorf("unsupported big int type %T", val) | ||
} | ||
} | ||
|
||
func ParseBigIntFromString(v string) (*big.Int, error) { | ||
valBigInt, success := new(big.Int).SetString(v, 10) | ||
if !success { | ||
return nil, errors.Errorf("unable to convert to integer %s", v) | ||
} | ||
|
||
return valBigInt, nil | ||
} |
43 changes: 43 additions & 0 deletions
43
core/services/ocr2/plugins/ccip/internal/parseutil/bigint_test.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
package parseutil | ||
|
||
import ( | ||
"math/big" | ||
"testing" | ||
|
||
"github.com/shopspring/decimal" | ||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func TestParseBigIntFromAny(t *testing.T) { | ||
decimalVal := decimal.New(123, 0) | ||
|
||
testCases := []struct { | ||
name string | ||
val any | ||
res *big.Int | ||
expErr bool | ||
}{ | ||
{name: "nil", val: nil, expErr: true}, | ||
{name: "string", val: "123", res: big.NewInt(123)}, | ||
{name: "decimal", val: decimal.New(123, 0), res: big.NewInt(123)}, | ||
{name: "decimal pointer", val: &decimalVal, res: big.NewInt(123)}, | ||
{name: "int64", val: int64(123), res: big.NewInt(123)}, | ||
{name: "int", val: 123, res: big.NewInt(123)}, | ||
{name: "float", val: 123.12, res: big.NewInt(123)}, | ||
{name: "uint8", val: uint8(12), expErr: true}, | ||
{name: "struct", val: struct{ name string }{name: "asd"}, expErr: true}, | ||
} | ||
|
||
for _, tc := range testCases { | ||
t.Run(tc.name, func(t *testing.T) { | ||
res, err := ParseBigIntFromAny(tc.val) | ||
if tc.expErr { | ||
assert.Error(t, err) | ||
return | ||
} | ||
assert.NoError(t, err) | ||
assert.Equal(t, tc.res, res) | ||
}) | ||
} | ||
|
||
} |
58 changes: 58 additions & 0 deletions
58
core/services/ocr2/plugins/ccip/internal/pricegetter/mock.go
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.