/
grpc_query.go
586 lines (468 loc) · 16.9 KB
/
grpc_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
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
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
package keeper
import (
"context"
"strconv"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"
"github.com/cosmos/cosmos-sdk/store/prefix"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/types/query"
"github.com/cosmosquad-labs/squad/v2/x/liquidity/amm"
"github.com/cosmosquad-labs/squad/v2/x/liquidity/types"
)
// Querier is used as Keeper will have duplicate methods if used directly, and gRPC names take precedence over keeper.
type Querier struct {
Keeper
}
var _ types.QueryServer = Querier{}
// Params queries the parameters of the liquidity module.
func (k Querier) Params(c context.Context, _ *types.QueryParamsRequest) (*types.QueryParamsResponse, error) {
ctx := sdk.UnwrapSDKContext(c)
var params types.Params
k.Keeper.paramSpace.GetParamSet(ctx, ¶ms)
return &types.QueryParamsResponse{Params: params}, nil
}
// Pairs queries all pairs.
func (k Querier) Pairs(c context.Context, req *types.QueryPairsRequest) (*types.QueryPairsResponse, error) {
if req == nil {
return nil, status.Error(codes.InvalidArgument, "empty request")
}
if len(req.Denoms) > 2 {
return nil, status.Errorf(codes.InvalidArgument, "too many denoms to query: %d", len(req.Denoms))
}
for _, denom := range req.Denoms {
if err := sdk.ValidateDenom(denom); err != nil {
return nil, status.Error(codes.InvalidArgument, err.Error())
}
}
ctx := sdk.UnwrapSDKContext(c)
store := ctx.KVStore(k.storeKey)
var keyPrefix []byte
var pairGetter func(key, value []byte) types.Pair
switch len(req.Denoms) {
case 0:
keyPrefix = types.PairKeyPrefix
pairGetter = func(_, value []byte) types.Pair {
return types.MustUnmarshalPair(k.cdc, value)
}
case 1:
keyPrefix = types.GetPairsByDenomIndexKeyPrefix(req.Denoms[0])
pairGetter = func(key, _ []byte) types.Pair {
_, _, pairId := types.ParsePairsByDenomsIndexKey(append(keyPrefix, key...))
pair, _ := k.GetPair(ctx, pairId)
return pair
}
case 2:
keyPrefix = types.GetPairsByDenomsIndexKeyPrefix(req.Denoms[0], req.Denoms[1])
pairGetter = func(key, _ []byte) types.Pair {
_, _, pairId := types.ParsePairsByDenomsIndexKey(append(keyPrefix, key...))
pair, _ := k.GetPair(ctx, pairId)
return pair
}
}
pairStore := prefix.NewStore(store, keyPrefix)
var pairs []types.Pair
pageRes, err := query.FilteredPaginate(pairStore, req.Pagination, func(key, value []byte, accumulate bool) (bool, error) {
pair := pairGetter(key, value)
if accumulate {
pairs = append(pairs, pair)
}
return true, nil
})
if err != nil {
return nil, status.Error(codes.Internal, err.Error())
}
return &types.QueryPairsResponse{Pairs: pairs, Pagination: pageRes}, nil
}
// Pair queries the specific pair.
func (k Querier) Pair(c context.Context, req *types.QueryPairRequest) (*types.QueryPairResponse, error) {
if req == nil {
return nil, status.Error(codes.InvalidArgument, "empty request")
}
if req.PairId == 0 {
return nil, status.Error(codes.InvalidArgument, "pair id cannot be 0")
}
ctx := sdk.UnwrapSDKContext(c)
pair, found := k.GetPair(ctx, req.PairId)
if !found {
return nil, status.Errorf(codes.NotFound, "pair %d doesn't exist", req.PairId)
}
return &types.QueryPairResponse{Pair: pair}, nil
}
// Pools queries all pools.
func (k Querier) Pools(c context.Context, req *types.QueryPoolsRequest) (*types.QueryPoolsResponse, error) {
if req == nil {
return nil, status.Error(codes.InvalidArgument, "empty request")
}
var disabled bool
if req.Disabled != "" {
var err error
disabled, err = strconv.ParseBool(req.Disabled)
if err != nil {
return nil, status.Error(codes.InvalidArgument, err.Error())
}
}
ctx := sdk.UnwrapSDKContext(c)
store := ctx.KVStore(k.storeKey)
var keyPrefix []byte
var poolGetter func(key, value []byte) types.Pool
var pairGetter func(id uint64) types.Pair
pairMap := map[uint64]types.Pair{}
switch {
case req.PairId == 0:
keyPrefix = types.PoolKeyPrefix
poolGetter = func(_, value []byte) types.Pool {
return types.MustUnmarshalPool(k.cdc, value)
}
pairGetter = func(id uint64) types.Pair {
pair, ok := pairMap[id]
if !ok {
pair, _ = k.GetPair(ctx, id)
pairMap[id] = pair
}
return pair
}
default:
keyPrefix = types.GetPoolsByPairIndexKeyPrefix(req.PairId)
poolGetter = func(key, _ []byte) types.Pool {
poolId := types.ParsePoolsByPairIndexKey(append(keyPrefix, key...))
pool, _ := k.GetPool(ctx, poolId)
return pool
}
pair, _ := k.GetPair(ctx, req.PairId)
pairGetter = func(_ uint64) types.Pair {
return pair
}
}
poolStore := prefix.NewStore(store, keyPrefix)
var poolsRes []types.PoolResponse
pageRes, err := query.FilteredPaginate(poolStore, req.Pagination, func(key, value []byte, accumulate bool) (bool, error) {
pool := poolGetter(key, value)
if req.Disabled != "" {
if pool.Disabled != disabled {
return false, nil
}
}
if accumulate {
pair := pairGetter(pool.PairId)
rx, ry := k.getPoolBalances(ctx, pool, pair)
ps := k.GetPoolCoinSupply(ctx, pool)
poolsRes = append(poolsRes, types.NewPoolResponse(pool, rx, ry, ps))
}
return true, nil
})
if err != nil {
return nil, status.Error(codes.Internal, err.Error())
}
return &types.QueryPoolsResponse{Pools: poolsRes, Pagination: pageRes}, nil
}
// Pool queries the specific pool.
func (k Querier) Pool(c context.Context, req *types.QueryPoolRequest) (*types.QueryPoolResponse, error) {
if req == nil {
return nil, status.Error(codes.InvalidArgument, "empty request")
}
if req.PoolId == 0 {
return nil, status.Error(codes.InvalidArgument, "pool id cannot be 0")
}
ctx := sdk.UnwrapSDKContext(c)
pool, found := k.GetPool(ctx, req.PoolId)
if !found {
return nil, status.Errorf(codes.NotFound, "pool %d doesn't exist", req.PoolId)
}
pair, _ := k.GetPair(ctx, pool.PairId)
rx, ry := k.getPoolBalances(ctx, pool, pair)
ps := k.GetPoolCoinSupply(ctx, pool)
return &types.QueryPoolResponse{Pool: types.NewPoolResponse(pool, rx, ry, ps)}, nil
}
// PoolByReserveAddress queries the specific pool by the reserve account address.
func (k Querier) PoolByReserveAddress(c context.Context, req *types.QueryPoolByReserveAddressRequest) (*types.QueryPoolResponse, error) {
if req == nil {
return nil, status.Error(codes.InvalidArgument, "empty request")
}
if req.ReserveAddress == "" {
return nil, status.Error(codes.InvalidArgument, "empty reserve account address")
}
ctx := sdk.UnwrapSDKContext(c)
reserveAddr, err := sdk.AccAddressFromBech32(req.ReserveAddress)
if err != nil {
return nil, status.Errorf(codes.InvalidArgument, "reserve account address %s is not valid", req.ReserveAddress)
}
pool, found := k.GetPoolByReserveAddress(ctx, reserveAddr)
if !found {
return nil, status.Errorf(codes.NotFound, "pool by %s doesn't exist", req.ReserveAddress)
}
rx, ry := k.GetPoolBalances(ctx, pool)
ps := k.GetPoolCoinSupply(ctx, pool)
return &types.QueryPoolResponse{Pool: types.NewPoolResponse(pool, rx, ry, ps)}, nil
}
// PoolByPoolCoinDenom queries the specific pool by the pool coin denomination.
func (k Querier) PoolByPoolCoinDenom(c context.Context, req *types.QueryPoolByPoolCoinDenomRequest) (*types.QueryPoolResponse, error) {
if req == nil {
return nil, status.Error(codes.InvalidArgument, "empty request")
}
if req.PoolCoinDenom == "" {
return nil, status.Error(codes.InvalidArgument, "empty pool coin denom")
}
ctx := sdk.UnwrapSDKContext(c)
poolId, err := types.ParsePoolCoinDenom(req.PoolCoinDenom)
if err != nil {
return nil, status.Errorf(codes.InvalidArgument, "failed to parse pool coin denom: %v", err)
}
pool, found := k.GetPool(ctx, poolId)
if !found {
return nil, status.Errorf(codes.NotFound, "pool %d doesn't exist", poolId)
}
rx, ry := k.GetPoolBalances(ctx, pool)
ps := k.GetPoolCoinSupply(ctx, pool)
return &types.QueryPoolResponse{Pool: types.NewPoolResponse(pool, rx, ry, ps)}, nil
}
// DepositRequests queries all deposit requests.
func (k Querier) DepositRequests(c context.Context, req *types.QueryDepositRequestsRequest) (*types.QueryDepositRequestsResponse, error) {
if req == nil {
return nil, status.Error(codes.InvalidArgument, "empty request")
}
if req.PoolId == 0 {
return nil, status.Error(codes.InvalidArgument, "pool id cannot be 0")
}
ctx := sdk.UnwrapSDKContext(c)
store := ctx.KVStore(k.storeKey)
drsStore := prefix.NewStore(store, types.DepositRequestKeyPrefix)
var drs []types.DepositRequest
pageRes, err := query.FilteredPaginate(drsStore, req.Pagination, func(key, value []byte, accumulate bool) (bool, error) {
dr, err := types.UnmarshalDepositRequest(k.cdc, value)
if err != nil {
return false, err
}
if dr.PoolId != req.PoolId {
return false, nil
}
if accumulate {
drs = append(drs, dr)
}
return true, nil
})
if err != nil {
return nil, status.Error(codes.Internal, err.Error())
}
return &types.QueryDepositRequestsResponse{DepositRequests: drs, Pagination: pageRes}, nil
}
// DepositRequest queries the specific deposit request.
func (k Querier) DepositRequest(c context.Context, req *types.QueryDepositRequestRequest) (*types.QueryDepositRequestResponse, error) {
if req == nil {
return nil, status.Error(codes.InvalidArgument, "empty request")
}
if req.PoolId == 0 {
return nil, status.Error(codes.InvalidArgument, "pool id cannot be 0")
}
if req.Id == 0 {
return nil, status.Error(codes.InvalidArgument, "id cannot be 0")
}
ctx := sdk.UnwrapSDKContext(c)
dq, found := k.GetDepositRequest(ctx, req.PoolId, req.Id)
if !found {
return nil, status.Errorf(codes.NotFound, "deposit request of pool id %d and request id %d doesn't exist or deleted", req.PoolId, req.Id)
}
return &types.QueryDepositRequestResponse{DepositRequest: dq}, nil
}
// WithdrawRequests queries all withdraw requests.
func (k Querier) WithdrawRequests(c context.Context, req *types.QueryWithdrawRequestsRequest) (*types.QueryWithdrawRequestsResponse, error) {
if req == nil {
return nil, status.Error(codes.InvalidArgument, "empty request")
}
if req.PoolId == 0 {
return nil, status.Error(codes.InvalidArgument, "pool id cannot be 0")
}
ctx := sdk.UnwrapSDKContext(c)
store := ctx.KVStore(k.storeKey)
drsStore := prefix.NewStore(store, types.WithdrawRequestKeyPrefix)
var wrs []types.WithdrawRequest
pageRes, err := query.FilteredPaginate(drsStore, req.Pagination, func(key, value []byte, accumulate bool) (bool, error) {
wr, err := types.UnmarshalWithdrawRequest(k.cdc, value)
if err != nil {
return false, err
}
if wr.PoolId != req.PoolId {
return false, nil
}
if accumulate {
wrs = append(wrs, wr)
}
return true, nil
})
if err != nil {
return nil, status.Error(codes.Internal, err.Error())
}
return &types.QueryWithdrawRequestsResponse{WithdrawRequests: wrs, Pagination: pageRes}, nil
}
// WithdrawRequest queries the specific withdraw request.
func (k Querier) WithdrawRequest(c context.Context, req *types.QueryWithdrawRequestRequest) (*types.QueryWithdrawRequestResponse, error) {
if req == nil {
return nil, status.Error(codes.InvalidArgument, "empty request")
}
if req.PoolId == 0 {
return nil, status.Error(codes.InvalidArgument, "pool id cannot be 0")
}
if req.Id == 0 {
return nil, status.Error(codes.InvalidArgument, "id cannot be 0")
}
ctx := sdk.UnwrapSDKContext(c)
wq, found := k.GetWithdrawRequest(ctx, req.PoolId, req.Id)
if !found {
return nil, status.Errorf(codes.NotFound, "withdraw request of pool id %d and request id %d doesn't exist or deleted", req.PoolId, req.Id)
}
return &types.QueryWithdrawRequestResponse{WithdrawRequest: wq}, nil
}
// Orders queries all orders.
func (k Querier) Orders(c context.Context, req *types.QueryOrdersRequest) (*types.QueryOrdersResponse, error) {
if req == nil {
return nil, status.Error(codes.InvalidArgument, "empty request")
}
if req.PairId == 0 {
return nil, status.Error(codes.InvalidArgument, "pair id cannot be 0")
}
ctx := sdk.UnwrapSDKContext(c)
store := ctx.KVStore(k.storeKey)
drsStore := prefix.NewStore(store, types.OrderKeyPrefix)
var orders []types.Order
pageRes, err := query.FilteredPaginate(drsStore, req.Pagination, func(key, value []byte, accumulate bool) (bool, error) {
order, err := types.UnmarshalOrder(k.cdc, value)
if err != nil {
return false, err
}
if order.PairId != req.PairId {
return false, nil
}
if accumulate {
orders = append(orders, order)
}
return true, nil
})
if err != nil {
return nil, status.Error(codes.Internal, err.Error())
}
return &types.QueryOrdersResponse{Orders: orders, Pagination: pageRes}, nil
}
// Order queries the specific order.
func (k Querier) Order(c context.Context, req *types.QueryOrderRequest) (*types.QueryOrderResponse, error) {
if req == nil {
return nil, status.Error(codes.InvalidArgument, "empty request")
}
if req.PairId == 0 {
return nil, status.Error(codes.InvalidArgument, "pair id cannot be 0")
}
if req.Id == 0 {
return nil, status.Error(codes.InvalidArgument, "id cannot be 0")
}
ctx := sdk.UnwrapSDKContext(c)
order, found := k.GetOrder(ctx, req.PairId, req.Id)
if !found {
return nil, status.Errorf(codes.NotFound, "order %d in pair %d not found", req.PairId, req.Id)
}
return &types.QueryOrderResponse{Order: order}, nil
}
// OrdersByOrderer returns orders made by an orderer.
func (k Querier) OrdersByOrderer(c context.Context, req *types.QueryOrdersByOrdererRequest) (*types.QueryOrdersResponse, error) {
if req == nil {
return nil, status.Error(codes.InvalidArgument, "empty request")
}
orderer, err := sdk.AccAddressFromBech32(req.Orderer)
if err != nil {
return nil, status.Errorf(codes.InvalidArgument, "orderer address %s is invalid", req.Orderer)
}
ctx := sdk.UnwrapSDKContext(c)
store := ctx.KVStore(k.storeKey)
keyPrefix := types.GetOrderIndexKeyPrefix(orderer)
orderStore := prefix.NewStore(store, keyPrefix)
var orders []types.Order
pageRes, err := query.FilteredPaginate(orderStore, req.Pagination, func(key, value []byte, accumulate bool) (bool, error) {
_, pairId, orderId := types.ParseOrderIndexKey(append(keyPrefix, key...))
if req.PairId != 0 && pairId != req.PairId {
return false, nil
}
order, _ := k.GetOrder(ctx, pairId, orderId)
if accumulate {
orders = append(orders, order)
}
return true, nil
})
if err != nil {
return nil, status.Error(codes.Internal, err.Error())
}
return &types.QueryOrdersResponse{Orders: orders, Pagination: pageRes}, nil
}
// OrderBooks queries virtual order books from user orders and pools.
func (k Querier) OrderBooks(c context.Context, req *types.QueryOrderBooksRequest) (*types.QueryOrderBooksResponse, error) {
if req == nil {
return nil, status.Error(codes.InvalidArgument, "empty request")
}
if len(req.PairIds) == 0 {
return nil, status.Error(codes.InvalidArgument, "pair ids must not be empty")
}
if len(req.PriceUnitPowers) == 0 {
req.PriceUnitPowers = []uint32{0, 1, 2}
}
if req.NumTicks == 0 {
return nil, status.Error(codes.InvalidArgument, "number of ticks must not be 0")
}
pairIdSet := map[uint64]struct{}{}
for _, pairId := range req.PairIds {
if _, ok := pairIdSet[pairId]; ok {
return nil, status.Errorf(codes.InvalidArgument, "duplicate pair id: %d", pairId)
}
pairIdSet[pairId] = struct{}{}
}
priceUnitPowerSet := map[uint32]struct{}{}
for _, p := range req.PriceUnitPowers {
if _, ok := priceUnitPowerSet[p]; ok {
return nil, status.Errorf(codes.InvalidArgument, "duplicate price unit power: %d", p)
}
priceUnitPowerSet[p] = struct{}{}
}
ctx := sdk.UnwrapSDKContext(c)
tickPrec := k.GetTickPrecision(ctx)
var pairs []types.OrderBookPairResponse
for _, pairId := range req.PairIds {
pair, found := k.GetPair(ctx, pairId)
if !found {
return nil, status.Errorf(codes.NotFound, "pair %d doesn't exist", pairId)
}
if pair.LastPrice == nil {
return nil, status.Errorf(codes.Unavailable, "pair %d does not have last price", pairId)
}
ob := amm.NewOrderBook()
_ = k.IterateOrdersByPair(ctx, pairId, func(order types.Order) (stop bool, err error) {
switch order.Status {
case types.OrderStatusNotExecuted,
types.OrderStatusNotMatched,
types.OrderStatusPartiallyMatched:
ob.AddOrder(types.NewUserOrder(order))
}
return false, nil
})
lowestPrice, highestPrice := k.PriceLimits(ctx, *pair.LastPrice)
_ = k.IteratePoolsByPair(ctx, pairId, func(pool types.Pool) (stop bool, err error) {
if pool.Disabled {
return false, nil
}
rx, ry := k.getPoolBalances(ctx, pool, pair)
ammPool := pool.AMMPool(rx.Amount, ry.Amount, sdk.Int{})
ob.AddOrder(amm.PoolOrders(ammPool, amm.DefaultOrderer, lowestPrice, highestPrice, int(tickPrec))...)
return false, nil
})
ov := ob.MakeView()
ov.Match()
var configs []types.OrderBookConfig
for _, p := range req.PriceUnitPowers {
configs = append(configs, types.OrderBookConfig{
PriceUnitPower: int(p),
MaxNumTicks: int(req.NumTicks),
})
}
pairs = append(
pairs, types.MakeOrderBookPairResponse(
pair.Id, ov, lowestPrice, highestPrice, int(tickPrec), configs...))
}
return &types.QueryOrderBooksResponse{
Pairs: pairs,
}, nil
}