Skip to content

Commit fb9562b

Browse files
committed
staticaddr: dest addr and fee rate for withdrawals
1 parent 0d30733 commit fb9562b

File tree

3 files changed

+73
-28
lines changed

3 files changed

+73
-28
lines changed

cmd/loop/staticaddr.go

+21-5
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,3 @@
1-
//go:build staticaddr
2-
// +build staticaddr
3-
41
package main
52

63
import (
@@ -145,6 +142,18 @@ var withdrawalCommand = cli.Command{
145142
Name: "all",
146143
Usage: "withdraws all static address deposits.",
147144
},
145+
cli.StringFlag{
146+
Name: "addr",
147+
Usage: "the optional address that the withdrawn " +
148+
"funds should be sent to, if let blank the " +
149+
"funds will go to lnd's wallet",
150+
},
151+
cli.Int64Flag{
152+
Name: "sat_per_vbyte",
153+
Usage: "(optional) a manual fee expressed in " +
154+
"sat/vbyte that should be used when crafting " +
155+
"the transaction",
156+
},
148157
},
149158
Action: withdraw,
150159
}
@@ -165,6 +174,7 @@ func withdraw(ctx *cli.Context) error {
165174
isUtxoSelected = ctx.IsSet("utxo")
166175
outpoints []*looprpc.OutPoint
167176
ctxb = context.Background()
177+
destAddr string
168178
)
169179

