test_fees.rs 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. use std::str::FromStr;
  2. use std::sync::Arc;
  3. use std::time::Duration;
  4. use bip39::Mnemonic;
  5. use cashu::{Bolt11Invoice, ProofsMethods};
  6. use cdk::amount::{Amount, SplitTarget};
  7. use cdk::nuts::CurrencyUnit;
  8. use cdk::wallet::{ReceiveOptions, SendKind, SendOptions, Wallet};
  9. use cdk_integration_tests::init_regtest::get_temp_dir;
  10. use cdk_integration_tests::{create_invoice_for_env, get_mint_url_from_env, pay_if_regtest};
  11. use cdk_sqlite::wallet::memory;
  12. #[tokio::test(flavor = "multi_thread", worker_threads = 1)]
  13. async fn test_swap() {
  14. let seed = Mnemonic::generate(12).unwrap().to_seed_normalized("");
  15. let wallet = Wallet::new(
  16. &get_mint_url_from_env(),
  17. CurrencyUnit::Sat,
  18. Arc::new(memory::empty().await.unwrap()),
  19. seed,
  20. None,
  21. )
  22. .expect("failed to create new wallet");
  23. let mint_quote = wallet.mint_quote(100.into(), None).await.unwrap();
  24. let invoice = Bolt11Invoice::from_str(&mint_quote.request).unwrap();
  25. pay_if_regtest(&get_temp_dir(), &invoice).await.unwrap();
  26. wallet
  27. .wait_for_payment(&mint_quote, Duration::from_secs(60))
  28. .await
  29. .unwrap();
  30. let _mint_amount = wallet
  31. .mint(&mint_quote.id, SplitTarget::default(), None)
  32. .await
  33. .unwrap();
  34. let proofs: Vec<Amount> = wallet
  35. .get_unspent_proofs()
  36. .await
  37. .unwrap()
  38. .iter()
  39. .map(|p| p.amount)
  40. .collect();
  41. println!("{:?}", proofs);
  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. wallet
  78. .wait_for_payment(&mint_quote, Duration::from_secs(60))
  79. .await
  80. .unwrap();
  81. let _mint_amount = wallet
  82. .mint(&mint_quote.id, SplitTarget::default(), None)
  83. .await
  84. .unwrap();
  85. let invoice_amount = 9;
  86. let invoice = create_invoice_for_env(&get_temp_dir(), Some(invoice_amount))
  87. .await
  88. .unwrap();
  89. let melt_quote = wallet.melt_quote(invoice.to_string(), None).await.unwrap();
  90. let proofs = wallet.get_unspent_proofs().await.unwrap();
  91. let proofs_total = proofs.total_amount().unwrap();
  92. let fee = wallet.get_proofs_fee(&proofs).await.unwrap();
  93. let melt = wallet
  94. .melt_proofs(&melt_quote.id, proofs.clone())
  95. .await
  96. .unwrap();
  97. let change = melt.change.unwrap().total_amount().unwrap();
  98. let idk = proofs.total_amount().unwrap() - Amount::from(invoice_amount) - change;
  99. println!("{}", idk);
  100. println!("{}", fee);
  101. println!("{}", proofs_total);
  102. println!("{}", change);
  103. let ln_fee = 1;
  104. assert_eq!(
  105. wallet.total_balance().await.unwrap(),
  106. Amount::from(100 - invoice_amount - u64::from(fee) - ln_fee)
  107. );
  108. }