Pārlūkot izejas kodu

feat: add mint and melt quotes in mint

thesimplekid 1 gadu atpakaļ
vecāks
revīzija
bde170b3ca

+ 30 - 0
crates/cashu-sdk/src/mint/mod.rs

@@ -8,6 +8,7 @@ use cashu::nuts::{
 #[cfg(feature = "nut07")]
 use cashu::nuts::{CheckSpendableRequest, CheckSpendableResponse};
 use cashu::secret::Secret;
+use cashu::types::{MeltQuote, MintQuote};
 use cashu::Amount;
 use serde::{Deserialize, Serialize};
 use thiserror::Error;
@@ -101,6 +102,35 @@ impl<L: LocalStore> Mint<L> {
         })
     }
 
+    pub async fn new_mint_quote(
+        &self,
+        request: String,
+        unit: CurrencyUnit,
+        amount: Amount,
+        expiry: u64,
+    ) -> Result<MintQuote, Error> {
+        let quote = MintQuote::new(request, unit, amount, expiry);
+
+        self.localstore.add_mint_quote(quote.clone()).await?;
+
+        Ok(quote)
+    }
+
+    pub async fn new_melt_quote(
+        &self,
+        request: String,
+        unit: CurrencyUnit,
+        amount: Amount,
+        fee_reserve: Amount,
+        expiry: u64,
+    ) -> Result<MeltQuote, Error> {
+        let quote = MeltQuote::new(request, unit, amount, fee_reserve, expiry);
+
+        self.localstore.add_melt_quote(quote.clone()).await?;
+
+        Ok(quote)
+    }
+
     /// Retrieve the public keys of the active keyset for distribution to
     /// wallet clients
     pub async fn keyset_pubkeys(&self, keyset_id: &Id) -> Result<Option<KeysResponse>, Error> {

+ 7 - 3
crates/cashu-sdk/src/wallet/mod.rs

@@ -194,7 +194,7 @@ impl<C: Client, L: LocalStore> Wallet<C, L> {
             id: quote_res.quote.clone(),
             amount,
             unit: unit.clone(),
-            request: Bolt11Invoice::from_str(&quote_res.request).unwrap(),
+            request: quote_res.request,
             paid: quote_res.paid,
             expiry: quote_res.expiry,
         };
@@ -496,11 +496,15 @@ impl<C: Client, L: LocalStore> Wallet<C, L> {
         &mut self,
         mint_url: UncheckedUrl,
         unit: CurrencyUnit,
-        request: Bolt11Invoice,
+        request: String,
     ) -> Result<MeltQuote, Error> {
         let quote_res = self
             .client
-            .post_melt_quote(mint_url.clone().try_into()?, unit.clone(), request.clone())
+            .post_melt_quote(
+                mint_url.clone().try_into()?,
+                unit.clone(),
+                Bolt11Invoice::from_str(&request.clone()).unwrap(),
+            )
             .await?;
 
         let quote = MeltQuote {

+ 1 - 0
crates/cashu/Cargo.toml

@@ -38,6 +38,7 @@ url = { workspace = true }
 regex = "1.8.4"
 itertools = "0.11.0"
 thiserror = { workspace = true }
+uuid = { version = "1.6.1", features = ["v4"] }
 
 [dev-dependencies]
 # tokio = {version = "1.27.0", features = ["rt", "macros"] }

+ 42 - 4
crates/cashu/src/types.rs

@@ -1,9 +1,10 @@
 //! Types for `cashu-crab`
 
 use serde::{Deserialize, Serialize};
+use uuid::Uuid;
 
 use crate::nuts::{CurrencyUnit, Id, Proofs};
-use crate::{Amount, Bolt11Invoice};
+use crate::Amount;
 
 #[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
 pub struct ProofsStatus {
@@ -34,23 +35,60 @@ pub struct MintQuote {
     pub id: String,
     pub amount: Amount,
     pub unit: CurrencyUnit,
-    pub request: Bolt11Invoice,
+    pub request: String,
     pub paid: bool,
     pub expiry: u64,
 }
 
+impl MintQuote {
+    pub fn new(request: String, unit: CurrencyUnit, amount: Amount, expiry: u64) -> Self {
+        let id = Uuid::new_v4();
+
+        Self {
+            id: id.to_string(),
+            amount,
+            unit,
+            request,
+            paid: false,
+            expiry,
+        }
+    }
+}
+
 /// Melt Quote Info
 #[derive(Debug, Serialize, Deserialize, Clone, PartialEq)]
 pub struct MeltQuote {
     pub id: String,
-    pub amount: Amount,
-    pub request: Bolt11Invoice,
     pub unit: CurrencyUnit,
+    pub amount: Amount,
+    pub request: String,
     pub fee_reserve: Amount,
     pub paid: bool,
     pub expiry: u64,
 }
 
+impl MeltQuote {
+    pub fn new(
+        request: String,
+        unit: CurrencyUnit,
+        amount: Amount,
+        fee_reserve: Amount,
+        expiry: u64,
+    ) -> Self {
+        let id = Uuid::new_v4();
+
+        Self {
+            id: id.to_string(),
+            amount,
+            unit,
+            request,
+            fee_reserve,
+            paid: false,
+            expiry,
+        }
+    }
+}
+
 /// Keyset id
 #[derive(Debug, Hash, Clone, PartialEq, Eq, Serialize, Deserialize)]
 pub struct KeysetInfo {