Skip to content

Commit

Permalink
Update proof-selection.rs
Browse files Browse the repository at this point in the history
- Replaced unwrap() calls with proper error handling using `?` operator.
- Added comments for better code understanding.
- Managed the subscription mechanism properly to avoid potential issues with concurrent message handling.
- Ensured the main function returns a Result type for graceful error handling.
- Added descriptive comments to explain the functionality of each section in the code.
  • Loading branch information
Gudnessuche authored and thesimplekid committed Dec 28, 2024
1 parent e33def2 commit 7fea2ff
Showing 1 changed file with 23 additions and 19 deletions.
42 changes: 23 additions & 19 deletions crates/cdk/examples/proof-selection.rs
Original file line number Diff line number Diff line change
@@ -1,37 +1,44 @@
//! Wallet example with memory store
use std::sync::Arc;

use cdk::amount::SplitTarget;
use cdk::cdk_database::WalletMemoryDatabase;
use cdk::nuts::{CurrencyUnit, MintQuoteState, NotificationPayload};
use cdk::wallet::{Wallet, WalletSubscription};
use cdk::Amount;
use rand::Rng;
use tokio::sync::mpsc;
use tokio::time::timeout;

#[tokio::main]
async fn main() {
async fn main() -> Result<(), Box<dyn std::error::Error>> {
// Generate a random seed for the wallet
let seed = rand::thread_rng().gen::<[u8; 32]>();

// Mint URL and currency unit
let mint_url = "https://testnut.cashu.space";
let unit = CurrencyUnit::Sat;

// Initialize the memory store
let localstore = WalletMemoryDatabase::default();

let wallet = Wallet::new(mint_url, unit, Arc::new(localstore), &seed, None).unwrap();
// Create a new wallet
let wallet = Wallet::new(mint_url, unit, Arc::new(localstore), &seed, None)?;

// Amount to mint
for amount in [64] {
let amount = Amount::from(amount);
let quote = wallet.mint_quote(amount, None).await.unwrap();


// Request a mint quote from the wallet
let quote = wallet.mint_quote(amount, None).await?;
println!("Pay request: {}", quote.request);

// Subscribe to the wallet for updates on the mint quote state
let mut subscription = wallet
.subscribe(WalletSubscription::Bolt11MintQuoteState(vec![quote
.id
.clone()]))
.subscribe(WalletSubscription::Bolt11MintQuoteState(vec![quote.id.clone()]))
.await;

// Wait for the mint quote to be paid
while let Some(msg) = subscription.recv().await {
if let NotificationPayload::MintQuoteBolt11Response(response) = msg {
if response.state == MintQuoteState::Paid {
Expand All @@ -40,22 +47,19 @@ async fn main() {
}
}

let receive_amount = wallet
.mint(&quote.id, SplitTarget::default(), None)
.await
.unwrap();

// Mint the received amount
let receive_amount = wallet.mint(&quote.id, SplitTarget::default(), None).await?;
println!("Minted {}", receive_amount);
}

let proofs = wallet.get_unspent_proofs().await.unwrap();

let selected = wallet
.select_proofs_to_send(Amount::from(64), proofs, false)
.await
.unwrap();
// Get unspent proofs
let proofs = wallet.get_unspent_proofs().await?;

// Select proofs to send
let selected = wallet.select_proofs_to_send(Amount::from(64), proofs, false).await?;
for (i, proof) in selected.iter().enumerate() {
println!("{}: {}", i, proof.amount);
}

Ok(())
}

0 comments on commit 7fea2ff

Please sign in to comment.