test_fees.rs 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. use std::str::FromStr;
  2. use std::sync::Arc;
  3. use bip39::Mnemonic;
  4. use cashu::{Bolt11Invoice, ProofsMethods};
  5. use cdk::amount::{Amount, SplitTarget};
  6. use cdk::nuts::CurrencyUnit;
  7. use cdk::wallet::{ReceiveOptions, SendKind, SendOptions, Wallet};
  8. use cdk_integration_tests::init_regtest::get_temp_dir;
  9. use cdk_integration_tests::{create_invoice_for_env, get_mint_url_from_env, pay_if_regtest};
  10. use cdk_sqlite::wallet::memory;
  11. use tracing_subscriber::EnvFilter;
  12. #[tokio::test(flavor = "multi_thread", worker_threads = 1)]
  13. async fn test_swap() {
  14. // Set up logging
  15. let default_filter = "debug";
  16. let sqlx_filter = "sqlx=warn,hyper_util=warn,reqwest=warn,rustls=warn";
  17. let env_filter = EnvFilter::new(format!("{},{}", default_filter, sqlx_filter));
  18. tracing_subscriber::fmt().with_env_filter(env_filter).init();
  19. let seed = Mnemonic::generate(12).unwrap().to_seed_normalized("");
  20. let wallet = Wallet::new(
  21. &get_mint_url_from_env(),
  22. CurrencyUnit::Sat,
  23. Arc::new(memory::empty().await.unwrap()),
  24. seed,
  25. None,
  26. )
  27. .expect("failed to create new wallet");
  28. let mint_quote = wallet.mint_quote(100.into(), None).await.unwrap();
  29. let invoice = Bolt11Invoice::from_str(&mint_quote.request).unwrap();
  30. pay_if_regtest(&get_temp_dir(), &invoice).await.unwrap();
  31. let proofs = wallet
  32. .wait_and_mint_quote(
  33. mint_quote.clone(),
  34. SplitTarget::default(),
  35. None,
  36. tokio::time::Duration::from_secs(60),
  37. )
  38. .await
  39. .expect("payment");
  40. println!("{:?}", proofs);
  41. println!("{:?}", wallet.get_mint_keysets().await.unwrap());
  42. let send = wallet
  43. .prepare_send(
  44. 4.into(),
  45. SendOptions {
  46. send_kind: SendKind::OfflineExact,
  47. ..Default::default()
  48. },
  49. )
  50. .await
  51. .unwrap();
  52. let proofs = send.proofs();
  53. let fee = wallet.get_proofs_fee(&proofs).await.unwrap();
  54. assert_eq!(fee, 1.into());
  55. let send = send.confirm(None).await.unwrap();
  56. let rec_amount = wallet
  57. .receive(&send.to_string(), ReceiveOptions::default())
  58. .await
  59. .unwrap();
  60. assert_eq!(rec_amount, 3.into());
  61. let wallet_balance = wallet.total_balance().await.unwrap();
  62. assert_eq!(wallet_balance, 99.into());
  63. }
  64. #[tokio::test(flavor = "multi_thread", worker_threads = 1)]
  65. async fn test_fake_melt_change_in_quote() {
  66. let wallet = Wallet::new(
  67. &get_mint_url_from_env(),
  68. CurrencyUnit::Sat,
  69. Arc::new(memory::empty().await.unwrap()),
  70. Mnemonic::generate(12).unwrap().to_seed_normalized(""),
  71. None,
  72. )
  73. .expect("failed to create new wallet");
  74. let mint_quote = wallet.mint_quote(100.into(), None).await.unwrap();
  75. let bolt11 = Bolt11Invoice::from_str(&mint_quote.request).unwrap();
  76. pay_if_regtest(&get_temp_dir(), &bolt11).await.unwrap();
  77. let _proofs = wallet
  78. .wait_and_mint_quote(
  79. mint_quote.clone(),
  80. SplitTarget::default(),
  81. None,
  82. tokio::time::Duration::from_secs(60),
  83. )
  84. .await
  85. .expect("payment");
  86. let invoice_amount = 9;
  87. let invoice = create_invoice_for_env(Some(invoice_amount)).await.unwrap();
  88. let melt_quote = wallet.melt_quote(invoice.to_string(), None).await.unwrap();
  89. let proofs = wallet.get_unspent_proofs().await.unwrap();
  90. let proofs_total = proofs.total_amount().unwrap();
  91. let fee = wallet.get_proofs_fee(&proofs).await.unwrap();
  92. let melt = wallet
  93. .melt_proofs(&melt_quote.id, proofs.clone())
  94. .await
  95. .unwrap();
  96. let change = melt.change.unwrap().total_amount().unwrap();
  97. let idk = proofs.total_amount().unwrap() - Amount::from(invoice_amount) - change;
  98. println!("{}", idk);
  99. println!("{}", fee);
  100. println!("{}", proofs_total);
  101. println!("{}", change);
  102. let ln_fee = 1;
  103. assert_eq!(
  104. wallet.total_balance().await.unwrap(),
  105. Amount::from(100 - invoice_amount - u64::from(fee) - ln_fee)
  106. );
  107. }