Skip to content

Commit

Permalink
fix: return correct fees
Browse files Browse the repository at this point in the history
  • Loading branch information
ngutech21 committed Jan 10, 2024
1 parent 4dcf9c2 commit 1ef15ac
Show file tree
Hide file tree
Showing 6 changed files with 114 additions and 10 deletions.
8 changes: 8 additions & 0 deletions moksha-core/src/amount.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,14 @@ impl SplitAmount {
.map(|_| generate_random_string())
.collect::<Vec<String>>()
}

pub fn len(&self) -> usize {
self.0.len()
}

pub fn is_empty(&self) -> bool {
self.0.is_empty()
}
}

impl From<u64> for Amount {
Expand Down
10 changes: 10 additions & 0 deletions moksha-core/src/blind.rs
Original file line number Diff line number Diff line change
Expand Up @@ -106,4 +106,14 @@ mod tests {
assert!(result.is_ok());
assert!(result.unwrap().is_empty());
}

#[test]
fn test_serialize() -> anyhow::Result<()> {
let result = BlindedMessage::blank(4000.into(), "00ffd48b8f5ecf80".to_owned())?;
for (blinded_message, _, _) in result {
let out = serde_json::to_string(&blinded_message)?;
assert!(!out.is_empty());
}
Ok(())
}
}
2 changes: 1 addition & 1 deletion moksha-core/src/dhke.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ use crate::error::MokshaCoreError;
use bitcoin_hashes::{sha256, Hash};
use secp256k1::{All, PublicKey, Scalar, Secp256k1, SecretKey};

#[derive(Clone)]
#[derive(Clone, Debug)]
pub struct Dhke {
secp: Secp256k1<All>,
}
Expand Down
62 changes: 62 additions & 0 deletions moksha-mint/src/fixtures/blinded_messages_blank_4000.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
[
{
"amount": 1,
"B_": "036a08d1174c0b0405cf21e2aa31de36085dcdd527f38aa701728ac36ece40099e",
"id": "00ffd48b8f5ecf80"
},
{
"amount": 1,
"B_": "024153ab08aaca0622453504076fbac48d7db691582a2b1ae41486038ed96809ab",
"id": "00ffd48b8f5ecf80"
},
{
"amount": 1,
"B_": "02f7c3636710cbad8702778067cc376368884878c12093272d8567aebe7ef681bd",
"id": "00ffd48b8f5ecf80"
},
{
"amount": 1,
"B_": "02100e486e2742356d5d8f19391416f2eea9d4894f157743ea1c8300bc48eb8a65",
"id": "00ffd48b8f5ecf80"
},
{
"amount": 1,
"B_": "03a6e88ff14bc5db30ed8e7b5c076299cc17db94aef0455d04f4bd8c7982759722",
"id": "00ffd48b8f5ecf80"
},
{
"amount": 1,
"B_": "030ab2c23379d807f22c926a4ab09aabd0a1db55bd96cacf9e229530578eff6132",
"id": "00ffd48b8f5ecf80"
},
{
"amount": 1,
"B_": "031e66678307276ed027f172b952107579f6e9c7b22cb685c4347d5a4a9036a537",
"id": "00ffd48b8f5ecf80"
},
{
"amount": 1,
"B_": "02b267d1806d7ec893db2066115823e0018e5a7a3452ab7a06f95904567d484211",
"id": "00ffd48b8f5ecf80"
},
{
"amount": 1,
"B_": "021be658384127f40360f2370e3bd3d99440a87bebdbb2c2e144d9bac67a6321f4",
"id": "00ffd48b8f5ecf80"
},
{
"amount": 1,
"B_": "02b6eb0a2efff85f90781eebd3319d917c3c36bf4e984c81b3bf680fcdc1afc83b",
"id": "00ffd48b8f5ecf80"
},
{
"amount": 1,
"B_": "0202311c8541f880514d2fa608a2de6f579fec007fc0b2c92aa52e524fb612ec20",
"id": "00ffd48b8f5ecf80"
},
{
"amount": 1,
"B_": "03dfa943f4287e83e270aad5d5cbd91c1ed2e28aaa124d67501f5f74554b6fddac",
"id": "00ffd48b8f5ecf80"
}
]
33 changes: 24 additions & 9 deletions moksha-mint/src/mint.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use std::{collections::HashSet, sync::Arc};
use std::{collections::HashSet, sync::Arc, vec};

