2018-01-10 20:11:44 -08:00
|
|
|
package types
|
2016-04-01 15:19:07 -07:00
|
|
|
|
|
|
|
import (
|
2019-05-31 08:16:08 -07:00
|
|
|
"encoding/json"
|
2016-04-01 15:19:07 -07:00
|
|
|
"fmt"
|
2018-01-06 12:53:31 -08:00
|
|
|
"regexp"
|
2017-05-22 02:22:41 -07:00
|
|
|
"sort"
|
2016-04-01 15:19:07 -07:00
|
|
|
"strings"
|
|
|
|
)
|
|
|
|
|
2018-11-20 01:22:35 -08:00
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
// Coin
|
|
|
|
|
|
|
|
// Coin hold some amount of one currency.
|
|
|
|
//
|
|
|
|
// CONTRACT: A coin will never hold a negative amount of any denomination.
|
|
|
|
//
|
|
|
|
// TODO: Make field members private for further safety.
|
2016-04-01 15:19:07 -07:00
|
|
|
type Coin struct {
|
2018-11-20 01:22:35 -08:00
|
|
|
Denom string `json:"denom"`
|
|
|
|
|
|
|
|
// To allow the use of unsigned integers (see: #1273) a larger refactor will
|
|
|
|
// need to be made. So we use signed integers for now with safety measures in
|
|
|
|
// place preventing negative values being used.
|
|
|
|
Amount Int `json:"amount"`
|
2018-06-15 14:16:45 -07:00
|
|
|
}
|
|
|
|
|
2018-11-20 01:22:35 -08:00
|
|
|
// NewCoin returns a new coin with a denomination and amount. It will panic if
|
|
|
|
// the amount is negative.
|
2018-07-30 17:09:50 -07:00
|
|
|
func NewCoin(denom string, amount Int) Coin {
|
2019-06-18 09:02:31 -07:00
|
|
|
if err := validate(denom, amount); err != nil {
|
|
|
|
panic(err)
|
2018-11-20 01:22:35 -08:00
|
|
|
}
|
|
|
|
|
2018-06-15 14:16:45 -07:00
|
|
|
return Coin{
|
|
|
|
Denom: denom,
|
2018-07-10 17:36:50 -07:00
|
|
|
Amount: amount,
|
2018-06-15 14:16:45 -07:00
|
|
|
}
|
2016-04-01 15:19:07 -07:00
|
|
|
}
|
|
|
|
|
2018-11-20 01:22:35 -08:00
|
|
|
// NewInt64Coin returns a new coin with a denomination and amount. It will panic
|
|
|
|
// if the amount is negative.
|
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)
|
|
|
|
}
|
|
|
|
|
2019-06-18 09:02:31 -07:00
|
|
|
// validate returns an error if the Coin has a negative amount or if
|
|
|
|
// the denom is invalid.
|
|
|
|
func validate(denom string, amount Int) error {
|
|
|
|
if err := validateDenom(denom); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2019-10-14 13:05:42 -07:00
|
|
|
if amount.IsNegative() {
|
2019-06-18 09:02:31 -07:00
|
|
|
return fmt.Errorf("negative coin amount: %v", amount)
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// IsValid returns true if the Coin has a non-negative amount and the denom is vaild.
|
|
|
|
func (coin Coin) IsValid() bool {
|
|
|
|
if err := validate(coin.Denom, coin.Amount); err != nil {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
2017-07-12 09:54:07 -07:00
|
|
|
// IsZero returns if this represents no money
|
|
|
|
func (coin Coin) IsZero() bool {
|
2018-06-15 14:16:45 -07:00
|
|
|
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 {
|
2019-02-15 07:33:23 -08:00
|
|
|
if coin.Denom != other.Denom {
|
|
|
|
panic(fmt.Sprintf("invalid coin denominations; %s, %s", coin.Denom, other.Denom))
|
|
|
|
}
|
|
|
|
|
|
|
|
return !coin.Amount.LT(other.Amount)
|
2018-03-25 10:35:45 -07:00
|
|
|
}
|
|
|
|
|
2018-09-19 08:25:52 -07:00
|
|
|
// IsLT returns true if they are the same type and the receiver is
|
|
|
|
// a smaller value
|
|
|
|
func (coin Coin) IsLT(other Coin) bool {
|
2019-02-15 07:33:23 -08:00
|
|
|
if coin.Denom != other.Denom {
|
|
|
|
panic(fmt.Sprintf("invalid coin denominations; %s, %s", coin.Denom, other.Denom))
|
|
|
|
}
|
|
|
|
|
|
|
|
return coin.Amount.LT(other.Amount)
|
2018-09-19 08:25:52 -07:00
|
|
|
}
|
|
|
|
|
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 {
|
2019-02-15 07:33:23 -08:00
|
|
|
if coin.Denom != other.Denom {
|
|
|
|
panic(fmt.Sprintf("invalid coin denominations; %s, %s", coin.Denom, other.Denom))
|
|
|
|
}
|
|
|
|
|
|
|
|
return coin.Amount.Equal(other.Amount)
|
2018-03-25 10:35:45 -07:00
|
|
|
}
|
|
|
|
|
2018-11-20 01:22:35 -08:00
|
|
|
// Adds amounts of two coins with same denom. If the coins differ in denom then
|
|
|
|
// it panics.
|
2019-02-21 09:35:55 -08:00
|
|
|
func (coin Coin) Add(coinB Coin) Coin {
|
2019-02-15 07:33:23 -08:00
|
|
|
if coin.Denom != coinB.Denom {
|
2018-11-20 01:22:35 -08:00
|
|
|
panic(fmt.Sprintf("invalid coin denominations; %s, %s", coin.Denom, coinB.Denom))
|
2018-03-25 10:35:45 -07:00
|
|
|
}
|
2018-11-20 01:22:35 -08:00
|
|
|
|
2018-06-15 14:16:45 -07:00
|
|
|
return Coin{coin.Denom, coin.Amount.Add(coinB.Amount)}
|
2018-03-25 10:35:45 -07:00
|
|
|
}
|
|
|
|
|
2018-11-20 01:22:35 -08:00
|
|
|
// Subtracts amounts of two coins with same denom. If the coins differ in denom
|
|
|
|
// then it panics.
|
2019-02-21 09:35:55 -08:00
|
|
|
func (coin Coin) Sub(coinB Coin) Coin {
|
2019-02-15 07:33:23 -08:00
|
|
|
if coin.Denom != coinB.Denom {
|
2018-11-20 01:22:35 -08:00
|
|
|
panic(fmt.Sprintf("invalid coin denominations; %s, %s", coin.Denom, coinB.Denom))
|
|
|
|
}
|
|
|
|
|
|
|
|
res := Coin{coin.Denom, coin.Amount.Sub(coinB.Amount)}
|
2019-01-14 08:11:24 -08:00
|
|
|
if res.IsNegative() {
|
2019-07-24 12:13:35 -07:00
|
|
|
panic("negative coin amount")
|
2018-03-25 10:35:45 -07:00
|
|
|
}
|
2018-11-20 01:22:35 -08:00
|
|
|
|
|
|
|
return res
|
|
|
|
}
|
|
|
|
|
|
|
|
// IsPositive returns true if coin amount is positive.
|
|
|
|
//
|
|
|
|
// TODO: Remove once unsigned integers are used.
|
|
|
|
func (coin Coin) IsPositive() bool {
|
2019-01-14 08:11:24 -08:00
|
|
|
return coin.Amount.Sign() == 1
|
2018-11-20 01:22:35 -08:00
|
|
|
}
|
|
|
|
|
2019-01-14 08:11:24 -08:00
|
|
|
// IsNegative returns true if the coin amount is negative and false otherwise.
|
2018-11-20 01:22:35 -08:00
|
|
|
//
|
|
|
|
// TODO: Remove once unsigned integers are used.
|
2019-01-14 08:11:24 -08:00
|
|
|
func (coin Coin) IsNegative() bool {
|
|
|
|
return coin.Amount.Sign() == -1
|
2017-07-12 09:54:07 -07:00
|
|
|
}
|
|
|
|
|
2018-11-20 01:22:35 -08: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
|
|
|
|
|
2019-03-07 16:55:08 -08:00
|
|
|
// NewCoins constructs a new coin set.
|
|
|
|
func NewCoins(coins ...Coin) Coins {
|
|
|
|
// remove zeroes
|
|
|
|
newCoins := removeZeroCoins(Coins(coins))
|
|
|
|
if len(newCoins) == 0 {
|
|
|
|
return Coins{}
|
|
|
|
}
|
|
|
|
|
|
|
|
newCoins.Sort()
|
|
|
|
|
|
|
|
// detect duplicate Denoms
|
|
|
|
if dupIndex := findDup(newCoins); dupIndex != -1 {
|
|
|
|
panic(fmt.Errorf("find duplicate denom: %s", newCoins[dupIndex]))
|
|
|
|
}
|
|
|
|
|
|
|
|
if !newCoins.IsValid() {
|
|
|
|
panic(fmt.Errorf("invalid coin set: %s", newCoins))
|
|
|
|
}
|
|
|
|
|
|
|
|
return newCoins
|
|
|
|
}
|
|
|
|
|
2019-05-31 08:16:08 -07:00
|
|
|
type coinsJSON Coins
|
|
|
|
|
|
|
|
// MarshalJSON implements a custom JSON marshaller for the Coins type to allow
|
|
|
|
// nil Coins to be encoded as an empty array.
|
|
|
|
func (coins Coins) MarshalJSON() ([]byte, error) {
|
|
|
|
if coins == nil {
|
|
|
|
return json.Marshal(coinsJSON(Coins{}))
|
|
|
|
}
|
|
|
|
|
|
|
|
return json.Marshal(coinsJSON(coins))
|
|
|
|
}
|
|
|
|
|
2017-03-01 10:07:12 -08:00
|
|
|
func (coins Coins) String() string {
|
2017-04-13 20:28:14 -07:00
|
|
|
if len(coins) == 0 {
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
|
2017-03-01 10:07:12 -08:00
|
|
|
out := ""
|
|
|
|
for _, coin := range coins {
|
2017-03-28 13:32:55 -07:00
|
|
|
out += fmt.Sprintf("%v,", coin.String())
|
2017-03-01 10:07:12 -08:00
|
|
|
}
|
2017-04-13 18:33:39 -07:00
|
|
|
return out[:len(out)-1]
|
2017-03-01 10:07:12 -08:00
|
|
|
}
|
|
|
|
|
2018-12-18 11:14:11 -08:00
|
|
|
// IsValid asserts the Coins are sorted, have positive amount,
|
|
|
|
// and Denom does not contain upper case characters.
|
2016-04-01 15:19:07 -07:00
|
|
|
func (coins Coins) IsValid() bool {
|
|
|
|
switch len(coins) {
|
|
|
|
case 0:
|
|
|
|
return true
|
|
|
|
case 1:
|
2019-03-01 12:10:22 -08:00
|
|
|
if err := validateDenom(coins[0].Denom); err != nil {
|
2019-01-02 10:17:27 -08:00
|
|
|
return false
|
|
|
|
}
|
2019-01-02 11:14:12 -08:00
|
|
|
return coins[0].IsPositive()
|
|
|
|
default:
|
2019-01-02 13:19:48 -08:00
|
|
|
// check single coin case
|
2019-01-02 11:14:12 -08:00
|
|
|
if !(Coins{coins[0]}).IsValid() {
|
2019-01-02 10:17:27 -08:00
|
|
|
return false
|
|
|
|
}
|
2019-01-02 13:19:48 -08:00
|
|
|
|
2016-04-01 15:19:07 -07:00
|
|
|
lowDenom := coins[0].Denom
|
|
|
|
for _, coin := range coins[1:] {
|
2018-12-18 11:14:11 -08:00
|
|
|
if strings.ToLower(coin.Denom) != coin.Denom {
|
|
|
|
return false
|
|
|
|
}
|
2016-04-01 15:19:07 -07:00
|
|
|
if coin.Denom <= lowDenom {
|
|
|
|
return false
|
|
|
|
}
|
2018-11-20 01:22:35 -08:00
|
|
|
if !coin.IsPositive() {
|
2016-04-01 15:19:07 -07:00
|
|
|
return false
|
|
|
|
}
|
2018-11-20 01:22:35 -08:00
|
|
|
|
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
|
|
|
}
|
2018-11-20 01:22:35 -08:00
|
|
|
|
2016-04-01 15:19:07 -07:00
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-02-21 09:35:55 -08:00
|
|
|
// Add adds two sets of coins.
|
2018-11-20 01:22:35 -08:00
|
|
|
//
|
|
|
|
// e.g.
|
|
|
|
// {2A} + {A, 2B} = {3A, 2B}
|
|
|
|
// {2A} + {0B} = {2A}
|
|
|
|
//
|
2019-02-21 09:35:55 -08:00
|
|
|
// NOTE: Add operates under the invariant that coins are sorted by
|
2018-11-20 01:22:35 -08:00
|
|
|
// denominations.
|
|
|
|
//
|
2019-02-21 09:35:55 -08:00
|
|
|
// CONTRACT: Add will never return Coins where one Coin has a non-positive
|
2018-11-20 01:22:35 -08:00
|
|
|
// amount. In otherwords, IsValid will always return true.
|
2019-02-21 09:35:55 -08:00
|
|
|
func (coins Coins) Add(coinsB Coins) Coins {
|
|
|
|
return coins.safeAdd(coinsB)
|
2018-11-20 01:22:35 -08:00
|
|
|
}
|
|
|
|
|
2019-02-21 09:35:55 -08:00
|
|
|
// safeAdd will perform addition of two coins sets. If both coin sets are
|
2018-11-20 01:22:35 -08:00
|
|
|
// empty, then an empty set is returned. If only a single set is empty, the
|
|
|
|
// other set is returned. Otherwise, the coins are compared in order of their
|
|
|
|
// denomination and addition only occurs when the denominations match, otherwise
|
|
|
|
// the coin is simply added to the sum assuming it's not zero.
|
2019-02-21 09:35:55 -08:00
|
|
|
func (coins Coins) safeAdd(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)
|
2018-11-20 01:22:35 -08:00
|
|
|
|
2016-04-01 15:19:07 -07:00
|
|
|
for {
|
|
|
|
if indexA == lenA {
|
|
|
|
if indexB == lenB {
|
2018-11-20 01:22:35 -08:00
|
|
|
// return nil coins if both sets are empty
|
2016-04-01 15:19:07 -07:00
|
|
|
return sum
|
|
|
|
}
|
2018-11-20 01:22:35 -08:00
|
|
|
|
|
|
|
// return set B (excluding zero coins) if set A is empty
|
|
|
|
return append(sum, removeZeroCoins(coinsB[indexB:])...)
|
2016-04-01 15:19:07 -07:00
|
|
|
} else if indexB == lenB {
|
2018-11-20 01:22:35 -08:00
|
|
|
// return set A (excluding zero coins) if set B is empty
|
|
|
|
return append(sum, removeZeroCoins(coins[indexA:])...)
|
2016-04-01 15:19:07 -07:00
|
|
|
}
|
2018-11-20 01:22:35 -08:00
|
|
|
|
2017-07-06 20:37:45 -07:00
|
|
|
coinA, coinB := coins[indexA], coinsB[indexB]
|
2018-11-20 01:22:35 -08:00
|
|
|
|
2016-04-01 15:19:07 -07:00
|
|
|
switch strings.Compare(coinA.Denom, coinB.Denom) {
|
2018-11-20 01:22:35 -08:00
|
|
|
case -1: // coin A denom < coin B denom
|
|
|
|
if !coinA.IsZero() {
|
2018-11-07 00:14:48 -08:00
|
|
|
sum = append(sum, coinA)
|
|
|
|
}
|
2018-11-20 01:22:35 -08:00
|
|
|
|
2017-07-06 05:59:45 -07:00
|
|
|
indexA++
|
2018-11-20 01:22:35 -08:00
|
|
|
|
|
|
|
case 0: // coin A denom == coin B denom
|
2019-02-21 09:35:55 -08:00
|
|
|
res := coinA.Add(coinB)
|
2018-11-20 01:22:35 -08:00
|
|
|
if !res.IsZero() {
|
|
|
|
sum = append(sum, res)
|
2016-04-01 15:19:07 -07:00
|
|
|
}
|
2018-11-20 01:22:35 -08:00
|
|
|
|
2017-07-06 05:59:45 -07:00
|
|
|
indexA++
|
|
|
|
indexB++
|
2018-11-20 01:22:35 -08:00
|
|
|
|
|
|
|
case 1: // coin A denom > coin B denom
|
|
|
|
if !coinB.IsZero() {
|
2018-11-07 00:14:48 -08:00
|
|
|
sum = append(sum, coinB)
|
|
|
|
}
|
2018-11-20 01:22:35 -08:00
|
|
|
|
2017-07-06 05:59:45 -07:00
|
|
|
indexB++
|
2016-04-01 15:19:07 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-03-12 08:02:18 -07:00
|
|
|
// DenomsSubsetOf returns true if receiver's denom set
|
|
|
|
// is subset of coinsB's denoms.
|
2019-03-11 07:26:46 -07:00
|
|
|
func (coins Coins) DenomsSubsetOf(coinsB Coins) bool {
|
2019-03-07 16:23:12 -08:00
|
|
|
// more denoms in B than in receiver
|
2019-03-11 07:26:46 -07:00
|
|
|
if len(coins) > len(coinsB) {
|
2019-03-07 16:23:12 -08:00
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2019-03-11 07:26:46 -07:00
|
|
|
for _, coin := range coins {
|
|
|
|
if coinsB.AmountOf(coin.Denom).IsZero() {
|
2019-03-07 16:23:12 -08:00
|
|
|
return false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
2019-02-21 09:35:55 -08:00
|
|
|
// Sub subtracts a set of coins from another.
|
2018-11-20 01:22:35 -08:00
|
|
|
//
|
|
|
|
// e.g.
|
|
|
|
// {2A, 3B} - {A} = {A, 3B}
|
|
|
|
// {2A} - {0B} = {2A}
|
|
|
|
// {A, B} - {A} = {B}
|
|
|
|
//
|
2019-02-21 09:35:55 -08:00
|
|
|
// CONTRACT: Sub will never return Coins where one Coin has a non-positive
|
2018-11-20 01:22:35 -08:00
|
|
|
// amount. In otherwords, IsValid will always return true.
|
2019-02-21 09:35:55 -08:00
|
|
|
func (coins Coins) Sub(coinsB Coins) Coins {
|
|
|
|
diff, hasNeg := coins.SafeSub(coinsB)
|
2018-11-20 01:22:35 -08:00
|
|
|
if hasNeg {
|
|
|
|
panic("negative coin amount")
|
2016-04-01 15:19:07 -07:00
|
|
|
}
|
2018-11-20 01:22:35 -08:00
|
|
|
|
|
|
|
return diff
|
2016-04-01 15:19:07 -07:00
|
|
|
}
|
|
|
|
|
2019-02-21 09:35:55 -08:00
|
|
|
// SafeSub performs the same arithmetic as Sub but returns a boolean if any
|
2018-11-20 01:22:35 -08:00
|
|
|
// negative coin amount was returned.
|
2019-02-21 09:35:55 -08:00
|
|
|
func (coins Coins) SafeSub(coinsB Coins) (Coins, bool) {
|
|
|
|
diff := coins.safeAdd(coinsB.negative())
|
2019-02-06 14:45:15 -08:00
|
|
|
return diff, diff.IsAnyNegative()
|
2016-04-01 15:19:07 -07:00
|
|
|
}
|
|
|
|
|
2019-03-12 08:02:18 -07:00
|
|
|
// IsAllGT returns true if for every denom in coinsB,
|
|
|
|
// the denom is present at a greater amount in coins.
|
2018-11-07 00:14:48 -08:00
|
|
|
func (coins Coins) IsAllGT(coinsB Coins) bool {
|
2019-03-07 16:23:12 -08:00
|
|
|
if len(coins) == 0 {
|
2018-11-07 00:14:48 -08:00
|
|
|
return false
|
|
|
|
}
|
2018-11-20 01:22:35 -08:00
|
|
|
|
2019-03-07 16:23:12 -08:00
|
|
|
if len(coinsB) == 0 {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
2019-03-11 07:26:46 -07:00
|
|
|
if !coinsB.DenomsSubsetOf(coins) {
|
2019-03-07 16:23:12 -08:00
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, coinB := range coinsB {
|
|
|
|
amountA, amountB := coins.AmountOf(coinB.Denom), coinB.Amount
|
|
|
|
if !amountA.GT(amountB) {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return true
|
2018-11-07 00:14:48 -08:00
|
|
|
}
|
|
|
|
|
2019-03-12 11:13:36 -07:00
|
|
|
// IsAllGTE returns false if for any denom in coinsB,
|
|
|
|
// the denom is present at a smaller amount in coins;
|
|
|
|
// else returns true.
|
2018-11-07 00:14:48 -08:00
|
|
|
func (coins Coins) IsAllGTE(coinsB Coins) bool {
|
2019-03-12 11:13:36 -07:00
|
|
|
if len(coinsB) == 0 {
|
2016-04-01 15:19:07 -07:00
|
|
|
return true
|
|
|
|
}
|
2018-11-20 01:22:35 -08:00
|
|
|
|
2019-03-12 11:13:36 -07:00
|
|
|
if len(coins) == 0 {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, coinB := range coinsB {
|
|
|
|
if coinB.Amount.GT(coins.AmountOf(coinB.Denom)) {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return true
|
2016-04-01 15:19:07 -07:00
|
|
|
}
|
|
|
|
|
2018-11-07 00:14:48 -08:00
|
|
|
// IsAllLT returns True iff for every denom in coins, the denom is present at
|
|
|
|
// a smaller amount in coinsB.
|
|
|
|
func (coins Coins) IsAllLT(coinsB Coins) bool {
|
2018-11-20 01:22:35 -08:00
|
|
|
return coinsB.IsAllGT(coins)
|
2018-11-07 00:14:48 -08:00
|
|
|
}
|
|
|
|
|
2018-11-20 01:22:35 -08:00
|
|
|
// IsAllLTE returns true iff for every denom in coins, the denom is present at
|
2018-11-07 00:14:48 -08:00
|
|
|
// a smaller or equal amount in coinsB.
|
|
|
|
func (coins Coins) IsAllLTE(coinsB Coins) bool {
|
2018-11-20 01:22:35 -08:00
|
|
|
return coinsB.IsAllGTE(coins)
|
2018-09-19 08:25:52 -07:00
|
|
|
}
|
|
|
|
|
2019-05-07 10:10:35 -07:00
|
|
|
// IsAnyGT returns true iff for any denom in coins, the denom is present at a
|
|
|
|
// greater amount in coinsB.
|
|
|
|
//
|
|
|
|
// e.g.
|
|
|
|
// {2A, 3B}.IsAnyGT{A} = true
|
|
|
|
// {2A, 3B}.IsAnyGT{5C} = false
|
|
|
|
// {}.IsAnyGT{5C} = false
|
|
|
|
// {2A, 3B}.IsAnyGT{} = false
|
|
|
|
func (coins Coins) IsAnyGT(coinsB Coins) bool {
|
|
|
|
if len(coinsB) == 0 {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, coin := range coins {
|
|
|
|
amt := coinsB.AmountOf(coin.Denom)
|
|
|
|
if coin.Amount.GT(amt) && !amt.IsZero() {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2019-02-07 18:14:54 -08:00
|
|
|
// IsAnyGTE returns true iff coins contains at least one denom that is present
|
|
|
|
// at a greater or equal amount in coinsB; it returns false otherwise.
|
|
|
|
//
|
|
|
|
// NOTE: IsAnyGTE operates under the invariant that both coin sets are sorted
|
|
|
|
// by denominations and there exists no zero coins.
|
|
|
|
func (coins Coins) IsAnyGTE(coinsB Coins) bool {
|
|
|
|
if len(coinsB) == 0 {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, coin := range coins {
|
|
|
|
amt := coinsB.AmountOf(coin.Denom)
|
2019-02-12 07:22:04 -08:00
|
|
|
if coin.Amount.GTE(amt) && !amt.IsZero() {
|
2019-02-07 18:14:54 -08:00
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2018-11-07 00:14:48 -08:00
|
|
|
// IsZero returns true if there are no coins 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
|
|
|
|
}
|
2018-11-20 01:22:35 -08:00
|
|
|
|
|
|
|
coins = coins.Sort()
|
|
|
|
coinsB = coinsB.Sort()
|
|
|
|
|
2017-07-06 20:37:45 -07:00
|
|
|
for i := 0; i < len(coins); i++ {
|
2019-02-15 07:33:23 -08:00
|
|
|
if !coins[i].IsEqual(coinsB[i]) {
|
2016-04-01 15:19:07 -07:00
|
|
|
return false
|
|
|
|
}
|
|
|
|
}
|
2018-11-20 01:22:35 -08:00
|
|
|
|
2016-04-01 15:19:07 -07:00
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
2018-11-20 01:22:35 -08:00
|
|
|
// Empty returns true if there are no coins and false otherwise.
|
|
|
|
func (coins Coins) Empty() bool {
|
|
|
|
return len(coins) == 0
|
|
|
|
}
|
|
|
|
|
|
|
|
// Returns the amount of a denom from coins
|
|
|
|
func (coins Coins) AmountOf(denom string) Int {
|
2019-03-01 12:10:22 -08:00
|
|
|
mustValidateDenom(denom)
|
2019-02-15 07:33:23 -08:00
|
|
|
|
2018-11-20 01:22:35 -08:00
|
|
|
switch len(coins) {
|
|
|
|
case 0:
|
|
|
|
return ZeroInt()
|
|
|
|
|
|
|
|
case 1:
|
|
|
|
coin := coins[0]
|
|
|
|
if coin.Denom == denom {
|
|
|
|
return coin.Amount
|
|
|
|
}
|
|
|
|
return ZeroInt()
|
|
|
|
|
|
|
|
default:
|
|
|
|
midIdx := len(coins) / 2 // 2:1, 3:1, 4:2
|
|
|
|
coin := coins[midIdx]
|
2019-08-19 09:06:27 -07:00
|
|
|
switch {
|
|
|
|
case denom < coin.Denom:
|
2018-11-20 01:22:35 -08:00
|
|
|
return coins[:midIdx].AmountOf(denom)
|
2019-08-19 09:06:27 -07:00
|
|
|
case denom == coin.Denom:
|
2018-11-20 01:22:35 -08:00
|
|
|
return coin.Amount
|
2019-08-19 09:06:27 -07:00
|
|
|
default:
|
2018-11-20 01:22:35 -08:00
|
|
|
return coins[midIdx+1:].AmountOf(denom)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-02-15 07:33:23 -08:00
|
|
|
// IsAllPositive returns true if there is at least one coin and all currencies
|
2018-11-20 01:22:35 -08:00
|
|
|
// have a positive value.
|
2019-02-15 07:33:23 -08:00
|
|
|
func (coins Coins) IsAllPositive() bool {
|
2016-04-01 15:19:07 -07:00
|
|
|
if len(coins) == 0 {
|
|
|
|
return false
|
|
|
|
}
|
2018-11-20 01:22:35 -08:00
|
|
|
|
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
|
|
|
|
}
|
|
|
|
}
|
2018-11-20 01:22:35 -08:00
|
|
|
|
2016-04-01 15:19:07 -07:00
|
|
|
return true
|
|
|
|
}
|
2017-01-15 15:10:51 -08:00
|
|
|
|
2019-02-06 14:45:15 -08:00
|
|
|
// IsAnyNegative returns true if there is at least one coin whose amount
|
|
|
|
// is negative; returns false otherwise. It returns false if the coin set
|
|
|
|
// is empty too.
|
2019-02-15 07:33:23 -08:00
|
|
|
//
|
2018-11-20 01:22:35 -08:00
|
|
|
// TODO: Remove once unsigned integers are used.
|
2019-02-06 14:45:15 -08:00
|
|
|
func (coins Coins) IsAnyNegative() bool {
|
2018-03-25 10:35:45 -07:00
|
|
|
for _, coin := range coins {
|
2019-01-14 08:11:24 -08:00
|
|
|
if coin.IsNegative() {
|
2019-02-06 14:45:15 -08:00
|
|
|
return true
|
2017-01-15 15:10:51 -08:00
|
|
|
}
|
|
|
|
}
|
2018-11-20 01:22:35 -08:00
|
|
|
|
2019-02-06 14:45:15 -08:00
|
|
|
return false
|
2017-01-15 15:10:51 -08:00
|
|
|
}
|
2017-05-22 02:22:41 -07:00
|
|
|
|
2018-11-20 01:22:35 -08:00
|
|
|
// negative returns a set of coins with all amount negative.
|
|
|
|
//
|
|
|
|
// TODO: Remove once unsigned integers are used.
|
|
|
|
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(),
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
return res
|
|
|
|
}
|
|
|
|
|
|
|
|
// removeZeroCoins removes all zero coins from the given coin set in-place.
|
|
|
|
func removeZeroCoins(coins Coins) Coins {
|
|
|
|
i, l := 0, len(coins)
|
|
|
|
for i < l {
|
|
|
|
if coins[i].IsZero() {
|
|
|
|
// remove coin
|
|
|
|
coins = append(coins[:i], coins[i+1:]...)
|
|
|
|
l--
|
2018-01-25 16:30:49 -08:00
|
|
|
} else {
|
2018-11-20 01:22:35 -08:00
|
|
|
i++
|
2018-01-25 16:30:49 -08:00
|
|
|
}
|
|
|
|
}
|
2018-11-20 01:22:35 -08:00
|
|
|
|
|
|
|
return coins[:i]
|
2018-01-25 16:30:49 -08:00
|
|
|
}
|
|
|
|
|
2018-11-20 01:22:35 -08:00
|
|
|
//-----------------------------------------------------------------------------
|
2017-12-21 03:26:40 -08:00
|
|
|
// Sort interface
|
2017-05-22 02:22:41 -07:00
|
|
|
|
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-11-20 01:22:35 -08:00
|
|
|
//-----------------------------------------------------------------------------
|
2018-01-06 12:53:31 -08:00
|
|
|
// Parsing
|
|
|
|
|
|
|
|
var (
|
|
|
|
// Denominations can be 3 ~ 16 characters long.
|
2019-03-01 12:10:22 -08:00
|
|
|
reDnmString = `[a-z][a-z0-9]{2,15}`
|
|
|
|
reAmt = `[[:digit:]]+`
|
|
|
|
reDecAmt = `[[:digit:]]*\.[[:digit:]]+`
|
|
|
|
reSpc = `[[:space:]]*`
|
|
|
|
reDnm = regexp.MustCompile(fmt.Sprintf(`^%s$`, reDnmString))
|
|
|
|
reCoin = regexp.MustCompile(fmt.Sprintf(`^(%s)%s(%s)$`, reAmt, reSpc, reDnmString))
|
|
|
|
reDecCoin = regexp.MustCompile(fmt.Sprintf(`^(%s)%s(%s)$`, reDecAmt, reSpc, reDnmString))
|
2018-01-06 12:53:31 -08:00
|
|
|
)
|
|
|
|
|
2019-03-01 12:10:22 -08:00
|
|
|
func validateDenom(denom string) error {
|
|
|
|
if !reDnm.MatchString(denom) {
|
2019-03-16 06:18:27 -07:00
|
|
|
return fmt.Errorf("invalid denom: %s", denom)
|
2019-02-15 07:33:23 -08:00
|
|
|
}
|
2019-03-01 12:10:22 -08:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func mustValidateDenom(denom string) {
|
|
|
|
if err := validateDenom(denom); err != nil {
|
|
|
|
panic(err)
|
2019-02-15 07:33:23 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-01-06 12:53:31 -08:00
|
|
|
// 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 {
|
2018-11-20 01:22:35 -08:00
|
|
|
return Coin{}, fmt.Errorf("invalid coin expression: %s", coinStr)
|
2018-01-06 12:53:31 -08:00
|
|
|
}
|
2018-11-20 01:22:35 -08:00
|
|
|
|
2018-01-06 12:53:31 -08:00
|
|
|
denomStr, amountStr := matches[2], matches[1]
|
|
|
|
|
2018-11-20 01:22:35 -08:00
|
|
|
amount, ok := NewIntFromString(amountStr)
|
|
|
|
if !ok {
|
|
|
|
return Coin{}, fmt.Errorf("failed to parse coin amount: %s", amountStr)
|
2018-01-06 12:53:31 -08:00
|
|
|
}
|
|
|
|
|
2019-03-01 12:10:22 -08:00
|
|
|
if err := validateDenom(denomStr); err != nil {
|
|
|
|
return Coin{}, fmt.Errorf("invalid denom cannot contain upper case characters or spaces: %s", err)
|
2018-12-18 11:14:11 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
return NewCoin(denomStr, 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.
|
2019-03-25 07:38:15 -07:00
|
|
|
func ParseCoins(coinsStr string) (Coins, error) {
|
2018-01-06 12:53:31 -08:00
|
|
|
coinsStr = strings.TrimSpace(coinsStr)
|
|
|
|
if len(coinsStr) == 0 {
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
coinStrs := strings.Split(coinsStr, ",")
|
2019-03-25 07:38:15 -07:00
|
|
|
coins := make(Coins, len(coinStrs))
|
|
|
|
for i, coinStr := range coinStrs {
|
2018-01-06 12:53:31 -08:00
|
|
|
coin, err := ParseCoin(coinStr)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2019-03-25 07:38:15 -07:00
|
|
|
|
|
|
|
coins[i] = coin
|
2018-01-06 12:53:31 -08:00
|
|
|
}
|
|
|
|
|
2019-03-25 07:38:15 -07:00
|
|
|
// sort coins for determinism
|
2018-01-06 12:53:31 -08:00
|
|
|
coins.Sort()
|
|
|
|
|
2019-03-25 07:38:15 -07:00
|
|
|
// validate coins before returning
|
2018-01-06 12:53:31 -08:00
|
|
|
if !coins.IsValid() {
|
2018-06-13 12:13:22 -07:00
|
|
|
return nil, fmt.Errorf("parseCoins invalid: %#v", coins)
|
2018-01-06 12:53:31 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
return coins, nil
|
|
|
|
}
|
2019-03-07 16:55:08 -08:00
|
|
|
|
|
|
|
// findDup works on the assumption that coins is sorted
|
|
|
|
func findDup(coins Coins) int {
|
|
|
|
if len(coins) <= 1 {
|
|
|
|
return -1
|
|
|
|
}
|
|
|
|
|
2019-05-08 06:51:30 -07:00
|
|
|
prevDenom := coins[0].Denom
|
2019-03-07 16:55:08 -08:00
|
|
|
for i := 1; i < len(coins); i++ {
|
2019-05-08 06:51:30 -07:00
|
|
|
if coins[i].Denom == prevDenom {
|
2019-03-07 16:55:08 -08:00
|
|
|
return i
|
|
|
|
}
|
2019-05-08 06:51:30 -07:00
|
|
|
prevDenom = coins[i].Denom
|
2019-03-07 16:55:08 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
return -1
|
|
|
|
}
|