Merge pull request #740 from KnowWhoami/test/remove_anyhow

test: use `expect` / `unwrap` instead of  `anyhow`
This commit is contained in:
thesimplekid
2025-05-01 12:43:05 +01:00
committed by GitHub
10 changed files with 724 additions and 569 deletions

File diff suppressed because it is too large Load Diff

View File

@@ -15,7 +15,6 @@ use std::sync::Arc;
use std::time::Duration;
use std::{char, env};
use anyhow::{bail, Result};
use bip39::Mnemonic;
use cashu::{MeltBolt11Request, PreMintSecrets};
use cdk::amount::{Amount, SplitTarget};
@@ -78,14 +77,15 @@ async fn get_notification<T: StreamExt<Item = Result<Message, E>> + Unpin, E: De
/// This ensures the entire mint-melt flow works correctly and that
/// WebSocket notifications are properly sent at each state transition.
#[tokio::test(flavor = "multi_thread", worker_threads = 1)]
async fn test_happy_mint_melt_round_trip() -> Result<()> {
async fn test_happy_mint_melt_round_trip() {
let wallet = Wallet::new(
&get_mint_url_from_env(),
CurrencyUnit::Sat,
Arc::new(memory::empty().await?),
&Mnemonic::generate(12)?.to_seed_normalized(""),
Arc::new(memory::empty().await.unwrap()),
&Mnemonic::generate(12).unwrap().to_seed_normalized(""),
None,
)?;
)
.expect("failed to create new wallet");
let (ws_stream, _) = connect_async(format!(
"{}/v1/ws",
@@ -95,22 +95,23 @@ async fn test_happy_mint_melt_round_trip() -> Result<()> {
.expect("Failed to connect");
let (mut write, mut reader) = ws_stream.split();
let mint_quote = wallet.mint_quote(100.into(), None).await?;
let mint_quote = wallet.mint_quote(100.into(), None).await.unwrap();
let invoice = Bolt11Invoice::from_str(&mint_quote.request)?;
let invoice = Bolt11Invoice::from_str(&mint_quote.request).unwrap();
pay_if_regtest(&invoice).await.unwrap();
let proofs = wallet
.mint(&mint_quote.id, SplitTarget::default(), None)
.await?;
.await
.unwrap();
let mint_amount = proofs.total_amount()?;
let mint_amount = proofs.total_amount().unwrap();
assert!(mint_amount == 100.into());
let invoice = create_invoice_for_env(Some(50)).await.unwrap();
let melt = wallet.melt_quote(invoice, None).await?;
let melt = wallet.melt_quote(invoice, None).await.unwrap();
write
.send(Message::Text(
@@ -126,10 +127,12 @@ async fn test_happy_mint_melt_round_trip() -> Result<()> {
"subId": "test-sub",
}
}))?
}))
.unwrap()
.into(),
))
.await?;
.await
.unwrap();
assert_eq!(
reader
@@ -179,8 +182,6 @@ async fn test_happy_mint_melt_round_trip() -> Result<()> {
assert_eq!(payload.amount, 50.into());
assert_eq!(payload.quote.to_string(), melt.id);
assert_eq!(payload.state, MeltQuoteState::Paid);
Ok(())
}
/// Tests basic minting functionality with payment verification
@@ -194,35 +195,37 @@ async fn test_happy_mint_melt_round_trip() -> Result<()> {
///
/// This ensures the basic minting flow works correctly from quote to token issuance.
#[tokio::test(flavor = "multi_thread", worker_threads = 1)]
async fn test_happy_mint() -> Result<()> {
async fn test_happy_mint() {
let wallet = Wallet::new(
&get_mint_url_from_env(),
CurrencyUnit::Sat,
Arc::new(memory::empty().await?),
&Mnemonic::generate(12)?.to_seed_normalized(""),
Arc::new(memory::empty().await.unwrap()),
&Mnemonic::generate(12).unwrap().to_seed_normalized(""),
None,
)?;
)
.expect("failed to create new wallet");
let mint_amount = Amount::from(100);
let mint_quote = wallet.mint_quote(mint_amount, None).await?;
let mint_quote = wallet.mint_quote(mint_amount, None).await.unwrap();
assert_eq!(mint_quote.amount, mint_amount);
let invoice = Bolt11Invoice::from_str(&mint_quote.request)?;
pay_if_regtest(&invoice).await?;
let invoice = Bolt11Invoice::from_str(&mint_quote.request).unwrap();
pay_if_regtest(&invoice).await.unwrap();
wait_for_mint_to_be_paid(&wallet, &mint_quote.id, 60).await?;
wait_for_mint_to_be_paid(&wallet, &mint_quote.id, 60)
.await
.unwrap();
let proofs = wallet
.mint(&mint_quote.id, SplitTarget::default(), None)
.await?;
.await
.unwrap();
let mint_amount = proofs.total_amount()?;
let mint_amount = proofs.total_amount().unwrap();
assert!(mint_amount == 100.into());
Ok(())
}
/// Tests wallet restoration and proof state verification
@@ -240,66 +243,70 @@ async fn test_happy_mint() -> Result<()> {
/// This ensures wallet restoration works correctly and that
/// the mint properly tracks spent proofs across wallet instances.
#[tokio::test(flavor = "multi_thread", worker_threads = 1)]
async fn test_restore() -> Result<()> {
let seed = Mnemonic::generate(12)?.to_seed_normalized("");
async fn test_restore() {
let seed = Mnemonic::generate(12).unwrap().to_seed_normalized("");
let wallet = Wallet::new(
&get_mint_url_from_env(),
CurrencyUnit::Sat,
Arc::new(memory::empty().await?),
Arc::new(memory::empty().await.unwrap()),
&seed,
None,
)?;
)
.expect("failed to create new wallet");
let mint_quote = wallet.mint_quote(100.into(), None).await?;
let mint_quote = wallet.mint_quote(100.into(), None).await.unwrap();
let invoice = Bolt11Invoice::from_str(&mint_quote.request)?;
pay_if_regtest(&invoice).await?;
let invoice = Bolt11Invoice::from_str(&mint_quote.request).unwrap();
pay_if_regtest(&invoice).await.unwrap();
wait_for_mint_to_be_paid(&wallet, &mint_quote.id, 60).await?;
wait_for_mint_to_be_paid(&wallet, &mint_quote.id, 60)
.await
.unwrap();
let _mint_amount = wallet
.mint(&mint_quote.id, SplitTarget::default(), None)
.await?;
.await
.unwrap();
assert_eq!(wallet.total_balance().await?, 100.into());
assert_eq!(wallet.total_balance().await.unwrap(), 100.into());
let wallet_2 = Wallet::new(
&get_mint_url_from_env(),
CurrencyUnit::Sat,
Arc::new(memory::empty().await?),
Arc::new(memory::empty().await.unwrap()),
&seed,
None,
)?;
)
.expect("failed to create new wallet");
assert_eq!(wallet_2.total_balance().await?, 0.into());
assert_eq!(wallet_2.total_balance().await.unwrap(), 0.into());
let restored = wallet_2.restore().await?;
let proofs = wallet_2.get_unspent_proofs().await?;
let restored = wallet_2.restore().await.unwrap();
let proofs = wallet_2.get_unspent_proofs().await.unwrap();
let expected_fee = wallet.get_proofs_fee(&proofs).await?;
let expected_fee = wallet.get_proofs_fee(&proofs).await.unwrap();
wallet_2
.swap(None, SplitTarget::default(), proofs, None, false)
.await?;
.await
.unwrap();
assert_eq!(restored, 100.into());
// Since we have to do a swap we expect to restore amount - fee
assert_eq!(
wallet_2.total_balance().await?,
wallet_2.total_balance().await.unwrap(),
Amount::from(100) - expected_fee
);
let proofs = wallet.get_unspent_proofs().await?;
let proofs = wallet.get_unspent_proofs().await.unwrap();
let states = wallet.check_proofs_spent(proofs).await?;
let states = wallet.check_proofs_spent(proofs).await.unwrap();
for state in states {
if state.state != State::Spent {
bail!("All proofs should be spent");
panic!("All proofs should be spent");
}
}
Ok(())
}
/// Tests that change outputs in a melt quote are correctly handled
@@ -313,38 +320,43 @@ async fn test_restore() -> Result<()> {
/// This ensures the mint correctly processes change outputs during melting operations
/// and that the wallet can properly verify the change amounts match expectations.
#[tokio::test(flavor = "multi_thread", worker_threads = 1)]
async fn test_fake_melt_change_in_quote() -> Result<()> {
async fn test_fake_melt_change_in_quote() {
let wallet = Wallet::new(
&get_mint_url_from_env(),
CurrencyUnit::Sat,
Arc::new(memory::empty().await?),
&Mnemonic::generate(12)?.to_seed_normalized(""),
Arc::new(memory::empty().await.unwrap()),
&Mnemonic::generate(12).unwrap().to_seed_normalized(""),
None,
)?;
)
.expect("failed to create new wallet");
let mint_quote = wallet.mint_quote(100.into(), None).await?;
let mint_quote = wallet.mint_quote(100.into(), None).await.unwrap();
let bolt11 = Bolt11Invoice::from_str(&mint_quote.request)?;
let bolt11 = Bolt11Invoice::from_str(&mint_quote.request).unwrap();
pay_if_regtest(&bolt11).await?;
pay_if_regtest(&bolt11).await.unwrap();
wait_for_mint_to_be_paid(&wallet, &mint_quote.id, 60).await?;
wait_for_mint_to_be_paid(&wallet, &mint_quote.id, 60)
.await
.unwrap();
let _mint_amount = wallet
.mint(&mint_quote.id, SplitTarget::default(), None)
.await?;
.await
.unwrap();
let invoice = create_invoice_for_env(Some(9)).await?;
let invoice = create_invoice_for_env(Some(9)).await.unwrap();
let proofs = wallet.get_unspent_proofs().await?;
let proofs = wallet.get_unspent_proofs().await.unwrap();
let melt_quote = wallet.melt_quote(invoice.to_string(), None).await?;
let melt_quote = wallet.melt_quote(invoice.to_string(), None).await.unwrap();
let keyset = wallet.get_active_mint_keyset().await?;
let keyset = wallet.get_active_mint_keyset().await.unwrap();
let premint_secrets = PreMintSecrets::random(keyset.id, 100.into(), &SplitTarget::default())?;
let premint_secrets =
PreMintSecrets::random(keyset.id, 100.into(), &SplitTarget::default()).unwrap();
let client = HttpClient::new(get_mint_url_from_env().parse()?, None);
let client = HttpClient::new(get_mint_url_from_env().parse().unwrap(), None);
let melt_request = MeltBolt11Request::new(
melt_quote.id.clone(),
@@ -352,11 +364,11 @@ async fn test_fake_melt_change_in_quote() -> Result<()> {
Some(premint_secrets.blinded_messages()),
);
let melt_response = client.post_melt(melt_request).await?;
let melt_response = client.post_melt(melt_request).await.unwrap();
assert!(melt_response.change.is_some());
let check = wallet.melt_quote_status(&melt_quote.id).await?;
let check = wallet.melt_quote_status(&melt_quote.id).await.unwrap();
let mut melt_change = melt_response.change.unwrap();
melt_change.sort_by(|a, b| a.amount.cmp(&b.amount));
@@ -364,11 +376,10 @@ async fn test_fake_melt_change_in_quote() -> Result<()> {
check.sort_by(|a, b| a.amount.cmp(&b.amount));
assert_eq!(melt_change, check);
Ok(())
}
#[tokio::test(flavor = "multi_thread", worker_threads = 1)]
async fn test_pay_invoice_twice() -> Result<()> {
async fn test_pay_invoice_twice() {
let ln_backend = match env::var("LN_BACKEND") {
Ok(val) => Some(val),
Err(_) => env::var("CDK_MINTD_LN_BACKEND").ok(),
@@ -376,38 +387,44 @@ async fn test_pay_invoice_twice() -> Result<()> {
if ln_backend.map(|ln| ln.to_uppercase()) == Some("FAKEWALLET".to_string()) {
// We can only perform this test on regtest backends as fake wallet just marks the quote as paid
return Ok(());
return;
}
let wallet = Wallet::new(
&get_mint_url_from_env(),
CurrencyUnit::Sat,
Arc::new(memory::empty().await?),
&Mnemonic::generate(12)?.to_seed_normalized(""),
Arc::new(memory::empty().await.unwrap()),
&Mnemonic::generate(12).unwrap().to_seed_normalized(""),
None,
)?;
)
.expect("failed to create new wallet");
let mint_quote = wallet.mint_quote(100.into(), None).await?;
let mint_quote = wallet.mint_quote(100.into(), None).await.unwrap();
pay_if_regtest(&mint_quote.request.parse()?).await?;
pay_if_regtest(&mint_quote.request.parse().unwrap())
.await
.unwrap();
wait_for_mint_to_be_paid(&wallet, &mint_quote.id, 60).await?;
wait_for_mint_to_be_paid(&wallet, &mint_quote.id, 60)
.await
.unwrap();
let proofs = wallet
.mint(&mint_quote.id, SplitTarget::default(), None)
.await?;
.await
.unwrap();
let mint_amount = proofs.total_amount()?;
let mint_amount = proofs.total_amount().unwrap();
assert_eq!(mint_amount, 100.into());
let invoice = create_invoice_for_env(Some(25)).await?;
let invoice = create_invoice_for_env(Some(25)).await.unwrap();
let melt_quote = wallet.melt_quote(invoice.clone(), None).await?;
let melt_quote = wallet.melt_quote(invoice.clone(), None).await.unwrap();
let melt = wallet.melt(&melt_quote.id).await.unwrap();
let melt_two = wallet.melt_quote(invoice, None).await?;
let melt_two = wallet.melt_quote(invoice, None).await.unwrap();
let melt_two = wallet.melt(&melt_two.id).await;
@@ -415,17 +432,15 @@ async fn test_pay_invoice_twice() -> Result<()> {
Err(err) => match err {
cdk::Error::RequestAlreadyPaid => (),
err => {
bail!("Wrong invoice already paid: {}", err.to_string());
panic!("Wrong invoice already paid: {}", err.to_string());
}
},
Ok(_) => {
bail!("Should not have allowed second payment");
panic!("Should not have allowed second payment");
}
}
let balance = wallet.total_balance().await?;
let balance = wallet.total_balance().await.unwrap();
assert_eq!(balance, (Amount::from(100) - melt.fee_paid - melt.amount));
Ok(())
}

View File

@@ -7,7 +7,6 @@
use std::collections::{HashMap, HashSet};
use std::sync::Arc;
use anyhow::Result;
use bip39::Mnemonic;
use cdk::cdk_database::MintDatabase;
use cdk::mint::{MintBuilder, MintMeltLimits};
@@ -19,8 +18,8 @@ use cdk_sqlite::mint::memory;
pub const MINT_URL: &str = "http://127.0.0.1:8088";
#[tokio::test(flavor = "multi_thread", worker_threads = 1)]
async fn test_correct_keyset() -> Result<()> {
let mnemonic = Mnemonic::generate(12)?;
async fn test_correct_keyset() {
let mnemonic = Mnemonic::generate(12).unwrap();
let fee_reserve = FeeReserve {
min_fee_reserve: 1.into(),
percent_fee_reserve: 1.0,
@@ -41,25 +40,31 @@ async fn test_correct_keyset() -> Result<()> {
MintMeltLimits::new(1, 5_000),
Arc::new(fake_wallet),
)
.await?;
.await
.unwrap();
mint_builder = mint_builder
.with_name("regtest mint".to_string())
.with_description("regtest mint".to_string())
.with_seed(mnemonic.to_seed_normalized("").to_vec());
let mint = mint_builder.build().await?;
let mint = mint_builder.build().await.unwrap();
localstore
.set_mint_info(mint_builder.mint_info.clone())
.await?;
.await
.unwrap();
let quote_ttl = QuoteTTL::new(10000, 10000);
localstore.set_quote_ttl(quote_ttl).await?;
localstore.set_quote_ttl(quote_ttl).await.unwrap();
mint.rotate_next_keyset(CurrencyUnit::Sat, 32, 0).await?;
mint.rotate_next_keyset(CurrencyUnit::Sat, 32, 0).await?;
mint.rotate_next_keyset(CurrencyUnit::Sat, 32, 0)
.await
.unwrap();
mint.rotate_next_keyset(CurrencyUnit::Sat, 32, 0)
.await
.unwrap();
let active = mint.localstore.get_active_keysets().await?;
let active = mint.localstore.get_active_keysets().await.unwrap();
let active = active
.get(&CurrencyUnit::Sat)
@@ -68,14 +73,15 @@ async fn test_correct_keyset() -> Result<()> {
let keyset_info = mint
.localstore
.get_keyset_info(active)
.await?
.await
.unwrap()
.expect("There is keyset");
assert!(keyset_info.derivation_path_index == Some(2));
let mint = mint_builder.build().await?;
let mint = mint_builder.build().await.unwrap();
let active = mint.localstore.get_active_keysets().await?;
let active = mint.localstore.get_active_keysets().await.unwrap();
let active = active
.get(&CurrencyUnit::Sat)
@@ -84,9 +90,9 @@ async fn test_correct_keyset() -> Result<()> {
let keyset_info = mint
.localstore
.get_keyset_info(active)
.await?
.await
.unwrap()
.expect("There is keyset");
assert!(keyset_info.derivation_path_index == Some(2));
Ok(())
}

View File

@@ -104,8 +104,6 @@ async fn get_wallet_balance(base_url: &str) -> u64 {
.await
.expect("Failed to parse balance response");
println!("Wallet balance: {:?}", balance);
balance["balance"]
.as_u64()
.expect("Could not parse balance as u64")

View File

@@ -2,7 +2,6 @@ use std::str::FromStr;
use std::sync::Arc;
use std::time::Duration;
use anyhow::{bail, Result};
use bip39::Mnemonic;
use cashu::ProofsMethods;
use cdk::amount::{Amount, SplitTarget};
@@ -40,46 +39,59 @@ async fn init_lnd_client() -> LndClient {
}
#[tokio::test(flavor = "multi_thread", worker_threads = 1)]
async fn test_internal_payment() -> Result<()> {
async fn test_internal_payment() {
let lnd_client = init_lnd_client().await;
let wallet = Wallet::new(
&get_mint_url_from_env(),
CurrencyUnit::Sat,
Arc::new(memory::empty().await?),
&Mnemonic::generate(12)?.to_seed_normalized(""),
Arc::new(memory::empty().await.unwrap()),
&Mnemonic::generate(12).unwrap().to_seed_normalized(""),
None,
)?;
)
.expect("failed to create new wallet");
let mint_quote = wallet.mint_quote(100.into(), None).await?;
let mint_quote = wallet.mint_quote(100.into(), None).await.unwrap();
lnd_client.pay_invoice(mint_quote.request).await?;
lnd_client
.pay_invoice(mint_quote.request)
.await
.expect("failed to pay invoice");
wait_for_mint_to_be_paid(&wallet, &mint_quote.id, 60).await?;
wait_for_mint_to_be_paid(&wallet, &mint_quote.id, 60)
.await
.unwrap();
let _mint_amount = wallet
.mint(&mint_quote.id, SplitTarget::default(), None)
.await?;
.await
.unwrap();
assert!(wallet.total_balance().await? == 100.into());
assert!(wallet.total_balance().await.unwrap() == 100.into());
let wallet_2 = Wallet::new(
&get_mint_url_from_env(),
CurrencyUnit::Sat,
Arc::new(memory::empty().await?),
&Mnemonic::generate(12)?.to_seed_normalized(""),
Arc::new(memory::empty().await.unwrap()),
&Mnemonic::generate(12).unwrap().to_seed_normalized(""),
None,
)?;
)
.expect("failed to create new wallet");
let mint_quote = wallet_2.mint_quote(10.into(), None).await?;
let mint_quote = wallet_2.mint_quote(10.into(), None).await.unwrap();
let melt = wallet.melt_quote(mint_quote.request.clone(), None).await?;
let melt = wallet
.melt_quote(mint_quote.request.clone(), None)
.await
.unwrap();
assert_eq!(melt.amount, 10.into());
let _melted = wallet.melt(&melt.id).await.unwrap();
wait_for_mint_to_be_paid(&wallet, &mint_quote.id, 60).await?;
wait_for_mint_to_be_paid(&wallet, &mint_quote.id, 60)
.await
.unwrap();
let _wallet_2_mint = wallet_2
.mint(&mint_quote.id, SplitTarget::default(), None)
@@ -89,9 +101,9 @@ async fn test_internal_payment() -> Result<()> {
let check_paid = match get_mint_port("0") {
8085 => {
let cln_one_dir = get_cln_dir("one");
let cln_client = ClnClient::new(cln_one_dir.clone(), None).await?;
let cln_client = ClnClient::new(cln_one_dir.clone(), None).await.unwrap();
let payment_hash = Bolt11Invoice::from_str(&mint_quote.request)?;
let payment_hash = Bolt11Invoice::from_str(&mint_quote.request).unwrap();
cln_client
.check_incoming_payment_status(&payment_hash.payment_hash().to_string())
.await
@@ -104,8 +116,9 @@ async fn test_internal_payment() -> Result<()> {
get_lnd_cert_file_path(&lnd_two_dir),
get_lnd_macaroon_path(&lnd_two_dir),
)
.await?;
let payment_hash = Bolt11Invoice::from_str(&mint_quote.request)?;
.await
.unwrap();
let payment_hash = Bolt11Invoice::from_str(&mint_quote.request).unwrap();
lnd_client
.check_incoming_payment_status(&payment_hash.payment_hash().to_string())
.await
@@ -117,33 +130,32 @@ async fn test_internal_payment() -> Result<()> {
match check_paid {
InvoiceStatus::Unpaid => (),
_ => {
bail!("Invoice has incorrect status: {:?}", check_paid);
panic!("Invoice has incorrect status: {:?}", check_paid);
}
}
let wallet_2_balance = wallet_2.total_balance().await?;
let wallet_2_balance = wallet_2.total_balance().await.unwrap();
assert!(wallet_2_balance == 10.into());
let wallet_1_balance = wallet.total_balance().await?;
let wallet_1_balance = wallet.total_balance().await.unwrap();
assert!(wallet_1_balance == 90.into());
Ok(())
}
#[tokio::test(flavor = "multi_thread", worker_threads = 1)]
async fn test_websocket_connection() -> Result<()> {
async fn test_websocket_connection() {
let wallet = Wallet::new(
&get_mint_url_from_env(),
CurrencyUnit::Sat,
Arc::new(wallet::memory::empty().await?),
&Mnemonic::generate(12)?.to_seed_normalized(""),
Arc::new(wallet::memory::empty().await.unwrap()),
&Mnemonic::generate(12).unwrap().to_seed_normalized(""),
None,
)?;
)
.expect("failed to create new wallet");
// Create a small mint quote to test notifications
let mint_quote = wallet.mint_quote(10.into(), None).await?;
let mint_quote = wallet.mint_quote(10.into(), None).await.unwrap();
// Subscribe to notifications for this quote
let mut subscription = wallet
@@ -156,76 +168,92 @@ async fn test_websocket_connection() -> Result<()> {
let msg = timeout(Duration::from_secs(10), subscription.recv())
.await
.expect("timeout waiting for unpaid notification")
.ok_or_else(|| anyhow::anyhow!("No unpaid notification received"))?;
.expect("No paid notification received");
match msg {
NotificationPayload::MintQuoteBolt11Response(response) => {
assert_eq!(response.quote.to_string(), mint_quote.id);
assert_eq!(response.state, MintQuoteState::Unpaid);
}
_ => bail!("Unexpected notification type"),
_ => panic!("Unexpected notification type"),
}
let lnd_client = init_lnd_client().await;
lnd_client.pay_invoice(mint_quote.request).await?;
lnd_client
.pay_invoice(mint_quote.request)
.await
.expect("failed to pay invoice");
// Wait for paid notification with 10 second timeout
let msg = timeout(Duration::from_secs(10), subscription.recv())
.await
.expect("timeout waiting for paid notification")
.ok_or_else(|| anyhow::anyhow!("No paid notification received"))?;
.expect("No paid notification received");
match msg {
NotificationPayload::MintQuoteBolt11Response(response) => {
assert_eq!(response.quote.to_string(), mint_quote.id);
assert_eq!(response.state, MintQuoteState::Paid);
Ok(())
}
_ => bail!("Unexpected notification type"),
_ => panic!("Unexpected notification type"),
}
}
#[tokio::test(flavor = "multi_thread", worker_threads = 1)]
async fn test_multimint_melt() -> Result<()> {
async fn test_multimint_melt() {
let lnd_client = init_lnd_client().await;
let db = Arc::new(memory::empty().await?);
let db = Arc::new(memory::empty().await.unwrap());
let wallet1 = Wallet::new(
&get_mint_url_from_env(),
CurrencyUnit::Sat,
db,
&Mnemonic::generate(12)?.to_seed_normalized(""),
&Mnemonic::generate(12).unwrap().to_seed_normalized(""),
None,
)?;
)
.expect("failed to create new wallet");
let db = Arc::new(memory::empty().await?);
let db = Arc::new(memory::empty().await.unwrap());
let wallet2 = Wallet::new(
&get_second_mint_url_from_env(),
CurrencyUnit::Sat,
db,
&Mnemonic::generate(12)?.to_seed_normalized(""),
&Mnemonic::generate(12).unwrap().to_seed_normalized(""),
None,
)?;
)
.expect("failed to create new wallet");
let mint_amount = Amount::from(100);
// Fund the wallets
let quote = wallet1.mint_quote(mint_amount, None).await?;
lnd_client.pay_invoice(quote.request.clone()).await?;
wait_for_mint_to_be_paid(&wallet1, &quote.id, 60).await?;
let quote = wallet1.mint_quote(mint_amount, None).await.unwrap();
lnd_client
.pay_invoice(quote.request.clone())
.await
.expect("failed to pay invoice");
wait_for_mint_to_be_paid(&wallet1, &quote.id, 60)
.await
.unwrap();
wallet1
.mint(&quote.id, SplitTarget::default(), None)
.await?;
.await
.unwrap();
let quote = wallet2.mint_quote(mint_amount, None).await?;
lnd_client.pay_invoice(quote.request.clone()).await?;
wait_for_mint_to_be_paid(&wallet2, &quote.id, 60).await?;
let quote = wallet2.mint_quote(mint_amount, None).await.unwrap();
lnd_client
.pay_invoice(quote.request.clone())
.await
.expect("failed to pay invoice");
wait_for_mint_to_be_paid(&wallet2, &quote.id, 60)
.await
.unwrap();
wallet2
.mint(&quote.id, SplitTarget::default(), None)
.await?;
.await
.unwrap();
// Get an invoice
let invoice = lnd_client.create_invoice(Some(50)).await?;
let invoice = lnd_client.create_invoice(Some(50)).await.unwrap();
// Get multi-part melt quotes
let melt_options = MeltOptions::Mpp {
@@ -254,28 +282,33 @@ async fn test_multimint_melt() -> Result<()> {
// Check
assert!(result1.state == result2.state);
assert!(result1.state == MeltQuoteState::Paid);
Ok(())
}
#[tokio::test(flavor = "multi_thread", worker_threads = 1)]
async fn test_cached_mint() -> Result<()> {
async fn test_cached_mint() {
let lnd_client = init_lnd_client().await;
let wallet = Wallet::new(
&get_mint_url_from_env(),
CurrencyUnit::Sat,
Arc::new(memory::empty().await?),
&Mnemonic::generate(12)?.to_seed_normalized(""),
Arc::new(memory::empty().await.unwrap()),
&Mnemonic::generate(12).unwrap().to_seed_normalized(""),
None,
)?;
)
.expect("failed to create new wallet");
let mint_amount = Amount::from(100);
let quote = wallet.mint_quote(mint_amount, None).await?;
lnd_client.pay_invoice(quote.request.clone()).await?;
let quote = wallet.mint_quote(mint_amount, None).await.unwrap();
lnd_client
.pay_invoice(quote.request.clone())
.await
.expect("failed to pay invoice");
wait_for_mint_to_be_paid(&wallet, &quote.id, 60).await?;
wait_for_mint_to_be_paid(&wallet, &quote.id, 60)
.await
.unwrap();
let active_keyset_id = wallet.get_active_mint_keyset().await?.id;
let active_keyset_id = wallet.get_active_mint_keyset().await.unwrap().id;
let http_client = HttpClient::new(get_mint_url_from_env().parse().unwrap(), None);
let premint_secrets =
PreMintSecrets::random(active_keyset_id, 100.into(), &SplitTarget::default()).unwrap();
@@ -288,52 +321,59 @@ async fn test_cached_mint() -> Result<()> {
let secret_key = quote.secret_key;
request.sign(secret_key.expect("Secret key on quote"))?;
request
.sign(secret_key.expect("Secret key on quote"))
.unwrap();
let response = http_client.post_mint(request.clone()).await?;
let response1 = http_client.post_mint(request).await?;
let response = http_client.post_mint(request.clone()).await.unwrap();
let response1 = http_client.post_mint(request).await.unwrap();
assert!(response == response1);
Ok(())
}
#[tokio::test(flavor = "multi_thread", worker_threads = 1)]
async fn test_regtest_melt_amountless() -> Result<()> {
async fn test_regtest_melt_amountless() {
let lnd_client = init_lnd_client().await;
let wallet = Wallet::new(
&get_mint_url_from_env(),
CurrencyUnit::Sat,
Arc::new(memory::empty().await?),
&Mnemonic::generate(12)?.to_seed_normalized(""),
Arc::new(memory::empty().await.unwrap()),
&Mnemonic::generate(12).unwrap().to_seed_normalized(""),
None,
)?;
)
.expect("failed to create new wallet");
let mint_amount = Amount::from(100);
let mint_quote = wallet.mint_quote(mint_amount, None).await?;
let mint_quote = wallet.mint_quote(mint_amount, None).await.unwrap();
assert_eq!(mint_quote.amount, mint_amount);
lnd_client.pay_invoice(mint_quote.request).await?;
lnd_client
.pay_invoice(mint_quote.request)
.await
.expect("failed to pay invoice");
let proofs = wallet
.mint(&mint_quote.id, SplitTarget::default(), None)
.await?;
.await
.unwrap();
let amount = proofs.total_amount()?;
let amount = proofs.total_amount().unwrap();
assert!(mint_amount == amount);
let invoice = lnd_client.create_invoice(None).await?;
let invoice = lnd_client.create_invoice(None).await.unwrap();
let options = MeltOptions::new_amountless(5_000);
let melt_quote = wallet.melt_quote(invoice.clone(), Some(options)).await?;
let melt_quote = wallet
.melt_quote(invoice.clone(), Some(options))
.await
.unwrap();
let melt = wallet.melt(&melt_quote.id).await.unwrap();
assert!(melt.amount == 5.into());
Ok(())
}

View File

@@ -1,7 +1,6 @@
use std::str::FromStr;
use std::sync::Arc;
use anyhow::Result;
use bip39::Mnemonic;
use cashu::{Bolt11Invoice, ProofsMethods};
use cdk::amount::{Amount, SplitTarget};
@@ -13,28 +12,31 @@ use cdk_integration_tests::{
use cdk_sqlite::wallet::memory;
#[tokio::test(flavor = "multi_thread", worker_threads = 1)]
async fn test_swap() -> Result<()> {
let seed = Mnemonic::generate(12)?.to_seed_normalized("");
async fn test_swap() {
let seed = Mnemonic::generate(12).unwrap().to_seed_normalized("");
let wallet = Wallet::new(
&get_mint_url_from_env(),
CurrencyUnit::Sat,
Arc::new(memory::empty().await?),
Arc::new(memory::empty().await.unwrap()),
&seed,
None,
)?;
)
.expect("failed to create new wallet");
let mint_quote = wallet.mint_quote(100.into(), None).await?;
let mint_quote = wallet.mint_quote(100.into(), None).await.unwrap();
let invoice = Bolt11Invoice::from_str(&mint_quote.request)?;
pay_if_regtest(&invoice).await?;
let invoice = Bolt11Invoice::from_str(&mint_quote.request).unwrap();
pay_if_regtest(&invoice).await.unwrap();
let _mint_amount = wallet
.mint(&mint_quote.id, SplitTarget::default(), None)
.await?;
.await
.unwrap();
let proofs: Vec<Amount> = wallet
.get_unspent_proofs()
.await?
.await
.unwrap()
.iter()
.map(|p| p.amount)
.collect();
@@ -49,65 +51,72 @@ async fn test_swap() -> Result<()> {
..Default::default()
},
)
.await?;
.await
.unwrap();
let proofs = send.proofs();
let fee = wallet.get_proofs_fee(&proofs).await?;
let fee = wallet.get_proofs_fee(&proofs).await.unwrap();
assert_eq!(fee, 1.into());
let send = wallet.send(send, None).await?;
let send = wallet.send(send, None).await.unwrap();
let rec_amount = wallet
.receive(&send.to_string(), ReceiveOptions::default())
.await?;
.await
.unwrap();
assert_eq!(rec_amount, 3.into());
let wallet_balance = wallet.total_balance().await?;
let wallet_balance = wallet.total_balance().await.unwrap();
assert_eq!(wallet_balance, 99.into());
Ok(())
}
#[tokio::test(flavor = "multi_thread", worker_threads = 1)]
async fn test_fake_melt_change_in_quote() -> Result<()> {
async fn test_fake_melt_change_in_quote() {
let wallet = Wallet::new(
&get_mint_url_from_env(),
CurrencyUnit::Sat,
Arc::new(memory::empty().await?),
&Mnemonic::generate(12)?.to_seed_normalized(""),
Arc::new(memory::empty().await.unwrap()),
&Mnemonic::generate(12).unwrap().to_seed_normalized(""),
None,
)?;
)
.expect("failed to create new wallet");
let mint_quote = wallet.mint_quote(100.into(), None).await?;
let mint_quote = wallet.mint_quote(100.into(), None).await.unwrap();
let bolt11 = Bolt11Invoice::from_str(&mint_quote.request)?;
let bolt11 = Bolt11Invoice::from_str(&mint_quote.request).unwrap();
pay_if_regtest(&bolt11).await?;
pay_if_regtest(&bolt11).await.unwrap();
wait_for_mint_to_be_paid(&wallet, &mint_quote.id, 60).await?;
wait_for_mint_to_be_paid(&wallet, &mint_quote.id, 60)
.await
.unwrap();
let _mint_amount = wallet
.mint(&mint_quote.id, SplitTarget::default(), None)
.await?;
.await
.unwrap();
let invoice_amount = 9;
let invoice = create_invoice_for_env(Some(invoice_amount)).await?;
let invoice = create_invoice_for_env(Some(invoice_amount)).await.unwrap();
let melt_quote = wallet.melt_quote(invoice.to_string(), None).await?;
let melt_quote = wallet.melt_quote(invoice.to_string(), None).await.unwrap();
let proofs = wallet.get_unspent_proofs().await?;
let proofs = wallet.get_unspent_proofs().await.unwrap();
let proofs_total = proofs.total_amount().unwrap();
let fee = wallet.get_proofs_fee(&proofs).await?;
let melt = wallet.melt_proofs(&melt_quote.id, proofs.clone()).await?;
let fee = wallet.get_proofs_fee(&proofs).await.unwrap();
let melt = wallet
.melt_proofs(&melt_quote.id, proofs.clone())
.await
.unwrap();
let change = melt.change.unwrap().total_amount().unwrap();
let idk = proofs.total_amount()? - Amount::from(invoice_amount) - change;
let idk = proofs.total_amount().unwrap() - Amount::from(invoice_amount) - change;
println!("{}", idk);
println!("{}", fee);
@@ -117,9 +126,7 @@ async fn test_fake_melt_change_in_quote() -> Result<()> {
let ln_fee = 1;
assert_eq!(
wallet.total_balance().await?,
wallet.total_balance().await.unwrap(),
Amount::from(100 - invoice_amount - u64::from(fee) - ln_fee)
);
Ok(())
}

View File

@@ -383,7 +383,7 @@ mod tests {
};
// Convert the Info struct to a debug string
let debug_output = format!("{:?}", info);
let debug_output = format!("{info:?}");
// Verify the debug output contains expected fields
assert!(debug_output.contains("url: \"http://example.com\""));

View File

@@ -42,7 +42,7 @@ mod tests {
use super::*;
#[test]
fn test_calc_fee() -> anyhow::Result<()> {
fn test_calc_fee() {
let keyset_id = Id::from_str("001711afb1de20cb").unwrap();
let fee = 2;
@@ -54,34 +54,32 @@ mod tests {
proofs_count.insert(keyset_id, 1);
let sum_fee = calculate_fee(&proofs_count, &keyset_fees)?;
let sum_fee = calculate_fee(&proofs_count, &keyset_fees).unwrap();
assert_eq!(sum_fee, 1.into());
proofs_count.insert(keyset_id, 500);
let sum_fee = calculate_fee(&proofs_count, &keyset_fees)?;
let sum_fee = calculate_fee(&proofs_count, &keyset_fees).unwrap();
assert_eq!(sum_fee, 1.into());
proofs_count.insert(keyset_id, 1000);
let sum_fee = calculate_fee(&proofs_count, &keyset_fees)?;
let sum_fee = calculate_fee(&proofs_count, &keyset_fees).unwrap();
assert_eq!(sum_fee, 2.into());
proofs_count.insert(keyset_id, 2000);
let sum_fee = calculate_fee(&proofs_count, &keyset_fees)?;
let sum_fee = calculate_fee(&proofs_count, &keyset_fees).unwrap();
assert_eq!(sum_fee, 4.into());
proofs_count.insert(keyset_id, 3500);
let sum_fee = calculate_fee(&proofs_count, &keyset_fees)?;
let sum_fee = calculate_fee(&proofs_count, &keyset_fees).unwrap();
assert_eq!(sum_fee, 7.into());
proofs_count.insert(keyset_id, 3501);
let sum_fee = calculate_fee(&proofs_count, &keyset_fees)?;
let sum_fee = calculate_fee(&proofs_count, &keyset_fees).unwrap();
assert_eq!(sum_fee, 8.into());
Ok(())
}
}

File diff suppressed because one or more lines are too long

View File

@@ -507,9 +507,9 @@ mod tests {
Wallet::select_proofs(1024.into(), proofs, &vec![id()], &HashMap::new(), false)
.unwrap();
assert_eq!(selected_proofs.len(), 1024);
for i in 0..1024 {
assert_eq!(selected_proofs[i].amount, 1.into());
}
selected_proofs
.iter()
.for_each(|proof| assert_eq!(proof.amount, Amount::ONE));
}
#[test]
@@ -527,9 +527,11 @@ mod tests {
.unwrap();
selected_proofs.sort();
assert_eq!(selected_proofs.len(), 32);
for i in 0..32 {
assert_eq!(selected_proofs[i].amount, (1 << i).into());
}
selected_proofs
.iter()
.enumerate()
.for_each(|(i, proof)| assert_eq!(proof.amount, (1 << i).into()));
}
#[test]