Skip to content

Commit c859d7b

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

File tree

3 files changed

+68
-0
lines changed

3 files changed

+68
-0
lines changed

examples/3l-node/cli.rs

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,16 @@ pub(crate) fn poll_for_user_input(node: &LightningNode, log_file_path: &str) {
112112
println!("{}", message.red());
113113
}
114114
}
115+
"listfailedswaps" => {
116+
if let Err(message) = list_failed_swaps(node) {
117+
println!("{}", message.red());
118+
}
119+
}
120+
"refundfailedswap" => {
121+
if let Err(message) = refund_failed_swap(node, &mut words) {
122+
println!("{}", message.red());
123+
}
124+
}
115125
"registertopup" => {
116126
if let Err(message) = register_topup(node, &mut words) {
117127
println!("{}", message.red());
@@ -219,6 +229,12 @@ fn setup_editor(history_path: &Path) -> Editor<CommandHinter, DefaultHistory> {
219229
"payopeninvoice ",
220230
));
221231

232+
hints.insert(CommandHint::new("listfailedswaps>", "listfailedswaps "));
233+
hints.insert(CommandHint::new(
234+
"refundfailedswap <swap address> <to address>",
235+
"refundfailedswap ",
236+
));
237+
222238
hints.insert(CommandHint::new(
223239
"registertopup <IBAN> <currency> [email]",
224240
"registertopup ",
@@ -261,6 +277,9 @@ fn help() {
261277
println!(" payinvoice <invoice>");
262278
println!(" payopeninvoice <invoice> <amount in SAT>");
263279
println!();
280+
println!(" listfailedswaps");
281+
println!(" refundfailedswap <swap address> <to address>");
282+
println!();
264283
println!(" registertopup <IBAN> <currency> [email]");
265284
println!(" listoffers");
266285
println!();
@@ -548,6 +567,52 @@ fn pay_open_invoice(
548567
Ok(())
549568
}
550569

570+
fn list_failed_swaps(node: &LightningNode) -> Result<(), String> {
571+
let failed_swaps = match node.get_unresolved_failed_swaps() {
572+
Ok(s) => s,
573+
Err(e) => return Err(e.to_string()),
574+
};
575+
576+
println!(
577+
"Total of {} failed swaps\n",
578+
failed_swaps.len().to_string().bold()
579+
);
580+
for swap in failed_swaps {
581+
let created_at: DateTime<Local> = swap.created_at.into();
582+
println!("Failed swap created at {created_at}:");
583+
println!(" Address {}", swap.address);
584+
println!(" Amount: {}", amount_to_string(swap.amount));
585+
}
586+
587+
Ok(())
588+
}
589+
590+
fn refund_failed_swap(
591+
node: &LightningNode,
592+
words: &mut dyn Iterator<Item = &str>,
593+
) -> Result<(), String> {
594+
let swap_address = words
595+
.next()
596+
.ok_or_else(|| "swap address is required".to_string())?;
597+
let to_address = words
598+
.next()
599+
.ok_or_else(|| "to address is required".to_string())?;
600+
601+
let fee_rate = match node.query_onchain_fee() {
602+
Ok(r) => r,
603+
Err(e) => return Err(e.to_string()),
604+
};
605+
606+
match node.refund_failed_swap(swap_address.into(), to_address.into(), fee_rate) {
607+
Ok(txid) => {
608+
println!("Successfully broadcasted refund transaction - txid: {txid}")
609+
}
610+
Err(e) => return Err(e.to_string()),
611+
}
612+
613+
Ok(())
614+
}
615+
551616
fn register_topup(
552617
node: &LightningNode,
553618
words: &mut dyn Iterator<Item = &str>,

src/lib.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -252,6 +252,7 @@ pub struct FailedSwapInfo {
252252
/// The amount that is available to be refunded. The refund will involve paying some
253253
/// onchain fees so it isn't possible to recover the entire amount.
254254
pub amount: Amount,
255+
pub created_at: SystemTime,
255256
}
256257

257258
#[derive(Clone, PartialEq, Debug)]
@@ -1087,6 +1088,7 @@ impl LightningNode {
10871088
.map(|s| FailedSwapInfo {
10881089
address: s.bitcoin_address.clone(),
10891090
amount: (s.confirmed_sats * 1000).to_amount_down(&self.get_exchange_rate()),
1091+
created_at: unix_timestamp_to_system_time(s.created_at as u64),
10901092
})
10911093
.collect())
10921094
}

src/lipalightninglib.udl

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -324,6 +324,7 @@ enum OfferStatus {
324324
dictionary FailedSwapInfo {
325325
string address;
326326
Amount amount;
327+
timestamp created_at;
327328
};
328329

329330
//

0 commit comments

Comments
 (0)