refactoring balance.coin validation
This commit is contained in:
parent
2cea5e5750
commit
40d22c7ab3
|
@ -30,40 +30,14 @@ func (b Balance) GetCoins() sdk.Coins {
|
||||||
|
|
||||||
// Validate checks for address and coins correctness.
|
// Validate checks for address and coins correctness.
|
||||||
func (b Balance) Validate() error {
|
func (b Balance) Validate() error {
|
||||||
_, err := sdk.AccAddressFromBech32(b.Address)
|
if _, err := sdk.AccAddressFromBech32(b.Address); err != nil {
|
||||||
if err != nil {
|
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
var prevDenom string
|
if err := b.Coins.Validate(); err != nil {
|
||||||
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
|
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
|
|
||||||
}
|
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue