From 314af42d92ddfdb5cb4412c5ad8ca140d20c4fe8 Mon Sep 17 00:00:00 2001 From: colin axner Date: Tue, 18 Jun 2019 09:02:31 -0700 Subject: [PATCH] Reintroduce Coin.IsValid() (#4558) Closes #4556 --- .../improvements/sdk/4556-Added-IsValid-f | 1 + types/coin.go | 28 ++++++++++++++++--- types/coin_test.go | 20 +++++++++++++ 3 files changed, 45 insertions(+), 4 deletions(-) create mode 100644 .pending/improvements/sdk/4556-Added-IsValid-f diff --git a/.pending/improvements/sdk/4556-Added-IsValid-f b/.pending/improvements/sdk/4556-Added-IsValid-f new file mode 100644 index 000000000..1d0f161af --- /dev/null +++ b/.pending/improvements/sdk/4556-Added-IsValid-f @@ -0,0 +1 @@ +#4556 Added IsValid function to Coin \ No newline at end of file diff --git a/types/coin.go b/types/coin.go index 8d0f03c7d..055f2ea51 100644 --- a/types/coin.go +++ b/types/coin.go @@ -28,10 +28,8 @@ type Coin struct { // NewCoin returns a new coin with a denomination and amount. It will panic if // the amount is negative. func NewCoin(denom string, amount Int) Coin { - mustValidateDenom(denom) - - if amount.LT(ZeroInt()) { - panic(fmt.Errorf("negative coin amount: %v", amount)) + if err := validate(denom, amount); err != nil { + panic(err) } return Coin{ @@ -51,6 +49,28 @@ func (coin Coin) String() string { return fmt.Sprintf("%v%v", coin.Amount, coin.Denom) } +// 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 + } + + if amount.LT(ZeroInt()) { + 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 +} + // IsZero returns if this represents no money func (coin Coin) IsZero() bool { return coin.Amount.IsZero() diff --git a/types/coin_test.go b/types/coin_test.go index 9d6177afe..5cbb3d8a4 100644 --- a/types/coin_test.go +++ b/types/coin_test.go @@ -48,6 +48,26 @@ func TestIsEqualCoin(t *testing.T) { } } +func TestCoinIsValid(t *testing.T) { + cases := []struct { + coin Coin + expectPass bool + }{ + {Coin{testDenom1, NewInt(-1)}, false}, + {Coin{testDenom1, NewInt(0)}, true}, + {Coin{testDenom1, NewInt(1)}, true}, + {Coin{"Atom", NewInt(1)}, false}, + {Coin{"a", NewInt(1)}, false}, + {Coin{"a very long coin denom", NewInt(1)}, false}, + {Coin{"atOm", NewInt(1)}, false}, + {Coin{" ", NewInt(1)}, false}, + } + + for i, tc := range cases { + require.Equal(t, tc.expectPass, tc.coin.IsValid(), "unexpected result for IsValid, tc #%d", i) + } +} + func TestAddCoin(t *testing.T) { cases := []struct { inputOne Coin