diff --git a/crates/cdk-integration-tests/tests/fake_wallet.rs b/crates/cdk-integration-tests/tests/fake_wallet.rs index e1eee402..884e63d1 100644 --- a/crates/cdk-integration-tests/tests/fake_wallet.rs +++ b/crates/cdk-integration-tests/tests/fake_wallet.rs @@ -5,7 +5,9 @@ use bip39::Mnemonic; use cdk::{ amount::SplitTarget, cdk_database::WalletMemoryDatabase, - nuts::{CurrencyUnit, MeltBolt11Request, MeltQuoteState, PreMintSecrets, State}, + nuts::{ + CurrencyUnit, MeltBolt11Request, MeltQuoteState, MintQuoteState, PreMintSecrets, State, + }, wallet::{ client::{HttpClient, HttpClientMethods}, Wallet, @@ -30,7 +32,7 @@ async fn test_fake_tokens_pending() -> Result<()> { let mint_quote = wallet.mint_quote(100.into(), None).await?; - sleep(Duration::from_millis(5)).await; + wait_for_mint_to_be_paid(&wallet, &mint_quote.id).await?; let _mint_amount = wallet .mint(&mint_quote.id, SplitTarget::default(), None) @@ -70,7 +72,7 @@ async fn test_fake_melt_payment_fail() -> Result<()> { let mint_quote = wallet.mint_quote(100.into(), None).await?; - sleep(Duration::from_millis(5)).await; + wait_for_mint_to_be_paid(&wallet, &mint_quote.id).await?; let _mint_amount = wallet .mint(&mint_quote.id, SplitTarget::default(), None) @@ -133,7 +135,7 @@ async fn test_fake_melt_payment_fail_and_check() -> Result<()> { let mint_quote = wallet.mint_quote(100.into(), None).await?; - sleep(Duration::from_millis(5)).await; + wait_for_mint_to_be_paid(&wallet, &mint_quote.id).await?; let _mint_amount = wallet .mint(&mint_quote.id, SplitTarget::default(), None) @@ -178,7 +180,7 @@ async fn test_fake_melt_payment_return_fail_status() -> Result<()> { let mint_quote = wallet.mint_quote(100.into(), None).await?; - sleep(Duration::from_millis(5)).await; + wait_for_mint_to_be_paid(&wallet, &mint_quote.id).await?; let _mint_amount = wallet .mint(&mint_quote.id, SplitTarget::default(), None) @@ -238,7 +240,7 @@ async fn test_fake_melt_payment_error_unknown() -> Result<()> { let mint_quote = wallet.mint_quote(100.into(), None).await?; - sleep(Duration::from_millis(5)).await; + wait_for_mint_to_be_paid(&wallet, &mint_quote.id).await?; let _mint_amount = wallet .mint(&mint_quote.id, SplitTarget::default(), None) @@ -299,7 +301,7 @@ async fn test_fake_melt_payment_err_paid() -> Result<()> { let mint_quote = wallet.mint_quote(100.into(), None).await?; - sleep(Duration::from_millis(5)).await; + wait_for_mint_to_be_paid(&wallet, &mint_quote.id).await?; let _mint_amount = wallet .mint(&mint_quote.id, SplitTarget::default(), None) @@ -337,7 +339,7 @@ async fn test_fake_melt_change_in_quote() -> Result<()> { let mint_quote = wallet.mint_quote(100.into(), None).await?; - sleep(Duration::from_millis(5)).await; + wait_for_mint_to_be_paid(&wallet, &mint_quote.id).await?; let _mint_amount = wallet .mint(&mint_quote.id, SplitTarget::default(), None) @@ -377,3 +379,15 @@ async fn test_fake_melt_change_in_quote() -> Result<()> { assert_eq!(melt_change, check); Ok(()) } + +// Keep polling the state of the mint quote id until it's paid +async fn wait_for_mint_to_be_paid(wallet: &Wallet, mint_quote_id: &str) -> Result<()> { + loop { + let status = wallet.mint_quote_state(mint_quote_id).await?; + if status.state == MintQuoteState::Paid { + return Ok(()); + } + + sleep(Duration::from_millis(5)).await; + } +}