-
Notifications
You must be signed in to change notification settings - Fork 0
/
grpc_orderbook.go
189 lines (154 loc) · 6.73 KB
/
grpc_orderbook.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
package keeper
import (
"context"
"strings"
"github.com/cosmos/cosmos-sdk/store/prefix"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/types/query"
"github.com/furynet/xfury/x/strategicreserve/types"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"
)
// OrderBooks queries all order books that match the given status
func (k Keeper) OrderBooks(c context.Context, req *types.QueryOrderBooksRequest) (*types.QueryOrderBooksResponse, error) {
if req == nil {
return nil, status.Error(codes.InvalidArgument, "empty request")
}
// validate the provided status, return all the orderbooks if the status is empty
if req.Status != "" && !(req.Status == types.OrderBookStatus_ORDER_BOOK_STATUS_STATUS_ACTIVE.String() ||
req.Status == types.OrderBookStatus_ORDER_BOOK_STATUS_STATUS_SETTLED.String()) {
return nil, status.Errorf(codes.InvalidArgument, "invalid order book status %s", req.Status)
}
var orderBooks []types.OrderBook
ctx := sdk.UnwrapSDKContext(c)
store := ctx.KVStore(k.storeKey)
bookStore := prefix.NewStore(store, types.OrderBookKeyPrefix)
pageRes, err := query.FilteredPaginate(bookStore, req.Pagination, func(key []byte, value []byte, accumulate bool) (bool, error) {
orderBook, err := types.UnmarshalOrderBook(k.cdc, value)
if err != nil {
return false, err
}
if req.Status != "" && !strings.EqualFold(orderBook.Status.String(), req.Status) {
return false, nil
}
if accumulate {
orderBooks = append(orderBooks, orderBook)
}
return true, nil
})
if err != nil {
return nil, status.Error(codes.Internal, err.Error())
}
return &types.QueryOrderBooksResponse{Orderbooks: orderBooks, Pagination: pageRes}, nil
}
// OrderBook queries strategicreserve info for given order book id
func (k Keeper) OrderBook(c context.Context, req *types.QueryOrderBookRequest) (*types.QueryOrderBookResponse, error) {
if req == nil {
return nil, status.Error(codes.InvalidArgument, "empty request")
}
if req.OrderBookUid == "" {
return nil, status.Error(codes.InvalidArgument, "book id can not be empty")
}
ctx := sdk.UnwrapSDKContext(c)
orderBook, found := k.GetOrderBook(ctx, req.OrderBookUid)
if !found {
return nil, status.Errorf(codes.NotFound, "order book %s not found", req.OrderBookUid)
}
return &types.QueryOrderBookResponse{OrderBook: orderBook}, nil
}
// OrderBookParticipations queries participations info for given strategicreserve
func (k Keeper) OrderBookParticipations(c context.Context, req *types.QueryOrderBookParticipationsRequest) (*types.QueryOrderBookParticipationsResponse, error) {
if req == nil {
return nil, status.Error(codes.InvalidArgument, "empty request")
}
if req.OrderBookUid == "" {
return nil, status.Error(codes.InvalidArgument, "book id cannot be empty")
}
var orderBookParticipations []types.OrderBookParticipation
ctx := sdk.UnwrapSDKContext(c)
store := prefix.NewStore(ctx.KVStore(k.storeKey), types.OrderBookParticipationKeyPrefix)
participationStore := prefix.NewStore(store, types.GetOrderBookParticipationsKey(req.OrderBookUid))
pageRes, err := query.FilteredPaginate(participationStore, req.Pagination, func(key []byte, value []byte, accumulate bool) (bool, error) {
var bookParticipation types.OrderBookParticipation
if err := k.cdc.Unmarshal(value, &bookParticipation); err != nil {
return false, err
}
if accumulate {
orderBookParticipations = append(orderBookParticipations, bookParticipation)
}
return true, nil
})
if err != nil {
return nil, status.Error(codes.Internal, err.Error())
}
return &types.QueryOrderBookParticipationsResponse{
OrderBookParticipations: orderBookParticipations,
Pagination: pageRes,
}, nil
}
// OrderBookParticipation queries book participation info for given order book id and participation index
func (k Keeper) OrderBookParticipation(c context.Context, req *types.QueryOrderBookParticipationRequest) (*types.QueryOrderBookParticipationResponse, error) {
if req == nil {
return nil, status.Error(codes.InvalidArgument, "empty request")
}
if req.OrderBookUid == "" {
return nil, status.Error(codes.InvalidArgument, "book id can not be empty")
}
if req.ParticipationIndex < 1 {
return nil, status.Error(codes.InvalidArgument, "participation index can not be less than 1")
}
ctx := sdk.UnwrapSDKContext(c)
orderBookParticipation, found := k.GetOrderBookParticipation(ctx, req.OrderBookUid, req.ParticipationIndex)
if !found {
return nil, status.Errorf(codes.NotFound, "book participation %s, %d not found", req.OrderBookUid, req.ParticipationIndex)
}
return &types.QueryOrderBookParticipationResponse{OrderBookParticipation: orderBookParticipation}, nil
}
// OrderBookExposures queries exposures info for given strategicreserve
func (k Keeper) OrderBookExposures(c context.Context, req *types.QueryOrderBookExposuresRequest) (*types.QueryOrderBookExposuresResponse, error) {
if req == nil {
return nil, status.Error(codes.InvalidArgument, "empty request")
}
if req.OrderBookUid == "" {
return nil, status.Error(codes.InvalidArgument, "book id cannot be empty")
}
var orderBookExposures []types.OrderBookOddsExposure
ctx := sdk.UnwrapSDKContext(c)
store := prefix.NewStore(ctx.KVStore(k.storeKey), types.OrderBookOddsExposureKeyPrefix)
exposureStore := prefix.NewStore(store, types.GetOrderBookOddsExposuresKey(req.OrderBookUid))
pageRes, err := query.FilteredPaginate(exposureStore, req.Pagination, func(key []byte, value []byte, accumulate bool) (bool, error) {
var orderBookExposure types.OrderBookOddsExposure
if err := k.cdc.Unmarshal(value, &orderBookExposure); err != nil {
return false, err
}
if accumulate {
orderBookExposures = append(orderBookExposures, orderBookExposure)
}
return true, nil
})
if err != nil {
return nil, status.Error(codes.Internal, err.Error())
}
return &types.QueryOrderBookExposuresResponse{
OrderBookExposures: orderBookExposures,
Pagination: pageRes,
}, nil
}
// OrderBookExposure queries book exposure info for given order book id and odds id
func (k Keeper) OrderBookExposure(c context.Context, req *types.QueryOrderBookExposureRequest) (*types.QueryOrderBookExposureResponse, error) {
if req == nil {
return nil, status.Error(codes.InvalidArgument, "empty request")
}
if req.OrderBookUid == "" {
return nil, status.Error(codes.InvalidArgument, "book id can not be empty")
}
if req.OddsUid == "" {
return nil, status.Error(codes.InvalidArgument, "odds id can not be empty")
}
ctx := sdk.UnwrapSDKContext(c)
orderBookExposure, found := k.GetOrderBookOddsExposure(ctx, req.OrderBookUid, req.OddsUid)
if !found {
return nil, status.Errorf(codes.NotFound, "book exposure %s, %s not found", req.OrderBookUid, req.OddsUid)
}
return &types.QueryOrderBookExposureResponse{OrderBookExposure: orderBookExposure}, nil
}