@@ -4,9 +4,12 @@ import (
44 "context"
55 "encoding/hex"
66 "fmt"
7+ "strconv"
8+ "strings"
79
810 "github.com/lightninglabs/loop/looprpc"
911 "github.com/lightningnetwork/lnd/lntypes"
12+ "github.com/lightningnetwork/lnd/routing/route"
1013 "github.com/urfave/cli"
1114)
1215
@@ -16,6 +19,23 @@ var listSwapsCommand = cli.Command{
1619 Description : "Allows the user to get a list of all swaps that are " +
1720 "currently stored in the database" ,
1821 Action : listSwaps ,
22+ Flags : []cli.Flag {
23+ cli.BoolFlag {
24+ Name : "loop_out_only" ,
25+ Usage : "only list swaps that are loop out swaps" ,
26+ },
27+ cli.BoolFlag {
28+ Name : "loop_in_only" ,
29+ Usage : "only list swaps that are loop in swaps" ,
30+ },
31+ cli.BoolFlag {
32+ Name : "pending_only" ,
33+ Usage : "only list pending swaps" ,
34+ },
35+ labelFlag ,
36+ channelFlag ,
37+ lastHopFlag ,
38+ },
1939}
2040
2141func listSwaps (ctx * cli.Context ) error {
@@ -25,8 +45,64 @@ func listSwaps(ctx *cli.Context) error {
2545 }
2646 defer cleanup ()
2747
48+ if ctx .Bool ("loop_out_only" ) && ctx .Bool ("loop_in_only" ) {
49+ return fmt .Errorf ("only one of loop_out_only and loop_in_only " +
50+ "can be set" )
51+ }
52+
53+ filter := & looprpc.ListSwapsFilter {}
54+
55+ // Set the swap type filter.
56+ switch {
57+ case ctx .Bool ("loop_out_only" ):
58+ filter .SwapType = looprpc .ListSwapsFilter_LOOP_OUT
59+ case ctx .Bool ("loop_in_only" ):
60+ filter .SwapType = looprpc .ListSwapsFilter_LOOP_IN
61+ }
62+
63+ // Set the pending only filter.
64+ filter .PendingOnly = ctx .Bool ("pending_only" )
65+
66+ // Parse outgoing channel set. Don't string split if the flag is empty.
67+ // Otherwise, strings.Split returns a slice of length one with an empty
68+ // element.
69+ var outgoingChanSet []uint64
70+ if ctx .IsSet (channelFlag .Name ) {
71+ chanStrings := strings .Split (ctx .String (channelFlag .Name ), "," )
72+ for _ , chanString := range chanStrings {
73+ chanID , err := strconv .ParseUint (chanString , 10 , 64 )
74+ if err != nil {
75+ return fmt .Errorf ("error parsing channel id " +
76+ "\" %v\" " , chanString )
77+ }
78+ outgoingChanSet = append (outgoingChanSet , chanID )
79+ }
80+ filter .OutgoingChanSet = outgoingChanSet
81+ }
82+
83+ // Parse last hop.
84+ var lastHop []byte
85+ if ctx .IsSet (lastHopFlag .Name ) {
86+ lastHopVertex , err := route .NewVertexFromStr (
87+ ctx .String (lastHopFlag .Name ),
88+ )
89+ if err != nil {
90+ return err
91+ }
92+
93+ lastHop = lastHopVertex [:]
94+ filter .LoopInLastHop = lastHop
95+ }
96+
97+ // Parse label.
98+ if ctx .IsSet (labelFlag .Name ) {
99+ filter .Label = ctx .String (labelFlag .Name )
100+ }
101+
28102 resp , err := client .ListSwaps (
29- context .Background (), & looprpc.ListSwapsRequest {},
103+ context .Background (), & looprpc.ListSwapsRequest {
104+ ListSwapFilter : filter ,
105+ },
30106 )
31107 if err != nil {
32108 return err
0 commit comments