diff options
Diffstat (limited to 'vendor/github.com/onrik/ethrpc/helpers.go')
-rw-r--r-- | vendor/github.com/onrik/ethrpc/helpers.go | 40 |
1 files changed, 40 insertions, 0 deletions
diff --git a/vendor/github.com/onrik/ethrpc/helpers.go b/vendor/github.com/onrik/ethrpc/helpers.go new file mode 100644 index 000000000..e98030055 --- /dev/null +++ b/vendor/github.com/onrik/ethrpc/helpers.go @@ -0,0 +1,40 @@ +package ethrpc + +import ( + "fmt" + "math/big" + "strconv" + "strings" +) + +// ParseInt parse hex string value to int +func ParseInt(value string) (int, error) { + i, err := strconv.ParseInt(strings.TrimPrefix(value, "0x"), 16, 64) + if err != nil { + return 0, err + } + + return int(i), nil +} + +// ParseBigInt parse hex string value to big.Int +func ParseBigInt(value string) (big.Int, error) { + i := big.Int{} + _, err := fmt.Sscan(value, &i) + + return i, err +} + +// IntToHex convert int to hexadecimal representation +func IntToHex(i int) string { + return fmt.Sprintf("0x%x", i) +} + +// BigToHex covert big.Int to hexadecimal representation +func BigToHex(bigInt big.Int) string { + if bigInt.BitLen() == 0 { + return "0x0" + } + + return "0x" + strings.TrimPrefix(fmt.Sprintf("%x", bigInt.Bytes()), "0") +} |