@@ -20,7 +20,20 @@ use std::sync::{Arc, RwLock};
20
20
#[ cfg( not( feature = "uniffi" ) ) ]
21
21
type FeeRate = bitcoin:: FeeRate ;
22
22
#[ cfg( feature = "uniffi" ) ]
23
- type FeeRate = crate :: uniffi_types:: FeeRate ;
23
+ type FeeRate = Arc < crate :: uniffi_types:: FeeRate > ;
24
+
25
+ macro_rules! maybe_map_fee_rate_opt {
26
+ ( $fee_rate_opt: expr) => { {
27
+ #[ cfg( not( feature = "uniffi" ) ) ]
28
+ {
29
+ $fee_rate_opt
30
+ }
31
+ #[ cfg( feature = "uniffi" ) ]
32
+ {
33
+ $fee_rate_opt. map( |f| f. 0 )
34
+ }
35
+ } } ;
36
+ }
24
37
25
38
/// A payment handler allowing to send and receive on-chain payments.
26
39
///
@@ -59,7 +72,6 @@ impl OnchainPayment {
59
72
/// a reasonable estimate from the configured chain source.
60
73
///
61
74
/// [`BalanceDetails::total_anchor_channels_reserve_sats`]: crate::BalanceDetails::total_anchor_channels_reserve_sats
62
- #[ cfg( not( feature = "uniffi" ) ) ]
63
75
pub fn send_to_address (
64
76
& self , address : & bitcoin:: Address , amount_sats : u64 , fee_rate : Option < FeeRate > ,
65
77
) -> Result < Txid , Error > {
@@ -72,32 +84,8 @@ impl OnchainPayment {
72
84
crate :: total_anchor_channels_reserve_sats ( & self . channel_manager , & self . config ) ;
73
85
let send_amount =
74
86
OnchainSendAmount :: ExactRetainingReserve { amount_sats, cur_anchor_reserve_sats } ;
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 be used on the resulting transaction. Otherwise we'll retrieve
84
- /// a reasonable 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 ) )
87
+ let fee_rate_opt = maybe_map_fee_rate_opt ! ( fee_rate) ;
88
+ self . wallet . send_to_address ( address, send_amount, fee_rate_opt)
101
89
}
102
90
103
91
/// Send an on-chain payment to the given address, draining the available funds.
@@ -115,7 +103,6 @@ impl OnchainPayment {
115
103
/// we'll retrieve an estimate from the configured chain source.
116
104
///
117
105
/// [`BalanceDetails::spendable_onchain_balance_sats`]: crate::balance::BalanceDetails::spendable_onchain_balance_sats
118
- #[ cfg( not( feature = "uniffi" ) ) ]
119
106
pub fn send_all_to_address (
120
107
& self , address : & bitcoin:: Address , retain_reserves : bool , fee_rate : Option < FeeRate > ,
121
108
) -> Result < Txid , Error > {
@@ -132,41 +119,7 @@ impl OnchainPayment {
132
119
OnchainSendAmount :: AllDrainingReserve
133
120
} ;
134
121
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
- ///
152
- /// [`BalanceDetails::spendable_onchain_balance_sats`]: crate::balance::BalanceDetails::spendable_onchain_balance_sats
153
- #[ cfg( feature = "uniffi" ) ]
154
- pub fn send_all_to_address (
155
- & self , address : & bitcoin:: Address , retain_reserves : bool , fee_rate : Option < Arc < FeeRate > > ,
156
- ) -> Result < Txid , Error > {
157
- let rt_lock = self . runtime . read ( ) . unwrap ( ) ;
158
- if rt_lock. is_none ( ) {
159
- return Err ( Error :: NotRunning ) ;
160
- }
161
-
162
- let send_amount = if retain_reserves {
163
- let cur_anchor_reserve_sats =
164
- crate :: total_anchor_channels_reserve_sats ( & self . channel_manager , & self . config ) ;
165
- OnchainSendAmount :: AllRetainingReserve { cur_anchor_reserve_sats }
166
- } else {
167
- OnchainSendAmount :: AllDrainingReserve
168
- } ;
169
-
170
- self . wallet . send_to_address ( address, send_amount, fee_rate. map ( |f| f. 0 ) )
122
+ let fee_rate_opt = maybe_map_fee_rate_opt ! ( fee_rate) ;
123
+ self . wallet . send_to_address ( address, send_amount, fee_rate_opt)
171
124
}
172
125
}
0 commit comments