test_fees.rs 3.6 KB

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