Skip to content

Commit 0c64b70

Browse files
committed
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 0c64b70

File tree

5 files changed

+1001
-855
lines changed

5 files changed

+1001
-855
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: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -587,7 +587,33 @@ func (s *swapClientServer) ListSwaps(ctx context.Context,
587587
rpcSwaps = append(rpcSwaps, rpcSwap)
588588
idx++
589589
}
590-
return &looprpc.ListSwapsResponse{Swaps: rpcSwaps}, nil
590+
591+
filteredSwapCount := len(rpcSwaps)
592+
endIndex := filteredSwapCount
593+
594+
// Apply pagination if max_swaps or index_offset is specified
595+
if req.MaxSwaps > 0 || req.IndexOffset > 0 {
596+
// Calculate the end index for pagination
597+
endIndex = int(req.IndexOffset + req.MaxSwaps)
598+
if endIndex > filteredSwapCount {
599+
endIndex = filteredSwapCount
600+
}
601+
602+
// Make sure we don't go out of bounds
603+
if int(req.IndexOffset) < filteredSwapCount {
604+
rpcSwaps = rpcSwaps[req.IndexOffset:endIndex]
605+
} else {
606+
// If the offset is beyond the available swaps, return empty
607+
rpcSwaps = []*looprpc.SwapStatus{}
608+
}
609+
}
610+
response := looprpc.ListSwapsResponse{
611+
Swaps: rpcSwaps,
612+
FirstIndexOffset: req.IndexOffset,
613+
LastIndexOffset: uint32(endIndex),
614+
TotalFilteredSwaps: uint32(filteredSwapCount),
615+
}
616+
return &response, nil
591617
}
592618

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

0 commit comments

Comments
 (0)