Skip to content

Commit

Permalink
cmd: add listswaps filtering
Browse files Browse the repository at this point in the history
  • Loading branch information
sputn1ck committed Dec 24, 2023
1 parent 4a9419f commit c17c923
Show file tree
Hide file tree
Showing 2 changed files with 85 additions and 6 deletions.
13 changes: 8 additions & 5 deletions cmd/loop/loopout.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,13 @@ import (
"github.com/urfave/cli"
)

var (
channelFlag = cli.StringFlag{
Name: "channel",
Usage: "the comma-separated list of short " +
"channel IDs of the channels to loop out",
}
)
var loopOutCommand = cli.Command{
Name: "out",
Usage: "perform an off-chain to on-chain swap (looping out)",
Expand All @@ -28,11 +35,6 @@ var loopOutCommand = cli.Command{
Optionally a BASE58/bech32 encoded bitcoin destination address may be
specified. If not specified, a new wallet address will be generated.`,
Flags: []cli.Flag{
cli.StringFlag{
Name: "channel",
Usage: "the comma-separated list of short " +
"channel IDs of the channels to loop out",
},
cli.StringFlag{
Name: "addr",
Usage: "the optional address that the looped out funds " +
Expand Down Expand Up @@ -93,6 +95,7 @@ var loopOutCommand = cli.Command{
forceFlag,
labelFlag,
verboseFlag,
channelFlag,
},
Action: loopOut,
}
Expand Down
78 changes: 77 additions & 1 deletion cmd/loop/swaps.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,12 @@ import (
"context"
"encoding/hex"
"fmt"
"strconv"
"strings"

"github.com/lightninglabs/loop/looprpc"
"github.com/lightningnetwork/lnd/lntypes"
"github.com/lightningnetwork/lnd/routing/route"
"github.com/urfave/cli"
)

Expand All @@ -16,6 +19,23 @@ var listSwapsCommand = cli.Command{
Description: "Allows the user to get a list of all swaps that are " +
"currently stored in the database",
Action: listSwaps,
Flags: []cli.Flag{
cli.BoolFlag{
Name: "loop_out_only",
Usage: "only list swaps that are loop out swaps",
},
cli.BoolFlag{
Name: "loop_in_only",
Usage: "only list swaps that are loop in swaps",
},
cli.BoolFlag{
Name: "pending_only",
Usage: "only list pending swaps",
},
labelFlag,
channelFlag,
lastHopFlag,
},
}

func listSwaps(ctx *cli.Context) error {
Expand All @@ -25,8 +45,64 @@ func listSwaps(ctx *cli.Context) error {
}
defer cleanup()

if ctx.Bool("loop_out_only") && ctx.Bool("loop_in_only") {
return fmt.Errorf("only one of loop_out_only and loop_in_only " +
"can be set")
}

filter := &looprpc.ListSwapsFilter{}

// Set the swap type filter.
switch {
case ctx.Bool("loop_out_only"):
filter.SwapType = looprpc.ListSwapsFilter_LOOP_OUT
case ctx.Bool("loop_in_only"):
filter.SwapType = looprpc.ListSwapsFilter_LOOP_IN
}

// Set the pending only filter.
filter.PendingOnly = ctx.Bool("pending_only")

// Parse outgoing channel set. Don't string split if the flag is empty.
// Otherwise, strings.Split returns a slice of length one with an empty
// element.
var outgoingChanSet []uint64
if ctx.IsSet(channelFlag.Name) {
chanStrings := strings.Split(ctx.String(channelFlag.Name), ",")
for _, chanString := range chanStrings {
chanID, err := strconv.ParseUint(chanString, 10, 64)
if err != nil {
return fmt.Errorf("error parsing channel id "+
"\"%v\"", chanString)
}
outgoingChanSet = append(outgoingChanSet, chanID)
}
filter.OutgoingChanSet = outgoingChanSet
}

// Parse last hop.
var lastHop []byte
if ctx.IsSet(lastHopFlag.Name) {
lastHopVertex, err := route.NewVertexFromStr(
ctx.String(lastHopFlag.Name),
)
if err != nil {
return err
}

lastHop = lastHopVertex[:]
filter.LoopInLastHop = lastHop
}

// Parse label.
if ctx.IsSet(labelFlag.Name) {
filter.Label = ctx.String(labelFlag.Name)
}

resp, err := client.ListSwaps(
context.Background(), &looprpc.ListSwapsRequest{},
context.Background(), &looprpc.ListSwapsRequest{
ListSwapFilter: filter,
},
)
if err != nil {
return err
Expand Down

0 comments on commit c17c923

Please sign in to comment.