Skip to content

Commit d9597e8

Browse files
authored
Merge pull request #129 from halseth/loop-out-quote-deadline
Loop out quote with fast option
2 parents 68ebd14 + d5c9a66 commit d9597e8

File tree

12 files changed

+234
-151
lines changed

12 files changed

+234
-151
lines changed

client.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -369,7 +369,9 @@ func (s *Client) LoopOutQuote(ctx context.Context,
369369
return nil, ErrSwapAmountTooHigh
370370
}
371371

372-
quote, err := s.Server.GetLoopOutQuote(ctx, request.Amount)
372+
quote, err := s.Server.GetLoopOutQuote(
373+
ctx, request.Amount, request.SwapPublicationDeadline,
374+
)
373375
if err != nil {
374376
return nil, err
375377
}

cmd/loop/loopout.go

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -93,18 +93,26 @@ func loopOut(ctx *cli.Context) error {
9393
}
9494
defer cleanup()
9595

96+
// Set our maximum swap wait time. If a fast swap is requested we set
97+
// it to now, otherwise to 30 minutes in the future.
98+
fast := ctx.Bool("fast")
99+
swapDeadline := time.Now()
100+
if !fast {
101+
swapDeadline = time.Now().Add(defaultSwapWaitTime)
102+
}
103+
96104
sweepConfTarget := int32(ctx.Uint64("conf_target"))
97105
quoteReq := &looprpc.QuoteRequest{
98-
Amt: int64(amt),
99-
ConfTarget: sweepConfTarget,
106+
Amt: int64(amt),
107+
ConfTarget: sweepConfTarget,
108+
SwapPublicationDeadline: uint64(swapDeadline.Unix()),
100109
}
101110
quote, err := client.LoopOutQuote(context.Background(), quoteReq)
102111
if err != nil {
103112
return err
104113
}
105114