170180
switch {
@@ -183,10 +193,16 @@ func withdraw(ctx *cli.Context) error {
183193
return fmt.Errorf("unknown withdrawal request")
184194
}
185195

196+
if ctx.IsSet("addr") {
197+
destAddr = ctx.String("addr")
198+
}
199+
186200
resp, err := client.WithdrawDeposits(ctxb,
187201
&looprpc.WithdrawDepositsRequest{
188-
Outpoints: outpoints,
189-
All: isAllSelected,
202+
Outpoints: outpoints,
203+
All: isAllSelected,
204+
DestAddr: destAddr,
205+
SatPerVbyte: int64(ctx.Uint64("sat_per_vbyte")),
190206
})
191207
if err != nil {
192208
return err

loopd/swapclient_server.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -1447,7 +1447,7 @@ func (s *swapClientServer) WithdrawDeposits(ctx context.Context,
14471447
}
14481448

14491449
txhash, pkScript, err := s.withdrawalManager.DeliverWithdrawalRequest(
1450-
ctx, outpoints,
1450+
ctx, outpoints, req.DestAddr, req.SatPerVbyte,
14511451
)
14521452
if err != nil {
14531453
return nil, err

staticaddr/withdraw/manager.go

+51-22
Original file line numberDiff line numberDiff line change
@@ -71,8 +71,10 @@ type ManagerConfig struct {
7171
// newWithdrawalRequest is used to send withdrawal request to the manager main
7272
// loop.
7373
type newWithdrawalRequest struct {
74-
outpoints []wire.OutPoint
75-
respChan chan *newWithdrawalResponse
74+
outpoints []wire.OutPoint
75+
respChan chan *newWithdrawalResponse
76+
destAddr string
77+
satPerVbyte int64
7678
}
7779

7880
// newWithdrawalResponse is used to return withdrawal info and error to the
@@ -156,7 +158,8 @@ func (m *Manager) Run(ctx context.Context, currentHeight uint32) error {
156158

157159
case request := <-m.newWithdrawalRequestChan:
158160
txHash, pkScript, err = m.WithdrawDeposits(
159-
ctx, request.outpoints,
161+
ctx, request.outpoints, request.destAddr,
162+
request.satPerVbyte,
160163
)
161164
if err != nil {
162165
log.Errorf("Error withdrawing deposits: %v",
@@ -258,7 +261,8 @@ func (m *Manager) WaitInitComplete() {
258261

259262
// WithdrawDeposits starts a deposits withdrawal flow.
260263
func (m *Manager) WithdrawDeposits(ctx context.Context,
261-
outpoints []wire.OutPoint) (string, string, error) {
264+
outpoints []wire.OutPoint, destAddr string, satPerVbyte int64) (string,
265+
string, error) {
262266

263267
if len(outpoints) == 0 {
264268
return "", "", fmt.Errorf("no outpoints selected to " +
@@ -274,17 +278,32 @@ func (m *Manager) WithdrawDeposits(ctx context.Context,
274278
return "", "", ErrWithdrawingInactiveDeposits
275279
}
276280

277-
// Generate the withdrawal address from our local lnd wallet.
278-
withdrawalAddress, err := m.cfg.WalletKit.NextAddr(
279-
ctx, lnwallet.DefaultAccountName,
280-
walletrpc.AddressType_TAPROOT_PUBKEY, false,
281+
var (
282+
withdrawalAddress btcutil.Address
283+
err error
281284
)
282-
if err != nil {
283-
return "", "", err
285+
286+
// Check if the user provided an address to withdraw to. If not, we'll
287+
// generate a new address for them.
288+
if destAddr != "" {
289+
withdrawalAddress, err = btcutil.DecodeAddress(
290+
destAddr, m.cfg.ChainParams,
291+
)
292+
if err != nil {
293+
return "", "", err
294+
}
295+
} else {
296+
withdrawalAddress, err = m.cfg.WalletKit.NextAddr(
297+
ctx, lnwallet.DefaultAccountName,
298+
walletrpc.AddressType_TAPROOT_PUBKEY, false,
299+
)
300+
if err != nil {
301+
return "", "", err
302+
}
284303
}
285304

286305
finalizedTx, err := m.createFinalizedWithdrawalTx(
287-
ctx, deposits, withdrawalAddress,
306+
ctx, deposits, withdrawalAddress, satPerVbyte,
288307
)
289308
if err != nil {
290309
return "", "", err
@@ -335,8 +354,8 @@ func (m *Manager) WithdrawDeposits(ctx context.Context,
335354
}
336355

337356
func (m *Manager) createFinalizedWithdrawalTx(ctx context.Context,
338-
deposits []*deposit.Deposit, withdrawalAddress btcutil.Address) (
339-
*wire.MsgTx, error) {
357+
deposits []*deposit.Deposit, withdrawalAddress btcutil.Address,
358+
satPerVbyte int64) (*wire.MsgTx, error) {
340359

341360
// Create a musig2 session for each deposit.
342361
withdrawalSessions, clientNonces, err := m.createMusig2Sessions(
@@ -346,12 +365,19 @@ func (m *Manager) createFinalizedWithdrawalTx(ctx context.Context,
346365
return nil, err
347366
}
348367

349-
// Get the fee rate for the withdrawal sweep.
350-
withdrawalSweepFeeRate, err := m.cfg.WalletKit.EstimateFeeRate(
351-
ctx, defaultConfTarget,
352-
)
353-
if err != nil {
354-
return nil, err
368+
var withdrawalSweepFeeRate chainfee.SatPerKWeight
369+
if satPerVbyte == 0 {
370+
// Get the fee rate for the withdrawal sweep.
371+
withdrawalSweepFeeRate, err = m.cfg.WalletKit.EstimateFeeRate(
372+
ctx, defaultConfTarget,
373+
)
374+
if err != nil {
375+
return nil, err
376+
}
377+
} else {
378+
withdrawalSweepFeeRate = chainfee.SatPerKVByte(
379+
satPerVbyte * 1000,
380+
).FeePerKWeight()
355381
}
356382

357383
outpoints := toOutpoints(deposits)
@@ -788,11 +814,14 @@ func (m *Manager) republishWithdrawals(ctx context.Context) error {
788814
// DeliverWithdrawalRequest forwards a withdrawal request to the manager main
789815
// loop.
790816
func (m *Manager) DeliverWithdrawalRequest(ctx context.Context,
791-
outpoints []wire.OutPoint) (string, string, error) {
817+
outpoints []wire.OutPoint, destAddr string, satPerVbyte int64) (string,
818+
string, error) {
792819

793820
request := newWithdrawalRequest{
794-
outpoints: outpoints,
795-
respChan: make(chan *newWithdrawalResponse),
821+
outpoints: outpoints,
822+
destAddr: destAddr,
823+
satPerVbyte: satPerVbyte,
824+
respChan: make(chan *newWithdrawalResponse),
796825
}
797826

798827
// Send the new loop-in request to the manager run loop.

0 commit comments

Comments
 (0)