Browse Source

feat: restart minting in cdk-cli with quote id

thesimplekid 1 month ago
parent
commit
8f5764aad2
2 changed files with 29 additions and 19 deletions
  1. 28 18
      crates/cdk-cli/src/sub_commands/mint.rs
  2. 1 1
      flake.nix

+ 28 - 18
crates/cdk-cli/src/sub_commands/mint.rs

@@ -1,7 +1,7 @@
 use std::str::FromStr;
 use std::sync::Arc;
 
-use anyhow::Result;
+use anyhow::{anyhow, Result};
 use cdk::amount::SplitTarget;
 use cdk::cdk_database::{Error, WalletDatabase};
 use cdk::mint_url::MintUrl;
@@ -18,13 +18,16 @@ pub struct MintSubCommand {
     /// Mint url
     mint_url: MintUrl,
     /// Amount
-    amount: u64,
+    amount: Option<u64>,
     /// Currency unit e.g. sat
     #[arg(default_value = "sat")]
     unit: String,
     /// Quote description
     #[serde(skip_serializing_if = "Option::is_none")]
     description: Option<String>,
+    /// Quote Id
+    #[arg(short, long)]
+    quote_id: Option<String>,
 }
 
 pub async fn mint(
@@ -50,29 +53,36 @@ pub async fn mint(
         }
     };
 
-    let quote = wallet
-        .mint_quote(Amount::from(sub_command_args.amount), description)
-        .await?;
+    let quote_id = match &sub_command_args.quote_id {
+        None => {
+            let amount = sub_command_args
+                .amount
+                .ok_or(anyhow!("Amount must be defined"))?;
+            let quote = wallet.mint_quote(Amount::from(amount), description).await?;
 
-    println!("Quote: {:#?}", quote);
+            println!("Quote: {:#?}", quote);
 
-    println!("Please pay: {}", quote.request);
+            println!("Please pay: {}", quote.request);
 
-    let mut subscription = wallet
-        .subscribe(WalletSubscription::Bolt11MintQuoteState(vec![quote
-            .id
-            .clone()]))
-        .await;
+            let mut subscription = wallet
+                .subscribe(WalletSubscription::Bolt11MintQuoteState(vec![quote
+                    .id
+                    .clone()]))
+                .await;
 
-    while let Some(msg) = subscription.recv().await {
-        if let NotificationPayload::MintQuoteBolt11Response(response) = msg {
-            if response.state == MintQuoteState::Paid {
-                break;
+            while let Some(msg) = subscription.recv().await {
+                if let NotificationPayload::MintQuoteBolt11Response(response) = msg {
+                    if response.state == MintQuoteState::Paid {
+                        break;
+                    }
+                }
             }
+            quote.id
         }
-    }
+        Some(quote_id) => quote_id.to_string(),
+    };
 
-    let proofs = wallet.mint(&quote.id, SplitTarget::default(), None).await?;
+    let proofs = wallet.mint(&quote_id, SplitTarget::default(), None).await?;
 
     let receive_amount = proofs.total_amount()?;
 

+ 1 - 1
flake.nix

@@ -68,7 +68,7 @@
         # Nightly used for formatting
         nightly_toolchain = pkgs.rust-bin.selectLatestNightlyWith (toolchain: toolchain.default.override {
           targets = [ "wasm32-unknown-unknown" ]; # wasm
-          extensions = [ "rustfmt" "clippy" "rust-src" ];
+          extensions = [ "rustfmt" "clippy" "rust-src" "rust-analyzer" ];
 
         });