github.com/fibonacci-chain/fbc@v0.0.0-20231124064014-c7636198c1e9/app/utils/int.go (about)

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