Skip to content

Commit 8a2b5b9

Browse files
committed
feat: expose calculate_cpfp_fee_rate
Adds public method to calculate appropriate fee rates for CPFP (Child-Pays-For-Parent) transactions. This allows users to determine the optimal fee rate before creating CPFP transactions to accelerate parent transaction confirmation. - Add public calculate_cpfp_fee_rate method to OnchainPayment - Expose method in UDL bindings - Handle uniffi/non-uniffi return type differences
1 parent cd77ab1 commit 8a2b5b9

File tree

3 files changed

+43
-1
lines changed

3 files changed

+43
-1
lines changed

bindings/ldk_node.udl

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -230,6 +230,8 @@ interface OnchainPayment {
230230
Txid bump_fee_by_rbf([ByRef]Txid txid, FeeRate fee_rate);
231231
[Throws=NodeError]
232232
Txid accelerate_by_cpfp([ByRef]Txid txid, FeeRate? fee_rate, Address? destination_address);
233+
[Throws=NodeError]
234+
FeeRate calculate_cpfp_fee_rate([ByRef]Txid parent_txid, boolean urgent);
233235
};
234236

235237
enum CoinSelectionAlgorithm {

src/payment/onchain.rs

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -302,4 +302,44 @@ impl OnchainPayment {
302302
// Pass through to the wallet implementation
303303
self.wallet.accelerate_by_cpfp(txid, fee_rate_param, destination_address)
304304
}
305+
306+
/// Calculates an appropriate fee rate for a CPFP transaction to ensure
307+
/// the parent transaction gets confirmed within the target number of blocks.
308+
///
309+
/// This method analyzes the parent transaction's current fee rate and calculates
310+
/// how much the child transaction needs to pay to bring the combined package
311+
/// fee rate up to the target level.
312+
///
313+
/// # Arguments
314+
///
315+
/// * `parent_txid` - The transaction ID of the parent transaction to accelerate
316+
/// * `urgent` - If true, uses a more aggressive fee rate for faster confirmation
317+
///
318+
/// # Returns
319+
///
320+
/// The fee rate that should be used for the child transaction.
321+
///
322+
/// # Errors
323+
///
324+
/// * [`Error::NotRunning`] - If the node is not running
325+
/// * [`Error::TransactionNotFound`] - If the parent transaction can't be found
326+
/// * [`Error::TransactionAlreadyConfirmed`] - If the parent transaction is already confirmed
327+
/// * [`Error::WalletOperationFailed`] - If fee calculation fails
328+
pub fn calculate_cpfp_fee_rate(&self, parent_txid: &Txid, urgent: bool) -> Result<FeeRate, Error> {
329+
let rt_lock = self.runtime.read().unwrap();
330+
if rt_lock.is_none() {
331+
return Err(Error::NotRunning);
332+
}
333+
334+
let fee_rate = self.wallet.calculate_cpfp_fee_rate(parent_txid, urgent)?;
335+
336+
#[cfg(not(feature = "uniffi"))]
337+
{
338+
Ok(fee_rate)
339+
}
340+
#[cfg(feature = "uniffi")]
341+
{
342+
Ok(Arc::new(fee_rate))
343+
}
344+
}
305345
}

src/wallet/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -616,7 +616,7 @@ where
616616
// Get current mempool fee rates from fee estimator based on urgency
617617
let target = if urgent {
618618
ConfirmationTarget::Lightning(
619-
lightning::chain::chaininterface::ConfirmationTarget::UrgentOnChainSweep,
619+
lightning::chain::chaininterface::ConfirmationTarget::MaximumFeeEstimate,
620620
)
621621
} else {
622622
ConfirmationTarget::OnchainPayment

0 commit comments

Comments
 (0)