-
Notifications
You must be signed in to change notification settings - Fork 367
/
msg_server.go
153 lines (120 loc) · 3.91 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
package keeper
import (
"context"
sdk "github.com/cosmos/cosmos-sdk/types"
sdkerrors "github.com/cosmos/cosmos-sdk/types/errors"
"github.com/kava-labs/kava/x/swap/types"
)
type msgServer struct {
keeper Keeper
}
// NewMsgServerImpl returns an implementation of the swap MsgServer interface
// for the provided Keeper.
func NewMsgServerImpl(keeper Keeper) types.MsgServer {
return &msgServer{keeper: keeper}
}
var _ types.MsgServer = msgServer{}
// Deposit handles MsgDeposit messages
func (m msgServer) Deposit(goCtx context.Context, msg *types.MsgDeposit) (*types.MsgDepositResponse, error) {
ctx := sdk.UnwrapSDKContext(goCtx)
if err := checkDeadline(ctx, msg); err != nil {
return nil, err
}
depositor, err := sdk.AccAddressFromBech32(msg.Depositor)
if err != nil {
return nil, err
}
if err := m.keeper.Deposit(ctx, depositor, msg.TokenA, msg.TokenB, msg.Slippage); err != nil {
return nil, err
}
ctx.EventManager().EmitEvent(
sdk.NewEvent(
sdk.EventTypeMessage,
sdk.NewAttribute(sdk.AttributeKeyModule, types.AttributeValueCategory),
sdk.NewAttribute(sdk.AttributeKeySender, depositor.String()),
),
)
return &types.MsgDepositResponse{}, nil
}
// Withdraw handles MsgWithdraw messages
func (m msgServer) Withdraw(goCtx context.Context, msg *types.MsgWithdraw) (*types.MsgWithdrawResponse, error) {
ctx := sdk.UnwrapSDKContext(goCtx)
if err := checkDeadline(ctx, msg); err != nil {
return nil, err
}
from, err := sdk.AccAddressFromBech32(msg.From)
if err != nil {
return nil, err
}
if err := m.keeper.Withdraw(ctx, from, msg.Shares, msg.MinTokenA, msg.MinTokenB); err != nil {
return nil, err
}
ctx.EventManager().EmitEvent(
sdk.NewEvent(
sdk.EventTypeMessage,
sdk.NewAttribute(sdk.AttributeKeyModule, types.AttributeValueCategory),
sdk.NewAttribute(sdk.AttributeKeySender, from.String()),
),
)
return &types.MsgWithdrawResponse{}, nil
}
// SwapExactForTokens handles MsgSwapExactForTokens messages
func (m msgServer) SwapExactForTokens(goCtx context.Context, msg *types.MsgSwapExactForTokens) (*types.MsgSwapExactForTokensResponse, error) {
ctx := sdk.UnwrapSDKContext(goCtx)
if err := checkDeadline(ctx, msg); err != nil {
return nil, err
}
requester, err := sdk.AccAddressFromBech32(msg.Requester)
if err != nil {
return nil, err
}
if err := m.keeper.SwapExactForTokens(ctx, requester, msg.ExactTokenA, msg.TokenB, msg.Slippage); err != nil {
return nil, err
}
ctx.EventManager().EmitEvent(
sdk.NewEvent(
sdk.EventTypeMessage,
sdk.NewAttribute(sdk.AttributeKeyModule, types.AttributeValueCategory),
sdk.NewAttribute(sdk.AttributeKeySender, requester.String()),
),
)
return &types.MsgSwapExactForTokensResponse{}, nil
}
// SwapForExactTokens handles MsgSwapForExactTokens messages
func (m msgServer) SwapForExactTokens(goCtx context.Context, msg *types.MsgSwapForExactTokens) (*types.MsgSwapForExactTokensResponse, error) {
ctx := sdk.UnwrapSDKContext(goCtx)
if err := checkDeadline(ctx, msg); err != nil {
return nil, err
}
requester, err := sdk.AccAddressFromBech32(msg.Requester)
if err != nil {
return nil, err
}
if err := m.keeper.SwapForExactTokens(ctx, requester, msg.TokenA, msg.ExactTokenB, msg.Slippage); err != nil {
return nil, err
}
ctx.EventManager().EmitEvent(
sdk.NewEvent(
sdk.EventTypeMessage,
sdk.NewAttribute(sdk.AttributeKeyModule, types.AttributeValueCategory),
sdk.NewAttribute(sdk.AttributeKeySender, requester.String()),
),
)
return &types.MsgSwapForExactTokensResponse{}, nil
}
// checkDeadline returns an error if block time exceeds an included deadline
func checkDeadline(ctx sdk.Context, msg sdk.Msg) error {
deadlineMsg, ok := msg.(types.MsgWithDeadline)
if !ok {
return nil
}
if deadlineMsg.DeadlineExceeded(ctx.BlockTime()) {
return sdkerrors.Wrapf(
types.ErrDeadlineExceeded,
"block time %d >= deadline %d",
ctx.BlockTime().Unix(),
deadlineMsg.GetDeadline().Unix(),
)
}
return nil
}