unbalanced.rs 1.8 KB

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