106115
// Show a warning if a slow swap was requested.
107-
fast := ctx.Bool("fast")
108116
warning := ""
109117
if fast {
110118
warning = "Fast swap requested."
@@ -125,13 +133,6 @@ func loopOut(ctx *cli.Context) error {
125133
unchargeChannel = ctx.Uint64("channel")
126134
}
127135

128-
// Set our maximum swap wait time. If a fast swap is requested we set
129-
// it to now, otherwise to 30 minutes in the future.
130-
swapDeadline := time.Now()
131-
if !fast {
132-
swapDeadline = time.Now().Add(defaultSwapWaitTime)
133-
}
134-
135136
resp, err := client.LoopOut(context.Background(), &looprpc.LoopOutRequest{
136137
Amt: int64(amt),
137138
Dest: destAddr,

cmd/loop/quote.go

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package main
22

33
import (
44
"context"
5+
"time"
56

67
"github.com/lightninglabs/loop/looprpc"
78
"github.com/urfave/cli"
@@ -20,6 +21,16 @@ var quoteCommand = cli.Command{
2021
"should be swept within in a Loop Out",
2122
Value: 6,
2223
},
24+
cli.BoolFlag{
25+
Name: "fast",
26+
Usage: "Indicate you want to swap immediately, " +
27+
"paying potentially a higher fee. If not " +
28+
"set the swap server might choose to wait up " +
29+
"to 30 minutes before publishing the swap " +
30+
"HTLC on-chain, to save on chain fees. Not " +
31+
"setting this flag might result in a lower " +
32+
"swap fee.",
33+
},
2334
},
2435
Action: quote,
2536
}
@@ -43,10 +54,17 @@ func quote(ctx *cli.Context) error {
4354
}
4455
defer cleanup()
4556

57+
fast := ctx.Bool("fast")
58+
swapDeadline := time.Now()
59+
if !fast {
60+
swapDeadline = time.Now().Add(defaultSwapWaitTime)
61+
}
62+
4663
ctxb := context.Background()
4764
resp, err := client.LoopOutQuote(ctxb, &looprpc.QuoteRequest{
48-
Amt: int64(amt),
49-
ConfTarget: int32(ctx.Uint64("conf_target")),
65+
Amt: int64(amt),
66+
ConfTarget: int32(ctx.Uint64("conf_target")),
67+
SwapPublicationDeadline: uint64(swapDeadline.Unix()),
5068
})
5169
if err != nil {
5270
return err

interface.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,10 @@ type LoopOutQuoteRequest struct {
9999
// client sweep tx.
100100
SweepConfTarget int32
101101

102+
// SwapPublicationDeadline can be set by the client to allow the server
103+
// delaying publication of the swap HTLC to save on chain fees.
104+
SwapPublicationDeadline time.Time
105+
102106
// TODO: Add argument to specify confirmation target for server
103107
// publishing htlc. This may influence the swap fee quote, because the
104108
// server needs to pay more for faster confirmations.

loopd/swapclient_server.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -266,6 +266,9 @@ func (s *swapClientServer) LoopOutQuote(ctx context.Context,
266266
quote, err := s.impl.LoopOutQuote(ctx, &loop.LoopOutQuoteRequest{
267267
Amount: btcutil.Amount(req.Amt),
268268
SweepConfTarget: confTarget,
269+
SwapPublicationDeadline: time.Unix(
270+
int64(req.SwapPublicationDeadline), 0,
271+
),
269272
})
270273
if err != nil {
271274
return nil, err

looprpc/client.pb.go

Lines changed: 102 additions & 87 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

looprpc/client.proto

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -356,6 +356,15 @@ message QuoteRequest {
356356
actor.
357357
*/
358358
bool external_htlc = 3;
359+
360+
/**
361+
The latest time (in unix seconds) we allow the server to wait before
362+
publishing the HTLC on chain. Setting this to a larger value will give the
363+
server the opportunity to batch multiple swaps together, and wait for
364+
low-fee periods before publishing the HTLC, potentially resulting in a
365+
lower total swap fee.
366+
*/
367+
uint64 swap_publication_deadline = 4;
359368
}
360369

361370
message QuoteResponse {

looprpc/client.swagger.json

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,14 @@
7878
"required": false,
7979
"type": "boolean",
8080
"format": "boolean"
81+
},
82+
{
83+
"name": "swap_publication_deadline",
84+
"description": "*\nThe latest time (in unix seconds) we allow the server to wait before\npublishing the HTLC on chain. Setting this to a larger value will give the\nserver the opportunity to batch multiple swaps together, and wait for\nlow-fee periods before publishing the HTLC, potentially resulting in a\nlower total swap fee.",
85+
"in": "query",
86+
"required": false,
87+
"type": "string",
88+
"format": "uint64"
8189
}
8290
],
8391
"tags": [
@@ -165,6 +173,14 @@
165173
"required": false,
166174
"type": "boolean",
167175
"format": "boolean"
176+
},
177+
{
178+
"name": "swap_publication_deadline",
179+
"description": "*\nThe latest time (in unix seconds) we allow the server to wait before\npublishing the HTLC on chain. Setting this to a larger value will give the\nserver the opportunity to batch multiple swaps together, and wait for\nlow-fee periods before publishing the HTLC, potentially resulting in a\nlower total swap fee.",
180+
"in": "query",
181+
"required": false,
182+
"type": "string",
183+
"format": "uint64"
168184
}
169185
],
170186
"tags": [

looprpc/server.pb.go

Lines changed: 55 additions & 46 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

looprpc/server.proto

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,9 @@ message ServerLoopOutResponse {
4242
message ServerLoopOutQuoteRequest {
4343
/// The swap amount. If zero, a quote for a maximum amt swap will be given.
4444
uint64 amt = 1;
45+
46+
/// The unix time in seconds we want the on-chain swap to be published by.
47+
int64 swap_publication_deadline = 2;
4548
}
4649

4750
message ServerLoopOutQuote {

0 commit comments

Comments
 (0)