Skip to content

Commit c8172ad

Browse files
committed
cmd: add listswaps filtering
1 parent a38e817 commit c8172ad

File tree

2 files changed

+85
-6
lines changed

2 files changed

+85
-6
lines changed

cmd/loop/loopout.go

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,13 @@ import (
1515
"github.com/urfave/cli"
1616
)
1717

18+
var (
19+
channelFlag = cli.StringFlag{
20+
Name: "channel",
21+
Usage: "the comma-separated list of short " +
22+
"channel IDs of the channels to loop out",
23+
}
24+
)
1825
var loopOutCommand = cli.Command{
1926
Name: "out",
2027
Usage: "perform an off-chain to on-chain swap (looping out)",
@@ -28,11 +35,6 @@ var loopOutCommand = cli.Command{
2835
Optionally a BASE58/bech32 encoded bitcoin destination address may be
2936
specified. If not specified, a new wallet address will be generated.`,
3037
Flags: []cli.Flag{
31-
cli.StringFlag{
32-
Name: "channel",
33-
Usage: "the comma-separated list of short " +
34-
"channel IDs of the channels to loop out",
35-
},
3638
cli.StringFlag{
3739
Name: "addr",
3840
Usage: "the optional address that the looped out funds " +
@@ -93,6 +95,7 @@ var loopOutCommand = cli.Command{
9395
forceFlag,
9496
labelFlag,
9597
verboseFlag,
98+
channelFlag,
9699
},
97100
Action: loopOut,
98101
}

cmd/loop/swaps.go

Lines changed: 77 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -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

2141
func 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

Comments
 (0)