@@ -75,6 +75,7 @@ type newWithdrawalRequest struct {
7575 respChan chan * newWithdrawalResponse
7676 destAddr string
7777 satPerVbyte int64
78+ amount int64
7879}
7980
8081// newWithdrawalResponse is used to return withdrawal info and error to the
@@ -156,10 +157,10 @@ func (m *Manager) Run(ctx context.Context, currentHeight uint32) error {
156157 err )
157158 }
158159
159- case request := <- m .newWithdrawalRequestChan :
160+ case req := <- m .newWithdrawalRequestChan :
160161 txHash , pkScript , err = m .WithdrawDeposits (
161- ctx , request .outpoints , request .destAddr ,
162- request .satPerVbyte ,
162+ ctx , req .outpoints , req .destAddr ,
163+ req .satPerVbyte , req . amount ,
163164 )
164165 if err != nil {
165166 log .Errorf ("Error withdrawing deposits: %v" ,
@@ -174,7 +175,7 @@ func (m *Manager) Run(ctx context.Context, currentHeight uint32) error {
174175 err : err ,
175176 }
176177 select {
177- case request .respChan <- resp :
178+ case req .respChan <- resp :
178179
179180 case <- ctx .Done ():
180181 // Notify subroutines that the main loop has
@@ -261,8 +262,8 @@ func (m *Manager) WaitInitComplete() {
261262
262263// WithdrawDeposits starts a deposits withdrawal flow.
263264func (m * Manager ) WithdrawDeposits (ctx context.Context ,
264- outpoints []wire.OutPoint , destAddr string , satPerVbyte int64 ) ( string ,
265- string , error ) {
265+ outpoints []wire.OutPoint , destAddr string , satPerVbyte int64 ,
266+ amount int64 ) ( string , string , error ) {
266267
267268 if len (outpoints ) == 0 {
268269 return "" , "" , fmt .Errorf ("no outpoints selected to " +
@@ -272,7 +273,8 @@ func (m *Manager) WithdrawDeposits(ctx context.Context,
272273 // Ensure that the deposits are in a state in which they can be
273274 // withdrawn.
274275 deposits , allActive := m .cfg .DepositManager .AllOutpointsActiveDeposits (
275- outpoints , deposit .Deposited )
276+ outpoints , deposit .Deposited ,
277+ )
276278
277279 if ! allActive {
278280 return "" , "" , ErrWithdrawingInactiveDeposits
@@ -302,8 +304,27 @@ func (m *Manager) WithdrawDeposits(ctx context.Context,
302304 }
303305 }
304306
307+ if amount > 0 {
308+ // Check that the amount is within the range of the deposits.
309+ totalValue := withdrawalValue (m .toPrevOuts (deposits , nil ))
310+ if amount > int64 (totalValue ) {
311+ return "" , "" , fmt .Errorf ("amount to withdraw is " +
312+ "greater than the total value of the " +
313+ "deposits selected" )
314+ }
315+
316+ // We'll hint the user if the remaining amount is below the dust
317+ // limit and hence will go towards miner fees.
318+ dustLimit := lnwallet .DustLimitForSize (input .P2TRSize )
319+ if totalValue - btcutil .Amount (amount ) < dustLimit {
320+ log .Warnf ("The remaining amount after withdrawal is " +
321+ "below the dust limit of %v and will go " +
322+ "towards miner fees" , dustLimit )
323+ }
324+ }
325+
305326 finalizedTx , err := m .createFinalizedWithdrawalTx (
306- ctx , deposits , withdrawalAddress , satPerVbyte ,
327+ ctx , deposits , withdrawalAddress , satPerVbyte , amount ,
307328 )
308329 if err != nil {
309330 return "" , "" , err
@@ -355,7 +376,7 @@ func (m *Manager) WithdrawDeposits(ctx context.Context,
355376
356377func (m * Manager ) createFinalizedWithdrawalTx (ctx context.Context ,
357378 deposits []* deposit.Deposit , withdrawalAddress btcutil.Address ,
358- satPerVbyte int64 ) (* wire.MsgTx , error ) {
379+ satPerVbyte int64 , amount int64 ) (* wire.MsgTx , error ) {
359380
360381 // Create a musig2 session for each deposit.
361382 withdrawalSessions , clientNonces , err := m .createMusig2Sessions (
@@ -387,6 +408,7 @@ func (m *Manager) createFinalizedWithdrawalTx(ctx context.Context,
387408 ClientNonces : clientNonces ,
388409 ClientSweepAddr : withdrawalAddress .String (),
389410 TxFeeRate : uint64 (withdrawalSweepFeeRate ),
411+ Amount : amount ,
390412 },
391413 )
392414 if err != nil {
@@ -814,13 +836,14 @@ func (m *Manager) republishWithdrawals(ctx context.Context) error {
814836// DeliverWithdrawalRequest forwards a withdrawal request to the manager main
815837// loop.
816838func (m * Manager ) DeliverWithdrawalRequest (ctx context.Context ,
817- outpoints []wire.OutPoint , destAddr string , satPerVbyte int64 ) ( string ,
818- string , error ) {
839+ outpoints []wire.OutPoint , destAddr string , satPerVbyte int64 ,
840+ amount int64 ) ( string , string , error ) {
819841
820842 request := newWithdrawalRequest {
821843 outpoints : outpoints ,
822844 destAddr : destAddr ,
823845 satPerVbyte : satPerVbyte ,
846+ amount : amount ,
824847 respChan : make (chan * newWithdrawalResponse ),
825848 }
826849
0 commit comments