From 01afaa6227af40d89eb2b9cb39d01e219566e7b7 Mon Sep 17 00:00:00 2001 From: ngutech21 Date: Thu, 7 Dec 2023 16:16:14 +0100 Subject: [PATCH] chore: get total_fees from lnd --- moksha-core/src/primitives.rs | 1 + moksha-mint/src/lightning/alby.rs | 1 + moksha-mint/src/lightning/lnbits.rs | 10 ++++++++-- moksha-mint/src/lightning/mod.rs | 10 ++++++++-- moksha-mint/src/mint.rs | 9 +++++---- moksha-mint/src/model.rs | 2 ++ moksha-mint/src/server.rs | 7 ++++++- 7 files changed, 31 insertions(+), 9 deletions(-) diff --git a/moksha-core/src/primitives.rs b/moksha-core/src/primitives.rs index f85f3e52..b1dc20ab 100644 --- a/moksha-core/src/primitives.rs +++ b/moksha-core/src/primitives.rs @@ -179,6 +179,7 @@ pub struct PostMeltQuoteBolt11Response { pub struct PostMeltBolt11Request { pub quote: String, pub inputs: Proofs, + pub outputs: Vec, } #[derive(Deserialize, Serialize, Debug, Clone)] diff --git a/moksha-mint/src/lightning/alby.rs b/moksha-mint/src/lightning/alby.rs index 555e8533..f86c2cac 100644 --- a/moksha-mint/src/lightning/alby.rs +++ b/moksha-mint/src/lightning/alby.rs @@ -116,6 +116,7 @@ impl AlbyClient { .as_str() .expect("payment_hash is empty") .to_owned(), + total_fees: 0, // FIXME return fees for alby }) } diff --git a/moksha-mint/src/lightning/lnbits.rs b/moksha-mint/src/lightning/lnbits.rs index 25c16aba..d1a5c73a 100644 --- a/moksha-mint/src/lightning/lnbits.rs +++ b/moksha-mint/src/lightning/lnbits.rs @@ -123,8 +123,14 @@ impl LNBitsClient { &serde_json::to_string(&serde_json::json!({ "out": true, "bolt11": bolt11 }))?, ) .await?; - - Ok(serde_json::from_str(&body)?) + let payment_hash = serde_json::from_str::(&body)?["payment_hash"] + .as_str() + .expect("payment_hash is empty") + .to_owned(); + Ok(PayInvoiceResult { + payment_hash, + total_fees: 0, + }) } pub async fn is_invoice_paid(&self, payment_hash: &str) -> Result { diff --git a/moksha-mint/src/lightning/mod.rs b/moksha-mint/src/lightning/mod.rs index 4a066aa8..eb5e2277 100644 --- a/moksha-mint/src/lightning/mod.rs +++ b/moksha-mint/src/lightning/mod.rs @@ -302,6 +302,7 @@ impl Lightning for StrikeLightning { Ok(PayInvoiceResult { payment_hash: hex::encode(payment_hash), + total_fees: 0, // FIXME return fees for strike }) } } @@ -442,7 +443,7 @@ impl Lightning for LndLightning { payment_request, ..Default::default() }; - let payment = self + let payment_response = self .client_lock() .await .expect("failed to lock client") //FIXME map error @@ -451,8 +452,13 @@ impl Lightning for LndLightning { .expect("failed to pay invoice") .into_inner(); + let total_fees = payment_response + .payment_route + .map_or(0, |route| route.total_fees_msat) as u64; + Ok(PayInvoiceResult { - payment_hash: hex::encode(payment.payment_hash), + payment_hash: hex::encode(payment_response.payment_hash), + total_fees, }) } } diff --git a/moksha-mint/src/mint.rs b/moksha-mint/src/mint.rs index 186c155c..544f53bc 100644 --- a/moksha-mint/src/mint.rs +++ b/moksha-mint/src/mint.rs @@ -191,23 +191,23 @@ impl Mint { .amount_milli_satoshis() .expect("Invoice amount is missing"); - if amount_msat < (proofs_amount / 1000) { + if amount_msat < (proofs_amount / 1_000) { return Err(MokshaMintError::InvoiceAmountTooLow(format!( "Invoice amount is too low: {amount_msat}", ))); } - self.db.add_used_proofs(proofs)?; // TODO check invoice let result = self.lightning.pay_invoice(payment_request).await?; + self.db.add_used_proofs(proofs)?; let _remaining_amount = (amount_msat - (proofs_amount / 1000)) * 1000; // FIXME check if output amount matches remaining_amount - let output = self.create_blinded_signatures(blinded_messages, keyset)?; + let change = self.create_blinded_signatures(blinded_messages, keyset)?; - Ok((true, result.payment_hash, output)) + Ok((true, result.payment_hash, change)) } pub fn check_used_proofs(&self, proofs: &Proofs) -> Result<(), MokshaMintError> { @@ -447,6 +447,7 @@ mod tests { lightning.expect_pay_invoice().returning(|_| { Ok(PayInvoiceResult { payment_hash: "hash".to_string(), + total_fees: 0, }) .map_err(|_err: LightningError| MokshaMintError::InvoiceNotFound("".to_string())) }); diff --git a/moksha-mint/src/model.rs b/moksha-mint/src/model.rs index c8c1208a..9ece6572 100644 --- a/moksha-mint/src/model.rs +++ b/moksha-mint/src/model.rs @@ -35,6 +35,8 @@ pub struct CreateInvoiceResult { #[derive(Debug, Serialize, Deserialize)] pub struct PayInvoiceResult { pub payment_hash: String, + /// total fees in msat + pub total_fees: u64, } #[derive(Debug, Serialize, Deserialize)] diff --git a/moksha-mint/src/server.rs b/moksha-mint/src/server.rs index 6b6c674a..9903d56f 100644 --- a/moksha-mint/src/server.rs +++ b/moksha-mint/src/server.rs @@ -408,7 +408,12 @@ async fn post_melt_bolt11( payment_request, .. } => { let (paid, payment_preimage, change) = mint - .melt(payment_request, &melt_request.inputs, &[], &mint.keyset) + .melt( + payment_request, + &melt_request.inputs, + &melt_request.outputs, + &mint.keyset, + ) .await?; Ok(Json(PostMeltBolt11Response {