github.com/lino-network/lino@v0.6.11/types/coin.go (about)

     1  package types
     2  
     3  import (
     4  	"fmt"
     5  	"math"
     6  
     7  	"math/big"
     8  
     9  	sdk "github.com/cosmos/cosmos-sdk/types"
    10  )
    11  
    12  // LNO - exposed type
    13  type LNO = string
    14  
    15  var (
    16  	// LowerBoundRat - the lower bound of Rat
    17  	LowerBoundRat = NewDecFromRat(1, Decimals)
    18  	// UpperBoundRat - the upper bound of Rat
    19  	UpperBoundRat = sdk.NewDec(math.MaxInt64 / Decimals)
    20  )
    21  
    22  // Coin - 10^5 Coin = 1 LNO
    23  type Coin struct {
    24  	// Amount *big.Int `json:"amount"`
    25  	Amount sdk.Int `json:"amount"`
    26  }
    27  
    28  // NewCoin - return coin from sdk.Int.
    29  func NewCoin(amount sdk.Int) Coin {
    30  	return Coin{Amount: amount}
    31  }
    32  
    33  // NewCoinFromInt64 - return int64 amount of Coin
    34  func NewCoinFromInt64(amount int64) Coin {
    35  	// return Coin{big.NewInt(amount)}
    36  	return Coin{sdk.NewInt(amount)}
    37  }
    38  
    39  // NewCoinFromBigInt - return big.Int amount of Coin
    40  func NewCoinFromBigInt(amount *big.Int) Coin {
    41  	sdkInt := sdk.NewIntFromBigInt(amount)
    42  	return Coin{sdkInt}
    43  }
    44  
    45  // NewCoinFromString - return string amount of Coin
    46  func NewCoinFromString(amount string) (Coin, bool) {
    47  	res, ok := sdk.NewIntFromString(amount)
    48  	return Coin{res}, ok
    49  }
    50  
    51  // LinoToCoin - convert 1 LNO to 10^5 Coin
    52  func LinoToCoin(lino LNO) (Coin, sdk.Error) {
    53  	rat, err := sdk.NewDecFromStr(lino)
    54  	if err != nil {
    55  		return NewCoinFromInt64(0), ErrInvalidCoins("Illegal LNO")
    56  	}
    57  	if rat.GT(UpperBoundRat) {
    58  		return NewCoinFromInt64(0), ErrInvalidCoins("LNO overflow")
    59  	}
    60  	if rat.LT(LowerBoundRat) {
    61  		return NewCoinFromInt64(0), ErrInvalidCoins("LNO can't be less than lower bound")
    62  	}
    63  	return DecToCoin(rat.Mul(sdk.NewDec(Decimals))), nil
    64  }
    65  
    66  func MustLinoToCoin(lino LNO) Coin {
    67  	c, err := LinoToCoin(lino)
    68  	if err != nil {
    69  		panic(err)
    70  	}
    71  	return c
    72  }
    73  
    74  // DecToCoin - convert sdk.Dec to LNO coin
    75  // XXX(yumin): the unit of @p rat must be coin.
    76  func DecToCoin(rat sdk.Dec) Coin {
    77  	return NewCoinFromBigInt(rat.RoundInt().BigInt())
    78  }
    79  
    80  // ToDec - convert Coin to sdk.Dec
    81  func (coin Coin) ToDec() sdk.Dec {
    82  	return sdk.NewDecFromBigInt(coin.Amount.BigInt())
    83  }
    84  
    85  // ToInt64 - convert Coin to int64
    86  func (coin Coin) ToInt64() (int64, sdk.Error) {
    87  	if !coin.Amount.BigInt().IsInt64() {
    88  		return 0, ErrAmountOverflow()
    89  	}
    90  	return coin.Amount.BigInt().Int64(), nil
    91  }
    92  
    93  // String - provides a human-readable representation of a coin
    94  func (coin Coin) String() string {
    95  	return fmt.Sprintf("coin:%v", coin.Amount)
    96  }
    97  
    98  // IsZero - returns if this represents no money
    99  func (coin Coin) IsZero() bool {
   100  	return coin.Amount.Sign() == 0
   101  }
   102  
   103  // IsGT - returns true if the receiver is greater value
   104  func (coin Coin) IsGT(other Coin) bool {
   105  	return coin.Amount.GT(other.Amount)
   106  }
   107  
   108  // IsGTE - returns true if they are the same type and the receiver is
   109  // an equal or greater value
   110  func (coin Coin) IsGTE(other Coin) bool {
   111  	return coin.Amount.GT(other.Amount) || coin.Amount.Equal(other.Amount)
   112  }
   113  
   114  // IsEqual - returns true if the two sets of Coins have the same value
   115  func (coin Coin) IsEqual(other Coin) bool {
   116  	return coin.Amount.Equal(other.Amount)
   117  }
   118  
   119  // IsPositive - returns true if coin amount is positive
   120  func (coin Coin) IsPositive() bool {
   121  	return coin.Amount.IsPositive()
   122  }
   123  
   124  // IsPositive - returns true if coin amount is positive
   125  func (coin Coin) IsNegative() bool {
   126  	return coin.Amount.IsNegative()
   127  }
   128  
   129  // IsNotNegative - returns true if coin amount is not negative
   130  func (coin Coin) IsNotNegative() bool {
   131  	return coin.Amount.Sign() >= 0
   132  }
   133  
   134  // Plus - Adds amounts of two coins with same denom
   135  func (coin Coin) Plus(coinB Coin) Coin {
   136  	r := coin.Amount.Add(coinB.Amount)
   137  	return Coin{r}
   138  }
   139  
   140  // Minus - Subtracts amounts of two coins with same denom
   141  func (coin Coin) Minus(coinB Coin) Coin {
   142  	sdkInt := coin.Amount.Sub(coinB.Amount)
   143  	return Coin{sdkInt}
   144  }
   145  
   146  // Neg - return a new neged coin.
   147  func (coin Coin) Neg() Coin {
   148  	return Coin{coin.Amount.Neg()}
   149  }
   150  
   151  // ToLino - return LINO str
   152  func (coin Coin) ToLino() LNO {
   153  	return coin.Amount.QuoRaw(Decimals).String()
   154  }