-
Notifications
You must be signed in to change notification settings - Fork 1
/
grpc_query_order.go
103 lines (97 loc) · 3.59 KB
/
grpc_query_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
package query
import (
"context"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/fibonacci-chain/core/x/dex/types"
)
// To be deprecated once offchain query is built
func (k KeeperWrapper) GetOrder(c context.Context, req *types.QueryGetOrderByIDRequest) (*types.QueryGetOrderByIDResponse, error) {
ctx := sdk.UnwrapSDKContext(c)
longBooks := k.GetAllLongBook(ctx, req.ContractAddr)
for _, longBook := range longBooks {
for _, allocation := range longBook.Entry.Allocations {
if allocation.OrderId == req.Id {
return &types.QueryGetOrderByIDResponse{
Order: &types.Order{
Id: req.Id,
Price: longBook.Price,
Quantity: allocation.Quantity,
PriceDenom: longBook.Entry.PriceDenom,
AssetDenom: longBook.Entry.AssetDenom,
OrderType: types.OrderType_LIMIT,
Status: types.OrderStatus_PLACED,
ContractAddr: req.ContractAddr,
PositionDirection: types.PositionDirection_LONG,
Account: allocation.Account,
},
}, nil
}
}
}
shortBooks := k.GetAllShortBook(ctx, req.ContractAddr)
for _, shortBook := range shortBooks {
for _, allocation := range shortBook.Entry.Allocations {
if allocation.OrderId == req.Id {
return &types.QueryGetOrderByIDResponse{
Order: &types.Order{
Id: req.Id,
Price: shortBook.Price,
Quantity: allocation.Quantity,
PriceDenom: shortBook.Entry.PriceDenom,
AssetDenom: shortBook.Entry.AssetDenom,
OrderType: types.OrderType_LIMIT,
Status: types.OrderStatus_PLACED,
ContractAddr: req.ContractAddr,
PositionDirection: types.PositionDirection_SHORT,
Account: allocation.Account,
},
}, nil
}
}
}
return &types.QueryGetOrderByIDResponse{}, types.ErrInvalidOrderID
}
// To be deprecated once offchain query is built
func (k KeeperWrapper) GetOrders(c context.Context, req *types.QueryGetOrdersRequest) (*types.QueryGetOrdersResponse, error) {
ctx := sdk.UnwrapSDKContext(c)
orders := []*types.Order{}
longBooks := k.GetAllLongBook(ctx, req.ContractAddr)
for _, longBook := range longBooks {
for _, allocation := range longBook.Entry.Allocations {
if allocation.Account == req.Account {
orders = append(orders, &types.Order{
Id: allocation.OrderId,
Price: longBook.Price,
Quantity: allocation.Quantity,
PriceDenom: longBook.Entry.PriceDenom,
AssetDenom: longBook.Entry.AssetDenom,
OrderType: types.OrderType_LIMIT,
Status: types.OrderStatus_PLACED,
ContractAddr: req.ContractAddr,
PositionDirection: types.PositionDirection_LONG,
Account: allocation.Account,
})
}
}
}
shortBooks := k.GetAllShortBook(ctx, req.ContractAddr)
for _, shortBook := range shortBooks {
for _, allocation := range shortBook.Entry.Allocations {
if allocation.Account == req.Account {
orders = append(orders, &types.Order{
Id: allocation.OrderId,
Price: shortBook.Price,
Quantity: allocation.Quantity,
PriceDenom: shortBook.Entry.PriceDenom,
AssetDenom: shortBook.Entry.AssetDenom,
OrderType: types.OrderType_LIMIT,
Status: types.OrderStatus_PLACED,
ContractAddr: req.ContractAddr,
PositionDirection: types.PositionDirection_SHORT,
Account: allocation.Account,
})
}
}
}
return &types.QueryGetOrdersResponse{Orders: orders}, nil
}