test_fees.rs 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. use std::str::FromStr;
  2. use std::sync::Arc;
  3. use bip39::Mnemonic;
  4. use cashu::{Bolt11Invoice, PaymentMethod, 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
  29. .mint_quote(PaymentMethod::BOLT11, Some(100.into()), None, None)
  30. .await
  31. .unwrap();
  32. let invoice = Bolt11Invoice::from_str(&mint_quote.request).unwrap();
  33. pay_if_regtest(&get_temp_dir(), &invoice).await.unwrap();
  34. let proofs = wallet
  35. .wait_and_mint_quote(
  36. mint_quote.clone(),
  37. SplitTarget::default(),
  38. None,
  39. tokio::time::Duration::from_secs(60),
  40. )
  41. .await
  42. .expect("payment");
  43. println!("{:?}", proofs);
  44. println!("{:?}", wallet.get_mint_keysets().await.unwrap());
  45. let send = wallet
  46. .prepare_send(
  47. 4.into(),
  48. SendOptions {
  49. send_kind: SendKind::OfflineExact,
  50. ..Default::default()
  51. },
  52. )
  53. .await
  54. .unwrap();
  55. let proofs = send.proofs();
  56. let fee = wallet.get_proofs_fee(&proofs).await.unwrap().total;
  57. assert_eq!(fee, 1.into());
  58. let send = send.confirm(None).await.unwrap();
  59. let rec_amount = wallet
  60. .receive(&send.to_string(), ReceiveOptions::default())
  61. .await
  62. .unwrap();
  63. assert_eq!(rec_amount, 3.into());
  64. let wallet_balance = wallet.total_balance().await.unwrap();
  65. assert_eq!(wallet_balance, 99.into());
  66. }
  67. #[tokio::test(flavor = "multi_thread", worker_threads = 1)]
  68. async fn test_fake_melt_change_in_quote() {
  69. let wallet = Wallet::new(
  70. &get_mint_url_from_env(),
  71. CurrencyUnit::Sat,
  72. Arc::new(memory::empty().await.unwrap()),
  73. Mnemonic::generate(12).unwrap().to_seed_normalized(""),
  74. None,
  75. )
  76. .expect("failed to create new wallet");
  77. let mint_quote = wallet
  78. .mint_quote(PaymentMethod::BOLT11, Some(100.into()), None, None)
  79. .await
  80. .unwrap();
  81. let bolt11 = Bolt11Invoice::from_str(&mint_quote.request).unwrap();
  82. pay_if_regtest(&get_temp_dir(), &bolt11).await.unwrap();
  83. let _proofs = wallet
  84. .wait_and_mint_quote(
  85. mint_quote.clone(),
  86. SplitTarget::default(),
  87. None,
  88. tokio::time::Duration::from_secs(60),
  89. )
  90. .await
  91. .expect("payment");
  92. let invoice_amount = 9;
  93. let invoice = create_invoice_for_env(Some(invoice_amount)).await.unwrap();
  94. let melt_quote = wallet
  95. .melt_quote(PaymentMethod::BOLT11, invoice.to_string(), None, None)
  96. .await
  97. .unwrap();
  98. let proofs = wallet.get_unspent_proofs().await.unwrap();
  99. let proofs_total = proofs.total_amount().unwrap();
  100. let fee_breakdown = wallet.get_proofs_fee(&proofs).await.unwrap();
  101. let prepared = wallet
  102. .prepare_melt_proofs(
  103. &melt_quote.id,
  104. proofs.clone(),
  105. std::collections::HashMap::new(),
  106. )
  107. .await
  108. .unwrap();
  109. let melt = prepared.confirm().await.unwrap();
  110. let change = melt.change().unwrap().total_amount().unwrap();
  111. let idk = proofs.total_amount().unwrap() - Amount::from(invoice_amount) - change;
  112. println!("{}", idk);
  113. println!("{}", fee_breakdown.total);
  114. println!("{}", proofs_total);
  115. println!("{}", change);
  116. let ln_fee = 1;
  117. assert_eq!(
  118. wallet.total_balance().await.unwrap(),
  119. Amount::from(100 - invoice_amount - u64::from(fee_breakdown.total) - ln_fee)
  120. );
  121. }