@@ -122,4 +122,63 @@ impl OnchainPayment {
122122 let fee_rate_opt = maybe_map_fee_rate_opt ! ( fee_rate) ;
123123 self . wallet . send_to_address ( address, send_amount, fee_rate_opt)
124124 }
125+
126+ /// Estimates the fee for sending an on-chain payment to the given address.
127+ ///
128+ /// This will respect any on-chain reserve we need to keep, i.e., won't allow to cut into
129+ /// [`BalanceDetails::total_anchor_channels_reserve_sats`].
130+ ///
131+ /// If `fee_rate` is set it will be used for estimating the resulting transaction. Otherwise we'll retrieve
132+ /// a reasonable estimate from the configured chain source.
133+ ///
134+ /// [`BalanceDetails::total_anchor_channels_reserve_sats`]: crate::BalanceDetails::total_anchor_channels_reserve_sats
135+ pub fn estimate_send_to_address (
136+ & self , address : & Address , amount_sats : u64 , fee_rate : Option < FeeRate > ,
137+ ) -> Result < bitcoin:: Amount , Error > {
138+ let rt_lock = self . runtime . read ( ) . unwrap ( ) ;
139+ if rt_lock. is_none ( ) {
140+ return Err ( Error :: NotRunning ) ;
141+ }
142+
143+ let cur_anchor_reserve_sats =
144+ crate :: total_anchor_channels_reserve_sats ( & self . channel_manager , & self . config ) ;
145+ let send_amount =
146+ OnchainSendAmount :: ExactRetainingReserve { amount_sats, cur_anchor_reserve_sats } ;
147+ let fee_rate_opt = maybe_map_fee_rate_opt ! ( fee_rate) ;
148+ self . wallet . estimate_fee ( address, send_amount, fee_rate_opt)
149+ }
150+
151+ /// Estimates the fee for sending an on-chain payment to the given address, draining the available funds.
152+ ///
153+ /// This is useful if you have closed all channels and want to migrate funds to another
154+ /// on-chain wallet.
155+ ///
156+ /// Please note that if `retain_reserves` is set to `false` this will **not** retain any on-chain reserves, which might be potentially
157+ /// dangerous if you have open Anchor channels for which you can't trust the counterparty to
158+ /// spend the Anchor output after channel closure. If `retain_reserves` is set to `true`, this
159+ /// will try to send all spendable onchain funds, i.e.,
160+ /// [`BalanceDetails::spendable_onchain_balance_sats`].
161+ ///
162+ /// If `fee_rate` is set it will be used on the resulting transaction. Otherwise a reasonable
163+ /// we'll retrieve an estimate from the configured chain source.
164+ ///
165+ /// [`BalanceDetails::spendable_onchain_balance_sats`]: crate::balance::BalanceDetails::spendable_onchain_balance_sats
166+ pub fn estimate_send_all_to_address (
167+ & self , address : & Address , retain_reserves : bool , fee_rate : Option < FeeRate > ,
168+ ) -> Result < bitcoin:: Amount , Error > {
169+ let rt_lock = self . runtime . read ( ) . unwrap ( ) ;
170+ if rt_lock. is_none ( ) {
171+ return Err ( Error :: NotRunning ) ;
172+ }
173+
174+ let send_amount = if retain_reserves {
175+ let cur_anchor_reserve_sats =
176+ crate :: total_anchor_channels_reserve_sats ( & self . channel_manager , & self . config ) ;
177+ OnchainSendAmount :: AllRetainingReserve { cur_anchor_reserve_sats }
178+ } else {
179+ OnchainSendAmount :: AllDrainingReserve
180+ } ;
181+ let fee_rate_opt = maybe_map_fee_rate_opt ! ( fee_rate) ;
182+ self . wallet . estimate_fee ( address, send_amount, fee_rate_opt)
183+ }
125184}
0 commit comments