-
Notifications
You must be signed in to change notification settings - Fork 3
/
msg_server.go
129 lines (97 loc) · 3.84 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
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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
package keeper
// DONTCOVER
// Although written in msg_server_test.go, it is approached at the keeper level rather than at the msgServer level
// so is not included in the coverage.
import (
"context"
"fmt"
"time"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmosquad-labs/squad/v3/x/farming/types"
)
type msgServer struct {
Keeper
}
// NewMsgServerImpl returns an implementation of the farming MsgServer interface
// for the provided Keeper.
func NewMsgServerImpl(keeper Keeper) types.MsgServer {
return &msgServer{Keeper: keeper}
}
var _ types.MsgServer = msgServer{}
// CreateFixedAmountPlan defines a method for creating fixed amount farming plan.
func (k msgServer) CreateFixedAmountPlan(goCtx context.Context, msg *types.MsgCreateFixedAmountPlan) (*types.MsgCreateFixedAmountPlanResponse, error) {
ctx := sdk.UnwrapSDKContext(goCtx)
poolAcc, err := k.DerivePrivatePlanFarmingPoolAcc(ctx, msg.Name)
if err != nil {
return nil, err
}
if _, err := k.Keeper.CreateFixedAmountPlan(ctx, msg, poolAcc, msg.GetCreator(), types.PlanTypePrivate); err != nil {
return nil, err
}
return &types.MsgCreateFixedAmountPlanResponse{}, nil
}
// CreateRatioPlan defines a method for creating ratio farming plan.
func (k msgServer) CreateRatioPlan(goCtx context.Context, msg *types.MsgCreateRatioPlan) (*types.MsgCreateRatioPlanResponse, error) {
ctx := sdk.UnwrapSDKContext(goCtx)
if !EnableRatioPlan {
return nil, types.ErrRatioPlanDisabled
}
poolAcc, err := k.DerivePrivatePlanFarmingPoolAcc(ctx, msg.Name)
if err != nil {
return nil, err
}
if _, err := k.Keeper.CreateRatioPlan(ctx, msg, poolAcc, msg.GetCreator(), types.PlanTypePrivate); err != nil {
return nil, err
}
plans := k.GetPlans(ctx)
if err := types.ValidateTotalEpochRatio(plans); err != nil {
return nil, err
}
return &types.MsgCreateRatioPlanResponse{}, nil
}
// Stake defines a method for staking coins to the farming plan.
func (k msgServer) Stake(goCtx context.Context, msg *types.MsgStake) (*types.MsgStakeResponse, error) {
ctx := sdk.UnwrapSDKContext(goCtx)
if err := k.Keeper.Stake(ctx, msg.GetFarmer(), msg.StakingCoins); err != nil {
return nil, err
}
return &types.MsgStakeResponse{}, nil
}
// Unstake defines a method for unstaking coins from the farming plan.
func (k msgServer) Unstake(goCtx context.Context, msg *types.MsgUnstake) (*types.MsgUnstakeResponse, error) {
ctx := sdk.UnwrapSDKContext(goCtx)
if err := k.Keeper.Unstake(ctx, msg.GetFarmer(), msg.UnstakingCoins); err != nil {
return nil, err
}
return &types.MsgUnstakeResponse{}, nil
}
// Harvest defines a method for claiming farming rewards from the farming plan.
func (k msgServer) Harvest(goCtx context.Context, msg *types.MsgHarvest) (*types.MsgHarvestResponse, error) {
ctx := sdk.UnwrapSDKContext(goCtx)
if err := k.Keeper.Harvest(ctx, msg.GetFarmer(), msg.StakingCoinDenoms); err != nil {
return nil, err
}
return &types.MsgHarvestResponse{}, nil
}
func (k msgServer) RemovePlan(goCtx context.Context, msg *types.MsgRemovePlan) (*types.MsgRemovePlanResponse, error) {
ctx := sdk.UnwrapSDKContext(goCtx)
if err := k.Keeper.RemovePlan(ctx, msg.GetCreator(), msg.PlanId); err != nil {
return nil, err
}
return &types.MsgRemovePlanResponse{}, nil
}
// AdvanceEpoch defines a method for advancing epoch by one, just for testing purpose
// and shouldn't be used in real world.
func (k msgServer) AdvanceEpoch(goCtx context.Context, msg *types.MsgAdvanceEpoch) (*types.MsgAdvanceEpochResponse, error) {
ctx := sdk.UnwrapSDKContext(goCtx)
if EnableAdvanceEpoch {
currentEpochDays := k.GetCurrentEpochDays(ctx)
ctx = ctx.WithBlockTime(ctx.BlockTime().Add(time.Duration(currentEpochDays) * types.Day))
if err := k.Keeper.AdvanceEpoch(ctx); err != nil {
return nil, err
}
} else {
return nil, fmt.Errorf("AdvanceEpoch is disabled")
}
return &types.MsgAdvanceEpochResponse{}, nil
}