refactoring balance.coin validation
This commit is contained in:
parent
2cea5e5750
commit
40d22c7ab3
|
@ -30,38 +30,12 @@ func (b Balance) GetCoins() sdk.Coins {
|
|||
|
||||
// Validate checks for address and coins correctness.
|
||||
func (b Balance) Validate() error {
|
||||
_, err := sdk.AccAddressFromBech32(b.Address)
|
||||
if err != nil {
|
||||
if _, err := sdk.AccAddressFromBech32(b.Address); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
var prevDenom string
|
||||
if !b.Coins.Empty() {
|
||||
prevDenom = b.Coins[0].Denom
|
||||
}
|
||||
seenDenoms := make(map[string]bool)
|
||||
|
||||
// NOTE: we perform a custom validation since the coins.Validate function
|
||||
// errors on zero balance coins
|
||||
for _, coin := range b.Coins {
|
||||
if seenDenoms[coin.Denom] {
|
||||
return fmt.Errorf("duplicate denomination %s", coin.Denom)
|
||||
}
|
||||
|
||||
if err := sdk.ValidateDenom(coin.Denom); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if coin.Denom < prevDenom {
|
||||
return fmt.Errorf("denomination %s is not sorted", coin.Denom)
|
||||
}
|
||||
|
||||
if coin.IsNegative() {
|
||||
return fmt.Errorf("coin %s amount is cannot be negative", coin.Denom)
|
||||
}
|
||||
|
||||
seenDenoms[coin.Denom] = true
|
||||
prevDenom = coin.Denom
|
||||
if err := b.Coins.Validate(); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return nil
|
||||
|
|
Loading…
Reference in New Issue