@@ -355,83 +355,75 @@ where
355355 let mut locked_wallet = self . inner . lock ( ) . unwrap ( ) ;
356356
357357 // Prepare the tx_builder. We properly check the reserve requirements (again) further down.
358+ const DUST_LIMIT_SATS : u64 = 546 ;
358359 let tx_builder = match send_amount {
359360 OnchainSendAmount :: ExactRetainingReserve { amount_sats, .. } => {
360361 let mut tx_builder = locked_wallet. build_tx ( ) ;
361362 let amount = Amount :: from_sat ( amount_sats) ;
362363 tx_builder. add_recipient ( address. script_pubkey ( ) , amount) . fee_rate ( fee_rate) ;
363364 tx_builder
364365 } ,
365- OnchainSendAmount :: AllRetainingReserve { cur_anchor_reserve_sats } => {
366- const DUST_LIMIT_SATS : u64 = 546 ;
367- if cur_anchor_reserve_sats > DUST_LIMIT_SATS {
368- let change_address_info =
369- locked_wallet. peek_address ( KeychainKind :: Internal , 0 ) ;
370- let balance = locked_wallet. balance ( ) ;
371- let spendable_amount_sats = self
372- . get_balances_inner ( balance, cur_anchor_reserve_sats)
373- . map ( |( _, s) | s)
374- . unwrap_or ( 0 ) ;
375- let tmp_tx = {
376- let mut tmp_tx_builder = locked_wallet. build_tx ( ) ;
377- tmp_tx_builder
378- . drain_wallet ( )
379- . drain_to ( address. script_pubkey ( ) )
380- . add_recipient (
381- change_address_info. address . script_pubkey ( ) ,
382- Amount :: from_sat ( cur_anchor_reserve_sats) ,
383- )
384- . fee_rate ( fee_rate) ;
385- match tmp_tx_builder. finish ( ) {
386- Ok ( psbt) => psbt. unsigned_tx ,
387- Err ( err) => {
388- log_error ! (
389- self . logger,
390- "Failed to create temporary transaction: {}" ,
391- err
392- ) ;
393- return Err ( err. into ( ) ) ;
394- } ,
395- }
396- } ;
397-
398- let estimated_tx_fee =
399- locked_wallet. calculate_fee ( & tmp_tx) . map_err ( |e| {
366+ OnchainSendAmount :: AllRetainingReserve { cur_anchor_reserve_sats }
367+ if cur_anchor_reserve_sats > DUST_LIMIT_SATS =>
368+ {
369+ let change_address_info = locked_wallet. peek_address ( KeychainKind :: Internal , 0 ) ;
370+ let balance = locked_wallet. balance ( ) ;
371+ let spendable_amount_sats = self
372+ . get_balances_inner ( balance, cur_anchor_reserve_sats)
373+ . map ( |( _, s) | s)
374+ . unwrap_or ( 0 ) ;
375+ let tmp_tx = {
376+ let mut tmp_tx_builder = locked_wallet. build_tx ( ) ;
377+ tmp_tx_builder
378+ . drain_wallet ( )
379+ . drain_to ( address. script_pubkey ( ) )
380+ . add_recipient (
381+ change_address_info. address . script_pubkey ( ) ,
382+ Amount :: from_sat ( cur_anchor_reserve_sats) ,
383+ )
384+ . fee_rate ( fee_rate) ;
385+ match tmp_tx_builder. finish ( ) {
386+ Ok ( psbt) => psbt. unsigned_tx ,
387+ Err ( err) => {
400388 log_error ! (
401389 self . logger,
402- "Failed to calculate fee of temporary transaction: {}" ,
403- e
390+ "Failed to create temporary transaction: {}" ,
391+ err
404392 ) ;
405- e
406- } ) ?;
407- let estimated_spendable_amount = Amount :: from_sat (
408- spendable_amount_sats. saturating_sub ( estimated_tx_fee. to_sat ( ) ) ,
409- ) ;
410-
411- if estimated_spendable_amount == Amount :: ZERO {
412- log_error ! ( self . logger,
413- "Unable to send payment without infringing on Anchor reserves. Available: {}sats, estimated fee required: {}sats." ,
414- spendable_amount_sats,
415- estimated_tx_fee,
416- ) ;
417- return Err ( Error :: InsufficientFunds ) ;
393+ return Err ( err. into ( ) ) ;
394+ } ,
418395 }
396+ } ;
419397
420- let mut tx_builder = locked_wallet. build_tx ( ) ;
421- tx_builder
422- . add_recipient ( address. script_pubkey ( ) , estimated_spendable_amount)
423- . fee_absolute ( estimated_tx_fee) ;
424- tx_builder
425- } else {
426- let mut tx_builder = locked_wallet. build_tx ( ) ;
427- tx_builder
428- . drain_wallet ( )
429- . drain_to ( address. script_pubkey ( ) )
430- . fee_rate ( fee_rate) ;
431- tx_builder
398+ let estimated_tx_fee = locked_wallet. calculate_fee ( & tmp_tx) . map_err ( |e| {
399+ log_error ! (
400+ self . logger,
401+ "Failed to calculate fee of temporary transaction: {}" ,
402+ e
403+ ) ;
404+ e
405+ } ) ?;
406+ let estimated_spendable_amount = Amount :: from_sat (
407+ spendable_amount_sats. saturating_sub ( estimated_tx_fee. to_sat ( ) ) ,
408+ ) ;
409+
410+ if estimated_spendable_amount == Amount :: ZERO {
411+ log_error ! ( self . logger,
412+ "Unable to send payment without infringing on Anchor reserves. Available: {}sats, estimated fee required: {}sats." ,
413+ spendable_amount_sats,
414+ estimated_tx_fee,
415+ ) ;
416+ return Err ( Error :: InsufficientFunds ) ;
432417 }
418+
419+ let mut tx_builder = locked_wallet. build_tx ( ) ;
420+ tx_builder
421+ . add_recipient ( address. script_pubkey ( ) , estimated_spendable_amount)
422+ . fee_absolute ( estimated_tx_fee) ;
423+ tx_builder
433424 } ,
434- OnchainSendAmount :: AllDrainingReserve => {
425+ OnchainSendAmount :: AllDrainingReserve
426+ | OnchainSendAmount :: AllRetainingReserve { cur_anchor_reserve_sats : _ } => {
435427 let mut tx_builder = locked_wallet. build_tx ( ) ;
436428 tx_builder. drain_wallet ( ) . drain_to ( address. script_pubkey ( ) ) . fee_rate ( fee_rate) ;
437429 tx_builder
0 commit comments