@@ -17,6 +17,11 @@ use bitcoin::{Address, Txid};
17
17
18
18
use std:: sync:: { Arc , RwLock } ;
19
19
20
+ #[ cfg( not( feature = "uniffi" ) ) ]
21
+ type FeeRate = bitcoin:: FeeRate ;
22
+ #[ cfg( feature = "uniffi" ) ]
23
+ type FeeRate = crate :: uniffi_types:: FeeRate ;
24
+
20
25
/// A payment handler allowing to send and receive on-chain payments.
21
26
///
22
27
/// Should be retrieved by calling [`Node::onchain_payment`].
@@ -50,9 +55,13 @@ impl OnchainPayment {
50
55
/// This will respect any on-chain reserve we need to keep, i.e., won't allow to cut into
51
56
/// [`BalanceDetails::total_anchor_channels_reserve_sats`].
52
57
///
58
+ /// If `fee_rate` is set it will used on the resulting transaction. Otherwise a reasonable
59
+ /// we'll retrieve an estimate from the configured chain source.
60
+ ///
53
61
/// [`BalanceDetails::total_anchor_channels_reserve_sats`]: crate::BalanceDetails::total_anchor_channels_reserve_sats
62
+ #[ cfg( not( feature = "uniffi" ) ) ]
54
63
pub fn send_to_address (
55
- & self , address : & bitcoin:: Address , amount_sats : u64 ,
64
+ & self , address : & bitcoin:: Address , amount_sats : u64 , fee_rate : Option < FeeRate > ,
56
65
) -> Result < Txid , Error > {
57
66
let rt_lock = self . runtime . read ( ) . unwrap ( ) ;
58
67
if rt_lock. is_none ( ) {
@@ -63,7 +72,32 @@ impl OnchainPayment {
63
72
crate :: total_anchor_channels_reserve_sats ( & self . channel_manager , & self . config ) ;
64
73
let send_amount =
65
74
OnchainSendAmount :: ExactRetainingReserve { amount_sats, cur_anchor_reserve_sats } ;
66
- self . wallet . send_to_address ( address, send_amount)
75
+ self . wallet . send_to_address ( address, send_amount, fee_rate)
76
+ }
77
+
78
+ /// Send an on-chain payment to the given address.
79
+ ///
80
+ /// This will respect any on-chain reserve we need to keep, i.e., won't allow to cut into
81
+ /// [`BalanceDetails::total_anchor_channels_reserve_sats`].
82
+ ///
83
+ /// If `fee_rate` is set it will used on the resulting transaction. Otherwise a reasonable
84
+ /// we'll retrieve an estimate from the configured chain source.
85
+ ///
86
+ /// [`BalanceDetails::total_anchor_channels_reserve_sats`]: crate::BalanceDetails::total_anchor_channels_reserve_sats
87
+ #[ cfg( feature = "uniffi" ) ]
88
+ pub fn send_to_address (
89
+ & self , address : & bitcoin:: Address , amount_sats : u64 , fee_rate : Option < Arc < FeeRate > > ,
90
+ ) -> Result < Txid , Error > {
91
+ let rt_lock = self . runtime . read ( ) . unwrap ( ) ;
92
+ if rt_lock. is_none ( ) {
93
+ return Err ( Error :: NotRunning ) ;
94
+ }
95
+
96
+ let cur_anchor_reserve_sats =
97
+ crate :: total_anchor_channels_reserve_sats ( & self . channel_manager , & self . config ) ;
98
+ let send_amount =
99
+ OnchainSendAmount :: ExactRetainingReserve { amount_sats, cur_anchor_reserve_sats } ;
100
+ self . wallet . send_to_address ( address, send_amount, fee_rate. map ( |f| f. 0 ) )
67
101
}
68
102
69
103
/// Send an on-chain payment to the given address, draining the available funds.
@@ -77,9 +111,48 @@ impl OnchainPayment {
77
111
/// will try to send all spendable onchain funds, i.e.,
78
112
/// [`BalanceDetails::spendable_onchain_balance_sats`].
79
113
///
114
+ /// If `fee_rate` is set it will be used on the resulting transaction. Otherwise a reasonable
115
+ /// we'll retrieve an estimate from the configured chain source.
116
+ ///
117
+ /// [`BalanceDetails::spendable_onchain_balance_sats`]: crate::balance::BalanceDetails::spendable_onchain_balance_sats
118
+ #[ cfg( not( feature = "uniffi" ) ) ]
119
+ pub fn send_all_to_address (
120
+ & self , address : & bitcoin:: Address , retain_reserves : bool , fee_rate : Option < FeeRate > ,
121
+ ) -> Result < Txid , Error > {
122
+ let rt_lock = self . runtime . read ( ) . unwrap ( ) ;
123
+ if rt_lock. is_none ( ) {
124
+ return Err ( Error :: NotRunning ) ;
125
+ }
126
+
127
+ let send_amount = if retain_reserves {
128
+ let cur_anchor_reserve_sats =
129
+ crate :: total_anchor_channels_reserve_sats ( & self . channel_manager , & self . config ) ;
130
+ OnchainSendAmount :: AllRetainingReserve { cur_anchor_reserve_sats }
131
+ } else {
132
+ OnchainSendAmount :: AllDrainingReserve
133
+ } ;
134
+
135
+ self . wallet . send_to_address ( address, send_amount, fee_rate)
136
+ }
137
+
138
+ /// Send an on-chain payment to the given address, draining the available funds.
139
+ ///
140
+ /// This is useful if you have closed all channels and want to migrate funds to another
141
+ /// on-chain wallet.
142
+ ///
143
+ /// Please note that if `retain_reserves` is set to `false` this will **not** retain any on-chain reserves, which might be potentially
144
+ /// dangerous if you have open Anchor channels for which you can't trust the counterparty to
145
+ /// spend the Anchor output after channel closure. If `retain_reserves` is set to `true`, this
146
+ /// will try to send all spendable onchain funds, i.e.,
147
+ /// [`BalanceDetails::spendable_onchain_balance_sats`].
148
+ ///
149
+ /// If `fee_rate` is set it will be used on the resulting transaction. Otherwise a reasonable
150
+ /// we'll retrieve an estimate from the configured chain source.
151
+ ///
80
152
/// [`BalanceDetails::spendable_onchain_balance_sats`]: crate::balance::BalanceDetails::spendable_onchain_balance_sats
153
+ #[ cfg( feature = "uniffi" ) ]
81
154
pub fn send_all_to_address (
82
- & self , address : & bitcoin:: Address , retain_reserves : bool ,
155
+ & self , address : & bitcoin:: Address , retain_reserves : bool , fee_rate : Option < Arc < FeeRate > > ,
83
156
) -> Result < Txid , Error > {
84
157
let rt_lock = self . runtime . read ( ) . unwrap ( ) ;
85
158
if rt_lock. is_none ( ) {
@@ -94,6 +167,6 @@ impl OnchainPayment {
94
167
OnchainSendAmount :: AllDrainingReserve
95
168
} ;
96
169
97
- self . wallet . send_to_address ( address, send_amount)
170
+ self . wallet . send_to_address ( address, send_amount, fee_rate . map ( |f| f . 0 ) )
98
171
}
99
172
}
0 commit comments