Skip to content

Commit

Permalink
fix: update quote after melt
Browse files Browse the repository at this point in the history
  • Loading branch information
ngutech21 committed Dec 9, 2023
1 parent a82e59d commit e0bd115
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 39 deletions.
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

13 changes: 4 additions & 9 deletions moksha-mint/src/database.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,10 @@
use std::{collections::HashMap, sync::Arc};

use moksha_core::proof::Proofs;
use moksha_core::{primitives::Quote, proof::Proofs};
use rocksdb::DB;
use serde::{de::DeserializeOwned, Serialize};

use crate::{
error::MokshaMintError,
model::{Invoice, Quote},
};
use crate::{error::MokshaMintError, model::Invoice};
#[cfg(test)]
use mockall::automock;

Expand Down Expand Up @@ -183,14 +180,12 @@ mod tests {

use moksha_core::{
dhke,
primitives::Quote,
proof::{Proof, Proofs},
};
use uuid::Uuid;

use crate::{
database::Database,
model::{Invoice, Quote},
};
use crate::{database::Database, model::Invoice};

#[test]
fn test_write_proofs() -> anyhow::Result<()> {
Expand Down
16 changes: 0 additions & 16 deletions moksha-mint/src/model.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,19 +48,3 @@ pub struct CreateInvoiceParams {
pub webhook: Option<String>,
pub internal: Option<bool>,
}

#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
pub enum Quote {
Bolt11Mint {
quote_id: Uuid,
payment_request: String,
expiry: u64,
},
Bolt11Melt {
quote_id: Uuid,
amount: u64,
fee_reserve: u64,
payment_request: String,
expiry: u64,
},
}
39 changes: 25 additions & 14 deletions moksha-mint/src/server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,14 @@ use moksha_core::keyset::{generate_hash, Keysets, V1Keysets};
use uuid::Uuid;

use crate::mint::Mint;
use crate::model::{GetMintQuery, PostMintQuery, Quote};
use crate::model::{GetMintQuery, PostMintQuery};
use moksha_core::primitives::{
CheckFeesRequest, CheckFeesResponse, CurrencyUnit, KeyResponse, KeysResponse, MintInfoNut,
MintInfoResponse, MintLegacyInfoResponse, PaymentMethod, PaymentRequest, PostMeltBolt11Request,
PostMeltBolt11Response, PostMeltQuoteBolt11Request, PostMeltQuoteBolt11Response,
PostMeltRequest, PostMeltResponse, PostMintBolt11Request, PostMintBolt11Response,
PostMintQuoteBolt11Request, PostMintQuoteBolt11Response, PostMintRequest, PostMintResponse,
PostSplitRequest, PostSplitResponse, PostSwapRequest, PostSwapResponse,
PostSplitRequest, PostSplitResponse, PostSwapRequest, PostSwapResponse, Quote,
};
use secp256k1::PublicKey;

Expand Down Expand Up @@ -376,25 +376,20 @@ async fn post_melt_quote_bolt11(
info!("fee_reserve: {}", fee_reserve);

let amount_sat = amount / 1_000;
// Store quote in db
let key = Uuid::new_v4();
let quote = Quote::Bolt11Melt {
quote_id: key,
amount: amount_sat,
fee_reserve,
expiry: invoice.expiry_time().as_secs(), // FIXME check if this is correct
payment_request: melt_request.request.clone(),
paid: false,
};
mint.db.add_quote(key.to_string(), quote)?;
mint.db.add_quote(key.to_string(), quote.clone())?; // FIXME get rid of clone

// TODO implement into for Quote
Ok(Json(PostMeltQuoteBolt11Response {
amount: amount_sat,
fee_reserve,
quote: key.to_string(),
paid: false,
expiry: invoice.expiry_time().as_secs(), // FIXME check if this is correct
}))
Ok(Json(quote.try_into().map_err(|_| {
crate::error::MokshaMintError::InvalidQuote("".to_string())
})?))
}

async fn post_melt_bolt11(
Expand All @@ -405,16 +400,32 @@ async fn post_melt_bolt11(

match quote {
Quote::Bolt11Melt {
payment_request, ..
quote_id,
payment_request,
amount,
expiry,
fee_reserve,
..
} => {
let (paid, payment_preimage, change) = mint
.melt(
payment_request,
payment_request.to_owned(),
&melt_request.inputs,
&melt_request.outputs,
&mint.keyset,
)
.await?;
let updated_quote = Quote::Bolt11Melt {
quote_id,
amount,
fee_reserve,
expiry,
payment_request,
paid,
};
// FIXME simplify code

mint.db.add_quote(quote_id.to_string(), updated_quote)?;

Ok(Json(PostMeltBolt11Response {
paid,
Expand Down

0 comments on commit e0bd115

Please sign in to comment.