-
Notifications
You must be signed in to change notification settings - Fork 0
/
int.go
46 lines (40 loc) · 987 Bytes
/
int.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
package utils
import (
"math/big"
"github.com/tendermint/go-amino"
)
// MarshalBigInt marshalls big int into text string for consistent encoding
func MarshalBigInt(i *big.Int) (string, error) {
bz, err := i.MarshalText()
if err != nil {
return "", err
}
return string(bz), nil
}
// MustMarshalBigInt marshalls big int into text string for consistent encoding.
// It panics if an error is encountered.
func MustMarshalBigInt(i *big.Int) string {
str, err := MarshalBigInt(i)
if err != nil {
panic(err)
}
return str
}
// UnmarshalBigInt unmarshalls string from *big.Int
func UnmarshalBigInt(s string) (*big.Int, error) {
ret := new(big.Int)
err := ret.UnmarshalText(amino.StrToBytes(s))
if err != nil {
return nil, err
}
return ret, nil
}
// MustUnmarshalBigInt unmarshalls string from *big.Int.
// It panics if an error is encountered.
func MustUnmarshalBigInt(s string) *big.Int {
ret, err := UnmarshalBigInt(s)
if err != nil {
panic(err)
}
return ret
}