test_fees.rs 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  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. #[tokio::test(flavor = "multi_thread", worker_threads = 1)]
  12. async fn test_swap() {
  13. let seed = Mnemonic::generate(12).unwrap().to_seed_normalized("");
  14. let wallet = Wallet::new(
  15. &get_mint_url_from_env(),
  16. CurrencyUnit::Sat,
  17. Arc::new(memory::empty().await.unwrap()),
  18. seed,
  19. None,
  20. )
  21. .expect("failed to create new wallet");
  22. let mint_quote = wallet.mint_quote(100.into(), None).await.unwrap();
  23. let invoice = Bolt11Invoice::from_str(&mint_quote.request).unwrap();
  24. pay_if_regtest(&get_temp_dir(), &invoice).await.unwrap();
  25. let proofs = wallet
  26. .wait_and_mint_quote(
  27. mint_quote.clone(),
  28. SplitTarget::default(),
  29. None,
  30. tokio::time::Duration::from_secs(60),
  31. )
  32. .await
  33. .expect("payment");
  34. println!("{:?}", proofs);
  35. let send = wallet
  36. .prepare_send(
  37. 4.into(),
  38. SendOptions {
  39. send_kind: SendKind::OfflineExact,
  40. ..Default::default()
  41. },
  42. )
  43. .await
  44. .unwrap();
  45. let proofs = send.proofs();
  46. let fee = wallet.get_proofs_fee(&proofs).await.unwrap();
  47. assert_eq!(fee, 1.into());
  48. let send = send.confirm(None).await.unwrap();
  49. let rec_amount = wallet
  50. .receive(&send.to_string(), ReceiveOptions::default())
  51. .await
  52. .unwrap();
  53. assert_eq!(rec_amount, 3.into());
  54. let wallet_balance = wallet.total_balance().await.unwrap();
  55. assert_eq!(wallet_balance, 99.into());
  56. }
  57. #[tokio::test(flavor = "multi_thread", worker_threads = 1)]
  58. async fn test_fake_melt_change_in_quote() {
  59. let wallet = Wallet::new(
  60. &get_mint_url_from_env(),
  61. CurrencyUnit::Sat,
  62. Arc::new(memory::empty().await.unwrap()),
  63. Mnemonic::generate(12).unwrap().to_seed_normalized(""),
  64. None,
  65. )
  66. .expect("failed to create new wallet");
  67. let mint_quote = wallet.mint_quote(100.into(), None).await.unwrap();
  68. let bolt11 = Bolt11Invoice::from_str(&mint_quote.request).unwrap();
  69. pay_if_regtest(&get_temp_dir(), &bolt11).await.unwrap();
  70. let _proofs = wallet
  71. .wait_and_mint_quote(
  72. mint_quote.clone(),
  73. SplitTarget::default(),
  74. None,
  75. tokio::time::Duration::from_secs(60),
  76. )
  77. .await
  78. .expect("payment");
  79. let invoice_amount = 9;
  80. let invoice = create_invoice_for_env(Some(invoice_amount)).await.unwrap();
  81. let melt_quote = wallet.melt_quote(invoice.to_string(), None).await.unwrap();
  82. let proofs = wallet.get_unspent_proofs().await.unwrap();
  83. let proofs_total = proofs.total_amount().unwrap();
  84. let fee = wallet.get_proofs_fee(&proofs).await.unwrap();
  85. let melt = wallet
  86. .melt_proofs(&melt_quote.id, proofs.clone())
  87. .await
  88. .unwrap();
  89. let change = melt.change.unwrap().total_amount().unwrap();
  90. let idk = proofs.total_amount().unwrap() - Amount::from(invoice_amount) - change;
  91. println!("{}", idk);
  92. println!("{}", fee);
  93. println!("{}", proofs_total);
  94. println!("{}", change);
  95. let ln_fee = 1;
  96. assert_eq!(
  97. wallet.total_balance().await.unwrap(),
  98. Amount::from(100 - invoice_amount - u64::from(fee) - ln_fee)
  99. );
  100. }