func ParseCoins(coinsStr string) (Coins, error) {
	coinsStr = strings.TrimSpace(coinsStr)
	if len(coinsStr) == 0 {
		return nil, nil
	}
	coinStrs := strings.Split(coinsStr, ",")
	coins := make(Coins, len(coinStrs))
	for i, coinStr := range coinStrs {
		coin, err := ParseCoin(coinStr)
		if err != nil {
			return nil, err
		}
		coins[i] = coin
	}
	coins.Sort()
	if !coins.IsValid() {
		return nil, fmt.Errorf("parseCoins invalid: %#v", coins)
	}
	return coins, nil
}
ParseCoins will parse out a list of coins separated by commas.
If nothing is provided, it returns nil Coins.
Returned coins are sorted.
package coin
import (
	"encoding/json"
	"fmt"
	"regexp"
	"sort"
	"strings"
	"github.com/pkg/errors"
)
type Coin struct {
	Denom string `json:"denom"`
	Amount Int `json:"amount"`
}
func NewCoin(denom string, amount Int) Coin {
	if err := validate(denom, amount); err != nil {
		panic(err)
	}
	return Coin{
		Denom:  denom,
		Amount: amount,
	}
}
func NewInt64Coin(denom string, amount int64) Coin {
	return NewCoin(denom, NewInt(amount))
}
func (coin Coin) String() string {
	return fmt.Sprintf("%v%v", coin.Amount, coin.Denom)
}
func validate(denom string, amount Int) error {
	if err := ValidateDenom(denom); err != nil {
		return err
	}
	if amount.IsNegative() {
		return fmt.Errorf("negative coin amount: %v", amount)
	}
	return nil
}
func (coin Coin) IsValid() bool {
	if err := validate(coin.Denom, coin.Amount); err != nil {
		return false
	}
	return true
}
func (coin Coin) IsZero() bool {
	return coin.Amount.IsZero()
}
func (coin Coin) IsGTE(other Coin) bool {
	if coin.Denom != other.Denom {
		panic(fmt.Sprintf("invalid coin denominations; %s, %s", coin.Denom, other.Denom))
	}
	return !coin.Amount.LT(other.Amount)
}
func (coin Coin) IsLT(other Coin) bool {
	if coin.Denom != other.Denom {
		panic(fmt.Sprintf("invalid coin denominations; %s, %s", coin.Denom, other.Denom))
	}
	return coin.Amount.LT(other.Amount)
}
func (coin Coin) IsEqual(other Coin) bool {
	if coin.Denom != other.Denom {
		panic(fmt.Sprintf("invalid coin denominations; %s, %s", coin.Denom, other.Denom))
	}
	return coin.Amount.Equal(other.Amount)
}
func (coin Coin) Add(coinB Coin) Coin {
	if coin.Denom != coinB.Denom {
		panic(fmt.Sprintf("invalid coin denominations; %s, %s", coin.Denom, coinB.Denom))
	}
	return Coin{coin.Denom, coin.Amount.Add(coinB.Amount)}
}
func (coin Coin) Sub(coinB Coin) Coin {
	if coin.Denom != coinB.Denom {
		panic(fmt.Sprintf("invalid coin denominations; %s, %s", coin.Denom, coinB.Denom))
	}
	res := Coin{coin.Denom, coin.Amount.Sub(coinB.Amount)}
	if res.IsNegative() {
		panic("negative coin amount")
	}
	return res
}
func (coin Coin) IsPositive() bool {
	return coin.Amount.Sign() == 1
}
func (coin Coin) IsNegative() bool {
	return coin.Amount.Sign() == -1
}
type Coins []Coin
func NewCoins(coins ...Coin) Coins {
	newCoins := removeZeroCoins(Coins(coins))
	if len(newCoins) == 0 {
		return Coins{}
	}
	newCoins.Sort()
	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
}
func NewInt64Coins(denom string, amount int64) Coins {
	return NewCoins(NewCoin(denom, NewInt(amount)))
}
type coinsJSON Coins
func (coins Coins) MarshalJSON() ([]byte, error) {
	if coins == nil {
		return json.Marshal(coinsJSON(Coins{}))
	}
	return json.Marshal(coinsJSON(coins))
}
func (coins Coins) String() string {
	if len(coins) == 0 {
		return ""
	}
	out := ""
	for _, coin := range coins {
		out += fmt.Sprintf("%v,", coin.String())
	}
	return out[:len(out)-1]
}
func (coins Coins) IsValid() bool {
	switch len(coins) {
	case 0:
		return true
	case 1:
		if err := ValidateDenom(coins[0].Denom); err != nil {
			return false
		}
		return coins[0].IsPositive()
	default:
		if !(Coins{coins[0]}).IsValid() {
			return false
		}
		lowDenom := coins[0].Denom
		for _, coin := range coins[1:] {
			if strings.ToLower(coin.Denom) != coin.Denom {
				return false
			}
			if coin.Denom <= lowDenom {
				return false
			}
			if !coin.IsPositive() {
				return false
			}
			lowDenom = coin.Denom
		}
		return true
	}
}
func (coins Coins) Add(coinsB ...Coin) Coins {
	return coins.safeAdd(coinsB)
}
func (coins Coins) safeAdd(coinsB Coins) Coins {
	sum := ([]Coin)(nil)
	indexA, indexB := 0, 0
	lenA, lenB := len(coins), len(coinsB)
	for {
		if indexA == lenA {
			if indexB == lenB {
				return sum
			}
			return append(sum, removeZeroCoins(coinsB[indexB:])...)
		} else if indexB == lenB {
			return append(sum, removeZeroCoins(coins[indexA:])...)
		}
		coinA, coinB := coins[indexA], coinsB[indexB]
		switch strings.Compare(coinA.Denom, coinB.Denom) {
		case -1:  
			if !coinA.IsZero() {
				sum = append(sum, coinA)
			}
			indexA++
		case 0:  
			res := coinA.Add(coinB)
			if !res.IsZero() {
				sum = append(sum, res)
			}
			indexA++
			indexB++
		case 1:  
			if !coinB.IsZero() {
				sum = append(sum, coinB)
			}
			indexB++
		}
	}
}
func (coins Coins) DenomsSubsetOf(coinsB Coins) bool {
	if len(coins) > len(coinsB) {
		return false
	}
	for _, coin := range coins {
		if coinsB.AmountOf(coin.Denom).IsZero() {
			return false
		}
	}
	return true
}
func (coins Coins) Sub(coinsB Coins) Coins {
	diff, hasNeg := coins.SafeSub(coinsB)
	if hasNeg {
		panic("negative coin amount")
	}
	return diff
}
func (coins Coins) SafeSub(coinsB Coins) (Coins, bool) {
	diff := coins.safeAdd(coinsB.negative())
	return diff, diff.IsAnyNegative()
}
func (coins Coins) IsAllGT(coinsB Coins) bool {
	if len(coins) == 0 {
		return false
	}
	if len(coinsB) == 0 {
		return true
	}
	if !coinsB.DenomsSubsetOf(coins) {
		return false
	}
	for _, coinB := range coinsB {
		amountA, amountB := coins.AmountOf(coinB.Denom), coinB.Amount
		if !amountA.GT(amountB) {
			return false
		}
	}
	return true
}
func (coins Coins) IsAllGTE(coinsB Coins) bool {
	if len(coinsB) == 0 {
		return true
	}
	if len(coins) == 0 {
		return false
	}
	for _, coinB := range coinsB {
		if coinB.Amount.GT(coins.AmountOf(coinB.Denom)) {
			return false
		}
	}
	return true
}
func (coins Coins) IsAllLT(coinsB Coins) bool {
	return coinsB.IsAllGT(coins)
}
func (coins Coins) IsAllLTE(coinsB Coins) bool {
	return coinsB.IsAllGTE(coins)
}
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
}
func (coins Coins) IsAnyGTE(coinsB Coins) bool {
	if len(coinsB) == 0 {
		return false
	}
	for _, coin := range coins {
		amt := coinsB.AmountOf(coin.Denom)
		if coin.Amount.GTE(amt) && !amt.IsZero() {
			return true
		}
	}
	return false
}
func (coins Coins) IsZero() bool {
	for _, coin := range coins {
		if !coin.IsZero() {
			return false
		}
	}
	return true
}
func (coins Coins) IsEqual(coinsB Coins) bool {
	if len(coins) != len(coinsB) {
		return false
	}
	coins = coins.Sort()
	coinsB = coinsB.Sort()
	for i := 0; i < len(coins); i++ {
		if !coins[i].IsEqual(coinsB[i]) {
			return false
		}
	}
	return true
}
func (coins Coins) Empty() bool {
	return len(coins) == 0
}
func (coins Coins) AmountOf(denom string) Int {
	mustValidateDenom(denom)
	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  
		coin := coins[midIdx]
		switch {
		case denom < coin.Denom:
			return coins[:midIdx].AmountOf(denom)
		case denom == coin.Denom:
			return coin.Amount
		default:
			return coins[midIdx+1:].AmountOf(denom)
		}
	}
}
func (coins Coins) GetDenomByIndex(i int) string {
	return coins[i].Denom
}
func (coins Coins) IsAllPositive() bool {
	if len(coins) == 0 {
		return false
	}
	for _, coin := range coins {
		if !coin.IsPositive() {
			return false
		}
	}
	return true
}
func (coins Coins) IsAnyNegative() bool {
	for _, coin := range coins {
		if coin.IsNegative() {
			return true
		}
	}
	return false
}
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
}
func removeZeroCoins(coins Coins) Coins {
	i, l := 0, len(coins)
	for i < l {
		if coins[i].IsZero() {
			coins = append(coins[:i], coins[i+1:]...)
			l--
		} else {
			i++
		}
	}
	return coins[:i]
}
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{}
func (coins Coins) Sort() Coins {
	sort.Sort(coins)
	return coins
}
var (
	reCreatorName  = `[a-z][a-z0-9]{2,16}`
	reSymbolString = `[a-z][a-z0-9]{2,16}`
	reDnmString    = fmt.Sprintf(`%s\/%s`, reCreatorName, reSymbolString)
	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))
)
func ValidateDenom(denom string) error {
	if !reDnm.MatchString(denom) {
		return errors.Wrapf(ErrCoinDenomInvalid, "invalid denom: %s", denom)
	}
	return nil
}
func mustValidateDenom(denom string) {
	if err := ValidateDenom(denom); err != nil {
		panic(err)
	}
}
func ParseCoin(coinStr string) (coin Coin, err error) {
	coinStr = strings.TrimSpace(coinStr)
	matches := reCoin.FindStringSubmatch(coinStr)
	if matches == nil {
		return Coin{}, fmt.Errorf("invalid coin expression: %s", coinStr)
	}
	denomStr, amountStr := matches[2], matches[1]
	amount, ok := NewIntFromString(amountStr)
	if !ok {
		return Coin{}, fmt.Errorf("failed to parse coin amount: %s", amountStr)
	}
	if err := ValidateDenom(denomStr); err != nil {
		return Coin{}, errors.Wrapf(ErrCoinDenomInvalid, "invalid denom cannot contain upper case characters or spaces")
	}
	return NewCoin(denomStr, amount), nil
}