-
Notifications
You must be signed in to change notification settings - Fork 0
/
msg_server.go
103 lines (84 loc) · 3.11 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
package keeper
import (
"context"
"github.com/osmosis-labs/osmosis/osmoutils"
"github.com/fury-labs/furya/v20/x/incentives/types"
errorsmod "cosmossdk.io/errors"
sdk "github.com/cosmos/cosmos-sdk/types"
sdkerrors "github.com/cosmos/cosmos-sdk/types/errors"
)
// msgServer provides a way to reference keeper pointer in the message server interface.
type msgServer struct {
keeper *Keeper
}
// NewMsgServerImpl returns an instance of MsgServer for the provided keeper.
func NewMsgServerImpl(keeper *Keeper) types.MsgServer {
return &msgServer{
keeper: keeper,
}
}
var _ types.MsgServer = msgServer{}
// CreateGauge creates a gauge and sends coins to the gauge.
// Emits create gauge event and returns the create gauge response.
func (server msgServer) CreateGauge(goCtx context.Context, msg *types.MsgCreateGauge) (*types.MsgCreateGaugeResponse, error) {
ctx := sdk.UnwrapSDKContext(goCtx)
owner, err := sdk.AccAddressFromBech32(msg.Owner)
if err != nil {
return nil, err
}
if err := server.keeper.chargeFeeIfSufficientFeeDenomBalance(ctx, owner, types.CreateGaugeFee, msg.Coins); err != nil {
return nil, err
}
gaugeID, err := server.keeper.CreateGauge(ctx, msg.IsPerpetual, owner, msg.Coins, msg.DistributeTo, msg.StartTime, msg.NumEpochsPaidOver, msg.PoolId)
if err != nil {
return nil, errorsmod.Wrap(sdkerrors.ErrInvalidRequest, err.Error())
}
ctx.EventManager().EmitEvents(sdk.Events{
sdk.NewEvent(
types.TypeEvtCreateGauge,
sdk.NewAttribute(types.AttributeGaugeID, osmoutils.Uint64ToString(gaugeID)),
),
})
return &types.MsgCreateGaugeResponse{}, nil
}
// AddToGauge adds coins to gauge.
// Emits add to gauge event and returns the add to gauge response.
func (server msgServer) AddToGauge(goCtx context.Context, msg *types.MsgAddToGauge) (*types.MsgAddToGaugeResponse, error) {
ctx := sdk.UnwrapSDKContext(goCtx)
owner, err := sdk.AccAddressFromBech32(msg.Owner)
if err != nil {
return nil, err
}
if err := server.keeper.chargeFeeIfSufficientFeeDenomBalance(ctx, owner, types.AddToGaugeFee, msg.Rewards); err != nil {
return nil, err
}
err = server.keeper.AddToGaugeRewards(ctx, owner, msg.Rewards, msg.GaugeId)
if err != nil {
return nil, errorsmod.Wrap(sdkerrors.ErrInvalidRequest, err.Error())
}
ctx.EventManager().EmitEvents(sdk.Events{
sdk.NewEvent(
types.TypeEvtAddToGauge,
sdk.NewAttribute(types.AttributeGaugeID, osmoutils.Uint64ToString(msg.GaugeId)),
),
})
return &types.MsgAddToGaugeResponse{}, nil
}
func (server msgServer) CreateGroup(goCtx context.Context, msg *types.MsgCreateGroup) (*types.MsgCreateGroupResponse, error) {
ctx := sdk.UnwrapSDKContext(goCtx)
owner, err := sdk.AccAddressFromBech32(msg.Owner)
if err != nil {
return nil, err
}
groupID, err := server.keeper.CreateGroup(ctx, msg.Coins, msg.NumEpochsPaidOver, owner, msg.PoolIds)
if err != nil {
return nil, errorsmod.Wrap(sdkerrors.ErrInvalidRequest, err.Error())
}
ctx.EventManager().EmitEvents(sdk.Events{
sdk.NewEvent(
types.TypeEvtCreateGroup,
sdk.NewAttribute(types.AttributeGroupID, osmoutils.Uint64ToString(groupID)),
),
})
return &types.MsgCreateGroupResponse{GroupId: groupID}, nil
}