diff --git a/crates/cdk-lnd/src/error.rs b/crates/cdk-lnd/src/error.rs index 87a8174f..02f178e1 100644 --- a/crates/cdk-lnd/src/error.rs +++ b/crates/cdk-lnd/src/error.rs @@ -11,9 +11,12 @@ pub enum Error { /// Unknown invoice #[error("Unknown invoice")] UnknownInvoice, - /// Connection Error + /// Connection error #[error("LND connection error")] Connection, + /// Payment failed + #[error("LND payment failed")] + PaymentFailed, } impl From for cdk::cdk_lightning::Error { diff --git a/crates/cdk-lnd/src/lib.rs b/crates/cdk-lnd/src/lib.rs index aad9a962..2f1caad3 100644 --- a/crates/cdk-lnd/src/lib.rs +++ b/crates/cdk-lnd/src/lib.rs @@ -189,19 +189,27 @@ impl MintLightning for Lnd { .lightning() .send_payment_sync(fedimint_tonic_lnd::tonic::Request::new(pay_req)) .await - .unwrap() + .map_err(|_| Error::PaymentFailed)? .into_inner(); - let total_spent = payment_response + let total_amount = payment_response .payment_route - .map_or(0, |route| route.total_fees_msat / MSAT_IN_SAT as i64) + .map_or(0, |route| route.total_amt_msat / MSAT_IN_SAT as i64) as u64; + let (status, payment_preimage) = match total_amount == 0 { + true => (MeltQuoteState::Unpaid, None), + false => ( + MeltQuoteState::Paid, + Some(hex::encode(payment_response.payment_preimage)), + ), + }; + Ok(PayInvoiceResponse { payment_hash: hex::encode(payment_response.payment_hash), - payment_preimage: Some(hex::encode(payment_response.payment_preimage)), - status: MeltQuoteState::Paid, - total_spent: total_spent.into(), + payment_preimage, + status, + total_spent: total_amount.into(), unit: CurrencyUnit::Sat, }) }