test_fees.rs 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  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::{
  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() {
  15. let seed = Mnemonic::generate(12).unwrap().to_seed_normalized("");
  16. let wallet = Wallet::new(
  17. &get_mint_url_from_env(),
  18. CurrencyUnit::Sat,
  19. Arc::new(memory::empty().await.unwrap()),
  20. &seed,
  21. None,
  22. )
  23. .expect("failed to create new wallet");
  24. let mint_quote = wallet.mint_quote(100.into(), None).await.unwrap();
  25. let invoice = Bolt11Invoice::from_str(&mint_quote.request).unwrap();
  26. pay_if_regtest(&get_temp_dir(), &invoice).await.unwrap();
  27. wait_for_mint_to_be_paid(&wallet, &mint_quote.id, 10)
  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. wait_for_mint_to_be_paid(&wallet, &mint_quote.id, 60)
  78. .await
  79. .unwrap();
  80. let _mint_amount = wallet
  81. .mint(&mint_quote.id, SplitTarget::default(), None)
  82. .await
  83. .unwrap();
  84. let invoice_amount = 9;
  85. let invoice = create_invoice_for_env(&get_temp_dir(), Some(invoice_amount))
  86. .await
  87. .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. }