Skip to content

Commit d446d68

Browse files
committed
cmd/loop: add listswaps and swapinfo commands
1 parent e070494 commit d446d68

File tree

2 files changed

+93
-0
lines changed

2 files changed

+93
-0
lines changed

cmd/loop/main.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,7 @@ func main() {
8383
app.Commands = []cli.Command{
8484
loopOutCommand, loopInCommand, termsCommand,
8585
monitorCommand, quoteCommand, listAuthCommand,
86+
listSwapsCommand, swapInfoCommand,
8687
}
8788

8889
err := app.Run(os.Args)

cmd/loop/swaps.go

Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
package main
2+
3+
import (
4+
"context"
5+
"encoding/hex"
6+
"fmt"
7+
8+
"github.com/lightninglabs/loop/looprpc"
9+
"github.com/lightningnetwork/lnd/lntypes"
10+
"github.com/urfave/cli"
11+
)
12+
13+
var listSwapsCommand = cli.Command{
14+
Name: "listswaps",
15+
Usage: "list all swaps in the local database",
16+
Description: "Allows the user to get a list of all swaps that are " +
17+
"currently stored in the database",
18+
Action: listSwaps,
19+
}
20+
21+
func listSwaps(ctx *cli.Context) error {
22+
client, cleanup, err := getClient(ctx)
23+
if err != nil {
24+
return err
25+
}
26+
defer cleanup()
27+
28+
resp, err := client.ListSwaps(
29+
context.Background(), &looprpc.ListSwapsRequest{},
30+
)
31+
if err != nil {
32+
return err
33+
}
34+
35+
printRespJSON(resp)
36+
return nil
37+
}
38+
39+
var swapInfoCommand = cli.Command{
40+
Name: "swapinfo",
41+
Usage: "show the status of a swap",
42+
ArgsUsage: "id",
43+
Description: "Allows the user to get the status of a single swap " +
44+
"currently stored in the database",
45+
Flags: []cli.Flag{
46+
cli.Uint64Flag{
47+
Name: "id",
48+
Usage: "the ID of the swap",
49+
},
50+
},
51+
Action: swapInfo,
52+
}
53+
54+
func swapInfo(ctx *cli.Context) error {
55+
args := ctx.Args()
56+
57+
var id string
58+
switch {
59+
case ctx.IsSet("id"):
60+
id = ctx.String("id")
61+
case ctx.NArg() > 0:
62+
id = args[0]
63+
args = args.Tail()
64+
default:
65+
// Show command help if no arguments and flags were provided.
66+
return cli.ShowCommandHelp(ctx, "swapinfo")
67+
}
68+
69+
if len(id) != hex.EncodedLen(lntypes.HashSize) {
70+
return fmt.Errorf("invalid swap ID")
71+
}
72+
idBytes, err := hex.DecodeString(id)
73+
if err != nil {
74+
return fmt.Errorf("cannot hex decode id: %v", err)
75+
}
76+
77+
client, cleanup, err := getClient(ctx)
78+
if err != nil {
79+
return err
80+
}
81+
defer cleanup()
82+
83+
resp, err := client.SwapInfo(
84+
context.Background(), &looprpc.SwapInfoRequest{Id: idBytes},
85+
)
86+
if err != nil {
87+
return err
88+
}
89+
90+
printRespJSON(resp)
91+
return nil
92+
}

0 commit comments

Comments
 (0)