@@ -67,10 +67,8 @@ func (k Querier) Orders(c context.Context, req *types.QueryOrdersRequest) (*type
6767
6868 states = append (states , byte (stateVal ))
6969 } else {
70- // request does not have pagination set. Start from open store
71- states = append (states , byte (types .OrderOpen ))
72- states = append (states , byte (types .OrderActive ))
73- states = append (states , byte (types .OrderClosed ))
70+ // request does not have a pagination set. Start from an open store
71+ states = append (states , []byte {byte (types .OrderOpen ), byte (types .OrderActive ), byte (types .OrderClosed )}... )
7472 }
7573
7674 var orders types.Orders
@@ -80,9 +78,11 @@ func (k Querier) Orders(c context.Context, req *types.QueryOrdersRequest) (*type
8078
8179 total := uint64 (0 )
8280
83- for idx := range states {
81+ var idx int
82+ var err error
83+
84+ for idx = range states {
8485 state := types .Order_State (states [idx ])
85- var err error
8686
8787 if idx > 0 {
8888 req .Pagination .Key = nil
@@ -125,34 +125,28 @@ func (k Querier) Orders(c context.Context, req *types.QueryOrdersRequest) (*type
125125 return nil , status .Error (codes .Internal , err .Error ())
126126 }
127127
128- if len (pageRes .NextKey ) > 0 {
129- nextKey := make ([]byte , len (searchPrefix )+ len (pageRes .NextKey ))
130- copy (nextKey , searchPrefix )
131- copy (nextKey [len (searchPrefix ):], pageRes .NextKey )
132-
133- pageRes .NextKey = nextKey
134- }
135-
136128 req .Pagination .Limit -= count
137129 total += count
138130
139131 if req .Pagination .Limit == 0 {
140- if len (pageRes .NextKey ) > 0 {
141- pageRes .NextKey , err = query .EncodePaginationKey (states [idx :], searchPrefix , pageRes .NextKey , nil )
142- if err != nil {
143- pageRes .Total = total
144- return & types.QueryOrdersResponse {
145- Orders : orders ,
146- Pagination : pageRes ,
147- }, status .Error (codes .Internal , err .Error ())
148- }
149- }
150-
151132 break
152133 }
153134 }
154135
155- pageRes .Total = total
136+ if pageRes != nil {
137+ pageRes .Total = total
138+
139+ if len (pageRes .NextKey ) > 0 {
140+ pageRes .NextKey , err = query .EncodePaginationKey (states [idx :], searchPrefix , pageRes .NextKey , nil )
141+ if err != nil {
142+ pageRes .Total = total
143+ return & types.QueryOrdersResponse {
144+ Orders : orders ,
145+ Pagination : pageRes ,
146+ }, status .Error (codes .Internal , err .Error ())
147+ }
148+ }
149+ }
156150
157151 return & types.QueryOrdersResponse {
158152 Orders : orders ,
@@ -196,7 +190,7 @@ func (k Querier) Bids(c context.Context, req *types.QueryBidsRequest) (*types.Qu
196190 }
197191 req .Pagination .Key = key
198192
199- if unsolicited [1 ] == 1 {
193+ if unsolicited [0 ] == 1 {
200194 reverseSearch = true
201195 }
202196 } else if req .Filters .State != "" {
@@ -210,7 +204,7 @@ func (k Querier) Bids(c context.Context, req *types.QueryBidsRequest) (*types.Qu
210204
211205 states = append (states , byte (stateVal ))
212206 } else {
213- // request does not have pagination set. Start from open store
207+ // request does not have a pagination set. Start from an open store
214208 states = append (states , byte (types .BidOpen ), byte (types .BidActive ), byte (types .BidLost ), byte (types .BidClosed ))
215209 }
216210
@@ -220,9 +214,11 @@ func (k Querier) Bids(c context.Context, req *types.QueryBidsRequest) (*types.Qu
220214
221215 total := uint64 (0 )
222216
223- for idx := range states {
217+ var idx int
218+ var err error
219+
220+ for idx = range states {
224221 state := types .Bid_State (states [idx ])
225- var err error
226222
227223 if idx > 0 {
228224 req .Pagination .Key = nil
@@ -278,41 +274,35 @@ func (k Querier) Bids(c context.Context, req *types.QueryBidsRequest) (*types.Qu
278274 return nil , status .Error (codes .Internal , err .Error ())
279275 }
280276
281- if len (pageRes .NextKey ) > 0 {
282- nextKey := make ([]byte , len (searchPrefix )+ len (pageRes .NextKey ))
283- copy (nextKey , searchPrefix )
284- copy (nextKey [len (searchPrefix ):], pageRes .NextKey )
285-
286- pageRes .NextKey = nextKey
287- }
288-
289277 req .Pagination .Limit -= count
290278 total += count
291279
292280 if req .Pagination .Limit == 0 {
293- if len (pageRes .NextKey ) > 0 {
294- unsolicited := make ([]byte , 1 )
295- unsolicited [0 ] = 0
296- if reverseSearch {
297- unsolicited [0 ] = 1
298- }
281+ break
282+ }
283+ }
299284
300- pageRes . NextKey , err = query . EncodePaginationKey ( states [ idx :], searchPrefix , pageRes . NextKey , unsolicited )
301- if err != nil {
302- pageRes . Total = total
303- return & types. QueryBidsResponse {
304- Bids : bids ,
305- Pagination : pageRes ,
306- }, status . Error ( codes . Internal , err . Error ())
307- }
285+ if pageRes != nil {
286+ pageRes . Total = total
287+
288+ if len ( pageRes . NextKey ) > 0 {
289+ unsolicited := make ([] byte , 1 )
290+ unsolicited [ 0 ] = 0
291+ if reverseSearch {
292+ unsolicited [ 0 ] = 1
308293 }
309294
310- break
295+ pageRes .NextKey , err = query .EncodePaginationKey (states [idx :], searchPrefix , pageRes .NextKey , unsolicited )
296+ if err != nil {
297+ pageRes .Total = total
298+ return & types.QueryBidsResponse {
299+ Bids : bids ,
300+ Pagination : pageRes ,
301+ }, status .Error (codes .Internal , err .Error ())
302+ }
311303 }
312304 }
313305
314- pageRes .Total = total
315-
316306 return & types.QueryBidsResponse {
317307 Bids : bids ,
318308 Pagination : pageRes ,
@@ -370,7 +360,7 @@ func (k Querier) Leases(c context.Context, req *types.QueryLeasesRequest) (*type
370360
371361 states = append (states , byte (stateVal ))
372362 } else {
373- // request does not have pagination set. Start from open store
363+ // request does not have a pagination set. Start from an open store
374364 states = append (states , byte (v1 .LeaseActive ), byte (v1 .LeaseInsufficientFunds ), byte (v1 .LeaseClosed ))
375365 }
376366
@@ -380,9 +370,11 @@ func (k Querier) Leases(c context.Context, req *types.QueryLeasesRequest) (*type
380370
381371 total := uint64 (0 )
382372
383- for idx := range states {
373+ var idx int
374+ var err error
375+
376+ for idx = range states {
384377 state := v1 .Lease_State (states [idx ])
385- var err error
386378
387379 if idx > 0 {
388380 req .Pagination .Key = nil
@@ -443,29 +435,31 @@ func (k Querier) Leases(c context.Context, req *types.QueryLeasesRequest) (*type
443435 total += count
444436
445437 if req .Pagination .Limit == 0 {
446- if len (pageRes .NextKey ) > 0 {
447- unsolicited := make ([]byte , 1 )
448- unsolicited [0 ] = 0
449- if reverseSearch {
450- unsolicited [0 ] = 1
451- }
438+ break
439+ }
440+ }
452441
453- pageRes . NextKey , err = query . EncodePaginationKey ( states [ idx :], searchPrefix , pageRes . NextKey , unsolicited )
454- if err != nil {
455- pageRes . Total = total
456- return & types. QueryLeasesResponse {
457- Leases : leases ,
458- Pagination : pageRes ,
459- }, status . Error ( codes . Internal , err . Error ())
460- }
442+ if pageRes != nil {
443+ pageRes . Total = total
444+
445+ if len ( pageRes . NextKey ) > 0 {
446+ unsolicited := make ([] byte , 1 )
447+ unsolicited [ 0 ] = 0
448+ if reverseSearch {
449+ unsolicited [ 0 ] = 1
461450 }
462451
463- break
452+ pageRes .NextKey , err = query .EncodePaginationKey (states [idx :], searchPrefix , pageRes .NextKey , unsolicited )
453+ if err != nil {
454+ pageRes .Total = total
455+ return & types.QueryLeasesResponse {
456+ Leases : leases ,
457+ Pagination : pageRes ,
458+ }, status .Error (codes .Internal , err .Error ())
459+ }
464460 }
465461 }
466462
467- pageRes .Total = total
468-
469463 return & types.QueryLeasesResponse {
470464 Leases : leases ,
471465 Pagination : pageRes ,
0 commit comments