Skip to content

Commit

Permalink
Merge pull request #1311 from getlipa/feat/add-more-accurate-reverse-…
Browse files Browse the repository at this point in the history
…swap-states

Add more specific reverse swap states
  • Loading branch information
gcomte authored Dec 18, 2024
2 parents 7dcdfeb + b3f8602 commit 564cc22
Show file tree
Hide file tree
Showing 5 changed files with 44 additions and 4 deletions.
17 changes: 16 additions & 1 deletion src/callbacks.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,13 +41,28 @@ pub trait EventsCallback: Send + Sync {
/// this swap.
fn swap_received(&self, payment_hash: String);

/// This callback will be called when a reverse swap has completed.
/// This callback will be called when a TX of an on-chain send has been broadcasted to the Bitcoin network.
///
/// Parameters:
/// * `reverse_swap_id` - can be used to find the [`Activity`](crate::Activity) corresponding to
/// this swap using [`get_by_reverse_swap`](crate::Activities::get_by_reverse_swap).
fn reverse_swap_sent(&self, reverse_swap_id: String);

/// This callback will be called when an on-chain send has been confirmed on-chain.
///
/// Parameters:
/// * `reverse_swap_id` - can be used to find the [`Activity`](crate::Activity) corresponding to
/// this swap using [`get_by_reverse_swap`](crate::Activities::get_by_reverse_swap).
fn reverse_swap_settled(&self, reverse_swap_id: String);

/// This callback will be called when an on-chain send has failed and is thus cancelled.
/// The initial invoice funds are unlocked for the sender. (payment failed)
///
/// Parameters:
/// * `reverse_swap_id` - can be used to find the [`Activity`](crate::Activity) corresponding to
/// this swap using [`get_by_reverse_swap`](crate::Activities::get_by_reverse_swap).
fn reverse_swap_cancelled(&self, reverse_swap_id: String);

/// This callback will be called when a change to the Breez services health is noticed
///
/// Parameters:
Expand Down
13 changes: 10 additions & 3 deletions src/event.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,11 +51,18 @@ impl EventListener for LipaEventListener {
.swap_received(hex::encode(details.payment_hash));
}
}
BreezEvent::ReverseSwapUpdated { details } => {
if details.status == ReverseSwapStatus::CompletedConfirmed {
BreezEvent::ReverseSwapUpdated { details } => match details.status {
ReverseSwapStatus::CompletedSeen => {
self.events_callback.reverse_swap_sent(details.id);
}
}
ReverseSwapStatus::CompletedConfirmed => {
self.events_callback.reverse_swap_settled(details.id);
}
ReverseSwapStatus::Cancelled => {
self.events_callback.reverse_swap_cancelled(details.id);
}
_ => {}
},
}
}
}
Expand Down
2 changes: 2 additions & 0 deletions src/lipalightninglib.udl
Original file line number Diff line number Diff line change
Expand Up @@ -499,6 +499,8 @@ callback interface EventsCallback {
void channel_closed(string channel_id, string reason);
void swap_received(string payment_hash);
void reverse_swap_sent(string reverse_swap_id);
void reverse_swap_settled(string reverse_swap_id);
void reverse_swap_cancelled(string reverse_swap_id);
void breez_health_status_changed_to(BreezHealthCheckStatus status);
void synced();
};
Expand Down
8 changes: 8 additions & 0 deletions tests/monitoring_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,14 @@ impl EventsCallback for ReturnFundsEventsHandler {
// do nothing
}

fn reverse_swap_settled(&self, _reverse_swap_id: String) {
// do nothing
}

fn reverse_swap_cancelled(&self, _reverse_swap_id: String) {
// do nothing
}

fn breez_health_status_changed_to(&self, _status: BreezHealthCheckStatus) {
// do nothing
}
Expand Down
8 changes: 8 additions & 0 deletions tests/print_events_handler/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,14 @@ impl EventsCallback for PrintEventsHandler {
println!("A reverse swap has been sent! Its id is {reverse_swap_id}");
}

fn reverse_swap_settled(&self, reverse_swap_id: String) {
println!("A reverse swap has been settled! Its id is {reverse_swap_id}");
}

fn reverse_swap_cancelled(&self, reverse_swap_id: String) {
println!("A reverse swap has been cancelled! Its id is {reverse_swap_id}");
}

fn breez_health_status_changed_to(&self, status: BreezHealthCheckStatus) {
println!("The Breez SDK health status changed to {status:?}");
}
Expand Down

0 comments on commit 564cc22

Please sign in to comment.