Skip to content

Commit

Permalink
Merge pull request #952 from breez/savage-payment-failure-label
Browse files Browse the repository at this point in the history
Add the payment label to the PaymentFailed event
  • Loading branch information
dangeross authored Apr 29, 2024
2 parents 5f51a18 + cec2202 commit 9806bfb
Show file tree
Hide file tree
Showing 7 changed files with 32 additions and 5 deletions.
1 change: 1 addition & 0 deletions libs/sdk-bindings/src/breez_sdk.udl
Original file line number Diff line number Diff line change
Expand Up @@ -389,6 +389,7 @@ dictionary PaymentFailedData {
string error;
string node_id;
LNInvoice? invoice;
string? label;
};

dictionary BackupFailedData {
Expand Down
14 changes: 11 additions & 3 deletions libs/sdk-core/src/breez_services.rs
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,7 @@ pub struct PaymentFailedData {
pub error: String,
pub node_id: String,
pub invoice: Option<LNInvoice>,
pub label: Option<String>,
}

/// Details of an invoice that has been paid, included as payload in an emitted [BreezEvent]
Expand Down Expand Up @@ -316,13 +317,18 @@ impl BreezServices {
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, req.label)
.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),
req.label,
payment_res,
)
.await?;
Expand All @@ -343,12 +349,12 @@ impl BreezServices {
req.node_id.clone(),
req.amount_msat,
req.extra_tlvs,
req.label,
req.label.clone(),
)
.map_err(Into::into)
.await;
let payment = self
.on_payment_completed(req.node_id, None, payment_res)
.on_payment_completed(req.node_id, None, req.label, payment_res)
.await?;
Ok(SendPaymentResponse { payment })
}
Expand Down Expand Up @@ -1219,6 +1225,7 @@ impl BreezServices {
&self,
node_id: String,
invoice: Option<LNInvoice>,
label: Option<String>,
payment_res: Result<Payment, SendPaymentError>,
) -> Result<Payment, SendPaymentError> {
self.do_sync(payment_res.is_ok()).await?;
Expand All @@ -1242,6 +1249,7 @@ impl BreezServices {
error: e.to_string(),
node_id,
invoice,
label,
},
})
.await?;
Expand Down
1 change: 1 addition & 0 deletions libs/sdk-core/src/bridge_generated.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1870,6 +1870,7 @@ impl support::IntoDart for PaymentFailedData {
self.error.into_into_dart().into_dart(),
self.node_id.into_into_dart().into_dart(),
self.invoice.into_dart(),
self.label.into_dart(),
]
.into_dart()
}
Expand Down
5 changes: 4 additions & 1 deletion libs/sdk-flutter/lib/bridge_generated.dart
Original file line number Diff line number Diff line change
Expand Up @@ -1355,11 +1355,13 @@ class PaymentFailedData {
final String error;
final String nodeId;
final LNInvoice? invoice;
final String? label;

const PaymentFailedData({
required this.error,
required this.nodeId,
this.invoice,
this.label,
});
}

Expand Down Expand Up @@ -3958,11 +3960,12 @@ class BreezSdkCoreImpl implements BreezSdkCore {

PaymentFailedData _wire2api_payment_failed_data(dynamic raw) {
final arr = raw as List<dynamic>;
if (arr.length != 3) throw Exception('unexpected arr length: expect 3 but see ${arr.length}');
if (arr.length != 4) throw Exception('unexpected arr length: expect 4 but see ${arr.length}');
return PaymentFailedData(
error: _wire2api_String(arr[0]),
nodeId: _wire2api_String(arr[1]),
invoice: _wire2api_opt_box_autoadd_ln_invoice(arr[2]),
label: _wire2api_opt_String(arr[3]),
);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2168,10 +2168,12 @@ fun asPaymentFailedData(paymentFailedData: ReadableMap): PaymentFailedData? {
val error = paymentFailedData.getString("error")!!
val nodeId = paymentFailedData.getString("nodeId")!!
val invoice = if (hasNonNullKey(paymentFailedData, "invoice")) paymentFailedData.getMap("invoice")?.let { asLnInvoice(it) } else null
val label = if (hasNonNullKey(paymentFailedData, "label")) paymentFailedData.getString("label") else null
return PaymentFailedData(
error,
nodeId,
invoice,
label,
)
}

Expand All @@ -2180,6 +2182,7 @@ fun readableMapOf(paymentFailedData: PaymentFailedData): ReadableMap {
"error" to paymentFailedData.error,
"nodeId" to paymentFailedData.nodeId,
"invoice" to paymentFailedData.invoice?.let { readableMapOf(it) },
"label" to paymentFailedData.label,
)
}

Expand Down
12 changes: 11 additions & 1 deletion libs/sdk-react-native/ios/BreezSDKMapper.swift
Original file line number Diff line number Diff line change
Expand Up @@ -2413,10 +2413,19 @@ enum BreezSDKMapper {
invoice = try asLnInvoice(lnInvoice: invoiceTmp)
}

var label: String?
if hasNonNilKey(data: paymentFailedData, key: "label") {
guard let labelTmp = paymentFailedData["label"] as? String else {
throw SdkError.Generic(message: errUnexpectedValue(fieldName: "label"))
}
label = labelTmp
}

return PaymentFailedData(
error: error,
nodeId: nodeId,
invoice: invoice
invoice: invoice,
label: label
)
}

Expand All @@ -2425,6 +2434,7 @@ enum BreezSDKMapper {
"error": paymentFailedData.error,
"nodeId": paymentFailedData.nodeId,
"invoice": paymentFailedData.invoice == nil ? nil : dictionaryOf(lnInvoice: paymentFailedData.invoice!),
"label": paymentFailedData.label == nil ? nil : paymentFailedData.label,
]
}

Expand Down
1 change: 1 addition & 0 deletions libs/sdk-react-native/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -341,6 +341,7 @@ export type PaymentFailedData = {
error: string
nodeId: string
invoice?: LnInvoice
label?: string
}

export type PrepareOnchainPaymentRequest = {
Expand Down

0 comments on commit 9806bfb

Please sign in to comment.