2019-03-15 23:27:44 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2020-02-21 09:34:08 +00:00
|
|
|
"fmt"
|
|
|
|
"os"
|
2020-01-07 12:46:40 +00:00
|
|
|
"time"
|
2019-03-15 23:27:44 +00:00
|
|
|
|
2020-02-21 09:34:08 +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",
|
2020-04-15 07:10:21 +00:00
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2020-02-21 09:34:08 +00:00
|
|
|
// 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",
|
2019-06-25 18:41:45 +00:00
|
|
|
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",
|
2020-04-15 07:10:21 +00:00
|
|
|
Value: uint64(loop.DefaultSweepConfTarget),
|
2019-06-25 18:41:45 +00:00
|
|
|
},
|
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.",
|
|
|
|
},
|
2019-06-25 18:41:45 +00:00
|
|
|
},
|
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 {
|
2020-02-06 13:03:53 +00:00
|
|
|
// 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
|
|
|
|
}
|