Răsfoiți Sursa

Improve p2pk.rs

- 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.
Jesus Christ 3 luni în urmă
părinte
comite
a1ac6d3ce1
1 a modificat fișierele cu 21 adăugiri și 16 ștergeri
  1. 21 16
      crates/cdk/examples/p2pk.rs

+ 21 - 16
crates/cdk/examples/p2pk.rs

@@ -1,5 +1,4 @@
 use std::sync::Arc;
-
 use cdk::amount::SplitTarget;
 use cdk::cdk_database::WalletMemoryDatabase;
 use cdk::error::Error;
@@ -11,25 +10,31 @@ use rand::Rng;
 
 #[tokio::main]
 async fn main() -> Result<(), Error> {
+    // Initialize the memory store for the wallet
     let localstore = WalletMemoryDatabase::default();
+    
+    // Generate a random seed for the wallet
     let seed = rand::thread_rng().gen::<[u8; 32]>();
 
+    // Define the mint URL and currency unit
     let mint_url = "https://testnut.cashu.space";
     let unit = CurrencyUnit::Sat;
     let amount = Amount::from(10);
 
-    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)?;
 
-    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!("Minting nuts ...");
 
+    // Subscribe to 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 {
@@ -38,19 +43,20 @@ async fn main() -> Result<(), Error> {
         }
     }
 
-    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?;
 
+    // Generate a secret key for spending conditions
     let secret = SecretKey::generate();
 
+    // Create spending conditions using the generated public key
     let spending_conditions = SpendingConditions::new_p2pk(secret.public_key(), None);
 
-    let bal = wallet.total_balance().await.unwrap();
-
+    // Get the total balance of the wallet
+    let bal = wallet.total_balance().await?;
     println!("{}", bal);
 
+    // Send a token with the specified amount and spending conditions
     let token = wallet
         .send(
             amount,
@@ -60,16 +66,15 @@ async fn main() -> Result<(), Error> {
             &SendKind::default(),
             false,
         )
-        .await
-        .unwrap();
+        .await?;
 
     println!("Created token locked to pubkey: {}", secret.public_key());
     println!("{}", token);
 
+    // Receive the token using the secret key
     let amount = wallet
         .receive(&token.to_string(), SplitTarget::default(), &[secret], &[])
-        .await
-        .unwrap();
+        .await?;
 
     println!("Redeemed locked token worth: {}", u64::from(amount));