Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix DecCoins constructor #5410

Merged
merged 5 commits into from
Dec 16, 2019
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 29 additions & 19 deletions types/dec_coin.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,17 +11,16 @@ import (
// ----------------------------------------------------------------------------
// Decimal Coin

// Coins which can have additional decimal points
// DecCoin defines a coin which can have additional decimal points
type DecCoin struct {
Denom string `json:"denom"`
Amount Dec `json:"amount"`
}

// NewDecCoin creates a new DecCoin instance from an Int.
func NewDecCoin(denom string, amount Int) DecCoin {
mustValidateDenom(denom)

if amount.IsNegative() {
panic(fmt.Sprintf("negative coin amount: %v\n", amount))
if err := validate(denom, amount); err != nil {
panic(err)
}

return DecCoin{
Expand All @@ -30,6 +29,7 @@ func NewDecCoin(denom string, amount Int) DecCoin {
}
}

// NewDecCoinFromDec creates a new DecCoin instance from a Dec.
func NewDecCoinFromDec(denom string, amount Dec) DecCoin {
mustValidateDenom(denom)

Expand All @@ -43,12 +43,10 @@ func NewDecCoinFromDec(denom string, amount Dec) DecCoin {
}
}

// NewDecCoinFromCoin creates a new DecCoin from a Coin.
func NewDecCoinFromCoin(coin Coin) DecCoin {
if coin.Amount.IsNegative() {
panic(fmt.Sprintf("negative decimal coin amount: %v\n", coin.Amount))
}
if strings.ToLower(coin.Denom) != coin.Denom {
panic(fmt.Sprintf("denom cannot contain upper case characters: %s\n", coin.Denom))
if err := validate(coin.Denom, coin.Amount); err != nil {
panic(err)
}

return DecCoin{
Expand Down Expand Up @@ -97,15 +95,15 @@ func (coin DecCoin) IsEqual(other DecCoin) bool {
return coin.Amount.Equal(other.Amount)
}

// Adds amounts of two coins with same denom
// Add adds amounts of two decimal coins with same denom.
func (coin DecCoin) Add(coinB DecCoin) DecCoin {
if coin.Denom != coinB.Denom {
panic(fmt.Sprintf("coin denom different: %v %v\n", coin.Denom, coinB.Denom))
}
return DecCoin{coin.Denom, coin.Amount.Add(coinB.Amount)}
}

// Subtracts amounts of two coins with same denom
// Sub subtracts amounts of two decimal coins with same denom.
func (coin DecCoin) Sub(coinB DecCoin) DecCoin {
if coin.Denom != coinB.Denom {
panic(fmt.Sprintf("coin denom different: %v %v\n", coin.Denom, coinB.Denom))
Expand Down Expand Up @@ -141,18 +139,30 @@ func (coin DecCoin) String() string {
return fmt.Sprintf("%v%v", coin.Amount, coin.Denom)
}

// IsValid returns true if the DecCoin has a non-negative amount and the denom is vaild.
func (coin DecCoin) IsValid() bool {
if err := ValidateDenom(coin.Denom); err != nil {
return false
}
return !coin.IsNegative()
}

// ----------------------------------------------------------------------------
// Decimal Coins

// coins with decimal
// DecCoins defines a slice of coins with decimal values
type DecCoins []DecCoin

// NewDecCoins constructs a new coin set with decimal values
// from regular Coins.
func NewDecCoins(coins Coins) DecCoins {
dcs := make(DecCoins, len(coins))
for i, coin := range coins {
dcs[i] = NewDecCoinFromCoin(coin)
decCoins := make(DecCoins, len(coins))
newCoins := NewCoins(coins...)
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this is where coins are sorted

for i, coin := range newCoins {
decCoins[i] = NewDecCoinFromCoin(coin)
}
return dcs

return decCoins
}

// String implements the Stringer interface for DecCoins. It returns a
Expand Down Expand Up @@ -404,7 +414,7 @@ func (coins DecCoins) Empty() bool {
return len(coins) == 0
}

// returns the amount of a denom from deccoins
// AmountOf returns the amount of a denom from deccoins
func (coins DecCoins) AmountOf(denom string) Dec {
mustValidateDenom(denom)

Expand Down Expand Up @@ -452,7 +462,7 @@ func (coins DecCoins) IsEqual(coinsB DecCoins) bool {
return true
}

// return whether all coins are zero
// IsZero returns whether all coins are zero
func (coins DecCoins) IsZero() bool {
for _, coin := range coins {
if !coin.Amount.IsZero() {
Expand Down
7 changes: 7 additions & 0 deletions types/dec_coin_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,13 @@ func TestAddDecCoins(t *testing.T) {
}
}

func TestSubDecCoins(t *testing.T) {
decCoins1 := NewDecCoins(Coins{NewCoin("mytoken", NewInt(10)), NewCoin("btc", NewInt(20)), NewCoin("eth", NewInt(30))})
decCoins2 := NewDecCoins(Coins{NewCoin("btc", NewInt(10)), NewCoin("eth", NewInt(15)), NewCoin("mytoken", NewInt(5))})
diff := decCoins1.Sub(decCoins2)
require.Equal(t, decCoins2, diff)
}
fedekunze marked this conversation as resolved.
Show resolved Hide resolved

func TestSortDecCoins(t *testing.T) {
good := DecCoins{
NewInt64DecCoin("gas", 1),
Expand Down