Skip to content

Commit

Permalink
Handle ReverseSwapUpdated event (#1258)
Browse files Browse the repository at this point in the history
  • Loading branch information
andrei-21 authored Nov 14, 2024
1 parent 2df807c commit ddd5a40
Show file tree
Hide file tree
Showing 6 changed files with 63 additions and 5 deletions.
35 changes: 35 additions & 0 deletions src/activities.rs
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,41 @@ impl Activities {
}
}

/// Get a reverse swap activity by reverse swap id.
///
/// Parameters:
/// * `reverse_swap_id` - the id of a reverse swap.
///
/// Requires network: **no**
pub fn get_by_reverse_swap(&self, reverse_swap_id: String) -> Result<Option<Activity>> {
const LEEWAY_FOR_REVERSE_SWAPS: u32 = 30;
let list_payments_request = ListPaymentsRequest {
filters: Some(vec![PaymentTypeFilter::Sent]),
metadata_filters: None,
from_timestamp: None,
to_timestamp: None,
include_failures: Some(false),
limit: Some(LEEWAY_FOR_REVERSE_SWAPS),
offset: None,
};

let is_swap_with_id = |p: &breez_sdk_core::Payment| {
if let breez_sdk_core::PaymentDetails::Ln { ref data } = p.details {
if let Some(ref swap_info) = data.reverse_swap_info {
return swap_info.id == reverse_swap_id;
}
}
false
};
self.rt_handle
.block_on(self.sdk.list_payments(list_payments_request))
.map_to_runtime_error(RuntimeErrorCode::NodeUnavailable, "Failed to list payments")?
.into_iter()
.find(is_swap_with_id)
.map(|p| self.activity_from_breez_payment(p))
.transpose()
}

/// Get an incoming payment by its payment hash.
///
/// Parameters:
Expand Down
9 changes: 8 additions & 1 deletion src/callbacks.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,10 +37,17 @@ pub trait EventsCallback: Send + Sync {
/// This callback will be called when an incoming swap has completed.
///
/// Parameters:
/// * `payment_hash` - can be used to find the [´Activity´](crate::Activity) corresponding to
/// * `payment_hash` - can be used to find the [`Activity`](crate::Activity) corresponding to
/// this swap.
fn swap_received(&self, payment_hash: String);

/// This callback will be called when a reverse swap has completed.
///
/// 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 a change to the Breez services health is noticed
///
/// Parameters:
Expand Down
12 changes: 8 additions & 4 deletions src/event.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use crate::{analytics::AnalyticsInterceptor, EventsCallback};

use breez_sdk_core::{BreezEvent, EventListener, PaymentDetails, SwapStatus};
use breez_sdk_core::{BreezEvent, EventListener, PaymentDetails, ReverseSwapStatus, SwapStatus};
use std::sync::Arc;

pub(crate) struct LipaEventListener {
Expand Down Expand Up @@ -32,13 +32,13 @@ impl EventListener for LipaEventListener {
self.events_callback.synced();
}
BreezEvent::PaymentSucceed { details } => {
if let PaymentDetails::Ln { data } = details.details.clone() {
if let PaymentDetails::Ln { data } = details.details {
self.events_callback
.payment_sent(data.payment_hash, data.payment_preimage)
}
}
BreezEvent::PaymentFailed { details } => {
if let Some(invoice) = details.invoice.clone() {
if let Some(invoice) = details.invoice {
self.events_callback.payment_failed(invoice.payment_hash)
}
}
Expand All @@ -51,7 +51,11 @@ impl EventListener for LipaEventListener {
.swap_received(hex::encode(details.payment_hash));
}
}
BreezEvent::ReverseSwapUpdated { .. } => {}
BreezEvent::ReverseSwapUpdated { details } => {
if details.status == ReverseSwapStatus::CompletedConfirmed {
self.events_callback.reverse_swap_sent(details.id);
}
}
}
}
}
Expand Down
4 changes: 4 additions & 0 deletions src/lipalightninglib.udl
Original file line number Diff line number Diff line change
Expand Up @@ -204,6 +204,9 @@ interface Activities {

[Throws=LnError]
Activity get(string hash);

[Throws=LnError]
Activity? get_by_reverse_swap(string reverse_swap_id);

[Throws=LnError]
IncomingPaymentInfo get_incoming_payment(string hash);
Expand Down Expand Up @@ -278,6 +281,7 @@ callback interface EventsCallback {
void payment_failed(string payment_hash);
void channel_closed(string channel_id, string reason);
void swap_received(string payment_hash);
void reverse_swap_sent(string reverse_swap_id);
void breez_health_status_changed_to(BreezHealthCheckStatus status);
void synced();
};
Expand Down
4 changes: 4 additions & 0 deletions tests/monitoring_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,10 @@ impl EventsCallback for ReturnFundsEventsHandler {
// do nothing
}

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

fn breez_health_status_changed_to(&self, _status: BreezHealthCheckStatus) {
// do nothing
}
Expand Down
4 changes: 4 additions & 0 deletions tests/print_events_handler/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,10 @@ impl EventsCallback for PrintEventsHandler {
println!("A swap has been received! Its hash is {payment_hash}");
}

fn reverse_swap_sent(&self, reverse_swap_id: String) {
println!("A reverse swap has been sent! 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 ddd5a40

Please sign in to comment.