-
Notifications
You must be signed in to change notification settings - Fork 201
/
gov_add_rate_limit.go
95 lines (75 loc) · 2.8 KB
/
gov_add_rate_limit.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
91
92
93
94
95
package types
import (
"fmt"
"regexp"
errorsmod "cosmossdk.io/errors"
sdkmath "cosmossdk.io/math"
sdkerrors "github.com/cosmos/cosmos-sdk/types/errors"
govtypes "github.com/cosmos/cosmos-sdk/x/gov/types/v1beta1"
)
const (
ProposalTypeAddRateLimit = "AddRateLimit"
)
func init() {
govtypes.RegisterProposalType(ProposalTypeAddRateLimit)
}
var (
_ govtypes.Content = &AddRateLimitProposal{}
)
func NewAddRateLimitProposal(title, description, denom, channelId string, maxPercentSend sdkmath.Int, maxPercentRecv sdkmath.Int, durationHours uint64) govtypes.Content {
return &AddRateLimitProposal{
Title: title,
Description: description,
Denom: denom,
ChannelId: channelId,
MaxPercentSend: maxPercentSend,
MaxPercentRecv: maxPercentRecv,
DurationHours: durationHours,
}
}
func (p *AddRateLimitProposal) GetTitle() string { return p.Title }
func (p *AddRateLimitProposal) GetDescription() string { return p.Description }
func (p *AddRateLimitProposal) ProposalRoute() string { return RouterKey }
func (p *AddRateLimitProposal) ProposalType() string {
return ProposalTypeAddRateLimit
}
func (p *AddRateLimitProposal) ValidateBasic() error {
err := govtypes.ValidateAbstract(p)
if err != nil {
return err
}
if p.Denom == "" {
return errorsmod.Wrapf(sdkerrors.ErrInvalidRequest, "invalid denom (%s)", p.Denom)
}
matched, err := regexp.MatchString(`^channel-\d+$`, p.ChannelId)
if err != nil {
return errorsmod.Wrapf(sdkerrors.ErrInvalidRequest, "unable to verify channel-id (%s)", p.ChannelId)
}
if !matched {
return errorsmod.Wrapf(sdkerrors.ErrInvalidRequest, "invalid channel-id (%s), must be of the format 'channel-{N}'", p.ChannelId)
}
if p.MaxPercentSend.GT(sdkmath.NewInt(100)) || p.MaxPercentSend.LT(sdkmath.ZeroInt()) {
return errorsmod.Wrapf(sdkerrors.ErrInvalidRequest, "max-percent-send percent must be between 0 and 100 (inclusively), Provided: %v", p.MaxPercentSend)
}
if p.MaxPercentRecv.GT(sdkmath.NewInt(100)) || p.MaxPercentRecv.LT(sdkmath.ZeroInt()) {
return errorsmod.Wrapf(sdkerrors.ErrInvalidRequest, "max-percent-recv percent must be between 0 and 100 (inclusively), Provided: %v", p.MaxPercentRecv)
}
if p.MaxPercentRecv.IsZero() && p.MaxPercentSend.IsZero() {
return errorsmod.Wrapf(sdkerrors.ErrInvalidRequest, "either the max send or max receive threshold must be greater than 0")
}
if p.DurationHours == 0 {
return errorsmod.Wrapf(sdkerrors.ErrInvalidRequest, "duration can not be zero")
}
return nil
}
func (p AddRateLimitProposal) String() string {
return fmt.Sprintf(`Add Rate Limit Proposal:
Title: %s
Description: %s
Denom: %s
ChannelId: %s
MaxPercentSend: %v
MaxPercentRecv: %v
DurationHours: %d
`, p.Title, p.Description, p.Denom, p.ChannelId, p.MaxPercentSend, p.MaxPercentRecv, p.DurationHours)
}