-
Notifications
You must be signed in to change notification settings - Fork 182
/
querier.go
277 lines (234 loc) · 8.13 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
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
package keeper
import (
"encoding/json"
"fmt"
"sort"
"strings"
"github.com/okex/exchain/x/dex/types"
"github.com/okex/exchain/libs/cosmos-sdk/codec"
sdk "github.com/okex/exchain/libs/cosmos-sdk/types"
"github.com/okex/exchain/x/common"
abci "github.com/okex/exchain/libs/tendermint/abci/types"
)
// NewQuerier is the module level router for state queries
func NewQuerier(keeper IKeeper) sdk.Querier {
return func(ctx sdk.Context, path []string, req abci.RequestQuery) (res []byte, err sdk.Error) {
switch path[0] {
case types.QueryProducts:
return queryProduct(ctx, req, keeper)
case types.QueryDeposits:
return queryDeposits(ctx, req, keeper)
case types.QueryMatchOrder:
return queryMatchOrder(ctx, req, keeper)
case types.QueryParameters:
return queryParams(ctx, req, keeper)
case types.QueryProductsDelisting:
return queryProductsDelisting(ctx, keeper)
case types.QueryOperator:
return queryOperator(ctx, req, keeper)
case types.QueryOperators:
return queryOperators(ctx, keeper)
default:
return nil, types.ErrDexUnknownQueryType()
}
}
}
func queryProduct(ctx sdk.Context, req abci.RequestQuery, keeper IKeeper) (res []byte, err sdk.Error) {
var params types.QueryDexInfoParams
errUnmarshal := types.ModuleCdc.UnmarshalJSON(req.Data, ¶ms)
if errUnmarshal != nil {
return nil, common.ErrUnMarshalJSONFailed(errUnmarshal.Error())
}
offset, limit := common.GetPage(int(params.Page), int(params.PerPage))
if offset < 0 || limit < 0 {
return nil, common.ErrInvalidPaginateParam(params.Page, params.PerPage)
}
var tokenPairs []*types.TokenPair
if params.Owner != "" {
ownerAddr, err := sdk.AccAddressFromBech32(params.Owner)
if err != nil {
return nil, common.ErrCreateAddrFromBech32Failed(params.Owner, err.Error())
}
tokenPairs = keeper.GetUserTokenPairs(ctx, ownerAddr)
} else {
tokenPairs = keeper.GetTokenPairs(ctx)
}
// sort tokenPairs
sort.SliceStable(tokenPairs, func(i, j int) bool {
return tokenPairs[i].ID < tokenPairs[j].ID
})
total := len(tokenPairs)
switch {
case total < offset:
tokenPairs = tokenPairs[0:0]
case total < offset+limit:
tokenPairs = tokenPairs[offset:]
default:
tokenPairs = tokenPairs[offset : offset+limit]
}
var response *common.ListResponse
if len(tokenPairs) > 0 {
response = common.GetListResponse(total, params.Page, params.PerPage, tokenPairs)
} else {
response = common.GetEmptyListResponse(total, params.Page, params.PerPage)
}
res, errMarshal := json.MarshalIndent(response, "", " ")
if errMarshal != nil {
return nil, common.ErrMarshalJSONFailed(errMarshal.Error())
}
return res, nil
}
type depositsData struct {
ProductName string `json:"product"`
ProductDeposits sdk.SysCoin `json:"deposits"`
Rank int `json:"rank"`
BlockHeight int64 `json:"block_height"`
Owner sdk.AccAddress `json:"owner"`
}
func queryDeposits(ctx sdk.Context, req abci.RequestQuery, keeper IKeeper) (res []byte, err sdk.Error) {
var params types.QueryDepositParams
errUnmarshal := types.ModuleCdc.UnmarshalJSON(req.Data, ¶ms)
if errUnmarshal != nil {
return nil, common.ErrUnMarshalJSONFailed(errUnmarshal.Error())
}
if params.Address == "" && params.BaseAsset == "" && params.QuoteAsset == "" {
return nil, types.ErrAddrAndProductAllRequired()
}
offset, limit := common.GetPage(int(params.Page), int(params.PerPage))
if offset < 0 || limit < 0 {
return nil, common.ErrInvalidPaginateParam(params.Page, params.PerPage)
}
tokenPairs := keeper.GetTokenPairsOrdered(ctx)
var deposits []depositsData
for i, tokenPair := range tokenPairs {
if tokenPair == nil {
return nil, types.ErrTokenPairIsRequired()
}
// filter address
if params.Address != "" && tokenPair.Owner.String() != params.Address {
continue
}
// filter base asset
if params.BaseAsset != "" && !strings.Contains(tokenPair.BaseAssetSymbol, params.BaseAsset) {
continue
}
// filter quote asset
if params.QuoteAsset != "" && !strings.Contains(tokenPair.QuoteAssetSymbol, params.QuoteAsset) {
continue
}
deposits = append(deposits, depositsData{fmt.Sprintf("%s_%s", tokenPair.BaseAssetSymbol, tokenPair.QuoteAssetSymbol), tokenPair.Deposits, i + 1, tokenPair.BlockHeight, tokenPair.Owner})
}
total := len(deposits)
switch {
case total < offset:
deposits = deposits[0:0]
case total < offset+limit:
deposits = deposits[offset:]
default:
deposits = deposits[offset : offset+limit]
}
sort.SliceStable(deposits, func(i, j int) bool {
return deposits[i].ProductDeposits.IsLT(deposits[j].ProductDeposits)
})
var response *common.ListResponse
if total > 0 {
response = common.GetListResponse(total, params.Page, params.PerPage, deposits)
} else {
response = common.GetEmptyListResponse(total, params.Page, params.PerPage)
}
res, errMarshal := json.MarshalIndent(response, "", " ")
if errMarshal != nil {
return nil, common.ErrMarshalJSONFailed(errMarshal.Error())
}
return res, nil
}
func queryMatchOrder(ctx sdk.Context, req abci.RequestQuery, keeper IKeeper) (res []byte, err sdk.Error) {
var params types.QueryDexInfoParams
errUnmarshal := types.ModuleCdc.UnmarshalJSON(req.Data, ¶ms)
if errUnmarshal != nil {
return nil, common.ErrUnMarshalJSONFailed(errUnmarshal.Error())
}
offset, limit := common.GetPage(int(params.Page), int(params.PerPage))
if offset < 0 || limit < 0 {
return nil, common.ErrInvalidPaginateParam(params.Page, params.PerPage)
}
tokenPairs := keeper.GetTokenPairsOrdered(ctx)
var products []string
for _, tokenPair := range tokenPairs {
if tokenPair == nil {
panic("the nil pointer is not expected")
}
products = append(products, fmt.Sprintf("%s_%s", tokenPair.BaseAssetSymbol, tokenPair.QuoteAssetSymbol))
}
switch {
case len(products) < offset:
products = products[0:0]
case len(products) < offset+limit:
products = products[offset:]
default:
products = products[offset : offset+limit]
}
res, errMarshal := codec.MarshalJSONIndent(types.ModuleCdc, products)
if errMarshal != nil {
return nil, common.ErrMarshalJSONFailed(errMarshal.Error())
}
return res, nil
}
func queryParams(ctx sdk.Context, _ abci.RequestQuery, keeper IKeeper) (res []byte, err sdk.Error) {
params := keeper.GetParams(ctx)
res, errUnmarshal := codec.MarshalJSONIndent(types.ModuleCdc, params)
if errUnmarshal != nil {
return nil, common.ErrMarshalJSONFailed(errUnmarshal.Error())
}
return res, nil
}
//queryProductsDelisting query the tokenpair name under dex delisting
func queryProductsDelisting(ctx sdk.Context, keeper IKeeper) (res []byte, err sdk.Error) {
var tokenPairNames []string
tokenPairs := keeper.GetTokenPairs(ctx)
tokenPairLen := len(tokenPairs)
for i := 0; i < tokenPairLen; i++ {
if tokenPairs[i] == nil {
panic("the nil pointer is not expected")
}
if tokenPairs[i].Delisting {
tokenPairNames = append(tokenPairNames, fmt.Sprintf("%s_%s", tokenPairs[i].BaseAssetSymbol, tokenPairs[i].QuoteAssetSymbol))
}
}
res, errUnmarshal := codec.MarshalJSONIndent(types.ModuleCdc, tokenPairNames)
if errUnmarshal != nil {
return nil, common.ErrMarshalJSONFailed(errUnmarshal.Error())
}
return res, nil
}
// nolint
func queryOperator(ctx sdk.Context, req abci.RequestQuery, keeper IKeeper) ([]byte, sdk.Error) {
var params types.QueryDexOperatorParams
err := types.ModuleCdc.UnmarshalJSON(req.Data, ¶ms)
if err != nil {
return nil, common.ErrUnMarshalJSONFailed(err.Error())
}
operator, isExist := keeper.GetOperator(ctx, params.Addr)
if !isExist {
return nil, types.ErrUnknownOperator(params.Addr)
}
bz, err := codec.MarshalJSONIndent(types.ModuleCdc, operator)
if err != nil {
return nil, common.ErrMarshalJSONFailed(err.Error())
}
return bz, nil
}
// nolint
func queryOperators(ctx sdk.Context, keeper IKeeper) ([]byte, sdk.Error) {
var operators types.DEXOperators
keeper.IterateOperators(ctx, func(operator types.DEXOperator) bool {
//info.HandlingFees = keeper.GetBankKeeper().GetCoins(ctx, info.HandlingFeeAddress).String()
operators = append(operators, operator)
return false
})
bz, err := codec.MarshalJSONIndent(types.ModuleCdc, operators)
if err != nil {
return nil, common.ErrMarshalJSONFailed(err.Error())
}
return bz, nil
}