cosmos-sdk/types/coin.go

335 lines
7.5 KiB
Go
Raw Normal View History

2018-01-10 20:11:44 -08:00
package types
2016-04-01 15:19:07 -07:00
import (
"fmt"
2018-01-06 12:53:31 -08:00
"regexp"
"sort"
2018-01-06 12:53:31 -08:00
"strconv"
2016-04-01 15:19:07 -07:00
"strings"
)
2017-07-06 05:59:45 -07:00
// Coin hold some amount of one currency
2016-04-01 15:19:07 -07:00
type Coin struct {
Denom string `json:"denom"`
Amount Int `json:"amount"`
}
2018-07-30 17:09:50 -07:00
func NewCoin(denom string, amount Int) Coin {
return Coin{
Denom: denom,
2018-07-10 17:36:50 -07:00
Amount: amount,
}
2016-04-01 15:19:07 -07:00
}
2018-07-30 17:09:50 -07:00
func NewInt64Coin(denom string, amount int64) Coin {
return NewCoin(denom, NewInt(amount))
}
2017-07-12 09:54:07 -07:00
// String provides a human-readable representation of a coin
2016-04-01 15:19:07 -07:00
func (coin Coin) String() string {
2017-03-28 13:32:55 -07:00
return fmt.Sprintf("%v%v", coin.Amount, coin.Denom)
}
2018-03-25 10:35:45 -07:00
// SameDenomAs returns true if the two coins are the same denom
func (coin Coin) SameDenomAs(other Coin) bool {
return (coin.Denom == other.Denom)
}
2017-07-12 09:54:07 -07:00
// IsZero returns if this represents no money
func (coin Coin) IsZero() bool {
return coin.Amount.IsZero()
2017-07-12 09:54:07 -07:00
}
// IsGTE returns true if they are the same type and the receiver is
// an equal or greater value
func (coin Coin) IsGTE(other Coin) bool {
return coin.SameDenomAs(other) && (!coin.Amount.LT(other.Amount))
2018-03-25 10:35:45 -07:00
}
// IsEqual returns true if the two sets of Coins have the same value
func (coin Coin) IsEqual(other Coin) bool {
return coin.SameDenomAs(other) && (coin.Amount.Equal(other.Amount))
2018-03-25 10:35:45 -07:00
}
// IsPositive returns true if coin amount is positive
func (coin Coin) IsPositive() bool {
return (coin.Amount.Sign() == 1)
2018-03-25 10:35:45 -07:00
}
// IsNotNegative returns true if coin amount is not negative
func (coin Coin) IsNotNegative() bool {
return (coin.Amount.Sign() != -1)
2018-03-25 10:35:45 -07:00
}
// Adds amounts of two coins with same denom
func (coin Coin) Plus(coinB Coin) Coin {
if !coin.SameDenomAs(coinB) {
return coin
}
return Coin{coin.Denom, coin.Amount.Add(coinB.Amount)}
2018-03-25 10:35:45 -07:00
}
// Subtracts amounts of two coins with same denom
func (coin Coin) Minus(coinB Coin) Coin {
if !coin.SameDenomAs(coinB) {
return coin
}
return Coin{coin.Denom, coin.Amount.Sub(coinB.Amount)}
2017-07-12 09:54:07 -07:00
}
2016-04-01 15:19:07 -07:00
//----------------------------------------
2017-12-21 03:26:40 -08:00
// Coins
2016-04-01 15:19:07 -07:00
2017-07-06 05:59:45 -07:00
// Coins is a set of Coin, one per currency
2016-04-01 15:19:07 -07:00
type Coins []Coin
func (coins Coins) String() string {
if len(coins) == 0 {
return ""
}
out := ""
for _, coin := range coins {
2017-03-28 13:32:55 -07:00
out += fmt.Sprintf("%v,", coin.String())
}
2017-04-13 18:33:39 -07:00
return out[:len(out)-1]
}
2017-07-06 05:59:45 -07:00
// IsValid asserts the Coins are sorted, and don't have 0 amounts
2016-04-01 15:19:07 -07:00
func (coins Coins) IsValid() bool {
switch len(coins) {
case 0:
return true
case 1:
2018-03-25 10:35:45 -07:00
return !coins[0].IsZero()
2016-04-01 15:19:07 -07:00
default:
lowDenom := coins[0].Denom
for _, coin := range coins[1:] {
if coin.Denom <= lowDenom {
return false
}
2018-03-25 10:35:45 -07:00
if coin.IsZero() {
2016-04-01 15:19:07 -07:00
return false
}
2017-01-31 03:24:49 -08:00
// we compare each coin against the last denom
lowDenom = coin.Denom
2016-04-01 15:19:07 -07:00
}
return true
}
}
2018-01-23 04:10:06 -08:00
// Plus combines two sets of coins
2018-01-23 07:22:14 -08:00
// CONTRACT: Plus will never return Coins where one Coin has a 0 amount.
2017-07-06 20:37:45 -07:00
func (coins Coins) Plus(coinsB Coins) Coins {
2018-04-07 00:02:00 -07:00
sum := ([]Coin)(nil)
2016-04-01 15:19:07 -07:00
indexA, indexB := 0, 0
2017-07-06 20:37:45 -07:00
lenA, lenB := len(coins), len(coinsB)
2016-04-01 15:19:07 -07:00
for {
if indexA == lenA {
if indexB == lenB {
return sum
}
2017-07-06 05:59:45 -07:00
return append(sum, coinsB[indexB:]...)
2016-04-01 15:19:07 -07:00
} else if indexB == lenB {
2017-07-06 20:37:45 -07:00
return append(sum, coins[indexA:]...)
2016-04-01 15:19:07 -07:00
}
2017-07-06 20:37:45 -07:00
coinA, coinB := coins[indexA], coinsB[indexB]
2016-04-01 15:19:07 -07:00
switch strings.Compare(coinA.Denom, coinB.Denom) {
case -1:
sum = append(sum, coinA)
2017-07-06 05:59:45 -07:00
indexA++
2016-04-01 15:19:07 -07:00
case 0:
if coinA.Amount.Add(coinB.Amount).IsZero() {
2016-04-01 15:19:07 -07:00
// ignore 0 sum coin type
} else {
2018-03-25 10:35:45 -07:00
sum = append(sum, coinA.Plus(coinB))
2016-04-01 15:19:07 -07:00
}
2017-07-06 05:59:45 -07:00
indexA++
indexB++
2016-04-01 15:19:07 -07:00
case 1:
sum = append(sum, coinB)
2017-07-06 05:59:45 -07:00
indexB++
2016-04-01 15:19:07 -07:00
}
}
}
2017-07-06 05:59:45 -07:00
// Negative returns a set of coins with all amount negative
2016-04-01 15:19:07 -07:00
func (coins Coins) Negative() Coins {
res := make([]Coin, 0, len(coins))
for _, coin := range coins {
res = append(res, Coin{
Denom: coin.Denom,
Amount: coin.Amount.Neg(),
2016-04-01 15:19:07 -07:00
})
}
return res
}
2017-07-06 05:59:45 -07:00
// Minus subtracts a set of coins from another (adds the inverse)
2017-07-06 20:37:45 -07:00
func (coins Coins) Minus(coinsB Coins) Coins {
return coins.Plus(coinsB.Negative())
2016-04-01 15:19:07 -07:00
}
2017-07-06 20:37:45 -07:00
// IsGTE returns True iff coins is NonNegative(), and for every
2017-07-06 05:59:45 -07:00
// currency in coinsB, the currency is present at an equal or greater
// amount in coinsB
2017-07-06 20:37:45 -07:00
func (coins Coins) IsGTE(coinsB Coins) bool {
diff := coins.Minus(coinsB)
2016-04-01 15:19:07 -07:00
if len(diff) == 0 {
return true
}
2018-01-03 17:20:21 -08:00
return diff.IsNotNegative()
2016-04-01 15:19:07 -07:00
}
2017-07-06 05:59:45 -07:00
// IsZero returns true if there are no coins
2018-03-17 13:20:24 -07:00
// or all coins are zero.
2016-04-01 15:19:07 -07:00
func (coins Coins) IsZero() bool {
2018-03-17 13:20:24 -07:00
for _, coin := range coins {
if !coin.IsZero() {
return false
}
}
return true
2016-04-01 15:19:07 -07:00
}
2017-07-06 05:59:45 -07:00
// IsEqual returns true if the two sets of Coins have the same value
2017-07-06 20:37:45 -07:00
func (coins Coins) IsEqual(coinsB Coins) bool {
if len(coins) != len(coinsB) {
2016-04-01 15:19:07 -07:00
return false
}
2017-07-06 20:37:45 -07:00
for i := 0; i < len(coins); i++ {
if coins[i].Denom != coinsB[i].Denom || !coins[i].Amount.Equal(coinsB[i].Amount) {
2016-04-01 15:19:07 -07:00
return false
}
}
return true
}
2017-07-06 05:59:45 -07:00
// IsPositive returns true if there is at least one coin, and all
// currencies have a positive value
2016-04-01 15:19:07 -07:00
func (coins Coins) IsPositive() bool {
if len(coins) == 0 {
return false
}
2018-03-25 10:35:45 -07:00
for _, coin := range coins {
if !coin.IsPositive() {
2016-04-01 15:19:07 -07:00
return false
}
}
return true
}
2018-01-03 17:20:21 -08:00
// IsNotNegative returns true if there is no currency with a negative value
2017-07-06 05:59:45 -07:00
// (even no coins is true here)
2018-01-03 17:20:21 -08:00
func (coins Coins) IsNotNegative() bool {
if len(coins) == 0 {
return true
}
2018-03-25 10:35:45 -07:00
for _, coin := range coins {
if !coin.IsNotNegative() {
return false
}
}
return true
}
2018-02-04 16:59:11 -08:00
// Returns the amount of a denom from coins
func (coins Coins) AmountOf(denom string) Int {
2018-01-25 16:30:49 -08:00
switch len(coins) {
case 0:
return ZeroInt()
2018-01-25 16:30:49 -08:00
case 1:
coin := coins[0]
if coin.Denom == denom {
return coin.Amount
}
return ZeroInt()
2018-01-25 16:30:49 -08:00
default:
midIdx := len(coins) / 2 // 2:1, 3:1, 4:2
coin := coins[midIdx]
if denom < coin.Denom {
return coins[:midIdx].AmountOf(denom)
2018-01-25 16:30:49 -08:00
} else if denom == coin.Denom {
return coin.Amount
} else {
return coins[midIdx+1:].AmountOf(denom)
2018-01-25 16:30:49 -08:00
}
}
}
2017-12-21 03:26:40 -08:00
//----------------------------------------
// Sort interface
2017-07-06 05:59:45 -07:00
//nolint
func (coins Coins) Len() int { return len(coins) }
func (coins Coins) Less(i, j int) bool { return coins[i].Denom < coins[j].Denom }
func (coins Coins) Swap(i, j int) { coins[i], coins[j] = coins[j], coins[i] }
var _ sort.Interface = Coins{}
// Sort is a helper function to sort the set of coins inplace
2018-04-05 04:55:10 -07:00
func (coins Coins) Sort() Coins {
sort.Sort(coins)
return coins
}
2017-12-25 00:57:07 -08:00
2018-01-06 12:53:31 -08:00
//----------------------------------------
// Parsing
var (
// Denominations can be 3 ~ 16 characters long.
reDnm = `[[:alpha:]][[:alnum:]]{2,15}`
reAmt = `[[:digit:]]+`
reSpc = `[[:space:]]*`
reCoin = regexp.MustCompile(fmt.Sprintf(`^(%s)%s(%s)$`, reAmt, reSpc, reDnm))
)
// ParseCoin parses a cli input for one coin type, returning errors if invalid.
// This returns an error on an empty string as well.
func ParseCoin(coinStr string) (coin Coin, err error) {
coinStr = strings.TrimSpace(coinStr)
matches := reCoin.FindStringSubmatch(coinStr)
if matches == nil {
err = fmt.Errorf("invalid coin expression: %s", coinStr)
2018-01-06 12:53:31 -08:00
return
}
denomStr, amountStr := matches[2], matches[1]
amount, err := strconv.Atoi(amountStr)
if err != nil {
return
}
return Coin{denomStr, NewInt(int64(amount))}, nil
2018-01-06 12:53:31 -08:00
}
// ParseCoins will parse out a list of coins separated by commas.
// If nothing is provided, it returns nil Coins.
// Returned coins are sorted.
func ParseCoins(coinsStr string) (coins Coins, err error) {
coinsStr = strings.TrimSpace(coinsStr)
if len(coinsStr) == 0 {
return nil, nil
}
coinStrs := strings.Split(coinsStr, ",")
for _, coinStr := range coinStrs {
coin, err := ParseCoin(coinStr)
if err != nil {
return nil, err
}
coins = append(coins, coin)
}
// Sort coins for determinism.
coins.Sort()
// Validate coins before returning.
if !coins.IsValid() {
return nil, fmt.Errorf("parseCoins invalid: %#v", coins)
2018-01-06 12:53:31 -08:00
}
return coins, nil
}