-
Notifications
You must be signed in to change notification settings - Fork 182
/
handler.go
346 lines (298 loc) · 9.88 KB
/
handler.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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
package order
import (
"encoding/json"
"fmt"
"math"
storetypes "github.com/cosmos/cosmos-sdk/store/types"
sdk "github.com/cosmos/cosmos-sdk/types"
sdkerrors "github.com/cosmos/cosmos-sdk/types/errors"
"github.com/okex/exchain/x/common"
"github.com/okex/exchain/x/common/perf"
"github.com/okex/exchain/x/order/keeper"
"github.com/okex/exchain/x/order/types"
"github.com/tendermint/tendermint/crypto/tmhash"
"github.com/tendermint/tendermint/libs/log"
"github.com/willf/bitset"
)
func CalculateGas(msg sdk.Msg, params *types.Params) (gas uint64) {
switch msg := msg.(type) {
case types.MsgNewOrders:
gas = msg.CalculateGas(params.NewOrderMsgGasUnit)
case types.MsgCancelOrders:
gas = msg.CalculateGas(params.CancelOrderMsgGasUnit)
default:
gas = math.MaxUint64
}
return gas
}
// NewOrderHandler returns the handler with version 0.
func NewOrderHandler(keeper keeper.Keeper) sdk.Handler {
return func(ctx sdk.Context, msg sdk.Msg) (*sdk.Result, error) {
// disable order tx handler
return nil, sdkerrors.Wrap(sdkerrors.ErrUnknownRequest, "Order messages are not allowd.")
gas := CalculateGas(msg, keeper.GetParams(ctx))
// consume gas that msg required, it will panic if gas is insufficient
ctx.GasMeter().ConsumeGas(gas, storetypes.GasWriteCostFlatDesc)
if ctx.IsCheckTx() {
return &sdk.Result{}, nil
} else {
// set an infinite gas meter and recovery it when return
gasMeter := ctx.GasMeter()
ctx = ctx.WithGasMeter(sdk.NewInfiniteGasMeter())
defer func() { ctx = ctx.WithGasMeter(gasMeter) }()
}
ctx = ctx.WithEventManager(sdk.NewEventManager())
var handlerFun func() (*sdk.Result, error)
var name string
logger := ctx.Logger().With("module", "order")
switch msg := msg.(type) {
case types.MsgNewOrders:
name = "handleMsgNewOrders"
handlerFun = func() (*sdk.Result, error) {
return handleMsgNewOrders(ctx, keeper, msg, logger)
}
case types.MsgCancelOrders:
name = "handleMsgCancelOrders"
handlerFun = func() (*sdk.Result, error) {
return handleMsgCancelOrders(ctx, keeper, msg, logger)
}
default:
errMsg := fmt.Sprintf("Invalid msg type: %v", msg.Type())
return sdk.ErrUnknownRequest(errMsg).Result()
}
seq := perf.GetPerf().OnDeliverTxEnter(ctx, types.ModuleName, name)
defer perf.GetPerf().OnDeliverTxExit(ctx, types.ModuleName, name, seq)
res, err := handlerFun()
common.SanityCheckHandler(res, err)
return res, err
}
}
// checkOrderNewMsg: check msg product, price & quantity fields
func checkOrderNewMsg(ctx sdk.Context, keeper keeper.Keeper, msg types.MsgNewOrder) error {
tokenPair := keeper.GetDexKeeper().GetTokenPair(ctx, msg.Product)
if tokenPair == nil {
return types.ErrTokenPairNotExist(msg.Product)
}
// check if the order is involved with the tokenpair in dex Delist
isDelisting, err := keeper.GetDexKeeper().CheckTokenPairUnderDexDelist(ctx, msg.Product)
if err != nil {
return err
}
if isDelisting {
return types.ErrTradingPairIsDelisting(msg.Product)
}
priceDigit := tokenPair.MaxPriceDigit
quantityDigit := tokenPair.MaxQuantityDigit
roundedPrice := msg.Price.RoundDecimal(priceDigit)
roundedQuantity := msg.Quantity.RoundDecimal(quantityDigit)
if !roundedPrice.Equal(msg.Price) {
return types.ErrPriceOverAccuracy(msg.Price, priceDigit)
}
if !roundedQuantity.Equal(msg.Quantity) {
return types.ErrQuantityOverAccuracy(msg.Quantity, quantityDigit)
}
if msg.Quantity.LT(tokenPair.MinQuantity) {
return types.ErrMsgQuantityLessThan(tokenPair.MinQuantity.String())
}
return nil
}
func getOrderFromMsg(ctx sdk.Context, k keeper.Keeper, msg types.MsgNewOrder, ratio string) *types.Order {
feeParams := k.GetParams(ctx)
feePerBlockAmount := feeParams.FeePerBlock.Amount.Mul(sdk.MustNewDecFromStr(ratio))
feePerBlock := sdk.NewDecCoinFromDec(feeParams.FeePerBlock.Denom, feePerBlockAmount)
return types.NewOrder(
fmt.Sprintf("%X", tmhash.Sum(ctx.TxBytes())),
msg.Sender,
msg.Product,
msg.Side,
msg.Price,
msg.Quantity,
ctx.BlockHeader().Time.Unix(),
feeParams.OrderExpireBlocks,
feePerBlock,
)
}
func handleNewOrder(ctx sdk.Context, k Keeper, sender sdk.AccAddress,
item types.OrderItem, ratio string, logger log.Logger) (types.OrderResult, sdk.CacheMultiStore, error) {
cacheItem := ctx.MultiStore().CacheMultiStore()
ctxItem := ctx.WithMultiStore(cacheItem)
msg := MsgNewOrder{
Sender: sender,
Product: item.Product,
Side: item.Side,
Price: item.Price,
Quantity: item.Quantity,
}
order := getOrderFromMsg(ctxItem, k, msg, ratio)
err := checkOrderNewMsg(ctxItem, k, msg)
if err == nil {
if k.IsProductLocked(ctx, msg.Product) {
err = types.ErrIsProductLocked(order.Product)
} else {
err = k.PlaceOrder(ctxItem, order)
}
}
res := types.OrderResult{
Error: err,
OrderID: order.OrderID,
}
if err == nil {
logger.Debug(fmt.Sprintf("BlockHeight<%d>, handler<%s>\n"+
" msg<Product:%s,Sender:%s,Price:%s,Quantity:%s,Side:%s>\n"+
" TxHash<%s>, Status<%s>\n"+
" result<The User have created an order {ID:%s,RemainQuantity:%s,Status:%s} >\n",
ctx.BlockHeight(), "handleMsgNewOrder",
msg.Product, msg.Sender, msg.Price.String(), msg.Quantity.String(), msg.Side,
order.TxHash, types.OrderStatus(types.OrderStatusOpen),
order.OrderID, order.RemainQuantity.String(), types.OrderStatus(order.Status)))
} else {
res.Message = err.Error()
}
return res, cacheItem, err
}
func handleMsgNewOrders(ctx sdk.Context, k Keeper, msg types.MsgNewOrders,
logger log.Logger) (*sdk.Result, error) {
event := sdk.NewEvent(sdk.EventTypeMessage, sdk.NewAttribute(sdk.AttributeKeyModule, types.ModuleName))
ratio := "1"
if len(msg.OrderItems) > 1 {
ratio = "0.8"
}
rs := make([]types.OrderResult, 0, len(msg.OrderItems))
var handlerResult bitset.BitSet
for idx, item := range msg.OrderItems {
res, cacheItem, err := handleNewOrder(ctx, k, msg.Sender, item, ratio, logger)
if err == nil {
cacheItem.Write()
handlerResult.Set(uint(idx))
}
rs = append(rs, res)
}
rss, err := json.Marshal(&rs)
if err != nil {
rss = []byte(fmt.Sprintf("failed to marshal result to JSON: %s", err))
}
event = event.AppendAttributes(sdk.NewAttribute("orders", string(rss)))
ctx.EventManager().EmitEvent(event)
if handlerResult.None() {
return types.ErrAllOrderFailedToExecute().Result()
}
k.AddTxHandlerMsgResult(handlerResult)
return &sdk.Result{
Events: ctx.EventManager().Events(),
}, nil
}
// ValidateMsgNewOrders validates whether the msg of newOrders is valid.
func ValidateMsgNewOrders(ctx sdk.Context, k keeper.Keeper, msg types.MsgNewOrders) (*sdk.Result, error) {
ratio := "1"
if len(msg.OrderItems) > 1 {
ratio = "0.8"
}
for _, item := range msg.OrderItems {
msg := MsgNewOrder{
Sender: msg.Sender,
Product: item.Product,
Side: item.Side,
Price: item.Price,
Quantity: item.Quantity,
}
err := checkOrderNewMsg(ctx, k, msg)
if err != nil {
return nil, err
}
if k.IsProductLocked(ctx, msg.Product) {
return types.ErrIsProductLocked(msg.Product).Result()
}
order := getOrderFromMsg(ctx, k, msg, ratio)
_, err = k.TryPlaceOrder(ctx, order)
if err != nil {
return common.ErrInsufficientCoins(DefaultParamspace, err.Error()).Result()
}
}
return &sdk.Result{}, nil
}
func handleCancelOrder(context sdk.Context, k Keeper, sender sdk.AccAddress, orderID string, logger log.Logger) (
types.OrderResult, sdk.CacheMultiStore) {
cacheItem := context.MultiStore().CacheMultiStore()
ctx := context.WithMultiStore(cacheItem)
// Check order
msg := MsgCancelOrder{
Sender: sender,
OrderID: orderID,
}
err := validateCancelOrder(ctx, k, msg)
var message string
if err == nil {
// cancel order
order := k.GetOrder(ctx, orderID)
fee := k.CancelOrder(ctx, order, logger)
message = fee.String()
}
cancelRes := types.OrderResult{
Error: err,
Message: message,
OrderID: orderID,
}
return cancelRes, cacheItem
}
func handleMsgCancelOrders(ctx sdk.Context, k Keeper, msg types.MsgCancelOrders, logger log.Logger) (*sdk.Result, error) {
cancelRes := []types.OrderResult{}
var handlerResult bitset.BitSet
for idx, orderID := range msg.OrderIDs {
res, cacheItem := handleCancelOrder(ctx, k, msg.Sender, orderID, logger)
cancelRes = append(cancelRes, res)
cacheItem.Write()
if res.Error == nil {
handlerResult.Set(uint(idx))
}
logger.Debug(fmt.Sprintf("BlockHeight<%d>, handler<%s>\n"+
" msg<Sender:%s,ID:%s>\n"+
" result<The User have canceled an order {ID:%s} >\n",
ctx.BlockHeight(), "handleMsgCancelOrder",
msg.Sender, orderID, orderID))
}
rss, err := json.Marshal(&cancelRes)
if err != nil {
rss = []byte(fmt.Sprintf("failed to marshal result to JSON: %s", err))
}
event := sdk.NewEvent(sdk.EventTypeMessage, sdk.NewAttribute(sdk.AttributeKeyModule, types.ModuleName))
event = event.AppendAttributes(sdk.NewAttribute("orders", string(rss)))
ctx.EventManager().EmitEvent(event)
if handlerResult.None() {
return types.ErrNoOrdersIsCanceled().Result()
}
k.AddTxHandlerMsgResult(handlerResult)
return &sdk.Result{
Events: ctx.EventManager().Events(),
}, nil
}
func validateCancelOrder(ctx sdk.Context, keeper keeper.Keeper, msg types.MsgCancelOrder) error {
order := keeper.GetOrder(ctx, msg.OrderID)
// Check order
if order == nil {
return types.ErrOrderIsNotExistOrClosed(msg.OrderID)
}
if order.Status != types.OrderStatusOpen {
return types.ErrOrderStatusIsNotOpen()
}
if !order.Sender.Equals(msg.Sender) {
return types.ErrNotOrderOwner(msg.OrderID)
}
if keeper.IsProductLocked(ctx, order.Product) {
return types.ErrIsProductLocked(order.Product)
}
return nil
}
// ValidateMsgCancelOrders validates whether the msg of cancelOrders is valid.
func ValidateMsgCancelOrders(ctx sdk.Context, keeper keeper.Keeper, msg types.MsgCancelOrders) error {
for _, orderID := range msg.OrderIDs {
msg := MsgCancelOrder{
Sender: msg.Sender,
OrderID: orderID,
}
err := validateCancelOrder(ctx, keeper, msg)
if err != nil {
return err
}
}
return nil
}