test_fees.rs 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. use std::str::FromStr;
  2. use std::sync::Arc;
  3. use anyhow::Result;
  4. use bip39::Mnemonic;
  5. use cashu::{Bolt11Invoice, ProofsMethods};
  6. use cdk::amount::{Amount, SplitTarget};
  7. use cdk::nuts::CurrencyUnit;
  8. use cdk::wallet::{SendKind, SendOptions, Wallet};
  9. use cdk_integration_tests::{
  10. create_invoice_for_env, get_mint_url_from_env, pay_if_regtest, wait_for_mint_to_be_paid,
  11. };
  12. use cdk_sqlite::wallet::memory;
  13. #[tokio::test(flavor = "multi_thread", worker_threads = 1)]
  14. async fn test_swap() -> Result<()> {
  15. let seed = Mnemonic::generate(12)?.to_seed_normalized("");
  16. let wallet = Wallet::new(
  17. &get_mint_url_from_env(),
  18. CurrencyUnit::Sat,
  19. Arc::new(memory::empty().await?),
  20. &seed,
  21. None,
  22. )?;
  23. let mint_quote = wallet.mint_quote(100.into(), None).await?;
  24. let invoice = Bolt11Invoice::from_str(&mint_quote.request)?;
  25. pay_if_regtest(&invoice).await?;
  26. let _mint_amount = wallet
  27. .mint(&mint_quote.id, SplitTarget::default(), None)
  28. .await?;
  29. let proofs: Vec<Amount> = wallet
  30. .get_unspent_proofs()
  31. .await?
  32. .iter()
  33. .map(|p| p.amount)
  34. .collect();
  35. println!("{:?}", proofs);
  36. let send = wallet
  37. .prepare_send(
  38. 4.into(),
  39. SendOptions {
  40. send_kind: SendKind::OfflineExact,
  41. ..Default::default()
  42. },
  43. )
  44. .await?;
  45. let proofs = send.proofs();
  46. let fee = wallet.get_proofs_fee(&proofs).await?;
  47. assert_eq!(fee, 1.into());
  48. let send = wallet.send(send, None).await?;
  49. let rec_amount = wallet
  50. .receive(&send.to_string(), SplitTarget::default(), &[], &[])
  51. .await?;
  52. assert_eq!(rec_amount, 3.into());
  53. let wallet_balance = wallet.total_balance().await?;
  54. assert_eq!(wallet_balance, 99.into());
  55. Ok(())
  56. }
  57. #[tokio::test(flavor = "multi_thread", worker_threads = 1)]
  58. async fn test_fake_melt_change_in_quote() -> Result<()> {
  59. let wallet = Wallet::new(
  60. &get_mint_url_from_env(),
  61. CurrencyUnit::Sat,
  62. Arc::new(memory::empty().await?),
  63. &Mnemonic::generate(12)?.to_seed_normalized(""),
  64. None,
  65. )?;
  66. let mint_quote = wallet.mint_quote(100.into(), None).await?;
  67. let bolt11 = Bolt11Invoice::from_str(&mint_quote.request)?;
  68. pay_if_regtest(&bolt11).await?;
  69. wait_for_mint_to_be_paid(&wallet, &mint_quote.id, 60).await?;
  70. let _mint_amount = wallet
  71. .mint(&mint_quote.id, SplitTarget::default(), None)
  72. .await?;
  73. let invoice_amount = 9;
  74. let invoice = create_invoice_for_env(Some(invoice_amount)).await?;
  75. let melt_quote = wallet.melt_quote(invoice.to_string(), None).await?;
  76. let proofs = wallet.get_unspent_proofs().await?;
  77. let proofs_total = proofs.total_amount().unwrap();
  78. let fee = wallet.get_proofs_fee(&proofs).await?;
  79. let melt = wallet.melt_proofs(&melt_quote.id, proofs.clone()).await?;
  80. let change = melt.change.unwrap().total_amount().unwrap();
  81. let idk = proofs.total_amount()? - Amount::from(invoice_amount) - change;
  82. println!("{}", idk);
  83. println!("{}", fee);
  84. println!("{}", proofs_total);
  85. println!("{}", change);
  86. let ln_fee = 1;
  87. assert_eq!(
  88. wallet.total_balance().await?,
  89. Amount::from(100 - invoice_amount - u64::from(fee) - ln_fee)
  90. );
  91. Ok(())
  92. }