Skip to content

Commit

Permalink
Merge pull request #853 from lightninglabs/static-addr-staging
Browse files Browse the repository at this point in the history
StaticAddr: merge staging to master
  • Loading branch information
hieblmi authored Dec 18, 2024
2 parents 2ec44f0 + acea303 commit a49c60a
Show file tree
Hide file tree
Showing 64 changed files with 13,684 additions and 886 deletions.
34 changes: 28 additions & 6 deletions client.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,9 +43,13 @@ var (
ErrSwapAmountTooHigh = errors.New("swap amount too high")

// ErrExpiryTooFar is returned when the server proposes an expiry that
// is too soon for us.
// is too far in the future.
ErrExpiryTooFar = errors.New("swap expiry too far")

// ErrExpiryTooSoon is returned when the server proposes an expiry that
// is too soon.
ErrExpiryTooSoon = errors.New("swap expiry too soon")

// ErrInsufficientBalance indicates insufficient confirmed balance to
// publish a swap.
ErrInsufficientBalance = errors.New("insufficient confirmed balance")
Expand Down Expand Up @@ -137,6 +141,22 @@ type ClientConfig struct {
// MaxPaymentRetries is the maximum times we retry an off-chain payment
// (used in loop out).
MaxPaymentRetries int

// MaxStaticAddrHtlcFeePercentage is the percentage of the swap amount
// that we allow the server to charge for the htlc transaction.
// Although highly unlikely, this is a defense against the server
// publishing the htlc without paying the swap invoice, forcing us to
// sweep the timeout path.
MaxStaticAddrHtlcFeePercentage float64

// MaxStaticAddrHtlcBackupFeePercentage is the percentage of the swap
// amount that we allow the server to charge for the htlc backup
// transactions. This is a defense against the server publishing the
// htlc backup without paying the swap invoice, forcing us to sweep the
// timeout path. This value is elevated compared to
// MaxStaticAddrHtlcFeePercentage since it serves the server as backup
// transaction in case of fee spikes.
MaxStaticAddrHtlcBackupFeePercentage float64
}

// NewClient returns a new instance to initiate swaps with.
Expand Down Expand Up @@ -694,9 +714,9 @@ func (s *Client) LoopIn(globalCtx context.Context,
return swapInfo, nil
}

// LoopInQuote takes an amount and returns a break down of estimated
// costs for the client. Both the swap server and the on-chain fee estimator are
// queried to get to build the quote response.
// LoopInQuote takes an amount and returns a breakdown of estimated costs for
// the client. Both the swap server and the on-chain fee estimator are queried
// to get to build the quote response.
func (s *Client) LoopInQuote(ctx context.Context,
request *LoopInQuoteRequest) (*LoopInQuote, error) {

Expand Down Expand Up @@ -742,7 +762,7 @@ func (s *Client) LoopInQuote(ctx context.Context,

quote, err := s.Server.GetLoopInQuote(
ctx, request.Amount, s.lndServices.NodePubkey, request.LastHop,
request.RouteHints, request.Initiator,
request.RouteHints, request.Initiator, request.NumDeposits,
)
if err != nil {
return nil, err
Expand All @@ -752,7 +772,9 @@ func (s *Client) LoopInQuote(ctx context.Context,

// We don't calculate the on-chain fee if the HTLC is going to be
// published externally.
if request.ExternalHtlc {
// We also don't calculate the on-chain fee if the loop in is funded by
// static address deposits because we don't publish the HTLC on-chain.
if request.ExternalHtlc || request.NumDeposits > 0 {
return &LoopInQuote{
SwapFee: swapFee,
MinerFee: 0,
Expand Down
24 changes: 16 additions & 8 deletions cmd/loop/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,15 @@ var (
Name: "verbose, v",
Usage: "show expanded details",
}

commands = []cli.Command{
loopOutCommand, loopInCommand, termsCommand,
monitorCommand, quoteCommand, listAuthCommand, fetchL402Command,
listSwapsCommand, swapInfoCommand, getLiquidityParamsCommand,
setLiquidityRuleCommand, suggestSwapCommand, setParamsCommand,
getInfoCommand, abandonSwapCommand, reservationsCommands,
instantOutCommand, listInstantOutsCommand,
}
)

const (
Expand Down Expand Up @@ -142,14 +151,7 @@ func main() {
tlsCertFlag,
macaroonPathFlag,
}
app.Commands = []cli.Command{
loopOutCommand, loopInCommand, termsCommand,
monitorCommand, quoteCommand, listAuthCommand, fetchL402Command,
listSwapsCommand, swapInfoCommand, getLiquidityParamsCommand,
setLiquidityRuleCommand, suggestSwapCommand, setParamsCommand,
getInfoCommand, abandonSwapCommand, reservationsCommands,
instantOutCommand, listInstantOutsCommand,
}
app.Commands = commands

err := app.Run(os.Args)
if err != nil {
Expand Down Expand Up @@ -279,6 +281,12 @@ func displayInDetails(req *looprpc.QuoteRequest,
"wallet.\n\n")
}

if req.DepositOutpoints != nil {
fmt.Printf("On-chain fees for static address loop-ins are not " +
"included.\nThey were already paid when the deposits " +
"were created.\n\n")
}

printQuoteInResp(req, resp, verbose)

fmt.Printf("\nCONTINUE SWAP? (y/n): ")
Expand Down
82 changes: 70 additions & 12 deletions cmd/loop/quote.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"os"
"time"

"github.com/btcsuite/btcd/btcutil"
"github.com/lightninglabs/loop"
"github.com/lightninglabs/loop/looprpc"
"github.com/lightningnetwork/lnd/routing/route"
Expand All @@ -19,10 +20,11 @@ var quoteCommand = cli.Command{
}

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",
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." +
"Either specify an amount or deposit outpoints.",
Flags: []cli.Flag{
cli.StringFlag{
Name: lastHopFlag.Name,
Expand All @@ -33,20 +35,38 @@ var quoteInCommand = cli.Command{
verboseFlag,
privateFlag,
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. Each" +
"additional outpoint can be added by " +
"specifying --deposit_outpoint tx_id:idx",
},
},
Action: quoteIn,
}

func quoteIn(ctx *cli.Context) error {
// 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")
}

args := ctx.Args()
amt, err := parseAmt(args[0])
if err != nil {
return err
var (
manualAmt btcutil.Amount
depositAmt btcutil.Amount
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)
Expand All @@ -62,11 +82,20 @@ func quoteIn(ctx *cli.Context) error {
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{
Amt: int64(amt),
Amt: int64(manualAmt),
ConfTarget: int32(ctx.Uint64("conf_target")),
LoopInRouteHints: hints,
Private: ctx.Bool(privateFlag.Name),
DepositOutpoints: depositOutpoints,
}

if ctx.IsSet(lastHopFlag.Name) {
Expand All @@ -80,7 +109,6 @@ func quoteIn(ctx *cli.Context) error {
quoteReq.LoopInLastHop = lastHopVertex[:]
}

ctxb := context.Background()
quoteResp, err := client.GetLoopInQuote(ctxb, quoteReq)
if err != nil {
return err
Expand All @@ -98,10 +126,36 @@ func quoteIn(ctx *cli.Context) error {
"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"))
return nil
}

func depositAmount(ctx context.Context, client looprpc.SwapClientClient,
depositOutpoints []string) (btcutil.Amount, error) {

addressSummary, err := client.ListStaticAddressDeposits(
ctx, &looprpc.ListStaticAddressDepositsRequest{
Outpoints: depositOutpoints,
},
)
if err != nil {
return 0, err
}

var depositAmt btcutil.Amount
for _, deposit := range addressSummary.FilteredDeposits {
depositAmt += btcutil.Amount(deposit.Value)
}

return depositAmt, nil
}

var quoteOutCommand = cli.Command{
Name: "out",
Usage: "get a quote for the cost of a loop out swap",
Expand Down Expand Up @@ -174,7 +228,11 @@ func printQuoteInResp(req *looprpc.QuoteRequest,

totalFee := resp.HtlcPublishFeeSat + resp.SwapFeeSat

fmt.Printf(satAmtFmt, "Send on-chain:", req.Amt)
if req.DepositOutpoints != nil {
fmt.Printf(satAmtFmt, "Previously deposited on-chain:", req.Amt)
} else {
fmt.Printf(satAmtFmt, "Send on-chain:", req.Amt)
}
fmt.Printf(satAmtFmt, "Receive off-chain:", req.Amt-totalFee)

switch {
Expand Down
Loading

0 comments on commit a49c60a

Please sign in to comment.