From a57a2947b5f037e0e28da38c2048541659e6f8c8 Mon Sep 17 00:00:00 2001 From: Oliver Gugger Date: Fri, 21 Feb 2020 09:55:20 +0100 Subject: [PATCH] cmd/loop: add loop in quote command --- cmd/loop/quote.go | 68 ++++++++++++++++++++++++++++++++++++++++++----- 1 file changed, 62 insertions(+), 6 deletions(-) diff --git a/cmd/loop/quote.go b/cmd/loop/quote.go index a776423..82a2342 100644 --- a/cmd/loop/quote.go +++ b/cmd/loop/quote.go @@ -11,6 +11,61 @@ import ( var quoteCommand = cli.Command{ Name: "quote", Usage: "get a quote for the cost of a swap", + Subcommands: []cli.Command{quoteInCommand, quoteOutCommand}, +} + +var quoteInCommand = cli.Command{ + Name: "in", + Usage: "get a quote for the cost of a loop in swap", + ArgsUsage: "amt", + Description: "Allows to determine the cost of a swap up front", + Flags: []cli.Flag{ + cli.Uint64Flag{ + Name: "conf_target", + Usage: "the number of blocks from the swap " + + "initiation height that the on-chain HTLC " + + "should be swept within in a Loop Out", + Value: 6, + }, + }, + Action: quoteIn, +} + +func quoteIn(ctx *cli.Context) error { + // Show command help if the incorrect number arguments was provided. + if ctx.NArg() != 1 { + return cli.ShowCommandHelp(ctx, "in") + } + + 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() + quoteReq := &looprpc.QuoteRequest{ + Amt: int64(amt), + ConfTarget: int32(ctx.Uint64("conf_target")), + } + quoteResp, err := client.GetLoopInQuote(ctxb, quoteReq) + if err != nil { + return err + } + + printRespJSON(quoteResp) + return nil +} + +var quoteOutCommand = cli.Command{ + Name: "out", + Usage: "get a quote for the cost of a loop out swap", ArgsUsage: "amt", Description: "Allows to determine the cost of a swap up front", Flags: []cli.Flag{ @@ -32,13 +87,13 @@ var quoteCommand = cli.Command{ "swap fee.", }, }, - Action: quote, + Action: quoteOut, } -func quote(ctx *cli.Context) error { +func quoteOut(ctx *cli.Context) error { // Show command help if the incorrect number arguments was provided. if ctx.NArg() != 1 { - return cli.ShowCommandHelp(ctx, "quote") + return cli.ShowCommandHelp(ctx, "out") } args := ctx.Args() @@ -60,15 +115,16 @@ func quote(ctx *cli.Context) error { } ctxb := context.Background() - resp, err := client.LoopOutQuote(ctxb, &looprpc.QuoteRequest{ + quoteReq := &looprpc.QuoteRequest{ Amt: int64(amt), ConfTarget: int32(ctx.Uint64("conf_target")), SwapPublicationDeadline: uint64(swapDeadline.Unix()), - }) + } + quoteResp, err := client.LoopOutQuote(ctxb, quoteReq) if err != nil { return err } - printRespJSON(resp) + printRespJSON(quoteResp) return nil }