@@ -66,8 +66,8 @@ use breez_sdk_core::{
66
66
parse, parse_invoice, BreezEvent , BreezServices , EventListener , GreenlightCredentials ,
67
67
GreenlightNodeConfig , InputType , ListPaymentsRequest , LnUrlPayRequest , LnUrlPayRequestData ,
68
68
LnUrlPayResult , LnUrlWithdrawRequest , LnUrlWithdrawResult , NodeConfig , OpenChannelFeeRequest ,
69
- OpeningFeeParams , PaymentDetails , PaymentStatus , PaymentTypeFilter , ReceiveOnchainRequest ,
70
- RefundRequest , SendPaymentRequest , SweepRequest ,
69
+ OpeningFeeParams , PaymentDetails , PaymentStatus , PaymentTypeFilter , PrepareRefundRequest ,
70
+ ReceiveOnchainRequest , RefundRequest , SendPaymentRequest , SweepRequest ,
71
71
} ;
72
72
use cipher:: generic_array:: typenum:: U32 ;
73
73
use crow:: { CountryCode , LanguageCode , OfferManager , TopupError , TopupInfo , TopupStatus } ;
@@ -278,6 +278,14 @@ pub struct FailedSwapInfo {
278
278
pub created_at : SystemTime ,
279
279
}
280
280
281
+ /// Information about a failed swap refund
282
+ pub struct FailedSwapRefundInfo {
283
+ /// The amount that will be sent (swap amount - onchain fee)
284
+ pub refund_amount : Amount ,
285
+ /// The amount that will be paid in onchain fees
286
+ pub onchain_fee : Amount ,
287
+ }
288
+
281
289
#[ derive( Clone , PartialEq , Debug ) ]
282
290
pub ( crate ) struct UserPreferences {
283
291
fiat_currency : String ,
@@ -1404,6 +1412,45 @@ impl LightningNode {
1404
1412
. collect ( ) )
1405
1413
}
1406
1414
1415
+ /// Prepares a failed swap refund in order to know how much will be recovered and how much
1416
+ /// will be paid in onchain fees
1417
+ ///
1418
+ /// Parameters:
1419
+ /// * `failed_swap_info` - the failed swap that will be prepared
1420
+ /// * `to_address` - the destination address to which funds will be sent
1421
+ /// * `onchain_fee_rate` - the fee rate that will be applied. The recommended one can be fetched
1422
+ /// using [`LightningNode::query_onchain_fee_rate`]
1423
+ pub fn prepare_refund_failed_swap (
1424
+ & self ,
1425
+ failed_swap_info : FailedSwapInfo ,
1426
+ to_address : String ,
1427
+ onchain_fee_rate : u32 ,
1428
+ ) -> Result < FailedSwapRefundInfo > {
1429
+ let response = self
1430
+ . rt
1431
+ . handle ( )
1432
+ . block_on ( self . sdk . prepare_refund ( PrepareRefundRequest {
1433
+ swap_address : failed_swap_info. address ,
1434
+ to_address,
1435
+ sat_per_vbyte : onchain_fee_rate,
1436
+ } ) )
1437
+ . map_to_runtime_error (
1438
+ RuntimeErrorCode :: NodeUnavailable ,
1439
+ "Failed to create and broadcast failed swap refund transaction" ,
1440
+ ) ?;
1441
+
1442
+ let rate = self . get_exchange_rate ( ) ;
1443
+ let onchain_fee = response. refund_tx_fee_sat . as_sats ( ) . to_amount_up ( & rate) ;
1444
+ let refund_amount = ( failed_swap_info. amount . sats - onchain_fee. sats )
1445
+ . as_sats ( )
1446
+ . to_amount_down ( & rate) ;
1447
+
1448
+ Ok ( FailedSwapRefundInfo {
1449
+ refund_amount,
1450
+ onchain_fee,
1451
+ } )
1452
+ }
1453
+
1407
1454
/// Creates and broadcasts a refund transaction to recover funds from a failed swap. Existing
1408
1455
/// failed swaps can be listed using [`LightningNode::get_unresolved_failed_swaps`].
1409
1456
///
0 commit comments