/
query.go
140 lines (124 loc) · 4.08 KB
/
query.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
// 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 (
"github.com/33cn/chain33/types"
pty "github.com/33cn/plugin/plugin/dapp/trade/types"
)
// 目前设计trade 的query, 有两个部分的大分类
// 1. 按token 分
// 可以用于 token的挂单查询 (按价格排序): OnBuy/OnSale
// token 的历史行情 (按价格排序): SoldOut/BoughtOut--> TODO 是否需要按时间(区块高度排序更合理)
// 2. 按 addr 分。 用于客户个人的钱包
// 自己未完成的交易 (按地址状态来)
// 自己的历史交易 (addr 的所有订单)
//
// 由于现价买/卖是没有orderID的, 用txhash 代替作为key
// key 有两种 orderID, txhash (0xAAAAAAAAAAAAAAA)
// 1.15 both buy/sell order
func (t *trade) Query_GetOnesOrderWithStatus(req *pty.ReqAddrAssets) (types.Message, error) {
return t.GetOnesOrderWithStatus(req)
}
// get order by id
func (t *trade) Query_GetOneOrder(req *pty.ReqAddrAssets) (types.Message, error) {
return t.GetOneOrder(req)
}
// query reply utils
const (
orderStatusInvalid = iota
orderStatusOn
orderStatusDone
orderStatusRevoke
)
const (
orderTypeInvalid = iota
orderTypeSell
orderTypeBuy
)
func fromStatus(status int32) (st, ty int32) {
switch status {
case pty.TradeOrderStatusOnSale:
return orderStatusOn, orderTypeSell
case pty.TradeOrderStatusSoldOut:
return orderStatusDone, orderTypeSell
case pty.TradeOrderStatusRevoked:
return orderStatusRevoke, orderTypeSell
case pty.TradeOrderStatusOnBuy:
return orderStatusOn, orderTypeBuy
case pty.TradeOrderStatusBoughtOut:
return orderStatusDone, orderTypeBuy
case pty.TradeOrderStatusBuyRevoked:
return orderStatusRevoke, orderTypeBuy
}
return orderStatusInvalid, orderTypeInvalid
}
// GetOnesOrderWithStatus by address-status
func (t *trade) GetOnesOrderWithStatus(req *pty.ReqAddrAssets) (types.Message, error) {
orderStatus, orderType := fromStatus(req.Status)
if orderStatus == orderStatusInvalid || orderType == orderTypeInvalid {
return nil, types.ErrInvalidParam
}
// 使用 owner isFinished 组合
var order pty.LocalOrder
if orderStatus == orderStatusOn {
order.IsFinished = false
} else {
order.IsFinished = true
}
order.Owner = req.Addr
if len(req.FromKey) > 0 {
order.TxIndex = req.FromKey
}
rows, err := listV2(t.GetLocalDB(), "owner_isFinished", &order, req.Count, req.Direction)
if err != nil {
tradelog.Error("GetOnesOrderWithStatus", "err", err)
return nil, err
}
return t.toTradeOrders(rows)
}
func fmtReply(cfg *types.Chain33Config, order *pty.LocalOrder) *pty.ReplyTradeOrder {
priceExec := order.PriceExec
priceSymbol := order.PriceSymbol
if priceExec == "" {
priceExec = cfg.GetCoinExec()
priceSymbol = cfg.GetCoinSymbol()
}
return &pty.ReplyTradeOrder{
TokenSymbol: order.AssetSymbol,
Owner: order.Owner,
AmountPerBoardlot: order.AmountPerBoardlot,
MinBoardlot: order.MinBoardlot,
PricePerBoardlot: order.PricePerBoardlot,
TotalBoardlot: order.TotalBoardlot,
TradedBoardlot: order.TradedBoardlot,
BuyID: order.BuyID,
Status: order.Status,
SellID: order.SellID,
TxHash: order.TxHash[0],
Height: order.Height,
Key: order.TxIndex,
BlockTime: order.BlockTime,
IsSellOrder: order.IsSellOrder,
AssetExec: order.AssetExec,
PriceExec: priceExec,
PriceSymbol: priceSymbol,
}
}
func (t *trade) GetOneOrder(req *pty.ReqAddrAssets) (types.Message, error) {
query := NewOrderTableV2(t.GetLocalDB())
tradelog.Debug("query GetData dbg", "primary", req.FromKey)
row, err := query.GetData([]byte(req.FromKey))
if err != nil {
tradelog.Error("query GetData failed", "key", req.FromKey, "err", err)
return nil, err
}
o, ok := row.Data.(*pty.LocalOrder)
if !ok {
tradelog.Error("query GetData failed", "err", "bad row type")
return nil, types.ErrTypeAsset
}
cfg := t.GetAPI().GetConfig()
reply := fmtReply(cfg, o)
return reply, nil
}