Skip to content

Commit 5c3ea0f

Browse files
committed
f DRY up via macro
1 parent 65cb813 commit 5c3ea0f

File tree

1 file changed

+18
-65
lines changed

1 file changed

+18
-65
lines changed

src/payment/onchain.rs

Lines changed: 18 additions & 65 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,20 @@ use std::sync::{Arc, RwLock};
2020
#[cfg(not(feature = "uniffi"))]
2121
type FeeRate = bitcoin::FeeRate;
2222
#[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+
}
2437

2538
/// A payment handler allowing to send and receive on-chain payments.
2639
///
@@ -59,7 +72,6 @@ impl OnchainPayment {
5972
/// a reasonable estimate from the configured chain source.
6073
///
6174
/// [`BalanceDetails::total_anchor_channels_reserve_sats`]: crate::BalanceDetails::total_anchor_channels_reserve_sats
62-
#[cfg(not(feature = "uniffi"))]
6375
pub fn send_to_address(
6476
&self, address: &bitcoin::Address, amount_sats: u64, fee_rate: Option<FeeRate>,
6577
) -> Result<Txid, Error> {
@@ -72,32 +84,8 @@ impl OnchainPayment {
7284
crate::total_anchor_channels_reserve_sats(&self.channel_manager, &self.config);
7385
let send_amount =
7486
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)
10189
}
10290

10391
/// Send an on-chain payment to the given address, draining the available funds.
@@ -115,7 +103,6 @@ impl OnchainPayment {
115103
/// we'll retrieve an estimate from the configured chain source.
116104
///
117105
/// [`BalanceDetails::spendable_onchain_balance_sats`]: crate::balance::BalanceDetails::spendable_onchain_balance_sats
118-
#[cfg(not(feature = "uniffi"))]
119106
pub fn send_all_to_address(
120107
&self, address: &bitcoin::Address, retain_reserves: bool, fee_rate: Option<FeeRate>,
121108
) -> Result<Txid, Error> {
@@ -132,41 +119,7 @@ impl OnchainPayment {
132119
OnchainSendAmount::AllDrainingReserve
133120
};
134121

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)
171124
}
172125
}

0 commit comments

Comments
 (0)