2
0
mirror of https://github.com/lightninglabs/loop synced 2024-11-13 13:10:30 +00:00
loop/cmd/loop/quote.go

138 lines
3.4 KiB
Go
Raw Normal View History

2019-03-15 23:27:44 +00:00
package main
import (
"context"
"fmt"
"os"
2020-01-07 12:46:40 +00:00
"time"
2019-03-15 23:27:44 +00:00
"github.com/lightninglabs/loop"
2019-03-15 23:27:44 +00:00
"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",
2020-02-21 08:55:20 +00:00
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{confTargetFlag},
Action: quoteIn,
2020-02-21 08:55:20 +00:00
}
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
}
// For loop in, the fee estimation is handed to lnd which tries to
// construct a real transaction to sample realistic fees to pay to the
// HTLC. If the wallet doesn't have enough funds to create this TX, we
// don't want to fail the quote. But the user should still be informed
// why the fee shows as -1.
if quoteResp.MinerFee == int64(loop.MinerFeeEstimationFailed) {
_, _ = fmt.Fprintf(os.Stderr, "Warning: Miner fee estimation "+
"not possible, lnd has insufficient funds to "+
"create a sample transaction for selected "+
"amount.\n")
}
2020-02-21 08:55:20 +00:00
printRespJSON(quoteResp)
return nil
}
var quoteOutCommand = cli.Command{
Name: "out",
Usage: "get a quote for the cost of a loop out swap",
2019-03-15 23:27:44 +00:00
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: uint64(loop.DefaultSweepConfTarget),
},
2020-01-07 12:46:40 +00:00
cli.BoolFlag{
Name: "fast",
Usage: "Indicate you want to swap immediately, " +
"paying potentially a higher fee. If not " +
"set the swap server might choose to wait up " +
"to 30 minutes before publishing the swap " +
"HTLC on-chain, to save on chain fees. Not " +
"setting this flag might result in a lower " +
"swap fee.",
},
},
2020-02-21 08:55:20 +00:00
Action: quoteOut,
2019-03-15 23:27:44 +00:00
}
2020-02-21 08:55:20 +00:00
func quoteOut(ctx *cli.Context) error {
// Show command help if the incorrect number arguments was provided.
if ctx.NArg() != 1 {
2020-02-21 08:55:20 +00:00
return cli.ShowCommandHelp(ctx, "out")
2019-03-15 23:27:44 +00:00
}
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()
2020-01-07 12:46:40 +00:00
fast := ctx.Bool("fast")
swapDeadline := time.Now()
if !fast {
swapDeadline = time.Now().Add(defaultSwapWaitTime)
}
2019-03-15 23:27:44 +00:00
ctxb := context.Background()
2020-02-21 08:55:20 +00:00
quoteReq := &looprpc.QuoteRequest{
2020-01-07 12:46:40 +00:00
Amt: int64(amt),
ConfTarget: int32(ctx.Uint64("conf_target")),
SwapPublicationDeadline: uint64(swapDeadline.Unix()),
2020-02-21 08:55:20 +00:00
}
quoteResp, err := client.LoopOutQuote(ctxb, quoteReq)
2019-03-15 23:27:44 +00:00
if err != nil {
return err
}
2020-02-21 08:55:20 +00:00
printRespJSON(quoteResp)
2019-03-15 23:27:44 +00:00
return nil
}