Skip to content

Commit 4fd0310

Browse files
committed
Implement failed swap resolving methods
1 parent f1621c3 commit 4fd0310

File tree

2 files changed

+66
-0
lines changed

2 files changed

+66
-0
lines changed

src/lib.rs

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -246,6 +246,14 @@ pub struct OfferInfo {
246246
pub status: OfferStatus,
247247
}
248248

249+
/// Information about a failed swap
250+
pub struct FailedSwapInfo {
251+
pub address: String,
252+
/// The amount that is available to be refunded. The refund will involve paying some
253+
/// onchain fees so it isn't possible to recover the entire amount.
254+
pub amount: Amount,
255+
}
256+
249257
#[derive(Clone, PartialEq, Debug)]
250258
pub(crate) struct UserPreferences {
251259
fiat_currency: String,
@@ -1064,6 +1072,53 @@ impl LightningNode {
10641072
.to_hex())
10651073
}
10661074

1075+
/// Lists all unresolved failed swaps. Each individual failed swap can be refunded
1076+
/// using [`LightningNode::refund_failed_swap`].
1077+
pub fn get_unresolved_failed_swaps(&self) -> Result<Vec<FailedSwapInfo>> {
1078+
Ok(self
1079+
.rt
1080+
.handle()
1081+
.block_on(self.sdk.list_refundables())
1082+
.map_to_runtime_error(
1083+
RuntimeErrorCode::NodeUnavailable,
1084+
"Failed to list refundable failed swaps",
1085+
)?
1086+
.iter()
1087+
.map(|s| FailedSwapInfo {
1088+
address: s.bitcoin_address.clone(),
1089+
amount: (s.confirmed_sats * 1000).to_amount_down(&self.get_exchange_rate()),
1090+
})
1091+
.collect())
1092+
}
1093+
1094+
/// Creates and broadcasts a refund transaction to recover funds from a failed swap. Existing
1095+
/// failed swaps can be listed using [`LightningNode::get_unresolved_failed_swaps`].
1096+
///
1097+
/// Parameters:
1098+
/// * `failed_swap_address` - the address of the failed swap (can be obtained from [`FailedSwapInfo`])
1099+
/// * `to_address` - the destination address to which funds will be sent
1100+
/// * `onchain_fee_rate` - the fee rate that will be applied. The recommeded one can be fetched
1101+
/// using [`LightningNode::query_onchain_fee`]
1102+
///
1103+
/// Returns the txid of the refund transaction.
1104+
pub fn refund_failed_swap(
1105+
&self,
1106+
failed_swap_address: String,
1107+
to_address: String,
1108+
onchain_fee_rate: u32,
1109+
) -> Result<String> {
1110+
self.rt
1111+
.handle()
1112+
.block_on(
1113+
self.sdk
1114+
.refund(failed_swap_address, to_address, onchain_fee_rate),
1115+
)
1116+
.map_to_runtime_error(
1117+
RuntimeErrorCode::NodeUnavailable,
1118+
"Failed to create and broadcast failed swap refund transaction",
1119+
)
1120+
}
1121+
10671122
/// Prints additional debug information to the logs.
10681123
///
10691124
/// Throws an error in case that the necessary information can't be retrieved.

src/lipalightninglib.udl

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,12 @@ interface LightningNode {
7676
[Throws=LnError]
7777
string sweep(string address, u32 onchain_fee);
7878

79+
[Throws=LnError]
80+
sequence<FailedSwapInfo> get_unresolved_failed_swaps();
81+
82+
[Throws=LnError]
83+
string refund_failed_swap(string failed_swap_address, string to_address, u32 onchain_fee_rate);
84+
7985
[Throws=LnError]
8086
void hide_topup(string id);
8187

@@ -315,6 +321,11 @@ enum OfferStatus {
315321
"SETTLED",
316322
};
317323

324+
dictionary FailedSwapInfo {
325+
string address;
326+
Amount amount;
327+
};
328+
318329
//
319330
// ----------------------------- TOP LEVEL FUNCTIONS + RELATED DEFINITIONS -----------------------------
320331
//

0 commit comments

Comments
 (0)