test_fees.rs 3.7 KB

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