use moksha_core::{
amount::Amount,
blind::{BlindedMessage, BlindedSignature, TotalAmount},
dhke::Dhke,
keyset::MintKeyset,
Expand Down Expand Up @@ -147,6 +148,7 @@ impl Mint {
pub async fn melt(
&self,
payment_request: String,
fee_reserve: u64,
proofs: &Proofs,
blinded_messages: &[BlindedMessage],
keyset: &MintKeyset,
Expand Down Expand Up @@ -178,10 +180,22 @@ impl Mint {
let result = self.lightning.pay_invoice(payment_request).await?;
self.db.add_used_proofs(proofs).await?;

let _remaining_amount = (amount_msat - (proofs_amount / 1000)) * 1000;

// FIXME check if output amount matches remaining_amount
let change = self.create_blinded_signatures(blinded_messages, keyset)?;
let change = if fee_reserve > 0 {
let return_fees = Amount(fee_reserve - result.total_fees).split();

let out: Vec<_> = blinded_messages[0..return_fees.len()]
.iter()
.zip(return_fees.into_iter())
.map(|(message, fee)| BlindedMessage {
amount: fee,
..message.clone()
})
.collect();

self.create_blinded_signatures(&out, keyset)?
} else {
vec![]
};

Ok((true, result.payment_hash, change))
}
Expand Down Expand Up @@ -429,7 +443,7 @@ mod tests {
lightning.expect_pay_invoice().returning(|_| {
Ok(PayInvoiceResult {
payment_hash: "hash".to_string(),
total_fees: 0,
total_fees: 2,
})
.map_err(|_err: LightningError| MokshaMintError::InvoiceNotFound("".to_string()))
});
Expand All @@ -445,14 +459,15 @@ mod tests {

let tokens = create_token_from_fixture("token_60.cashu".to_string())?;
let invoice = "some invoice".to_string();
let change = create_blinded_msgs_from_fixture("blinded_messages_40.json".to_string())?;
let change =
create_blinded_msgs_from_fixture("blinded_messages_blank_4000.json".to_string())?;

let (paid, _payment_hash, change) = mint
.melt(invoice, &tokens.proofs(), &change, &mint.keyset_legacy)
.melt(invoice, 4, &tokens.proofs(), &change, &mint.keyset_legacy)
.await?;

assert!(paid);
assert!(change.total_amount() == 40);
assert!(change.total_amount() == 2);
Ok(())
}

Expand Down
9 changes: 9 additions & 0 deletions moksha-mint/src/server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ use chrono::{Duration, Utc};
use moksha_core::keyset::{generate_hash, Keysets, V1Keyset, V1Keysets};
use moksha_core::proof::Proofs;
use moksha_core::proof::{P2SHScript, Proof};
use tracing_subscriber::EnvFilter;
use utoipa_swagger_ui::SwaggerUi;
use uuid::Uuid;

Expand Down Expand Up @@ -47,6 +48,7 @@ use utoipa::OpenApi;
pub async fn run_server(mint: Mint) -> anyhow::Result<()> {
tracing_subscriber::registry()
.with(tracing_subscriber::fmt::layer())
.with(EnvFilter::from_default_env())
.init();

if let Some(ref buildtime) = mint.config.build.build_time {
Expand Down Expand Up @@ -237,6 +239,7 @@ async fn post_legacy_melt(
let (paid, preimage, change) = mint
.melt(
melt_request.pr,
0, // FIXME set correct fee reserve for legacy api
&melt_request.proofs,
&melt_request.outputs,
&mint.keyset_legacy,
Expand Down Expand Up @@ -550,9 +553,15 @@ async fn post_melt_bolt11(
.get_bolt11_melt_quote(&Uuid::from_str(melt_request.quote.as_str())?)
.await?;

println!(
"post_melt_bolt11 fee_reserve >>>>>>>>>>>>>> : {:#?}",
&quote
);

let (paid, payment_preimage, change) = mint
.melt(
quote.payment_request.to_owned(),
quote.fee_reserve,
&melt_request.inputs,
&melt_request.outputs,
&mint.keyset,
Expand Down

0 comments on commit 1ef15ac

Please sign in to comment.