-
Notifications
You must be signed in to change notification settings - Fork 48
/
msg_server.go
173 lines (140 loc) · 5.19 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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
package keeper
import (
"context"
errorsmod "cosmossdk.io/errors"
"cosmossdk.io/math"
sdk "github.com/cosmos/cosmos-sdk/types"
govtypes "github.com/cosmos/cosmos-sdk/x/gov/types"
"github.com/elys-network/elys/x/masterchef/types"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"
)
type msgServer struct {
Keeper
}
// NewMsgServerImpl returns an implementation of the MsgServer interface
// for the provided Keeper.
func NewMsgServerImpl(keeper Keeper) types.MsgServer {
return &msgServer{Keeper: keeper}
}
var _ types.MsgServer = msgServer{}
func (k msgServer) AddExternalRewardDenom(goCtx context.Context, msg *types.MsgAddExternalRewardDenom) (*types.MsgAddExternalRewardDenomResponse, error) {
ctx := sdk.UnwrapSDKContext(goCtx)
if k.authority != msg.Authority {
return nil, errorsmod.Wrapf(govtypes.ErrInvalidSigner, "invalid authority; expected %s, got %s", k.authority, msg.Authority)
}
params := k.GetParams(ctx)
index := -1
for i, rewardDenom := range params.SupportedRewardDenoms {
if rewardDenom.Denom == msg.RewardDenom {
index = i
break
}
}
if index == -1 && msg.Supported {
params.SupportedRewardDenoms = append(params.SupportedRewardDenoms, &types.SupportedRewardDenom{
Denom: msg.RewardDenom,
MinAmount: msg.MinAmount,
})
}
if index != -1 && !msg.Supported {
params.SupportedRewardDenoms = append(
params.SupportedRewardDenoms[:index],
params.SupportedRewardDenoms[index+1:]...,
)
}
if index != -1 && msg.Supported {
params.SupportedRewardDenoms[index].MinAmount = msg.MinAmount
}
k.SetParams(ctx, params)
return &types.MsgAddExternalRewardDenomResponse{}, nil
}
func (k msgServer) AddExternalIncentive(goCtx context.Context, msg *types.MsgAddExternalIncentive) (*types.MsgAddExternalIncentiveResponse, error) {
ctx := sdk.UnwrapSDKContext(goCtx)
sender := sdk.MustAccAddressFromBech32(msg.Sender)
if msg.FromBlock < uint64(ctx.BlockHeight()) {
return nil, status.Error(codes.InvalidArgument, "invalid from block")
}
if msg.FromBlock >= msg.ToBlock {
return nil, status.Error(codes.InvalidArgument, "invalid block range")
}
if msg.AmountPerBlock.IsZero() {
return nil, status.Error(codes.InvalidArgument, "invalid amount per block")
}
found := false
params := k.GetParams(ctx)
for _, rewardDenom := range params.SupportedRewardDenoms {
if msg.RewardDenom == rewardDenom.Denom {
found = true
if msg.AmountPerBlock.Mul(
sdk.NewInt(int64(msg.ToBlock - msg.FromBlock)),
).LT(rewardDenom.MinAmount) {
return nil, status.Error(codes.InvalidArgument, "too small amount")
}
break
}
}
if !found {
return nil, status.Error(codes.InvalidArgument, "invalid reward denom")
}
amount := msg.AmountPerBlock.Mul(sdk.NewInt(int64(msg.ToBlock - msg.FromBlock)))
err := k.bankKeeper.SendCoinsFromAccountToModule(ctx, sender, types.ModuleName, sdk.Coins{sdk.NewCoin(msg.RewardDenom, amount)})
if err != nil {
return nil, err
}
k.Keeper.AddExternalIncentive(ctx, types.ExternalIncentive{
Id: 0,
RewardDenom: msg.RewardDenom,
PoolId: msg.PoolId,
FromBlock: msg.FromBlock,
ToBlock: msg.ToBlock,
AmountPerBlock: msg.AmountPerBlock,
Apr: math.LegacyZeroDec(),
})
return &types.MsgAddExternalIncentiveResponse{}, nil
}
func (k msgServer) ClaimRewards(goCtx context.Context, msg *types.MsgClaimRewards) (*types.MsgClaimRewardsResponse, error) {
ctx := sdk.UnwrapSDKContext(goCtx)
sender := sdk.MustAccAddressFromBech32(msg.Sender)
if len(msg.PoolIds) == 0 {
allPools := k.GetAllPools(ctx)
for _, pool := range allPools {
msg.PoolIds = append(msg.PoolIds, pool.PoolId)
}
}
coins := sdk.NewCoins()
for _, poolId := range msg.PoolIds {
k.AfterWithdraw(ctx, poolId, msg.Sender, sdk.ZeroInt())
for _, rewardDenom := range k.GetRewardDenoms(ctx, poolId) {
userRewardInfo, found := k.GetUserRewardInfo(ctx, msg.Sender, poolId, rewardDenom)
if found && userRewardInfo.RewardPending.IsPositive() {
coin := sdk.NewCoin(rewardDenom, userRewardInfo.RewardPending.TruncateInt())
coins = coins.Add(coin)
userRewardInfo.RewardPending = sdk.ZeroDec()
k.SetUserRewardInfo(ctx, userRewardInfo)
}
}
}
// Send coins for rest of rewards
err := k.cmk.SendCoinsFromModuleToAccount(ctx, types.ModuleName, sender, coins)
if err != nil {
return nil, err
}
return &types.MsgClaimRewardsResponse{}, nil
}
func (k msgServer) UpdateParams(goCtx context.Context, msg *types.MsgUpdateParams) (*types.MsgUpdateParamsResponse, error) {
ctx := sdk.UnwrapSDKContext(goCtx)
if k.authority != msg.Authority {
return nil, errorsmod.Wrapf(govtypes.ErrInvalidSigner, "invalid authority; expected %s, got %s", k.authority, msg.Authority)
}
k.SetParams(ctx, msg.Params)
return &types.MsgUpdateParamsResponse{}, nil
}
func (k msgServer) UpdatePoolMultipliers(goCtx context.Context, msg *types.MsgUpdatePoolMultipliers) (*types.MsgUpdatePoolMultipliersResponse, error) {
ctx := sdk.UnwrapSDKContext(goCtx)
if k.authority != msg.Authority {
return nil, errorsmod.Wrapf(govtypes.ErrInvalidSigner, "invalid authority; expected %s, got %s", k.authority, msg.Authority)
}
k.Keeper.UpdatePoolMultipliers(ctx, msg.PoolMultipliers)
return &types.MsgUpdatePoolMultipliersResponse{}, nil
}