payment_processor.rs 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. //! Tests where we expect the payment processor to respond with an error or pass
  2. use std::env;
  3. use std::sync::Arc;
  4. use anyhow::{bail, Result};
  5. use bip39::Mnemonic;
  6. use cdk::amount::{Amount, SplitTarget};
  7. use cdk::nuts::nut00::ProofsMethods;
  8. use cdk::nuts::CurrencyUnit;
  9. use cdk::wallet::Wallet;
  10. use cdk_fake_wallet::create_fake_invoice;
  11. use cdk_integration_tests::init_regtest::{get_lnd_dir, get_mint_url, LND_RPC_ADDR};
  12. use cdk_integration_tests::wait_for_mint_to_be_paid;
  13. use cdk_sqlite::wallet::memory;
  14. use ln_regtest_rs::ln_client::{LightningClient, LndClient};
  15. // This is the ln wallet we use to send/receive ln payements as the wallet
  16. async fn init_lnd_client() -> LndClient {
  17. let lnd_dir = get_lnd_dir("one");
  18. let cert_file = lnd_dir.join("tls.cert");
  19. let macaroon_file = lnd_dir.join("data/chain/bitcoin/regtest/admin.macaroon");
  20. LndClient::new(
  21. format!("https://{}", LND_RPC_ADDR),
  22. cert_file,
  23. macaroon_file,
  24. )
  25. .await
  26. .unwrap()
  27. }
  28. #[tokio::test(flavor = "multi_thread", worker_threads = 1)]
  29. async fn test_regtest_mint() -> Result<()> {
  30. let wallet = Wallet::new(
  31. &get_mint_url("0"),
  32. CurrencyUnit::Sat,
  33. Arc::new(memory::empty().await?),
  34. &Mnemonic::generate(12)?.to_seed_normalized(""),
  35. None,
  36. )?;
  37. let mint_amount = Amount::from(100);
  38. let mint_quote = wallet.mint_quote(mint_amount, None).await?;
  39. assert_eq!(mint_quote.amount, mint_amount);
  40. let ln_backend = env::var("LN_BACKEND")?;
  41. if ln_backend != "FAKEWALLET" {
  42. let lnd_client = init_lnd_client().await;
  43. lnd_client.pay_invoice(mint_quote.request).await?;
  44. }
  45. wait_for_mint_to_be_paid(&wallet, &mint_quote.id, 60).await?;
  46. let proofs = wallet
  47. .mint(&mint_quote.id, SplitTarget::default(), None)
  48. .await?;
  49. let mint_amount = proofs.total_amount()?;
  50. assert!(mint_amount == 100.into());
  51. Ok(())
  52. }
  53. #[tokio::test(flavor = "multi_thread", worker_threads = 1)]
  54. async fn test_regtest_mint_melt() -> Result<()> {
  55. let wallet = Wallet::new(
  56. &get_mint_url("0"),
  57. CurrencyUnit::Sat,
  58. Arc::new(memory::empty().await?),
  59. &Mnemonic::generate(12)?.to_seed_normalized(""),
  60. None,
  61. )?;
  62. let mint_amount = Amount::from(100);
  63. let mint_quote = wallet.mint_quote(mint_amount, None).await?;
  64. assert_eq!(mint_quote.amount, mint_amount);
  65. let ln_backend = env::var("LN_BACKEND")?;
  66. if ln_backend != "FAKEWALLET" {
  67. let lnd_client = init_lnd_client().await;
  68. lnd_client.pay_invoice(mint_quote.request).await?;
  69. }
  70. wait_for_mint_to_be_paid(&wallet, &mint_quote.id, 60).await?;
  71. let proofs = wallet
  72. .mint(&mint_quote.id, SplitTarget::default(), None)
  73. .await?;
  74. let mint_amount = proofs.total_amount()?;
  75. assert!(mint_amount == 100.into());
  76. let invoice = if ln_backend != "FAKEWALLET" {
  77. let lnd_client = init_lnd_client().await;
  78. lnd_client.create_invoice(Some(50)).await?
  79. } else {
  80. create_fake_invoice(50000, "".to_string()).to_string()
  81. };
  82. let melt_quote = wallet.melt_quote(invoice, None).await?;
  83. wallet.melt(&melt_quote.id).await?;
  84. Ok(())
  85. }
  86. #[tokio::test(flavor = "multi_thread", worker_threads = 1)]
  87. async fn test_pay_invoice_twice() -> Result<()> {
  88. let ln_backend = env::var("LN_BACKEND")?;
  89. if ln_backend == "FAKEWALLET" {
  90. // We can only preform this test on regtest backends as fake wallet just marks the quote as paid
  91. return Ok(());
  92. }
  93. let seed = Mnemonic::generate(12)?.to_seed_normalized("");
  94. let wallet = Wallet::new(
  95. &get_mint_url("0"),
  96. CurrencyUnit::Sat,
  97. Arc::new(memory::empty().await?),
  98. &seed,
  99. None,
  100. )?;
  101. let mint_quote = wallet.mint_quote(100.into(), None).await?;
  102. let lnd_client = init_lnd_client().await;
  103. lnd_client.pay_invoice(mint_quote.request).await?;
  104. wait_for_mint_to_be_paid(&wallet, &mint_quote.id, 60).await?;
  105. let proofs = wallet
  106. .mint(&mint_quote.id, SplitTarget::default(), None)
  107. .await?;
  108. let mint_amount = proofs.total_amount()?;
  109. assert_eq!(mint_amount, 100.into());
  110. let invoice = lnd_client.create_invoice(Some(25)).await?;
  111. let melt_quote = wallet.melt_quote(invoice.clone(), None).await?;
  112. let melt = wallet.melt(&melt_quote.id).await.unwrap();
  113. let melt_two = wallet.melt_quote(invoice, None).await?;
  114. let melt_two = wallet.melt(&melt_two.id).await;
  115. match melt_two {
  116. Err(err) => match err {
  117. cdk::Error::RequestAlreadyPaid => (),
  118. err => {
  119. bail!("Wrong invoice already paid: {}", err.to_string());
  120. }
  121. },
  122. Ok(_) => {
  123. bail!("Should not have allowed second payment");
  124. }
  125. }
  126. let balance = wallet.total_balance().await?;
  127. assert_eq!(balance, (Amount::from(100) - melt.fee_paid - melt.amount));
  128. Ok(())
  129. }