Skip to content

Commit

Permalink
chore: get total_fees from lnd
Browse files Browse the repository at this point in the history
  • Loading branch information
ngutech21 committed Dec 7, 2023
1 parent 072687b commit 01afaa6
Show file tree
Hide file tree
Showing 7 changed files with 31 additions and 9 deletions.
1 change: 1 addition & 0 deletions moksha-core/src/primitives.rs
Original file line number Diff line number Diff line change
Expand Up @@ -179,6 +179,7 @@ pub struct PostMeltQuoteBolt11Response {
pub struct PostMeltBolt11Request {
pub quote: String,
pub inputs: Proofs,
pub outputs: Vec<BlindedMessage>,
}

#[derive(Deserialize, Serialize, Debug, Clone)]
Expand Down
1 change: 1 addition & 0 deletions moksha-mint/src/lightning/alby.rs
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,7 @@ impl AlbyClient {
.as_str()
.expect("payment_hash is empty")
.to_owned(),
total_fees: 0, // FIXME return fees for alby
})
}

Expand Down
10 changes: 8 additions & 2 deletions moksha-mint/src/lightning/lnbits.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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::<serde_json::Value>(&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<bool, LightningError> {
Expand Down
10 changes: 8 additions & 2 deletions moksha-mint/src/lightning/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -302,6 +302,7 @@ impl Lightning for StrikeLightning {

Ok(PayInvoiceResult {
payment_hash: hex::encode(payment_hash),
total_fees: 0, // FIXME return fees for strike
})
}
}
Expand Down Expand Up @@ -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
Expand All @@ -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,
})
}
}
Expand Down
9 changes: 5 additions & 4 deletions moksha-mint/src/mint.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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> {
Expand Down Expand Up @@ -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()))
});
Expand Down
2 changes: 2 additions & 0 deletions moksha-mint/src/model.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)]
Expand Down
7 changes: 6 additions & 1 deletion moksha-mint/src/server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down

0 comments on commit 01afaa6

Please sign in to comment.