diff --git a/crates/cdk-integration-tests/src/init_fake_wallet.rs b/crates/cdk-integration-tests/src/init_fake_wallet.rs index 46a55216..3f1f5e22 100644 --- a/crates/cdk-integration-tests/src/init_fake_wallet.rs +++ b/crates/cdk-integration-tests/src/init_fake_wallet.rs @@ -30,7 +30,7 @@ where let fee_reserve = FeeReserve { min_fee_reserve: 1.into(), - percent_fee_reserve: 1.0, + percent_fee_reserve: 0.0, }; let fake_wallet = FakeWallet::new(fee_reserve, HashMap::default(), HashSet::default(), 0); @@ -48,7 +48,7 @@ where let fee_reserve = FeeReserve { min_fee_reserve: 1.into(), - percent_fee_reserve: 1.0, + percent_fee_reserve: 0.0, }; let fake_wallet = FakeWallet::new(fee_reserve, HashMap::default(), HashSet::default(), 0); diff --git a/crates/cdk-integration-tests/tests/fake_wallet.rs b/crates/cdk-integration-tests/tests/fake_wallet.rs index 422a077c..b22cb721 100644 --- a/crates/cdk-integration-tests/tests/fake_wallet.rs +++ b/crates/cdk-integration-tests/tests/fake_wallet.rs @@ -700,35 +700,123 @@ async fn test_fake_mint_multiple_unit_swap() -> Result<()> { } } - // let mut sat_outputs = pre_mint.blinded_messages(); + Ok(()) +} - // let mut usd_outputs = usd_pre_mint.blinded_messages(); +#[tokio::test(flavor = "multi_thread", worker_threads = 1)] +async fn test_fake_mint_multiple_unit_melt() -> Result<()> { + let wallet = Wallet::new( + MINT_URL, + CurrencyUnit::Sat, + Arc::new(WalletMemoryDatabase::default()), + &Mnemonic::generate(12)?.to_seed_normalized(""), + None, + )?; - // sat_outputs.append(&mut usd_outputs); + let mint_quote = wallet.mint_quote(100.into(), None).await.unwrap(); - // let mut mint_request = MintBolt11Request { - // quote: mint_quote.id, - // outputs: sat_outputs, - // signature: None, - // }; + wait_for_mint_to_be_paid(&wallet, &mint_quote.id, 60).await?; - // if let Some(secret_key) = quote_info.secret_key { - // mint_request.sign(secret_key)?; - // } + let proofs = wallet + .mint(&mint_quote.id, SplitTarget::None, None) + .await + .unwrap(); - // let response = http_client.post_mint(mint_request.clone()).await; + println!("Minted sat"); - // match response { - // Err(err) => match err { - // cdk::Error::UnsupportedUnit => (), - // err => { - // bail!("Wrong mint error returned: {}", err.to_string()); - // } - // }, - // Ok(_) => { - // bail!("Should not have allowed to mint with multiple units"); - // } - // } + let wallet_usd = Wallet::new( + MINT_URL, + CurrencyUnit::Usd, + Arc::new(WalletMemoryDatabase::default()), + &Mnemonic::generate(12)?.to_seed_normalized(""), + None, + )?; + + let mint_quote = wallet_usd.mint_quote(100.into(), None).await.unwrap(); + println!("Minted quote usd"); + + wait_for_mint_to_be_paid(&wallet_usd, &mint_quote.id, 60).await?; + + let usd_proofs = wallet_usd + .mint(&mint_quote.id, SplitTarget::None, None) + .await + .unwrap(); + + { + let inputs: Proofs = vec![ + proofs.first().expect("There is a proof").clone(), + usd_proofs.first().expect("There is a proof").clone(), + ]; + + let input_amount: u64 = inputs.total_amount()?.into(); + let invoice = create_fake_invoice((input_amount - 1) * 1000, "".to_string()); + let melt_quote = wallet.melt_quote(invoice.to_string(), None).await?; + + let melt_request = MeltBolt11Request { + quote: melt_quote.id, + inputs, + outputs: None, + }; + + let http_client = HttpClient::new(MINT_URL.parse()?); + let response = http_client.post_melt(melt_request.clone()).await; + + match response { + Err(err) => match err { + cdk::Error::UnsupportedUnit => (), + err => { + bail!("Wrong mint error returned: {}", err.to_string()); + } + }, + Ok(_) => { + bail!("Should not have allowed to melt with multiple units"); + } + } + } + + { + let inputs: Proofs = vec![proofs.first().expect("There is a proof").clone()]; + + let input_amount: u64 = inputs.total_amount()?.into(); + + let invoice = create_fake_invoice((input_amount - 1) * 1000, "".to_string()); + let active_keyset_id = wallet.get_active_mint_keyset().await?.id; + let usd_active_keyset_id = wallet_usd.get_active_mint_keyset().await?.id; + + let usd_pre_mint = PreMintSecrets::random( + usd_active_keyset_id, + inputs.total_amount()? + 100.into(), + &SplitTarget::None, + )?; + let pre_mint = PreMintSecrets::random(active_keyset_id, 100.into(), &SplitTarget::None)?; + + let mut usd_outputs = usd_pre_mint.blinded_messages(); + let mut sat_outputs = pre_mint.blinded_messages(); + + usd_outputs.append(&mut sat_outputs); + let quote = wallet.melt_quote(invoice.to_string(), None).await?; + + let melt_request = MeltBolt11Request { + quote: quote.id, + inputs, + outputs: Some(usd_outputs), + }; + + let http_client = HttpClient::new(MINT_URL.parse()?); + let response = http_client.post_melt(melt_request.clone()).await; + + match response { + Err(err) => match err { + cdk::Error::UnsupportedUnit => (), + err => { + bail!("Wrong mint error returned: {}", err.to_string()); + } + }, + Ok(_) => { + bail!("Should not have allowed to melt with multiple units"); + } + } + } Ok(()) } diff --git a/crates/cdk/src/mint/melt.rs b/crates/cdk/src/mint/melt.rs index e6eb86e9..7cd935f6 100644 --- a/crates/cdk/src/mint/melt.rs +++ b/crates/cdk/src/mint/melt.rs @@ -366,7 +366,7 @@ impl Mint { // Check that all input and output proofs are the same unit if keyset_units.len().gt(&1) { - return Err(Error::MultipleUnits); + return Err(Error::UnsupportedUnit); } tracing::debug!("Verified melt quote: {}", melt_request.quote); diff --git a/misc/fake_itests.sh b/misc/fake_itests.sh index 2df02f2a..deba74fd 100755 --- a/misc/fake_itests.sh +++ b/misc/fake_itests.sh @@ -76,7 +76,7 @@ done # Run cargo test -cargo test -p cdk-integration-tests --test fake_wallet test_fake_mint_multiple_unit_swap +cargo test -p cdk-integration-tests --test fake_wallet # cargo test -p cdk-integration-tests --test mint # Capture the exit status of cargo test