@@ -17,6 +17,11 @@ use bitcoin::{Address, Txid};
1717
1818use std:: sync:: { Arc , RwLock } ;
1919
20+ #[ cfg( not( feature = "uniffi" ) ) ]
21+ type FeeRate = bitcoin:: FeeRate ;
22+ #[ cfg( feature = "uniffi" ) ]
23+ type FeeRate = crate :: uniffi_types:: FeeRate ;
24+
2025/// A payment handler allowing to send and receive on-chain payments.
2126///
2227/// Should be retrieved by calling [`Node::onchain_payment`].
@@ -50,9 +55,13 @@ impl OnchainPayment {
5055 /// This will respect any on-chain reserve we need to keep, i.e., won't allow to cut into
5156 /// [`BalanceDetails::total_anchor_channels_reserve_sats`].
5257 ///
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+ ///
5361 /// [`BalanceDetails::total_anchor_channels_reserve_sats`]: crate::BalanceDetails::total_anchor_channels_reserve_sats
62+ #[ cfg( not( feature = "uniffi" ) ) ]
5463 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 > ,
5665 ) -> Result < Txid , Error > {
5766 let rt_lock = self . runtime . read ( ) . unwrap ( ) ;
5867 if rt_lock. is_none ( ) {
@@ -63,7 +72,32 @@ impl OnchainPayment {
6372 crate :: total_anchor_channels_reserve_sats ( & self . channel_manager , & self . config ) ;
6473 let send_amount =
6574 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 ) )
67101 }
68102
69103 /// Send an on-chain payment to the given address, draining the available funds.
@@ -77,9 +111,48 @@ impl OnchainPayment {
77111 /// will try to send all spendable onchain funds, i.e.,
78112 /// [`BalanceDetails::spendable_onchain_balance_sats`].
79113 ///
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+ ///
80152 /// [`BalanceDetails::spendable_onchain_balance_sats`]: crate::balance::BalanceDetails::spendable_onchain_balance_sats
153+ #[ cfg( feature = "uniffi" ) ]
81154 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 > > ,
83156 ) -> Result < Txid , Error > {
84157 let rt_lock = self . runtime . read ( ) . unwrap ( ) ;
85158 if rt_lock. is_none ( ) {
@@ -94,6 +167,6 @@ impl OnchainPayment {
94167 OnchainSendAmount :: AllDrainingReserve
95168 } ;
96169
97- self . wallet . send_to_address ( address, send_amount)
170+ self . wallet . send_to_address ( address, send_amount, fee_rate . map ( |f| f . 0 ) )
98171 }
99172}
0 commit comments