fees.rs 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. //! Test calc fee
  2. use std::collections::HashMap;
  3. use std::str::FromStr;
  4. use std::sync::Arc;
  5. use std::time::Duration;
  6. use anyhow::Result;
  7. use bip39::Mnemonic;
  8. use cdk::amount::SplitTarget;
  9. use cdk::cdk_database::WalletMemoryDatabase;
  10. use cdk::mint_url::MintUrl;
  11. use cdk::nuts::CurrencyUnit;
  12. use cdk::Wallet;
  13. use cdk_integration_tests::{create_backends_fake_wallet, start_mint, wallet_mint, MINT_URL};
  14. #[tokio::test(flavor = "multi_thread", worker_threads = 1)]
  15. pub async fn test_mint_fee() -> Result<()> {
  16. tokio::spawn(async move {
  17. let ln_backends = create_backends_fake_wallet();
  18. let mut supported_units = HashMap::new();
  19. supported_units.insert(CurrencyUnit::Sat, (1, 32));
  20. start_mint(ln_backends, supported_units)
  21. .await
  22. .expect("Could not start mint")
  23. });
  24. tokio::time::sleep(Duration::from_millis(500)).await;
  25. let mnemonic = Mnemonic::generate(12)?;
  26. let wallet = Wallet::new(
  27. MINT_URL,
  28. CurrencyUnit::Sat,
  29. Arc::new(WalletMemoryDatabase::default()),
  30. &mnemonic.to_seed_normalized(""),
  31. None,
  32. )?;
  33. let wallet = Arc::new(wallet);
  34. wallet_mint(
  35. Arc::clone(&wallet),
  36. 10000.into(),
  37. SplitTarget::Value(1.into()),
  38. )
  39. .await
  40. .unwrap();
  41. println!("Minted");
  42. let proofs = wallet
  43. .localstore
  44. .get_proofs(Some(MintUrl::from_str(MINT_URL)?), None, None, None)
  45. .await?;
  46. let proofs: Vec<cdk::nuts::Proof> = proofs.into_iter().map(|p| p.proof).collect();
  47. let five_proofs = proofs[..5].to_vec();
  48. let fee = wallet.get_proofs_fee(&five_proofs).await?;
  49. // Check wallet gets fee calc correct
  50. assert_eq!(fee, 1.into());
  51. let _swap = wallet
  52. .swap(None, SplitTarget::Value(1.into()), five_proofs, None, false)
  53. .await?;
  54. let wallet_bal = wallet.total_balance().await?;
  55. // Check 1 sat was paid in fees for the swap
  56. assert_eq!(wallet_bal, 9999.into());
  57. let proofs = wallet
  58. .localstore
  59. .get_proofs(Some(MintUrl::from_str(MINT_URL)?), None, None, None)
  60. .await?;
  61. let proofs: Vec<cdk::nuts::Proof> = proofs.into_iter().map(|p| p.proof).collect();
  62. let thousand_proofs = proofs[..1001].to_vec();
  63. let fee = wallet.get_proofs_fee(&thousand_proofs).await?;
  64. // Check wallet gets fee calc correct
  65. assert_eq!(fee, 2.into());
  66. let _swap = wallet
  67. .swap(
  68. None,
  69. SplitTarget::Value(1.into()),
  70. thousand_proofs,
  71. None,
  72. false,
  73. )
  74. .await?;
  75. let wallet_bal = wallet.total_balance().await?;
  76. // Check 1 sat was paid in fees for the swap
  77. assert_eq!(wallet_bal, 9997.into());
  78. Ok(())
  79. }