unbalanced.rs 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. //! Test that if a wallet attempts to swap for less outputs then inputs correct error is returned
  2. use std::collections::HashMap;
  3. use std::time::Duration;
  4. use anyhow::{bail, Result};
  5. use cdk::amount::SplitTarget;
  6. use cdk::nuts::{CurrencyUnit, PreMintSecrets, SwapRequest};
  7. use cdk::Error;
  8. use cdk::HttpClient;
  9. use cdk_integration_tests::{create_backends_fake_wallet, mint_proofs, start_mint, MINT_URL};
  10. #[tokio::test(flavor = "multi_thread", worker_threads = 1)]
  11. pub async fn test_unbalanced_swap() -> Result<()> {
  12. tokio::spawn(async move {
  13. let ln_backends = create_backends_fake_wallet();
  14. let mut supported_units = HashMap::new();
  15. supported_units.insert(CurrencyUnit::Sat, (0, 32));
  16. start_mint(ln_backends, supported_units)
  17. .await
  18. .expect("Could not start mint")
  19. });
  20. // Wait for mint server to start
  21. tokio::time::sleep(Duration::from_millis(500)).await;
  22. let wallet_client = HttpClient::new();
  23. let mint_keys = wallet_client.get_mint_keys(MINT_URL.parse()?).await?;
  24. let mint_keys = mint_keys.first().unwrap();
  25. let keyset_id = mint_keys.id;
  26. let pre_swap_proofs = mint_proofs(MINT_URL, 10.into(), keyset_id, mint_keys).await?;
  27. let pre_mint = PreMintSecrets::random(keyset_id, 9.into(), &SplitTarget::default())?;
  28. let swap_request = SwapRequest::new(pre_swap_proofs.clone(), pre_mint.blinded_messages());
  29. let _swap_response = match wallet_client
  30. .post_swap(MINT_URL.parse()?, swap_request)
  31. .await
  32. {
  33. Ok(res) => res,
  34. // In the context of this test an error response here is good.
  35. // It means the mint does not allow us to swap for more then we should by overflowing
  36. Err(err) => match err {
  37. Error::TransactionUnbalanced(_, _, _) => {
  38. return Ok(());
  39. }
  40. _ => {
  41. println!("{}", err);
  42. bail!("Wrong error code returned");
  43. }
  44. },
  45. };
  46. bail!("Transaction should not have succeeded")
  47. }