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