test_fees.rs 3.7 KB

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