/
msg_server.go
171 lines (140 loc) · 5.72 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
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"
"strconv"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/oracleNetworkProtocol/liquidity/x/liquidity/types"
)
type msgServer struct {
Keeper
}
// NewMsgServerImpl returns an implementation of the distribution MsgServer interface
// for the provided Keeper.
func NewMsgServerImpl(keeper Keeper) types.MsgServer {
return &msgServer{Keeper: keeper}
}
var _ types.MsgServer = msgServer{}
// Message server, handler for CreatePool msg
func (k msgServer) CreatePool(goCtx context.Context, msg *types.MsgCreatePool) (*types.MsgCreatePoolResponse, error) {
ctx := sdk.UnwrapSDKContext(goCtx)
if k.GetCircuitBreakerEnabled(ctx) {
return nil, types.ErrCircuitBreakerEnabled
}
pool, err := k.Keeper.CreatePool(ctx, msg)
if err != nil {
return nil, err
}
ctx.EventManager().EmitEvents(sdk.Events{
sdk.NewEvent(
sdk.EventTypeMessage,
sdk.NewAttribute(sdk.AttributeKeyModule, types.AttributeValueCategory),
),
sdk.NewEvent(
types.EventTypeCreatePool,
sdk.NewAttribute(types.AttributeValuePoolId, strconv.FormatUint(pool.Id, 10)),
sdk.NewAttribute(types.AttributeValuePoolTypeId, fmt.Sprintf("%d", msg.PoolTypeId)),
sdk.NewAttribute(types.AttributeValuePoolName, pool.Name()),
sdk.NewAttribute(types.AttributeValueReserveAccount, pool.ReserveAccountAddress),
sdk.NewAttribute(types.AttributeValueDepositCoins, msg.DepositCoins.String()),
sdk.NewAttribute(types.AttributeValuePoolCoinDenom, pool.PoolCoinDenom),
),
})
return &types.MsgCreatePoolResponse{}, nil
}
// Message server, handler for MsgDepositWithinBatch
func (k msgServer) DepositWithinBatch(goCtx context.Context, msg *types.MsgDepositWithinBatch) (*types.MsgDepositWithinBatchResponse, error) {
ctx := sdk.UnwrapSDKContext(goCtx)
if k.GetCircuitBreakerEnabled(ctx) {
return nil, types.ErrCircuitBreakerEnabled
}
poolBatch, found := k.GetPoolBatch(ctx, msg.PoolId)
if !found {
return nil, types.ErrPoolBatchNotExists
}
batchMsg, err := k.Keeper.DepositWithinBatch(ctx, msg)
if err != nil {
return nil, err
}
ctx.EventManager().EmitEvents(sdk.Events{
sdk.NewEvent(
sdk.EventTypeMessage,
sdk.NewAttribute(sdk.AttributeKeyModule, types.AttributeValueCategory),
),
sdk.NewEvent(
types.EventTypeDepositWithinBatch,
sdk.NewAttribute(types.AttributeValuePoolId, strconv.FormatUint(batchMsg.Msg.PoolId, 10)),
sdk.NewAttribute(types.AttributeValueBatchIndex, strconv.FormatUint(poolBatch.Index, 10)),
sdk.NewAttribute(types.AttributeValueMsgIndex, strconv.FormatUint(batchMsg.MsgIndex, 10)),
sdk.NewAttribute(types.AttributeValueDepositCoins, batchMsg.Msg.DepositCoins.String()),
),
})
return &types.MsgDepositWithinBatchResponse{}, nil
}
// Message server, handler for MsgWithdrawWithinBatch
func (k msgServer) WithdrawWithinBatch(goCtx context.Context, msg *types.MsgWithdrawWithinBatch) (*types.MsgWithdrawWithinBatchResponse, error) {
ctx := sdk.UnwrapSDKContext(goCtx)
// if k.GetCircuitBreakerEnabled(ctx) {
// return nil, types.ErrCircuitBreakerEnabled
// }
poolBatch, found := k.GetPoolBatch(ctx, msg.PoolId)
if !found {
return nil, types.ErrPoolBatchNotExists
}
batchMsg, err := k.Keeper.WithdrawWithinBatch(ctx, msg)
if err != nil {
return nil, err
}
ctx.EventManager().EmitEvents(sdk.Events{
sdk.NewEvent(
sdk.EventTypeMessage,
sdk.NewAttribute(sdk.AttributeKeyModule, types.AttributeValueCategory),
),
sdk.NewEvent(
types.EventTypeWithdrawWithinBatch,
sdk.NewAttribute(types.AttributeValuePoolId, strconv.FormatUint(batchMsg.Msg.PoolId, 10)),
sdk.NewAttribute(types.AttributeValueBatchIndex, strconv.FormatUint(poolBatch.Index, 10)),
sdk.NewAttribute(types.AttributeValueMsgIndex, strconv.FormatUint(batchMsg.MsgIndex, 10)),
sdk.NewAttribute(types.AttributeValuePoolCoinDenom, batchMsg.Msg.PoolCoin.Denom),
sdk.NewAttribute(types.AttributeValuePoolCoinAmount, batchMsg.Msg.PoolCoin.Amount.String()),
),
})
return &types.MsgWithdrawWithinBatchResponse{}, nil
}
// Message server, handler for MsgSwapWithinBatch
func (k msgServer) Swap(goCtx context.Context, msg *types.MsgSwapWithinBatch) (*types.MsgSwapWithinBatchResponse, error) {
ctx := sdk.UnwrapSDKContext(goCtx)
if k.GetCircuitBreakerEnabled(ctx) {
return nil, types.ErrCircuitBreakerEnabled
}
poolBatch, found := k.GetPoolBatch(ctx, msg.PoolId)
if !found {
return nil, types.ErrPoolBatchNotExists
}
batchMsg, err := k.Keeper.SwapWithinBatch(ctx, msg, types.CancelOrderLifeSpan)
if err != nil {
return nil, err
}
ctx.EventManager().EmitEvents(sdk.Events{
sdk.NewEvent(
sdk.EventTypeMessage,
sdk.NewAttribute(sdk.AttributeKeyModule, types.AttributeValueCategory),
),
sdk.NewEvent(
types.EventTypeSwapWithinBatch,
sdk.NewAttribute(types.AttributeValuePoolId, strconv.FormatUint(batchMsg.Msg.PoolId, 10)),
sdk.NewAttribute(types.AttributeValueBatchIndex, strconv.FormatUint(poolBatch.Index, 10)),
sdk.NewAttribute(types.AttributeValueMsgIndex, strconv.FormatUint(batchMsg.MsgIndex, 10)),
sdk.NewAttribute(types.AttributeValueSwapTypeId, strconv.FormatUint(uint64(batchMsg.Msg.SwapTypeId), 10)),
sdk.NewAttribute(types.AttributeValueOfferCoinDenom, batchMsg.Msg.OfferCoin.Denom),
sdk.NewAttribute(types.AttributeValueOfferCoinAmount, batchMsg.Msg.OfferCoin.Amount.String()),
sdk.NewAttribute(types.AttributeValueOfferCoinFeeAmount, batchMsg.Msg.OfferCoinFee.Amount.String()),
sdk.NewAttribute(types.AttributeValueDemandCoinDenom, batchMsg.Msg.DemandCoinDenom),
sdk.NewAttribute(types.AttributeValueOrderPrice, batchMsg.Msg.OrderPrice.String()),
),
})
return &types.MsgSwapWithinBatchResponse{}, nil
}