-
Notifications
You must be signed in to change notification settings - Fork 182
/
querier.go
220 lines (195 loc) · 6.08 KB
/
querier.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
package keeper
import (
"github.com/okex/exchain/libs/cosmos-sdk/codec"
sdk "github.com/okex/exchain/libs/cosmos-sdk/types"
abci "github.com/okex/exchain/libs/tendermint/abci/types"
"github.com/okex/exchain/x/common"
"github.com/okex/exchain/x/order/types"
)
// nolint
const (
DefaultBookSize = 200
)
// NewQuerier is the module level router for state queries
func NewQuerier(keeper Keeper) sdk.Querier {
return func(ctx sdk.Context, path []string, req abci.RequestQuery) (res []byte, err sdk.Error) {
switch path[0] {
case types.QueryOrderDetail:
return queryOrder(ctx, path[1:], req, keeper)
case types.QueryDepthBook:
return queryDepthBook(ctx, path[1:], req, keeper)
case types.QueryStore:
return queryStore(ctx, path[1:], req, keeper)
case types.QueryParameters:
return queryParameters(ctx, keeper)
case types.QueryDepthBookV2:
return queryDepthBookV2(ctx, path[1:], req, keeper)
default:
return nil, types.ErrUnknownOrderQueryType()
}
}
}
// nolint: unparam
func queryOrder(ctx sdk.Context, path []string, req abci.RequestQuery, keeper Keeper) (res []byte,
err sdk.Error) {
order := keeper.GetOrder(ctx, path[0])
if order == nil {
return nil, types.ErrOrderIsNotExist(path[0])
}
bz := keeper.cdc.MustMarshalJSON(order)
return bz, nil
}
// QueryDepthBookParams as input parameters when querying the depthBook
type QueryDepthBookParams struct {
Product string
Size uint
}
// NewQueryDepthBookParams creates a new instance of QueryProposalParams
func NewQueryDepthBookParams(product string, size uint) QueryDepthBookParams {
if size == 0 {
size = DefaultBookSize
}
return QueryDepthBookParams{
Product: product,
Size: size,
}
}
// nolint
type BookResItem struct {
Price string `json:"price"`
Quantity string `json:"quantity"`
}
// BookRes is used to return the result of queryDepthBook
type BookRes struct {
Asks []BookResItem `json:"asks"`
Bids []BookResItem `json:"bids"`
}
// nolint: unparam
func queryDepthBook(ctx sdk.Context, path []string, req abci.RequestQuery, keeper Keeper) ([]byte,
sdk.Error) {
var params QueryDepthBookParams
err := keeper.cdc.UnmarshalJSON(req.Data, ¶ms)
if err != nil {
return nil, common.ErrUnMarshalJSONFailed("incorrectly formatted request Data")
}
if params.Size == 0 {
return nil, types.ErrInvalidSizeParam(params.Size)
}
tokenPair := keeper.GetDexKeeper().GetTokenPair(ctx, params.Product)
if tokenPair == nil {
return nil, types.ErrOrderIsNotExist(params.Product)
}
depthBook := keeper.GetDepthBookFromDB(ctx, params.Product)
var asks []BookResItem
var bids []BookResItem
for _, item := range depthBook.Items {
if item.SellQuantity.IsPositive() {
asks = append([]BookResItem{{item.Price.String(), item.SellQuantity.String()}}, asks...)
}
if item.BuyQuantity.IsPositive() {
bids = append(bids, BookResItem{item.Price.String(), item.BuyQuantity.String()})
}
}
if uint(len(asks)) > params.Size {
asks = asks[:params.Size]
}
if uint(len(bids)) > params.Size {
bids = bids[:params.Size]
}
bookRes := BookRes{
Asks: asks,
Bids: bids,
}
bz := keeper.cdc.MustMarshalJSON(bookRes)
return bz, nil
}
// StoreStatistic is used to store the state of depthBook
type StoreStatistic struct {
StoreOrderNum int64
DepthBookNum map[string]int64
BookOrderIDsNum map[string]int64
}
func getStoreStatistic(ctx sdk.Context, keeper Keeper) *StoreStatistic {
storeOrderNum := keeper.GetStoreOrderNum(ctx)
ss := &StoreStatistic{
StoreOrderNum: storeOrderNum,
}
depthBookMap := make(map[string]types.DepthBook)
depthStore := ctx.KVStore(keeper.orderStoreKey)
iter := sdk.KVStorePrefixIterator(depthStore, types.DepthBookKey)
defer iter.Close()
for ; iter.Valid(); iter.Next() {
var depthBook types.DepthBook
bz := iter.Value()
keeper.cdc.MustUnmarshalBinaryBare(bz, &depthBook)
depthBookMap[types.GetKey(iter)] = depthBook
}
ss.DepthBookNum = make(map[string]int64, len(depthBookMap))
ss.BookOrderIDsNum = make(map[string]int64, len(depthBookMap)*500)
for product, depthBook := range depthBookMap {
ss.DepthBookNum[product] = int64(len(depthBook.Items))
for _, item := range depthBook.Items {
if item.BuyQuantity.IsPositive() {
key := types.FormatOrderIDsKey(product, item.Price, types.BuyOrder)
orderIDs := keeper.GetProductPriceOrderIDs(key)
ss.BookOrderIDsNum[key] = int64(len(orderIDs))
}
if item.SellQuantity.IsPositive() {
key := types.FormatOrderIDsKey(product, item.Price, types.SellOrder)
orderIDs := keeper.GetProductPriceOrderIDs(key)
ss.BookOrderIDsNum[key] = int64(len(orderIDs))
}
}
}
return ss
}
func queryStore(ctx sdk.Context, path []string, req abci.RequestQuery, keeper Keeper) ([]byte,
sdk.Error) {
ss := getStoreStatistic(ctx, keeper)
bz := keeper.cdc.MustMarshalJSON(ss)
return bz, nil
}
func queryParameters(ctx sdk.Context, keeper Keeper) (res []byte, err sdk.Error) {
params := keeper.GetParams(ctx)
res, errRes := codec.MarshalJSONIndent(keeper.cdc, params)
if errRes != nil {
return nil, common.ErrMarshalJSONFailed(errRes.Error())
}
return res, nil
}
func queryDepthBookV2(ctx sdk.Context, path []string, req abci.RequestQuery, keeper Keeper) ([]byte, sdk.Error) {
var params QueryDepthBookParams
err := keeper.cdc.UnmarshalJSON(req.Data, ¶ms)
if err != nil {
return nil, common.ErrUnMarshalJSONFailed(err.Error())
}
if params.Size == 0 {
return nil, types.ErrInvalidSizeParam(params.Size)
}
depthBook := keeper.GetDepthBookFromDB(ctx, params.Product)
var asks []BookResItem
var bids []BookResItem
for _, item := range depthBook.Items {
if item.SellQuantity.IsPositive() {
asks = append([]BookResItem{{item.Price.String(), item.SellQuantity.String()}}, asks...)
}
if item.BuyQuantity.IsPositive() {
bids = append(bids, BookResItem{item.Price.String(), item.BuyQuantity.String()})
}
}
if uint(len(asks)) > params.Size {
asks = asks[:params.Size]
}
if uint(len(bids)) > params.Size {
bids = bids[:params.Size]
}
bookRes := BookRes{
Asks: asks,
Bids: bids,
}
res, err := common.JSONMarshalV2(bookRes)
if err != nil {
return nil, common.ErrMarshalJSONFailed(err.Error())
}
return res, nil
}