-
Notifications
You must be signed in to change notification settings - Fork 204
/
gov.go
106 lines (91 loc) · 2.9 KB
/
gov.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
96
97
98
99
100
101
102
103
104
105
106
package gov
import (
sdkmath "cosmossdk.io/math"
sdk "github.com/cosmos/cosmos-sdk/types"
transfertypes "github.com/cosmos/ibc-go/v7/modules/apps/transfer/types"
channelkeeper "github.com/cosmos/ibc-go/v7/modules/core/04-channel/keeper"
"github.com/Stride-Labs/stride/v11/x/ratelimit/keeper"
"github.com/Stride-Labs/stride/v11/x/ratelimit/types"
)
// Adds a new rate limit. Fails if the rate limit already exists or the channel value is 0
func AddRateLimit(ctx sdk.Context, k keeper.Keeper, channelKeeper channelkeeper.Keeper, p *types.AddRateLimitProposal) error {
// Confirm the channel value is not zero
channelValue := k.GetChannelValue(ctx, p.Denom)
if channelValue.IsZero() {
return types.ErrZeroChannelValue
}
// Confirm the rate limit does not already exist
_, found := k.GetRateLimit(ctx, p.Denom, p.ChannelId)
if found {
return types.ErrRateLimitAlreadyExists
}
// Confirm the channel exists
_, found = channelKeeper.GetChannel(ctx, transfertypes.PortID, p.ChannelId)
if !found {
return types.ErrChannelNotFound
}
// Create and store the rate limit object
path := types.Path{
Denom: p.Denom,
ChannelId: p.ChannelId,
}
quota := types.Quota{
MaxPercentSend: p.MaxPercentSend,
MaxPercentRecv: p.MaxPercentRecv,
DurationHours: p.DurationHours,
}
flow := types.Flow{
Inflow: sdkmath.ZeroInt(),
Outflow: sdkmath.ZeroInt(),
ChannelValue: channelValue,
}
k.SetRateLimit(ctx, types.RateLimit{
Path: &path,
Quota: "a,
Flow: &flow,
})
return nil
}
// Updates an existing rate limit. Fails if the rate limit doesn't exist
func UpdateRateLimit(ctx sdk.Context, k keeper.Keeper, p *types.UpdateRateLimitProposal) error {
// Confirm the rate limit exists
_, found := k.GetRateLimit(ctx, p.Denom, p.ChannelId)
if !found {
return types.ErrRateLimitNotFound
}
// Update the rate limit object with the new quota information
// The flow should also get reset to 0
path := types.Path{
Denom: p.Denom,
ChannelId: p.ChannelId,
}
quota := types.Quota{
MaxPercentSend: p.MaxPercentSend,
MaxPercentRecv: p.MaxPercentRecv,
DurationHours: p.DurationHours,
}
flow := types.Flow{
Inflow: sdkmath.ZeroInt(),
Outflow: sdkmath.ZeroInt(),
ChannelValue: k.GetChannelValue(ctx, p.Denom),
}
k.SetRateLimit(ctx, types.RateLimit{
Path: &path,
Quota: "a,
Flow: &flow,
})
return nil
}
// Removes a rate limit. Fails if the rate limit doesn't exist
func RemoveRateLimit(ctx sdk.Context, k keeper.Keeper, msg *types.RemoveRateLimitProposal) error {
_, found := k.GetRateLimit(ctx, msg.Denom, msg.ChannelId)
if !found {
return types.ErrRateLimitNotFound
}
k.RemoveRateLimit(ctx, msg.Denom, msg.ChannelId)
return nil
}
// Resets the flow on a rate limit. Fails if the rate limit doesn't exist
func ResetRateLimit(ctx sdk.Context, k keeper.Keeper, msg *types.ResetRateLimitProposal) error {
return k.ResetRateLimit(ctx, msg.Denom, msg.ChannelId)
}