From 48d34ed398e06ae3149abbc4bb3ef0a99e57f181 Mon Sep 17 00:00:00 2001 From: Olaoluwa Osuntokun Date: Fri, 15 Mar 2019 16:27:44 -0700 Subject: [PATCH] cmd/loop: add command for quote --- cmd/loop/main.go | 19 ++++++++++++++++++- cmd/loop/quote.go | 47 +++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 65 insertions(+), 1 deletion(-) create mode 100644 cmd/loop/quote.go diff --git a/cmd/loop/main.go b/cmd/loop/main.go index 9e44fb0..1d91454 100644 --- a/cmd/loop/main.go +++ b/cmd/loop/main.go @@ -7,6 +7,8 @@ import ( "strconv" "time" + "github.com/golang/protobuf/jsonpb" + "github.com/golang/protobuf/proto" "github.com/lightninglabs/loop/looprpc" "github.com/lightninglabs/loop/swap" @@ -25,6 +27,21 @@ var ( maxRoutingFeeRate = int64(50000) ) +func printRespJSON(resp proto.Message) { + jsonMarshaler := &jsonpb.Marshaler{ + EmitDefaults: true, + Indent: " ", + } + + jsonStr, err := jsonMarshaler.MarshalToString(resp) + if err != nil { + fmt.Println("unable to decode response: ", err) + return + } + + fmt.Println(jsonStr) +} + func fatal(err error) { fmt.Fprintf(os.Stderr, "[loop] %v\n", err) os.Exit(1) @@ -44,7 +61,7 @@ func main() { }, } app.Commands = []cli.Command{ - loopOutCommand, termsCommand, monitorCommand, + loopOutCommand, termsCommand, monitorCommand, quoteCommand, } err := app.Run(os.Args) diff --git a/cmd/loop/quote.go b/cmd/loop/quote.go new file mode 100644 index 0000000..08d62c4 --- /dev/null +++ b/cmd/loop/quote.go @@ -0,0 +1,47 @@ +package main + +import ( + "context" + + "github.com/lightninglabs/loop/looprpc" + "github.com/urfave/cli" +) + +var quoteCommand = cli.Command{ + Name: "quote", + Usage: "get a quote for the cost of a swap", + ArgsUsage: "amt", + Description: "Allows to determine the cost of a swap up front", + Action: quote, +} + +func quote(ctx *cli.Context) error { + // Show command help if no arguments and flags were provided. + if ctx.NArg() < 1 { + cli.ShowCommandHelp(ctx, "quote") + return nil + } + + args := ctx.Args() + amt, err := parseAmt(args[0]) + if err != nil { + return err + } + + client, cleanup, err := getClient(ctx) + if err != nil { + return err + } + defer cleanup() + + ctxb := context.Background() + resp, err := client.LoopOutQuote(ctxb, &looprpc.QuoteRequest{ + Amt: int64(amt), + }) + if err != nil { + return err + } + + printRespJSON(resp) + return nil +}