Skip to content

Commit b8cbe31

Browse files
committed
looprpc: add simple pagination to the ListSwaps command
This commit adds the ability to set a max_swaps and an index_offset flag to the ListSwaps command. These new fields are applied AFTER the initial filtering step. The response also now passes additional information about the index count and total swaps filtered.
1 parent e734d69 commit b8cbe31

File tree

5 files changed

+1003
-856
lines changed

5 files changed

+1003
-856
lines changed

cmd/loop/swaps.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,14 @@ var listSwapsCommand = cli.Command{
3535
labelFlag,
3636
channelFlag,
3737
lastHopFlag,
38+
cli.Uint64Flag{
39+
Name: "max_swaps",
40+
Usage: "Max number of swaps to return after filtering",
41+
},
42+
cli.Uint64Flag{
43+
Name: "index_offset",
44+
Usage: "index to start listing filtered swaps from",
45+
},
3846
},
3947
}
4048

@@ -102,6 +110,8 @@ func listSwaps(ctx *cli.Context) error {
102110
resp, err := client.ListSwaps(
103111
context.Background(), &looprpc.ListSwapsRequest{
104112
ListSwapFilter: filter,
113+
IndexOffset: uint32(ctx.Uint("index_offset")),
114+
MaxSwaps: uint32(ctx.Uint("max_swaps")),
105115
},
106116
)
107117
if err != nil {

loopd/swapclient_server.go

Lines changed: 29 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -579,15 +579,42 @@ func (s *swapClientServer) ListSwaps(ctx context.Context,
579579
if !filterSwap(&swp, req.ListSwapFilter) {
580580
continue
581581
}
582-
583582
rpcSwap, err := s.marshallSwap(ctx, &swp)
584583
if err != nil {
585584
return nil, err
586585
}
587586
rpcSwaps = append(rpcSwaps, rpcSwap)
588587
idx++
589588
}
590-
return &looprpc.ListSwapsResponse{Swaps: rpcSwaps}, nil
589+
590+
filteredSwapCount := len(rpcSwaps)
591+
endIndex := filteredSwapCount
592+
593+
// Calculate the endIndex if necessary
594+
if req.MaxSwaps > 0 {
595+
endIndex = int(req.IndexOffset + req.MaxSwaps)
596+
if endIndex > filteredSwapCount {
597+
endIndex = filteredSwapCount
598+
}
599+
}
600+
601+
// If starting indexOffset is more than the total return empty
602+
if int(req.IndexOffset) >= filteredSwapCount {
603+
rpcSwaps = []*looprpc.SwapStatus{}
604+
}
605+
606+
// finally, only slice the slice if indexes are in range
607+
if int(req.IndexOffset) < filteredSwapCount {
608+
rpcSwaps = rpcSwaps[req.IndexOffset:endIndex]
609+
}
610+
611+
response := looprpc.ListSwapsResponse{
612+
Swaps: rpcSwaps,
613+
FirstIndexOffset: req.IndexOffset,
614+
LastIndexOffset: uint32(endIndex),
615+
TotalFilteredSwaps: uint32(filteredSwapCount),
616+
}
617+
return &response, nil
591618
}
592619

593620
// filterSwap filters the given swap based on the provided filter.

0 commit comments

Comments
 (0)