Skip to content

Commit

Permalink
Merge pull request #954 from breez/ok300-savage-send-payment-yield
Browse files Browse the repository at this point in the history
Simplify payment yield flow
  • Loading branch information
dangeross authored Apr 30, 2024
2 parents c6dd7b8 + 0789f93 commit 4a730da
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 53 deletions.
80 changes: 27 additions & 53 deletions libs/sdk-core/src/breez_services.rs
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,11 @@ pub enum BreezEvent {
details: SwapInfo,
},
}
impl BreezEvent {
pub(crate) fn payment_started(payment: Payment) -> Self {
Self::PaymentStarted { details: payment }
}
}

#[derive(Clone, Debug, PartialEq)]
pub struct BackupFailedData {
Expand Down Expand Up @@ -319,60 +324,29 @@ impl BreezServices {
None => {
let pending_payment =
self.persist_pending_payment(&parsed_invoice, amount_msat, req.label.clone())?;
self.notify_event_listeners(BreezEvent::PaymentStarted {
details: pending_payment.clone(),
})
.await?;

match req.pending_timeout_sec {
Some(secs) => {
let (tx, rx) = std::sync::mpsc::channel();
let cloned = self.clone();
tokio::spawn(async move {
let payment_res = cloned
.node_api
.send_payment(
parsed_invoice.bolt11.clone(),
req.amount_msat,
req.label.clone(),
)
.await;
let result = cloned
.on_payment_completed(
parsed_invoice.payee_pubkey.clone(),
Some(parsed_invoice),
payment_res.map_err(Into::into),
)
.await;
let _ = tx.send(result);
});
self.notify_event_listeners(BreezEvent::payment_started(pending_payment.clone()))
.await?;

match rx.recv_timeout(Duration::from_secs(secs)) {
Ok(result) => result.map(|payment| SendPaymentResponse { payment }),
Err(_) => Ok(SendPaymentResponse {
payment: pending_payment,
}),
}
}
None => {
let payment_res = self
.node_api
.send_payment(
parsed_invoice.bolt11.clone(),
req.amount_msat,
req.label.clone(),
)
.map_err(Into::into)
.await;
let payment = self
.on_payment_completed(
parsed_invoice.payee_pubkey.clone(),
Some(parsed_invoice),
payment_res,
)
.await?;
Ok(SendPaymentResponse { payment })
}
let pending_timeout_sec = req.pending_timeout_sec.unwrap_or(u64::MAX);

let (tx, rx) = std::sync::mpsc::channel();
let cloned = self.clone();
tokio::spawn(async move {
let payment_res = cloned
.node_api
.send_payment(parsed_invoice.bolt11.clone(), req.amount_msat, req.label)
.map_err(Into::into)
.await;
let payee_pk = parsed_invoice.payee_pubkey.clone();
let result = cloned
.on_payment_completed(payee_pk, Some(parsed_invoice), payment_res)
.await;
let _ = tx.send(result);
});

match rx.recv_timeout(Duration::from_secs(pending_timeout_sec)) {
Ok(result) => result.map(SendPaymentResponse::from_payment),
Err(_) => Ok(SendPaymentResponse::from_payment(pending_payment)),
}
}
}
Expand Down
5 changes: 5 additions & 0 deletions libs/sdk-core/src/models.rs
Original file line number Diff line number Diff line change
Expand Up @@ -912,6 +912,11 @@ pub struct SendSpontaneousPaymentRequest {
pub struct SendPaymentResponse {
pub payment: Payment,
}
impl SendPaymentResponse {
pub(crate) fn from_payment(payment: Payment) -> Self {
Self { payment }
}
}

#[derive(Clone, Debug, Serialize, Deserialize)]
pub struct ReportPaymentFailureDetails {
Expand Down

0 comments on commit 4a730da

Please sign in to comment.