cmd: quote for static address loop-ins

Slyghtning 3 weeks ago
parent 936698c639
commit 08bb91dead
No known key found for this signature in database
GPG Key ID: F82D456EA023C9BF

@ -6,6 +6,7 @@ import (
"os" "os"
"time" "time"
"github.com/btcsuite/btcd/btcutil"
"github.com/lightninglabs/loop" "github.com/lightninglabs/loop"
"github.com/lightninglabs/loop/looprpc" "github.com/lightninglabs/loop/looprpc"
"github.com/lightningnetwork/lnd/routing/route" "github.com/lightningnetwork/lnd/routing/route"
@ -19,10 +20,11 @@ var quoteCommand = cli.Command{
} }
var quoteInCommand = cli.Command{ var quoteInCommand = cli.Command{
Name: "in", Name: "in",
Usage: "get a quote for the cost of a loop in swap", Usage: "get a quote for the cost of a loop in swap",
ArgsUsage: "amt", ArgsUsage: "amt",
Description: "Allows to determine the cost of a swap up front", Description: "Allows to determine the cost of a swap up front." +
"Either specify an amount or deposit outpoints.",
Flags: []cli.Flag{ Flags: []cli.Flag{
cli.StringFlag{ cli.StringFlag{
Name: lastHopFlag.Name, Name: lastHopFlag.Name,
@ -33,20 +35,36 @@ var quoteInCommand = cli.Command{
verboseFlag, verboseFlag,
privateFlag, privateFlag,
routeHintsFlag, routeHintsFlag,
cli.StringSliceFlag{
Name: "deposit_outpoint",
Usage: "one or more static address deposit outpoints " +
"to quote for. Deposit outpoints are not to " +
"be used in combination with an amount.",
},
}, },
Action: quoteIn, Action: quoteIn,
} }
func quoteIn(ctx *cli.Context) error { func quoteIn(ctx *cli.Context) error {
// Show command help if the incorrect number arguments was provided. // Show command help if the incorrect number arguments was provided.
if ctx.NArg() != 1 { if ctx.NArg() != 1 && !ctx.IsSet("deposit_outpoint") {
return cli.ShowCommandHelp(ctx, "in") return cli.ShowCommandHelp(ctx, "in")
} }
args := ctx.Args() var (
amt, err := parseAmt(args[0]) manualAmt btcutil.Amount
if err != nil { depositAmt btcutil.Amount
return err depositOutpoints []string
err error
ctxb = context.Background()
)
if ctx.NArg() == 1 {
args := ctx.Args()
manualAmt, err = parseAmt(args[0])
if err != nil {
return err
}
} }
client, cleanup, err := getClient(ctx) client, cleanup, err := getClient(ctx)
@ -62,11 +80,20 @@ func quoteIn(ctx *cli.Context) error {
return err return err
} }
if ctx.IsSet("deposit_outpoint") {
depositOutpoints = ctx.StringSlice("deposit_outpoint")
depositAmt, err = depositAmount(ctxb, client, depositOutpoints)
if err != nil {
return err
}
}
quoteReq := &looprpc.QuoteRequest{ quoteReq := &looprpc.QuoteRequest{
Amt: int64(amt), Amt: int64(manualAmt),
ConfTarget: int32(ctx.Uint64("conf_target")), ConfTarget: int32(ctx.Uint64("conf_target")),
LoopInRouteHints: hints, LoopInRouteHints: hints,
Private: ctx.Bool(privateFlag.Name), Private: ctx.Bool(privateFlag.Name),
DepositOutpoints: depositOutpoints,
} }
if ctx.IsSet(lastHopFlag.Name) { if ctx.IsSet(lastHopFlag.Name) {
@ -80,7 +107,6 @@ func quoteIn(ctx *cli.Context) error {
quoteReq.LoopInLastHop = lastHopVertex[:] quoteReq.LoopInLastHop = lastHopVertex[:]
} }
ctxb := context.Background()
quoteResp, err := client.GetLoopInQuote(ctxb, quoteReq) quoteResp, err := client.GetLoopInQuote(ctxb, quoteReq)
if err != nil { if err != nil {
return err return err
@ -98,10 +124,39 @@ func quoteIn(ctx *cli.Context) error {
"amount.\n") "amount.\n")
} }
// If the user specified static address deposits, we quoted for their
// total value and need to display that value instead of the manually
// selected one.
if manualAmt == 0 {
quoteReq.Amt = int64(depositAmt)
}
printQuoteInResp(quoteReq, quoteResp, ctx.Bool("verbose")) printQuoteInResp(quoteReq, quoteResp, ctx.Bool("verbose"))
return nil return nil
} }
func depositAmount(ctx context.Context, client looprpc.SwapClientClient,
depositOutpoints []string) (btcutil.Amount, error) {
var (
depositAmt btcutil.Amount
)
addressSummary, err := client.GetStaticAddressSummary(
ctx, &looprpc.StaticAddressSummaryRequest{
Outpoints: depositOutpoints,
},
)
if err != nil {
return 0, err
}
for _, deposit := range addressSummary.FilteredDeposits {
depositAmt += btcutil.Amount(deposit.Value)
}
return depositAmt, nil
}
var quoteOutCommand = cli.Command{ var quoteOutCommand = cli.Command{
Name: "out", Name: "out",
Usage: "get a quote for the cost of a loop out swap", Usage: "get a quote for the cost of a loop out swap",

Loading…
Cancel
Save