-
Notifications
You must be signed in to change notification settings - Fork 0
/
local_order.go
454 lines (393 loc) · 14.4 KB
/
local_order.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
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
// Copyright Fuzamei Corp. 2018 All Rights Reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package executor
import (
"encoding/hex"
"fmt"
"strconv"
"github.com/33cn/chain33/common"
dbm "github.com/33cn/chain33/common/db"
"github.com/33cn/chain33/common/db/table"
"github.com/33cn/chain33/types"
pty "github.com/GM-Publicchain/gm/plugin/dapp/trade/types"
)
/*
现有接口
1. 查询地址对应的买单 (无分页)
1.1 只指定地址 -> owner
1.2 同时指定地址和token -> owner_asset
1.3 显示一个用户成交的所有买单 -> owner
1.4 显示一个用户成交的指定一个或者多个token所有买单 -> owner_asset 不支持多个
2. 分状态查询地址的买单: 状态 地址 (无分页) -> owner_status
3. 显示一个token 指定数量的买单 GetTokenBuyOrderByStatus -> asset_inBuy_status
4. 显示指定token出售者的一个或多个token 或 不指定token 的卖单 (无分页) -> owner_asset/owner_asset_isSell 不支持多个
5. 显示指定状态下的某地址卖单 (无分页) -> owner_isSell_status
6. 显示一个token 指定数量的卖单 -> asset_isSell
7. 根据状态分页列出某地址的订单(包括买单卖单) owner_status
*/
var opt_order_table = &table.Option{
Prefix: "LODB-trade",
Name: "order",
Primary: "txIndex",
// asset = asset_exec+asset_symbol
//
// status: 设计为可以同时查询几种的并集 , 存储为前缀, 需要提前设计需要合并的, 用前缀表示
// 进行中, 撤销, 部分成交 , 全部成交, 完成状态统一前缀. 数字和原来不一样
// 00 10 11 12 1*
// 排序过滤条件: 可以组合,status&isSell 和前缀冲突
Index: []string{
"key", // 内部查询用
"asset", // 按资产统计订单
"asset_isSell_status", // 接口 3
// "asset_status", 可能需求, 用于资产的交易历史
// "asset_isSell",
"owner", // 接口 1.1, 1.3
"owner_asset", // 接口 1.2, 1.4, 4, 7
"owner_asset_isSell", // 接口 4
"owner_asset_status", // 新需求, 在
"owner_isSell", // 接口 6
// "owner_isSell_status", 可能需求, 界面分开显示订单
// "owner_isSell_statusPrefix", // 状态可以定制组合, 成交历史需求
"owner_status", // 接口 2
"assset_isSell_isFinished", // 用 isFinish, 进行订单是否完成的列表功能
"owner_asset_isFinished",
"owner_isFinished",
// "owner_statusPrefix", // 状态可以定制组合 , 成交历史需求
},
}
// OrderRow order row
type OrderRow struct {
*pty.LocalOrder
}
// NewOrderRow create row
func NewOrderRow() *OrderRow {
return &OrderRow{LocalOrder: nil}
}
// CreateRow create row
func (r *OrderRow) CreateRow() *table.Row {
return &table.Row{Data: &pty.LocalOrder{}}
}
// SetPayload set payload
func (r *OrderRow) SetPayload(data types.Message) error {
if d, ok := data.(*pty.LocalOrder); ok {
r.LocalOrder = d
return nil
}
return types.ErrTypeAsset
}
// Get get index key
func (r *OrderRow) Get(key string) ([]byte, error) {
switch key {
case "txIndex":
return []byte(r.TxIndex), nil
case "key":
return []byte(r.Key), nil
case "asset":
return []byte(r.asset()), nil
case "asset_isSell_status":
return []byte(fmt.Sprintf("%s_%d_%s", r.asset(), r.isSell(), r.status())), nil
case "owner":
return []byte(r.Owner), nil
case "owner_asset":
return []byte(fmt.Sprintf("%s_%s", r.Owner, r.asset())), nil
case "owner_asset_isSell":
return []byte(fmt.Sprintf("%s_%s_%d", r.Owner, r.asset(), r.isSell())), nil
case "owner_asset_status":
return []byte(fmt.Sprintf("%s_%s_%s", r.Owner, r.asset(), r.status())), nil
case "owner_isSell":
return []byte(fmt.Sprintf("%s_%d", r.Owner, r.isSell())), nil
//case "owner_isSell_statusPrefix":
// return []byte(fmt.Sprintf("%s_%d_%s", r.Owner, r.asset(), r.isSell())), nil
case "owner_status":
return []byte(fmt.Sprintf("%s_%s", r.Owner, r.status())), nil
//case "owner_statusPrefix":
// return []byte(fmt.Sprintf("%s_%d", r.Owner, r.isSell())), nil
case "assset_isSell_isFinished":
return []byte(fmt.Sprintf("%s_%d_%d", r.Owner, r.isSell(), r.isFinished())), nil
case "owner_asset_isFinished":
return []byte(fmt.Sprintf("%s_%s_%d", r.Owner, r.asset(), r.isFinished())), nil
case "owner_isFinished":
return []byte(fmt.Sprintf("%s_%d", r.Owner, r.isFinished())), nil
default:
return nil, types.ErrNotFound
}
}
func (r *OrderRow) asset() string {
return r.LocalOrder.AssetExec + "." + r.LocalOrder.AssetSymbol
}
func (r *OrderRow) isSell() int {
if r.IsSellOrder {
return 1
}
return 0
}
func (r *OrderRow) isFinished() int {
if r.IsFinished {
return 1
}
return 0
}
// status: 设计为可以同时查询几种的并集 , 存储为前缀, 需要提前设计需要合并的, 用前缀表示
// 进行中, 撤销, 部分成交 , 全部成交, 完成状态统一前缀. 数字和原来不一样
// 01 10 11 12 19 -> 1*
func (r *OrderRow) status() string {
if r.Status == pty.TradeOrderStatusOnBuy || r.Status == pty.TradeOrderStatusOnSale {
return "01" // 试图用1 可以匹配所有完成的
} else if r.Status == pty.TradeOrderStatusSoldOut || r.Status == pty.TradeOrderStatusBoughtOut {
return "12"
} else if r.Status == pty.TradeOrderStatusRevoked || r.Status == pty.TradeOrderStatusBuyRevoked {
return "10"
} else if r.Status == pty.TradeOrderStatusSellHalfRevoked || r.Status == pty.TradeOrderStatusBuyHalfRevoked {
return "11"
} else if r.Status == pty.TradeOrderStatusGroupComplete {
return "1" // 1* match complete
}
return "XX"
}
// NewOrderTable create order table
func NewOrderTable(kvdb dbm.KV) *table.Table {
rowMeta := NewOrderRow()
rowMeta.SetPayload(&pty.LocalOrder{})
t, err := table.NewTable(rowMeta, kvdb, opt_order_table)
if err != nil {
panic(err)
}
return t
}
func (t *trade) genSellLimit(tx *types.Transaction, sell *pty.ReceiptSellBase,
sellorder *pty.SellOrder, txIndex string) *pty.LocalOrder {
order := &pty.LocalOrder{
AssetSymbol: sellorder.TokenSymbol,
TxIndex: txIndex,
Owner: sellorder.Address,
AmountPerBoardlot: sellorder.AmountPerBoardlot,
MinBoardlot: sellorder.MinBoardlot,
PricePerBoardlot: sellorder.PricePerBoardlot,
TotalBoardlot: sellorder.TotalBoardlot,
TradedBoardlot: sellorder.SoldBoardlot,
BuyID: "",
Status: sellorder.Status,
SellID: sell.SellID,
TxHash: []string{common.ToHex(tx.Hash())},
Height: sell.Height,
Key: sell.SellID,
BlockTime: t.GetBlockTime(),
IsSellOrder: true,
AssetExec: sellorder.AssetExec,
IsFinished: false,
}
return order
}
func (t *trade) updateSellLimit(tx *types.Transaction, sell *pty.ReceiptSellBase,
sellorder *pty.SellOrder, txIndex string, ldb *table.Table) *pty.LocalOrder {
xs, err := ldb.ListIndex("key", []byte(sell.SellID), nil, 1, 0)
if err != nil || len(xs) != 1 {
return nil
}
order, ok := xs[0].Data.(*pty.LocalOrder)
tradelog.Debug("Table dbg", "sell-update", order, "data", xs[0].Data)
if !ok {
tradelog.Error("Table failed", "sell-update", order)
return nil
}
status := sellorder.Status
if status == pty.TradeOrderStatusRevoked && sell.SoldBoardlot > 0 {
status = pty.TradeOrderStatusSellHalfRevoked
}
order.Status = status
order.TxHash = append(order.TxHash, common.ToHex(tx.Hash()))
order.TradedBoardlot = sellorder.SoldBoardlot
order.IsFinished = (status != pty.TradeOrderStatusOnSale)
tradelog.Debug("Table", "sell-update", order)
ldb.Replace(order)
return order
}
func (t *trade) rollBackSellLimit(tx *types.Transaction, sell *pty.ReceiptSellBase,
sellorder *pty.SellOrder, txIndex string, ldb *table.Table, tradedBoardlot int64) *pty.LocalOrder {
xs, err := ldb.ListIndex("key", []byte(sell.SellID), nil, 1, 0)
if err != nil || len(xs) != 1 {
return nil
}
order, ok := xs[0].Data.(*pty.LocalOrder)
if !ok {
return nil
}
// 撤销订单回滚, 只需要修改状态
// 其他的操作需要还修改数量
order.Status = pty.TradeOrderStatusOnSale
order.TxHash = order.TxHash[:len(order.TxHash)-1]
order.TradedBoardlot = order.TradedBoardlot - tradedBoardlot
order.IsFinished = (order.Status != pty.TradeOrderStatusOnSale)
ldb.Replace(order)
return order
}
func parseOrderAmountFloat(s string) int64 {
x, err := strconv.ParseFloat(s, 64)
if err != nil {
tradelog.Error("parseOrderAmountFloat", "decode receipt", err)
return 0
}
return int64(x * float64(types.TokenPrecision))
}
func parseOrderPriceFloat(s string) int64 {
x, err := strconv.ParseFloat(s, 64)
if err != nil {
tradelog.Error("parseOrderPriceFloat", "decode receipt", err)
return 0
}
return int64(x * float64(types.Coin))
}
func (t *trade) genSellMarket(tx *types.Transaction, sell *pty.ReceiptSellBase, txIndex string) *pty.LocalOrder {
order := &pty.LocalOrder{
AssetSymbol: sell.TokenSymbol,
TxIndex: txIndex,
Owner: sell.Owner,
AmountPerBoardlot: parseOrderAmountFloat(sell.AmountPerBoardlot),
MinBoardlot: sell.MinBoardlot,
PricePerBoardlot: parseOrderPriceFloat(sell.PricePerBoardlot),
TotalBoardlot: sell.TotalBoardlot,
TradedBoardlot: sell.SoldBoardlot,
BuyID: sell.BuyID,
Status: pty.TradeOrderStatusSoldOut,
SellID: calcTokenSellID(hex.EncodeToString(tx.Hash())),
TxHash: []string{common.ToHex(tx.Hash())},
Height: sell.Height,
Key: calcTokenSellID(hex.EncodeToString(tx.Hash())),
BlockTime: t.GetBlockTime(),
IsSellOrder: true,
AssetExec: sell.AssetExec,
IsFinished: true,
}
return order
}
func (t *trade) genBuyLimit(tx *types.Transaction, buy *pty.ReceiptBuyBase, txIndex string) *pty.LocalOrder {
order := &pty.LocalOrder{
AssetSymbol: buy.TokenSymbol,
TxIndex: txIndex,
Owner: buy.Owner,
AmountPerBoardlot: parseOrderAmountFloat(buy.AmountPerBoardlot),
MinBoardlot: buy.MinBoardlot,
PricePerBoardlot: parseOrderPriceFloat(buy.PricePerBoardlot),
TotalBoardlot: buy.TotalBoardlot,
TradedBoardlot: buy.BoughtBoardlot,
BuyID: buy.BuyID,
Status: pty.TradeOrderStatusOnBuy,
SellID: "",
TxHash: []string{common.ToHex(tx.Hash())},
Height: buy.Height,
Key: buy.BuyID,
BlockTime: t.GetBlockTime(),
IsSellOrder: false,
AssetExec: buy.AssetExec,
IsFinished: false,
}
return order
}
func (t *trade) updateBuyLimit(tx *types.Transaction, buy *pty.ReceiptBuyBase,
buyorder *pty.BuyLimitOrder, txIndex string, ldb *table.Table) *pty.LocalOrder {
xs, err := ldb.ListIndex("key", []byte(buy.BuyID), nil, 1, 0)
if err != nil || len(xs) != 1 {
return nil
}
order, ok := xs[0].Data.(*pty.LocalOrder)
if !ok {
return nil
}
status := buyorder.Status
if status == pty.TradeOrderStatusBuyRevoked && buy.BoughtBoardlot > 0 {
status = pty.TradeOrderStatusBuyHalfRevoked
}
order.Status = status
order.TxHash = append(order.TxHash, common.ToHex(tx.Hash()))
order.TradedBoardlot = buyorder.BoughtBoardlot
order.IsFinished = (status != pty.TradeOrderStatusOnBuy)
ldb.Replace(order)
return order
}
func (t *trade) rollbackBuyLimit(tx *types.Transaction, buy *pty.ReceiptBuyBase,
buyorder *pty.BuyLimitOrder, txIndex string, ldb *table.Table, traded int64) *pty.LocalOrder {
xs, err := ldb.ListIndex("key", []byte(buy.BuyID), nil, 1, 0)
if err != nil || len(xs) != 1 {
return nil
}
order, ok := xs[0].Data.(*pty.LocalOrder)
if !ok {
return nil
}
order.Status = pty.TradeOrderStatusOnBuy
order.TxHash = order.TxHash[:len(order.TxHash)-1]
order.TradedBoardlot = order.TradedBoardlot - traded
order.IsFinished = false
ldb.Replace(order)
return order
}
func (t *trade) genBuyMarket(tx *types.Transaction, buy *pty.ReceiptBuyBase, txIndex string) *pty.LocalOrder {
order := &pty.LocalOrder{
AssetSymbol: buy.TokenSymbol,
TxIndex: txIndex,
Owner: buy.Owner,
AmountPerBoardlot: parseOrderAmountFloat(buy.AmountPerBoardlot),
MinBoardlot: buy.MinBoardlot,
PricePerBoardlot: parseOrderPriceFloat(buy.PricePerBoardlot),
TotalBoardlot: buy.TotalBoardlot,
TradedBoardlot: buy.BoughtBoardlot,
BuyID: calcTokenBuyID(hex.EncodeToString(tx.Hash())),
Status: pty.TradeOrderStatusBoughtOut,
SellID: buy.SellID,
TxHash: []string{common.ToHex(tx.Hash())},
Height: buy.Height,
Key: calcTokenBuyID(hex.EncodeToString(tx.Hash())),
BlockTime: t.GetBlockTime(),
IsSellOrder: true,
AssetExec: buy.AssetExec,
IsFinished: true,
}
return order
}
func list(db dbm.KVDB, indexName string, data *pty.LocalOrder, count, direction int32) ([]*table.Row, error) {
query := NewOrderTable(db).GetQuery(db)
var primary []byte
if len(data.TxIndex) > 0 {
primary = []byte(data.TxIndex)
}
cur := &OrderRow{LocalOrder: data}
index, err := cur.Get(indexName)
if err != nil {
tradelog.Error("query List failed", "key", string(primary), "param", data, "err", err)
return nil, err
}
tradelog.Debug("query List dbg", "indexName", indexName, "index", string(index), "primary", primary, "count", count, "direction", direction)
rows, err := query.ListIndex(indexName, index, primary, count, direction)
if err != nil {
tradelog.Error("query List failed", "key", string(primary), "param", data, "err", err)
return nil, err
}
if len(rows) == 0 {
return nil, types.ErrNotFound
}
return rows, nil
}
/*
按 资产 查询 :
按 资产 & 地址 查询
按 地址
排序和分类
1. 时间顺序 txindex
1. 分类, 不同的状态 & 不同的性质: 买/卖
交易 -> 订单 按订单来 (交易和订单是多对多的关系,不适合joinTable)
交易 T1 Create -> T2 part-take -> T3 Revoke
订单左为进行中, 右为完成,
订单 (C1) | () -> (C1m) | (C2) -> () | (C2, C1r)
查询交易 / 查询订单
C -> C/M -> C/D
\
\ ->R
状态 1, TradeOrderStatusOnSale, 在售
状态 2: TradeOrderStatusSoldOut,售完
状态 3: TradeOrderStatusRevoked, 卖单被撤回
状态 4: TradeOrderStatusExpired, 订单超时(目前不支持订单超时)
状态 5: TradeOrderStatusOnBuy, 求购
状态 6: TradeOrderStatusBoughtOut, 购买完成
状态 7: TradeOrderStatusBuyRevoked, 买单被撤回
*/