-
Notifications
You must be signed in to change notification settings - Fork 28
/
validation.go
90 lines (76 loc) · 2.69 KB
/
validation.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
package types
import (
"strconv"
"strings"
sdk "github.com/cosmos/cosmos-sdk/types"
sdkerrors "github.com/cosmos/cosmos-sdk/types/errors"
)
const (
// MaxDescriptionLength length of the service and author description
MaxDescriptionLength = 280
)
// ValidatepPoolId validates the pool id
func ValidatepPoolId(poolId string) (uint64, error) {
seqStr := strings.TrimPrefix(poolId, PrefixFarmPool+"-")
seq, err := strconv.ParseUint(seqStr, 10, 64)
if err != nil || seq == 0 {
return 0, ErrInvalidPoolId
}
return seq, nil
}
// ValidateDescription validates the pool name
func ValidateDescription(description string) error {
if len(description) > MaxDescriptionLength {
return sdkerrors.Wrap(ErrInvalidDescription, description)
}
return nil
}
// ValidateLpTokenDenom validates the lp token denom
func ValidateLpTokenDenom(denom string) error {
return sdk.ValidateDenom(denom)
}
// ValidateCoins validates the coin
func ValidateCoins(field string, coins ...sdk.Coin) error {
if len(coins) == 0 {
return sdkerrors.Wrapf(sdkerrors.ErrInvalidCoins, "The %s should be greater than zero", field)
}
return sdk.NewCoins(coins...).Validate()
}
// ValidateAddress validates the address
func ValidateAddress(sender string) error {
_, err := sdk.AccAddressFromBech32(sender)
return err
}
// ValidateReward validates the coin
func ValidateReward(rewardPerBlock, totalReward sdk.Coins) error {
if len(rewardPerBlock) != len(totalReward) {
return sdkerrors.Wrapf(ErrNotMatch, "The length of rewardPerBlock and totalReward must be the same")
}
if !rewardPerBlock.DenomsSubsetOf(totalReward) {
return sdkerrors.Wrapf(ErrInvalidRewardRule, "rewardPerBlock and totalReward token types must be the same")
}
for i := range totalReward {
if !totalReward[i].IsGTE(rewardPerBlock[i]) {
return sdkerrors.Wrapf(ErrNotMatch, "The totalReward should be greater than or equal to rewardPerBlock")
}
//uint64 overflow check
h := totalReward[i].Amount.Quo(rewardPerBlock[i].Amount)
if !h.IsInt64() {
return sdkerrors.Wrapf(sdkerrors.ErrInvalidRequest, "Can not convert to int64, overflow")
}
}
return nil
}
func ValidateFund(rewardPerBlock, fundApplied, fundSelfBond []sdk.Coin) error {
if err := ValidateCoins("FundApplied", fundApplied...); err != nil {
return err
}
if err := sdk.NewCoins(fundSelfBond...).Validate(); err != nil {
return sdkerrors.Wrapf(err, "The fundSelfBond is invalid coin")
}
total := sdk.NewCoins(fundApplied...).Add(fundSelfBond...)
if len(fundApplied)+len(fundSelfBond) != total.Len() {
return sdkerrors.Wrapf(ErrInvalidProposal, "the type of The token bond by the user cannot be the same as the one applied for community pool")
}
return ValidateReward(rewardPerBlock, total)
}