-
Notifications
You must be signed in to change notification settings - Fork 12
/
msg_server.go
62 lines (50 loc) · 1.92 KB
/
msg_server.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
package keeper
import (
"context"
errorsmod "cosmossdk.io/errors"
sdk "github.com/cosmos/cosmos-sdk/types"
govtypes "github.com/cosmos/cosmos-sdk/x/gov/types"
"github.com/osmosis-labs/mesh-security-sdk/x/meshsecurity/types"
)
var _ types.MsgServer = msgServer{}
type msgServer struct {
k *Keeper
}
// NewMsgServer constructor
func NewMsgServer(k *Keeper) *msgServer {
return &msgServer{k: k}
}
// SetVirtualStakingMaxCap sets a new max cap limit for virtual staking
func (m msgServer) SetVirtualStakingMaxCap(goCtx context.Context, req *types.MsgSetVirtualStakingMaxCap) (*types.MsgSetVirtualStakingMaxCapResponse, error) {
if err := req.ValidateBasic(); err != nil {
return nil, err
}
if authority := m.k.GetAuthority(); authority != req.Authority {
return nil, govtypes.ErrInvalidSigner.Wrapf("invalid authority; expected %s, got %s", authority, req.Authority)
}
acc, err := sdk.AccAddressFromBech32(req.Contract)
if err != nil {
return nil, errorsmod.Wrap(err, "contract")
}
ctx := sdk.UnwrapSDKContext(goCtx)
if err := m.k.SetMaxCapLimit(ctx, acc, req.MaxCap); err != nil {
return nil, err
}
if !m.k.HasScheduledTask(ctx, types.SchedulerTaskHandleEpoch, acc, true) {
if err := m.k.ScheduleRegularRebalanceTask(ctx, acc); err != nil {
return nil, errorsmod.Wrap(err, "schedule regular rebalance task")
}
return &types.MsgSetVirtualStakingMaxCapResponse{}, nil
}
if req.MaxCap.IsZero() {
// no need to run regular rebalances with a new limit of 0
if err := m.k.DeleteAllScheduledTasks(ctx, types.SchedulerTaskHandleEpoch, acc); err != nil {
return nil, err
}
}
// schedule last rebalance callback to let the contract do undelegates and housekeeping
if err := m.k.ScheduleOneShotTask(ctx, types.SchedulerTaskHandleEpoch, acc, uint64(ctx.BlockHeight())); err != nil {
return nil, errorsmod.Wrap(err, "schedule one shot rebalance task")
}
return &types.MsgSetVirtualStakingMaxCapResponse{}, nil
}