test_fees.rs 3.9 KB

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