Skip to content

Commit

Permalink
Merge pull request #914 from breez/savage-908
Browse files Browse the repository at this point in the history
Improve payment asynchronicity
  • Loading branch information
dangeross authored Apr 11, 2024
2 parents 3b7fffe + 7465c77 commit 068d4a8
Show file tree
Hide file tree
Showing 16 changed files with 188 additions and 53 deletions.
5 changes: 4 additions & 1 deletion libs/sdk-bindings/src/breez_sdk.udl
Original file line number Diff line number Diff line change
Expand Up @@ -601,7 +601,7 @@ dictionary BitcoinAddressData {

dictionary LnUrlPaySuccessData {
SuccessActionProcessed? success_action;
string payment_hash;
Payment payment;
};

dictionary LnUrlErrorData {
Expand All @@ -617,6 +617,7 @@ dictionary LnUrlPayRequest {
LnUrlPayRequestData data;
u64 amount_msat;
string? comment = null;
string? payment_label = null;
};

dictionary LnUrlPayRequestData {
Expand Down Expand Up @@ -746,12 +747,14 @@ dictionary RedeemOnchainFundsResponse {
dictionary SendPaymentRequest {
string bolt11;
u64? amount_msat = null;
string? label = null;
};

dictionary SendSpontaneousPaymentRequest {
string node_id;
u64 amount_msat;
sequence<TlvEntry>? extra_tlvs = null;
string? label = null;
};

dictionary SendPaymentResponse {
Expand Down
17 changes: 12 additions & 5 deletions libs/sdk-core/src/breez_services.rs
Original file line number Diff line number Diff line change
Expand Up @@ -313,10 +313,10 @@ impl BreezServices {
{
Some(_) => Err(SendPaymentError::AlreadyPaid),
None => {
self.persist_pending_payment(&parsed_invoice, amount_msat)?;
self.persist_pending_payment(&parsed_invoice, amount_msat, req.label.clone())?;
let payment_res = self
.node_api
.send_payment(parsed_invoice.bolt11.clone(), req.amount_msat)
.send_payment(parsed_invoice.bolt11.clone(), req.amount_msat, req.label)
.map_err(Into::into)
.await;
let payment = self
Expand All @@ -339,7 +339,12 @@ impl BreezServices {
self.start_node().await?;
let payment_res = self
.node_api
.send_spontaneous_payment(req.node_id.clone(), req.amount_msat, req.extra_tlvs)
.send_spontaneous_payment(
req.node_id.clone(),
req.amount_msat,
req.extra_tlvs,
req.label,
)
.map_err(Into::into)
.await;
let payment = self
Expand Down Expand Up @@ -372,6 +377,7 @@ impl BreezServices {
let pay_req = SendPaymentRequest {
bolt11: cb.pr.clone(),
amount_msat: None,
label: req.payment_label,
};
let invoice = parse_invoice(cb.pr.as_str())?;

Expand Down Expand Up @@ -445,7 +451,7 @@ impl BreezServices {

Ok(LnUrlPayResult::EndpointSuccess {
data: LnUrlPaySuccessData {
payment_hash: details.payment_hash.clone(),
payment,
success_action: maybe_sa_processed,
},
})
Expand Down Expand Up @@ -1158,6 +1164,7 @@ impl BreezServices {
&self,
invoice: &LNInvoice,
amount_msat: u64,
label: Option<String>,
) -> Result<(), SendPaymentError> {
self.persister.insert_or_update_payments(
&[Payment {
Expand All @@ -1172,7 +1179,7 @@ impl BreezServices {
details: PaymentDetails::Ln {
data: LnPaymentDetails {
payment_hash: invoice.payment_hash.clone(),
label: String::new(),
label: label.unwrap_or_default(),
destination_pubkey: invoice.payee_pubkey.clone(),
payment_preimage: String::new(),
keysend: false,
Expand Down
9 changes: 9 additions & 0 deletions libs/sdk-core/src/bridge_generated.io.rs
Original file line number Diff line number Diff line change
Expand Up @@ -858,6 +858,7 @@ impl Wire2Api<LnUrlPayRequest> for wire_LnUrlPayRequest {
data: self.data.wire2api(),
amount_msat: self.amount_msat.wire2api(),
comment: self.comment.wire2api(),
payment_label: self.payment_label.wire2api(),
}
}
}
Expand Down Expand Up @@ -1070,6 +1071,7 @@ impl Wire2Api<SendPaymentRequest> for wire_SendPaymentRequest {
SendPaymentRequest {
bolt11: self.bolt11.wire2api(),
amount_msat: self.amount_msat.wire2api(),
label: self.label.wire2api(),
}
}
}
Expand All @@ -1079,6 +1081,7 @@ impl Wire2Api<SendSpontaneousPaymentRequest> for wire_SendSpontaneousPaymentRequ
node_id: self.node_id.wire2api(),
amount_msat: self.amount_msat.wire2api(),
extra_tlvs: self.extra_tlvs.wire2api(),
label: self.label.wire2api(),
}
}
}
Expand Down Expand Up @@ -1223,6 +1226,7 @@ pub struct wire_LnUrlPayRequest {
data: wire_LnUrlPayRequestData,
amount_msat: u64,
comment: *mut wire_uint_8_list,
payment_label: *mut wire_uint_8_list,
}

#[repr(C)]
Expand Down Expand Up @@ -1385,6 +1389,7 @@ pub struct wire_SendOnchainRequest {
pub struct wire_SendPaymentRequest {
bolt11: *mut wire_uint_8_list,
amount_msat: *mut u64,
label: *mut wire_uint_8_list,
}

#[repr(C)]
Expand All @@ -1393,6 +1398,7 @@ pub struct wire_SendSpontaneousPaymentRequest {
node_id: *mut wire_uint_8_list,
amount_msat: u64,
extra_tlvs: *mut wire_list_tlv_entry,
label: *mut wire_uint_8_list,
}

#[repr(C)]
Expand Down Expand Up @@ -1627,6 +1633,7 @@ impl NewWithNullPtr for wire_LnUrlPayRequest {
data: Default::default(),
amount_msat: Default::default(),
comment: core::ptr::null_mut(),
payment_label: core::ptr::null_mut(),
}
}
}
Expand Down Expand Up @@ -1989,6 +1996,7 @@ impl NewWithNullPtr for wire_SendPaymentRequest {
Self {
bolt11: core::ptr::null_mut(),
amount_msat: core::ptr::null_mut(),
label: core::ptr::null_mut(),
}
}
}
Expand All @@ -2005,6 +2013,7 @@ impl NewWithNullPtr for wire_SendSpontaneousPaymentRequest {
node_id: core::ptr::null_mut(),
amount_msat: Default::default(),
extra_tlvs: core::ptr::null_mut(),
label: core::ptr::null_mut(),
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion libs/sdk-core/src/bridge_generated.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1510,7 +1510,7 @@ impl rust2dart::IntoIntoDart<LnUrlPayResult> for LnUrlPayResult {
impl support::IntoDart for LnUrlPaySuccessData {
fn into_dart(self) -> support::DartAbi {
vec![
self.payment_hash.into_into_dart().into_dart(),
self.payment.into_into_dart().into_dart(),
self.success_action.into_dart(),
]
.into_dart()
Expand Down
16 changes: 11 additions & 5 deletions libs/sdk-core/src/greenlight/node_api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1059,7 +1059,12 @@ impl NodeAPI for Greenlight {
})
}

async fn send_payment(&self, bolt11: String, amount_msat: Option<u64>) -> NodeResult<Payment> {
async fn send_payment(
&self,
bolt11: String,
amount_msat: Option<u64>,
label: Option<String>,
) -> NodeResult<Payment> {
let mut description = None;
if !bolt11.is_empty() {
let invoice = parse_invoice(&bolt11)?;
Expand All @@ -1073,7 +1078,7 @@ impl NodeAPI for Greenlight {
amount_msat: amount_msat.map(|amt| cln::Amount { msat: amt }),
maxfeepercent: Some(self.sdk_config.maxfee_percent),
retry_for: Some(self.sdk_config.payment_timeout_sec),
label: None,
label,
maxdelay: None,
riskfactor: None,
localinvreqid: None,
Expand All @@ -1097,15 +1102,16 @@ impl NodeAPI for Greenlight {
node_id: String,
amount_msat: u64,
extra_tlvs: Option<Vec<TlvEntry>>,
label: Option<String>,
) -> NodeResult<Payment> {
let mut client: node::ClnClient = self.get_node_client().await?;
let request = cln::KeysendRequest {
destination: hex::decode(node_id)?,
amount_msat: Some(cln::Amount { msat: amount_msat }),
label: Some(format!(
label: label.or(Some(format!(
"breez-{}",
SystemTime::now().duration_since(UNIX_EPOCH)?.as_millis()
)),
))),
extratlvs: extra_tlvs.map(|tlvs| cln::TlvStream {
entries: tlvs
.into_iter()
Expand Down Expand Up @@ -1997,7 +2003,7 @@ impl TryFrom<cln::ListpaysPays> for Payment {
details: PaymentDetails::Ln {
data: LnPaymentDetails {
payment_hash: hex::encode(payment.payment_hash),
label: "".to_string(),
label: payment.label.unwrap_or_default(),
destination_pubkey: payment.destination.map(hex::encode).unwrap_or_default(),
payment_preimage: payment.preimage.map(hex::encode).unwrap_or_default(),
keysend: payment.bolt11.is_none(),
Expand Down
Loading

0 comments on commit 068d4a8

Please sign in to comment.