unbalanced.rs 1.8 KB

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