-
Notifications
You must be signed in to change notification settings - Fork 197
/
gov_update_rate_limit.go
95 lines (75 loc) · 2.85 KB
/
gov_update_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 (
ProposalTypeUpdateRateLimit = "UpdateRateLimit"
)
func init() {
govtypes.RegisterProposalType(ProposalTypeUpdateRateLimit)
}
var (
_ govtypes.Content = &UpdateRateLimitProposal{}
)
func NewUpdateRateLimitProposal(title, description, denom, channelId string, maxPercentSend sdkmath.Int, maxPercentRecv sdkmath.Int, durationHours uint64) govtypes.Content {
return &UpdateRateLimitProposal{
Title: title,
Description: description,
Denom: denom,
ChannelId: channelId,
MaxPercentSend: maxPercentSend,
MaxPercentRecv: maxPercentRecv,
DurationHours: durationHours,
}
}
func (p *UpdateRateLimitProposal) GetTitle() string { return p.Title }
func (p *UpdateRateLimitProposal) GetDescription() string { return p.Description }
func (p *UpdateRateLimitProposal) ProposalRoute() string { return RouterKey }
func (p *UpdateRateLimitProposal) ProposalType() string {
return ProposalTypeUpdateRateLimit
}
func (p *UpdateRateLimitProposal) 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 UpdateRateLimitProposal) String() string {
return fmt.Sprintf(`Update 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)
}