mirror of
https://github.com/aljazceru/cdk.git
synced 2025-12-23 23:55:01 +01:00
Introduce subscription support in the Wallet crate.
The main goal is to add a subscription to CDK Mint updates into the wallet. This feature will be particularly useful for improving the code whenever loops hit the mint server to check status changes. The goal is to add an easy-to-use interface that will hide the fact that we're connecting to WebSocket and subscribing to events. This will also hide the fact that the CDK-mint server may not support WebSocket updates. To be fully backward compatible, the HttpClientMethods traits have a new method, `subscribe,` which will return an object that implements `ActiveSubscription.` In the primary implementation, there is a `SubscriptionClient` that will attempt to connect through WebSocket and will fall to the HTTP-status pull and sleep approach (the current approach), but upper stream code will receive updates as if they come from a stream of updates through WebSocket. This `SubscriptionClient` struct will also manage reconnections to WebSockets (with automatic resubscriptions) and all the low-level stuff, providing an easy-to-use interface and leaving the upper-level code with a nice interface that is hard to misuse. When `ActiveSubscription` is dropped, it will automatically unsubscribe. Fixed bug with Default as described in https://github.com/cashubtc/cdk/pull/473#discussion_r1871032297
This commit is contained in:
committed by
thesimplekid
parent
86c4c2dfeb
commit
760564cee0
@@ -1,7 +1,6 @@
|
||||
use std::collections::{HashMap, HashSet};
|
||||
use std::str::FromStr;
|
||||
use std::sync::Arc;
|
||||
use std::time::Duration;
|
||||
|
||||
use anyhow::{bail, Result};
|
||||
use axum::Router;
|
||||
@@ -12,17 +11,19 @@ use cdk::cdk_lightning::MintLightning;
|
||||
use cdk::dhke::construct_proofs;
|
||||
use cdk::mint::FeeReserve;
|
||||
use cdk::mint_url::MintUrl;
|
||||
use cdk::nuts::nut17::Params;
|
||||
use cdk::nuts::{
|
||||
CurrencyUnit, Id, KeySet, MintBolt11Request, MintInfo, MintQuoteBolt11Request, MintQuoteState,
|
||||
Nuts, PaymentMethod, PreMintSecrets, Proofs, State,
|
||||
NotificationPayload, Nuts, PaymentMethod, PreMintSecrets, Proofs, State,
|
||||
};
|
||||
use cdk::types::{LnKey, QuoteTTL};
|
||||
use cdk::wallet::client::{HttpClient, MintConnector};
|
||||
use cdk::wallet::subscription::SubscriptionManager;
|
||||
use cdk::wallet::WalletSubscription;
|
||||
use cdk::{Mint, Wallet};
|
||||
use cdk_fake_wallet::FakeWallet;
|
||||
use init_regtest::{get_mint_addr, get_mint_port, get_mint_url};
|
||||
use tokio::sync::Notify;
|
||||
use tokio::time::sleep;
|
||||
use tower_http::cors::CorsLayer;
|
||||
|
||||
pub mod init_fake_wallet;
|
||||
@@ -129,15 +130,18 @@ pub async fn wallet_mint(
|
||||
) -> Result<()> {
|
||||
let quote = wallet.mint_quote(amount, description).await?;
|
||||
|
||||
loop {
|
||||
let status = wallet.mint_quote_state("e.id).await?;
|
||||
let mut subscription = wallet
|
||||
.subscribe(WalletSubscription::Bolt11MintQuoteState(vec![quote
|
||||
.id
|
||||
.clone()]))
|
||||
.await;
|
||||
|
||||
if status.state == MintQuoteState::Paid {
|
||||
break;
|
||||
while let Some(msg) = subscription.recv().await {
|
||||
if let NotificationPayload::MintQuoteBolt11Response(response) = msg {
|
||||
if response.state == MintQuoteState::Paid {
|
||||
break;
|
||||
}
|
||||
}
|
||||
println!("{:?}", status);
|
||||
|
||||
sleep(Duration::from_secs(2)).await;
|
||||
}
|
||||
|
||||
let receive_amount = wallet.mint("e.id, split_target, None).await?;
|
||||
@@ -169,17 +173,25 @@ pub async fn mint_proofs(
|
||||
|
||||
println!("Please pay: {}", mint_quote.request);
|
||||
|
||||
loop {
|
||||
let status = wallet_client
|
||||
.get_mint_quote_status(&mint_quote.quote)
|
||||
.await?;
|
||||
let subscription_client = SubscriptionManager::new(Arc::new(wallet_client.clone()));
|
||||
|
||||
if status.state == MintQuoteState::Paid {
|
||||
break;
|
||||
let mut subscription = subscription_client
|
||||
.subscribe(
|
||||
mint_url.parse()?,
|
||||
Params {
|
||||
filters: vec![mint_quote.quote.clone()],
|
||||
kind: cdk::nuts::nut17::Kind::Bolt11MintQuote,
|
||||
id: "sub".into(),
|
||||
},
|
||||
)
|
||||
.await;
|
||||
|
||||
while let Some(msg) = subscription.recv().await {
|
||||
if let NotificationPayload::MintQuoteBolt11Response(response) = msg {
|
||||
if response.state == MintQuoteState::Paid {
|
||||
break;
|
||||
}
|
||||
}
|
||||
println!("{:?}", status.state);
|
||||
|
||||
sleep(Duration::from_secs(2)).await;
|
||||
}
|
||||
|
||||
let premint_secrets = PreMintSecrets::random(keyset_id, amount, &SplitTarget::default())?;
|
||||
|
||||
Reference in New Issue
Block a user