Merge pull request #963 from crodas/feature/wait-for-invoice

Add `wait_for_payment` function in the wallet
This commit is contained in:
C
2025-08-18 08:57:45 -03:00
committed by GitHub
parent da21ce655a
commit f8d58e419f
18 changed files with 299 additions and 301 deletions

View File

@@ -1,6 +1,7 @@
use std::env;
use std::path::PathBuf;
use std::sync::Arc;
use std::time::Duration;
use anyhow::{bail, Result};
use bip39::Mnemonic;
@@ -8,8 +9,8 @@ use cashu::amount::SplitTarget;
use cashu::nut23::Amountless;
use cashu::{Amount, CurrencyUnit, MintRequest, PreMintSecrets, ProofsMethods};
use cdk::wallet::{HttpClient, MintConnector, Wallet};
use cdk_integration_tests::get_mint_url_from_env;
use cdk_integration_tests::init_regtest::{get_cln_dir, get_temp_dir};
use cdk_integration_tests::{get_mint_url_from_env, wait_for_mint_to_be_paid};
use cdk_sqlite::wallet::memory;
use ln_regtest_rs::ln_client::ClnClient;
@@ -115,7 +116,9 @@ async fn test_regtest_bolt12_mint_multiple() -> Result<()> {
.await
.unwrap();
wait_for_mint_to_be_paid(&wallet, &mint_quote.id, 60).await?;
wallet
.wait_for_payment(&mint_quote, Duration::from_secs(60))
.await?;
wallet.mint_bolt12_quote_state(&mint_quote.id).await?;
@@ -127,11 +130,13 @@ async fn test_regtest_bolt12_mint_multiple() -> Result<()> {
assert_eq!(proofs.total_amount().unwrap(), 10.into());
cln_client
.pay_bolt12_offer(Some(11_000), mint_quote.request)
.pay_bolt12_offer(Some(11_000), mint_quote.request.clone())
.await
.unwrap();
wait_for_mint_to_be_paid(&wallet, &mint_quote.id, 60).await?;
wallet
.wait_for_payment(&mint_quote, Duration::from_secs(60))
.await?;
wallet.mint_bolt12_quote_state(&mint_quote.id).await?;
@@ -181,7 +186,11 @@ async fn test_regtest_bolt12_multiple_wallets() -> Result<()> {
cln_client
.pay_bolt12_offer(None, quote_one.request.clone())
.await?;
wait_for_mint_to_be_paid(&wallet_one, &quote_one.id, 60).await?;
wallet_one
.wait_for_payment(&quote_one, Duration::from_secs(60))
.await?;
let proofs_one = wallet_one
.mint_bolt12(&quote_one.id, None, SplitTarget::default(), None)
.await?;
@@ -195,7 +204,10 @@ async fn test_regtest_bolt12_multiple_wallets() -> Result<()> {
cln_client
.pay_bolt12_offer(None, quote_two.request.clone())
.await?;
wait_for_mint_to_be_paid(&wallet_two, &quote_two.id, 60).await?;
wallet_two
.wait_for_payment(&quote_two, Duration::from_secs(60))
.await?;
let proofs_two = wallet_two
.mint_bolt12(&quote_two.id, None, SplitTarget::default(), None)
@@ -271,7 +283,9 @@ async fn test_regtest_bolt12_melt() -> Result<()> {
.await?;
// Wait for payment to be processed
wait_for_mint_to_be_paid(&wallet, &mint_quote.id, 60).await?;
wallet
.wait_for_payment(&mint_quote, Duration::from_secs(60))
.await?;
let offer = cln_client
.get_bolt12_offer(Some(10_000), true, "hhhhhhhh".to_string())
@@ -330,7 +344,9 @@ async fn test_regtest_bolt12_mint_extra() -> Result<()> {
.pay_bolt12_offer(Some(pay_amount_msats), mint_quote.request.clone())
.await?;
wait_for_mint_to_be_paid(&wallet, &mint_quote.id, 10).await?;
wallet
.wait_for_payment(&mint_quote, Duration::from_secs(10))
.await?;
let state = wallet.mint_bolt12_quote_state(&mint_quote.id).await?;

View File

@@ -1,6 +1,7 @@
use std::env;
use std::str::FromStr;
use std::sync::Arc;
use std::time::Duration;
use bip39::Mnemonic;
use cashu::{MintAuthRequest, MintInfo};
@@ -15,7 +16,7 @@ use cdk::nuts::{
use cdk::wallet::{AuthHttpClient, AuthMintConnector, HttpClient, MintConnector, WalletBuilder};
use cdk::{Error, OidcClient};
use cdk_fake_wallet::create_fake_invoice;
use cdk_integration_tests::{fund_wallet, wait_for_mint_to_be_paid};
use cdk_integration_tests::fund_wallet;
use cdk_sqlite::wallet::memory;
const MINT_URL: &str = "http://127.0.0.1:8087";
@@ -329,19 +330,12 @@ async fn test_mint_with_auth() {
let mint_amount: Amount = 100.into();
let mint_quote = wallet
.mint_quote(mint_amount, None)
let (_, proofs) = wallet
.mint_once_paid(mint_amount, None, Duration::from_secs(10))
.await
.expect("failed to get mint quote");
.unwrap();
wait_for_mint_to_be_paid(&wallet, &mint_quote.id, 60)
.await
.expect("failed to wait for payment");
let proofs = wallet
.mint(&mint_quote.id, SplitTarget::default(), None)
.await
.expect("could not mint");
let proofs = proofs.await.expect("could not mint");
assert!(proofs.total_amount().expect("Could not get proofs amount") == mint_amount);
}

View File

@@ -15,6 +15,7 @@
//! - Duplicate proof detection
use std::sync::Arc;
use std::time::Duration;
use bip39::Mnemonic;
use cashu::Amount;
@@ -27,7 +28,7 @@ use cdk::nuts::{
use cdk::wallet::types::TransactionDirection;
use cdk::wallet::{HttpClient, MintConnector, Wallet};
use cdk_fake_wallet::{create_fake_invoice, FakeInvoiceDescription};
use cdk_integration_tests::{attempt_to_swap_pending, wait_for_mint_to_be_paid};
use cdk_integration_tests::attempt_to_swap_pending;
use cdk_sqlite::wallet::memory;
const MINT_URL: &str = "http://127.0.0.1:8086";
@@ -46,7 +47,8 @@ async fn test_fake_tokens_pending() {
let mint_quote = wallet.mint_quote(100.into(), None).await.unwrap();
wait_for_mint_to_be_paid(&wallet, &mint_quote.id, 60)
wallet
.wait_for_payment(&mint_quote, Duration::from_secs(60))
.await
.unwrap();
@@ -88,7 +90,8 @@ async fn test_fake_melt_payment_fail() {
let mint_quote = wallet.mint_quote(100.into(), None).await.unwrap();
wait_for_mint_to_be_paid(&wallet, &mint_quote.id, 60)
wallet
.wait_for_payment(&mint_quote, Duration::from_secs(60))
.await
.unwrap();
@@ -153,7 +156,8 @@ async fn test_fake_melt_payment_fail_and_check() {
let mint_quote = wallet.mint_quote(100.into(), None).await.unwrap();
wait_for_mint_to_be_paid(&wallet, &mint_quote.id, 60)
wallet
.wait_for_payment(&mint_quote, Duration::from_secs(60))
.await
.unwrap();
@@ -201,7 +205,8 @@ async fn test_fake_melt_payment_return_fail_status() {
let mint_quote = wallet.mint_quote(100.into(), None).await.unwrap();
wait_for_mint_to_be_paid(&wallet, &mint_quote.id, 60)
wallet
.wait_for_payment(&mint_quote, Duration::from_secs(60))
.await
.unwrap();
@@ -264,7 +269,8 @@ async fn test_fake_melt_payment_error_unknown() {
let mint_quote = wallet.mint_quote(100.into(), None).await.unwrap();
wait_for_mint_to_be_paid(&wallet, &mint_quote.id, 60)
wallet
.wait_for_payment(&mint_quote, Duration::from_secs(60))
.await
.unwrap();
@@ -327,7 +333,8 @@ async fn test_fake_melt_payment_err_paid() {
let mint_quote = wallet.mint_quote(100.into(), None).await.unwrap();
wait_for_mint_to_be_paid(&wallet, &mint_quote.id, 60)
wallet
.wait_for_payment(&mint_quote, Duration::from_secs(60))
.await
.unwrap();
@@ -368,7 +375,8 @@ async fn test_fake_melt_change_in_quote() {
let mint_quote = wallet.mint_quote(100.into(), None).await.unwrap();
wait_for_mint_to_be_paid(&wallet, &mint_quote.id, 60)
wallet
.wait_for_payment(&mint_quote, Duration::from_secs(60))
.await
.unwrap();
@@ -437,7 +445,8 @@ async fn test_fake_mint_with_witness() {
.expect("failed to create new wallet");
let mint_quote = wallet.mint_quote(100.into(), None).await.unwrap();
wait_for_mint_to_be_paid(&wallet, &mint_quote.id, 60)
wallet
.wait_for_payment(&mint_quote, Duration::from_secs(60))
.await
.unwrap();
@@ -465,7 +474,8 @@ async fn test_fake_mint_without_witness() {
let mint_quote = wallet.mint_quote(100.into(), None).await.unwrap();
wait_for_mint_to_be_paid(&wallet, &mint_quote.id, 60)
wallet
.wait_for_payment(&mint_quote, Duration::from_secs(60))
.await
.unwrap();
@@ -505,7 +515,8 @@ async fn test_fake_mint_with_wrong_witness() {
let mint_quote = wallet.mint_quote(100.into(), None).await.unwrap();
wait_for_mint_to_be_paid(&wallet, &mint_quote.id, 60)
wallet
.wait_for_payment(&mint_quote, Duration::from_secs(60))
.await
.unwrap();
@@ -551,7 +562,8 @@ async fn test_fake_mint_inflated() {
let mint_quote = wallet.mint_quote(100.into(), None).await.unwrap();
wait_for_mint_to_be_paid(&wallet, &mint_quote.id, 60)
wallet
.wait_for_payment(&mint_quote, Duration::from_secs(60))
.await
.unwrap();
@@ -609,7 +621,8 @@ async fn test_fake_mint_multiple_units() {
let mint_quote = wallet.mint_quote(100.into(), None).await.unwrap();
wait_for_mint_to_be_paid(&wallet, &mint_quote.id, 60)
wallet
.wait_for_payment(&mint_quote, Duration::from_secs(60))
.await
.unwrap();
@@ -688,7 +701,8 @@ async fn test_fake_mint_multiple_unit_swap() {
let mint_quote = wallet.mint_quote(100.into(), None).await.unwrap();
wait_for_mint_to_be_paid(&wallet, &mint_quote.id, 60)
wallet
.wait_for_payment(&mint_quote, Duration::from_secs(60))
.await
.unwrap();
@@ -709,7 +723,8 @@ async fn test_fake_mint_multiple_unit_swap() {
let mint_quote = wallet_usd.mint_quote(100.into(), None).await.unwrap();
wait_for_mint_to_be_paid(&wallet_usd, &mint_quote.id, 60)
wallet
.wait_for_payment(&mint_quote, Duration::from_secs(60))
.await
.unwrap();
@@ -802,7 +817,8 @@ async fn test_fake_mint_multiple_unit_melt() {
let mint_quote = wallet.mint_quote(100.into(), None).await.unwrap();
wait_for_mint_to_be_paid(&wallet, &mint_quote.id, 60)
wallet
.wait_for_payment(&mint_quote, Duration::from_secs(60))
.await
.unwrap();
@@ -825,7 +841,8 @@ async fn test_fake_mint_multiple_unit_melt() {
let mint_quote = wallet_usd.mint_quote(100.into(), None).await.unwrap();
println!("Minted quote usd");
wait_for_mint_to_be_paid(&wallet_usd, &mint_quote.id, 60)
wallet
.wait_for_payment(&mint_quote, Duration::from_secs(60))
.await
.unwrap();
@@ -920,7 +937,8 @@ async fn test_fake_mint_input_output_mismatch() {
let mint_quote = wallet.mint_quote(100.into(), None).await.unwrap();
wait_for_mint_to_be_paid(&wallet, &mint_quote.id, 60)
wallet
.wait_for_payment(&mint_quote, Duration::from_secs(60))
.await
.unwrap();
@@ -978,7 +996,8 @@ async fn test_fake_mint_swap_inflated() {
let mint_quote = wallet.mint_quote(100.into(), None).await.unwrap();
wait_for_mint_to_be_paid(&wallet, &mint_quote.id, 60)
wallet
.wait_for_payment(&mint_quote, Duration::from_secs(60))
.await
.unwrap();
@@ -1022,7 +1041,8 @@ async fn test_fake_mint_swap_spend_after_fail() {
let mint_quote = wallet.mint_quote(100.into(), None).await.unwrap();
wait_for_mint_to_be_paid(&wallet, &mint_quote.id, 60)
wallet
.wait_for_payment(&mint_quote, Duration::from_secs(60))
.await
.unwrap();
@@ -1093,7 +1113,8 @@ async fn test_fake_mint_melt_spend_after_fail() {
let mint_quote = wallet.mint_quote(100.into(), None).await.unwrap();
wait_for_mint_to_be_paid(&wallet, &mint_quote.id, 60)
wallet
.wait_for_payment(&mint_quote, Duration::from_secs(60))
.await
.unwrap();
@@ -1165,7 +1186,8 @@ async fn test_fake_mint_duplicate_proofs_swap() {
let mint_quote = wallet.mint_quote(100.into(), None).await.unwrap();
wait_for_mint_to_be_paid(&wallet, &mint_quote.id, 60)
wallet
.wait_for_payment(&mint_quote, Duration::from_secs(60))
.await
.unwrap();
@@ -1245,7 +1267,8 @@ async fn test_fake_mint_duplicate_proofs_melt() {
let mint_quote = wallet.mint_quote(100.into(), None).await.unwrap();
wait_for_mint_to_be_paid(&wallet, &mint_quote.id, 60)
wallet
.wait_for_payment(&mint_quote, Duration::from_secs(60))
.await
.unwrap();

View File

@@ -22,9 +22,7 @@ use cdk::amount::{Amount, SplitTarget};
use cdk::nuts::nut00::ProofsMethods;
use cdk::nuts::{CurrencyUnit, MeltQuoteState, NotificationPayload, State};
use cdk::wallet::{HttpClient, MintConnector, Wallet};
use cdk_integration_tests::{
create_invoice_for_env, get_mint_url_from_env, pay_if_regtest, wait_for_mint_to_be_paid,
};
use cdk_integration_tests::{create_invoice_for_env, get_mint_url_from_env, pay_if_regtest};
use cdk_sqlite::wallet::memory;
use futures::{SinkExt, StreamExt};
use lightning_invoice::Bolt11Invoice;
@@ -111,7 +109,8 @@ async fn test_happy_mint_melt_round_trip() {
.await
.unwrap();
wait_for_mint_to_be_paid(&wallet, &mint_quote.id, 10)
wallet
.wait_for_payment(&mint_quote, Duration::from_secs(60))
.await
.unwrap();
@@ -234,7 +233,8 @@ async fn test_happy_mint() {
.await
.unwrap();
wait_for_mint_to_be_paid(&wallet, &mint_quote.id, 60)
wallet
.wait_for_payment(&mint_quote, Duration::from_secs(60))
.await
.unwrap();
@@ -281,7 +281,8 @@ async fn test_restore() {
.await
.unwrap();
wait_for_mint_to_be_paid(&wallet, &mint_quote.id, 60)
wallet
.wait_for_payment(&mint_quote, Duration::from_secs(60))
.await
.unwrap();
@@ -360,7 +361,8 @@ async fn test_fake_melt_change_in_quote() {
pay_if_regtest(&get_test_temp_dir(), &bolt11).await.unwrap();
wait_for_mint_to_be_paid(&wallet, &mint_quote.id, 60)
wallet
.wait_for_payment(&mint_quote, Duration::from_secs(60))
.await
.unwrap();
@@ -431,7 +433,8 @@ async fn test_pay_invoice_twice() {
.await
.unwrap();
wait_for_mint_to_be_paid(&wallet, &mint_quote.id, 60)
wallet
.wait_for_payment(&mint_quote, Duration::from_secs(60))
.await
.unwrap();

View File

@@ -27,9 +27,7 @@ use cdk::nuts::{
};
use cdk::wallet::{HttpClient, MintConnector, Wallet, WalletSubscription};
use cdk_integration_tests::init_regtest::{get_lnd_dir, LND_RPC_ADDR};
use cdk_integration_tests::{
get_mint_url_from_env, get_second_mint_url_from_env, wait_for_mint_to_be_paid,
};
use cdk_integration_tests::{get_mint_url_from_env, get_second_mint_url_from_env};
use cdk_sqlite::wallet::{self, memory};
use futures::join;
use ln_regtest_rs::ln_client::{LightningClient, LndClient};
@@ -84,11 +82,12 @@ async fn test_internal_payment() {
let mint_quote = wallet.mint_quote(100.into(), None).await.unwrap();
lnd_client
.pay_invoice(mint_quote.request)
.pay_invoice(mint_quote.request.clone())
.await
.expect("failed to pay invoice");
wait_for_mint_to_be_paid(&wallet, &mint_quote.id, 60)
wallet
.wait_for_payment(&mint_quote, Duration::from_secs(60))
.await
.unwrap();
@@ -119,7 +118,8 @@ async fn test_internal_payment() {
let _melted = wallet.melt(&melt.id).await.unwrap();
wait_for_mint_to_be_paid(&wallet_2, &mint_quote.id, 60)
wallet_2
.wait_for_payment(&mint_quote, Duration::from_secs(60))
.await
.unwrap();
@@ -261,7 +261,8 @@ async fn test_multimint_melt() {
.pay_invoice(quote.request.clone())
.await
.expect("failed to pay invoice");
wait_for_mint_to_be_paid(&wallet1, &quote.id, 60)
wallet1
.wait_for_payment(&quote, Duration::from_secs(60))
.await
.unwrap();
wallet1
@@ -274,7 +275,8 @@ async fn test_multimint_melt() {
.pay_invoice(quote.request.clone())
.await
.expect("failed to pay invoice");
wait_for_mint_to_be_paid(&wallet2, &quote.id, 60)
wallet2
.wait_for_payment(&quote, Duration::from_secs(60))
.await
.unwrap();
wallet2
@@ -334,7 +336,8 @@ async fn test_cached_mint() {
.await
.expect("failed to pay invoice");
wait_for_mint_to_be_paid(&wallet, &quote.id, 60)
wallet
.wait_for_payment(&quote, Duration::from_secs(60))
.await
.unwrap();

View File

@@ -1,5 +1,6 @@
use std::str::FromStr;
use std::sync::Arc;
use std::time::Duration;
use bip39::Mnemonic;
use cashu::{Bolt11Invoice, ProofsMethods};
@@ -7,9 +8,7 @@ use cdk::amount::{Amount, SplitTarget};
use cdk::nuts::CurrencyUnit;
use cdk::wallet::{ReceiveOptions, SendKind, SendOptions, Wallet};
use cdk_integration_tests::init_regtest::get_temp_dir;
use cdk_integration_tests::{
create_invoice_for_env, get_mint_url_from_env, pay_if_regtest, wait_for_mint_to_be_paid,
};
use cdk_integration_tests::{create_invoice_for_env, get_mint_url_from_env, pay_if_regtest};
use cdk_sqlite::wallet::memory;
#[tokio::test(flavor = "multi_thread", worker_threads = 1)]
@@ -29,7 +28,8 @@ async fn test_swap() {
let invoice = Bolt11Invoice::from_str(&mint_quote.request).unwrap();
pay_if_regtest(&get_temp_dir(), &invoice).await.unwrap();
wait_for_mint_to_be_paid(&wallet, &mint_quote.id, 10)
wallet
.wait_for_payment(&mint_quote, Duration::from_secs(60))
.await
.unwrap();
@@ -96,7 +96,8 @@ async fn test_fake_melt_change_in_quote() {
pay_if_regtest(&get_temp_dir(), &bolt11).await.unwrap();
wait_for_mint_to_be_paid(&wallet, &mint_quote.id, 60)
wallet
.wait_for_payment(&mint_quote, Duration::from_secs(60))
.await
.unwrap();