refactoring balance.coin validation

This commit is contained in:
Spoorthi 2021-08-03 19:43:38 +02:00
parent 2cea5e5750
commit 40d22c7ab3
1 changed files with 3 additions and 29 deletions

View File

@ -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