Skip to content

Commit 48d34ed

Browse files
committed
cmd/loop: add command for quote
1 parent c9549cc commit 48d34ed

File tree

2 files changed

+65
-1
lines changed

2 files changed

+65
-1
lines changed

cmd/loop/main.go

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@ import (
77
"strconv"
88
"time"
99

10+
"github.com/golang/protobuf/jsonpb"
11+
"github.com/golang/protobuf/proto"
1012
"github.com/lightninglabs/loop/looprpc"
1113
"github.com/lightninglabs/loop/swap"
1214

@@ -25,6 +27,21 @@ var (
2527
maxRoutingFeeRate = int64(50000)
2628
)
2729

30+
func printRespJSON(resp proto.Message) {
31+
jsonMarshaler := &jsonpb.Marshaler{
32+
EmitDefaults: true,
33+
Indent: " ",
34+
}
35+
36+
jsonStr, err := jsonMarshaler.MarshalToString(resp)
37+
if err != nil {
38+
fmt.Println("unable to decode response: ", err)
39+
return
40+
}
41+
42+
fmt.Println(jsonStr)
43+
}
44+
2845
func fatal(err error) {
2946
fmt.Fprintf(os.Stderr, "[loop] %v\n", err)
3047
os.Exit(1)
@@ -44,7 +61,7 @@ func main() {
4461
},
4562
}
4663
app.Commands = []cli.Command{
47-
loopOutCommand, termsCommand, monitorCommand,
64+
loopOutCommand, termsCommand, monitorCommand, quoteCommand,
4865
}
4966

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

cmd/loop/quote.go

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
package main
2+
3+
import (
4+
"context"
5+
6+
"github.com/lightninglabs/loop/looprpc"
7+
"github.com/urfave/cli"
8+
)
9+
10+
var quoteCommand = cli.Command{
11+
Name: "quote",
12+
Usage: "get a quote for the cost of a swap",
13+
ArgsUsage: "amt",
14+
Description: "Allows to determine the cost of a swap up front",
15+
Action: quote,
16+
}
17+
18+
func quote(ctx *cli.Context) error {
19+
// Show command help if no arguments and flags were provided.
20+
if ctx.NArg() < 1 {
21+
cli.ShowCommandHelp(ctx, "quote")
22+
return nil
23+
}
24+
25+
args := ctx.Args()
26+
amt, err := parseAmt(args[0])
27+
if err != nil {
28+
return err
29+
}
30+
31+
client, cleanup, err := getClient(ctx)
32+
if err != nil {
33+
return err
34+
}
35+
defer cleanup()
36+
37+
ctxb := context.Background()
38+
resp, err := client.LoopOutQuote(ctxb, &looprpc.QuoteRequest{
39+
Amt: int64(amt),
40+
})
41+
if err != nil {
42+
return err
43+
}
44+
45+
printRespJSON(resp)
46+
return nil
47+
}

0 commit comments

Comments
 (0)