Skip to content

Commit

Permalink
refactor: melt quote and melt to v1 in wallet sdk
Browse files Browse the repository at this point in the history
  • Loading branch information
thesimplekid committed Dec 29, 2023
1 parent dcc3509 commit 754936d
Show file tree
Hide file tree
Showing 17 changed files with 308 additions and 83 deletions.
8 changes: 6 additions & 2 deletions bindings/cashu-ffi/src/cashu.udl
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,12 @@ interface Secret {
sequence<u8> as_bytes();
};

interface MintQuoteInfo {
constructor(string id, Amount amount, string unit, Bolt11Invoice? request, boolean paid, u64 boolean);
interface MintQuote {
constructor(string id, Amount amount, string unit, Bolt11Invoice request, boolean paid, u64 boolean);
};

interface MeltQuote {
constructor(string id, Amount amount, string unit, Bolt11Invoice request, Amount fee_reserve, boolean paid, u64 boolean);
};

// NUT00
Expand Down
2 changes: 1 addition & 1 deletion bindings/cashu-ffi/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ mod ffi {
pub use crate::nuts::nut06::{MintInfo, MintVersion};
pub use crate::nuts::nut07::{CheckSpendableRequest, CheckSpendableResponse};
pub use crate::nuts::nut08::{MeltBolt11Request, MeltBolt11Response};
pub use crate::types::{Amount, Bolt11Invoice, KeySetInfo, MintQuoteInfo, Secret};
pub use crate::types::{Amount, Bolt11Invoice, KeySetInfo, MeltQuote, MintQuote, Secret};

// UDL
uniffi::include_scaffolding!("cashu");
Expand Down
49 changes: 49 additions & 0 deletions bindings/cashu-ffi/src/types/melt_quote.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
use std::ops::Deref;
use std::str::FromStr;
use std::sync::Arc;

use cashu::nuts::CurrencyUnit;
use cashu::types::MeltQuote as MeltQuoteSdk;

use crate::{Amount, Bolt11Invoice};

pub struct MeltQuote {
inner: MeltQuoteSdk,
}

impl Deref for MeltQuote {
type Target = MeltQuoteSdk;
fn deref(&self) -> &Self::Target {
&self.inner
}
}

impl From<MeltQuoteSdk> for MeltQuote {
fn from(inner: MeltQuoteSdk) -> MeltQuote {
MeltQuote { inner }
}
}

impl MeltQuote {
pub fn new(
id: String,
amount: Arc<Amount>,
unit: String,
request: Arc<Bolt11Invoice>,
fee_reserve: Arc<Amount>,
paid: bool,
expiry: u64,
) -> Self {
Self {
inner: MeltQuoteSdk {
id,
amount: amount.as_ref().deref().clone(),
unit: CurrencyUnit::from_str(&unit).unwrap(),
request: request.as_ref().deref().clone(),
fee_reserve: fee_reserve.as_ref().deref().clone(),
paid,
expiry,
},
}
}
}
47 changes: 47 additions & 0 deletions bindings/cashu-ffi/src/types/mint_quote.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
use std::ops::Deref;
use std::str::FromStr;
use std::sync::Arc;

use cashu::nuts::CurrencyUnit;
use cashu::types::MintQuote as MintQuoteSdk;

use crate::{Amount, Bolt11Invoice};

pub struct MintQuote {
inner: MintQuoteSdk,
}

impl Deref for MintQuote {
type Target = MintQuoteSdk;
fn deref(&self) -> &Self::Target {
&self.inner
}
}

impl From<MintQuoteSdk> for MintQuote {
fn from(inner: MintQuoteSdk) -> MintQuote {
MintQuote { inner }
}
}

impl MintQuote {
pub fn new(
id: String,
amount: Arc<Amount>,
unit: String,
request: Arc<Bolt11Invoice>,
paid: bool,
expiry: u64,
) -> Self {
Self {
inner: MintQuoteSdk {
id,
amount: amount.as_ref().deref().clone(),
unit: CurrencyUnit::from_str(&unit).unwrap(),
request: request.as_ref().deref().clone(),
paid,
expiry,
},
}
}
}
24 changes: 12 additions & 12 deletions bindings/cashu-ffi/src/types/mint_quote_info.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,42 +3,42 @@ use std::str::FromStr;
use std::sync::Arc;

use cashu::nuts::CurrencyUnit;
use cashu::types::MintQuoteInfo as MintQuoteInfoSdk;
use cashu::types::MintQuote as MintQuoteSdk;

use crate::{Amount, Bolt11Invoice};

pub struct MintQuoteInfo {
inner: MintQuoteInfoSdk,
pub struct MintQuote {
inner: MintQuoteSdk,
}

impl Deref for MintQuoteInfo {
type Target = MintQuoteInfoSdk;
impl Deref for MintQuote {
type Target = MintQuoteSdk;
fn deref(&self) -> &Self::Target {
&self.inner
}
}

impl From<MintQuoteInfoSdk> for MintQuoteInfo {
fn from(inner: MintQuoteInfoSdk) -> MintQuoteInfo {
MintQuoteInfo { inner }
impl From<MintQuoteSdk> for MintQuote {
fn from(inner: MintQuoteSdk) -> MintQuote {
MintQuote { inner }
}
}

impl MintQuoteInfo {
impl MintQuote {
pub fn new(
id: String,
amount: Arc<Amount>,
unit: String,
request: Option<Arc<Bolt11Invoice>>,
request: Arc<Bolt11Invoice>,
paid: bool,
expiry: u64,
) -> Self {
Self {
inner: MintQuoteInfoSdk {
inner: MintQuoteSdk {
id,
amount: amount.as_ref().deref().clone(),
unit: CurrencyUnit::from_str(&unit).unwrap(),
request: request.map(|r| r.as_ref().deref().clone()),
request: request.as_ref().deref().clone(),
paid,
expiry,
},
Expand Down
6 changes: 4 additions & 2 deletions bindings/cashu-ffi/src/types/mod.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
pub mod amount;
pub mod bolt11_invoice;
pub mod keyset_info;
pub mod mint_quote_info;
pub mod melt_quote;
pub mod mint_quote;
pub mod secret;

pub use amount::Amount;
pub use bolt11_invoice::Bolt11Invoice;
pub use keyset_info::KeySetInfo;
pub use mint_quote_info::MintQuoteInfo;
pub use melt_quote::MeltQuote;
pub use mint_quote::MintQuote;
pub use secret::Secret;
12 changes: 9 additions & 3 deletions bindings/cashu-sdk-ffi/src/cashu_sdk.udl
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,13 @@ interface Secret {
sequence<u8> as_bytes();
};

interface MintQuoteInfo {
constructor(string id, Amount amount, string unit, Bolt11Invoice? request, boolean paid, u64 boolean);
interface MintQuote {
constructor(string id, Amount amount, string unit, Bolt11Invoice request, boolean paid, u64 boolean);
};


interface MeltQuote {
constructor(string id, Amount amount, string unit, Bolt11Invoice request, Amount fee_reserve, boolean paid, u64 boolean);
};

// NUT00
Expand Down Expand Up @@ -299,6 +304,7 @@ interface Melted {
};

interface Wallet {
constructor(string mint_url, Keys mint_keys, sequence<MintQuote> mint_quotes, sequence<MeltQuote> melt_quotes);
// [Throws=CashuSdkError]
// ProofsStatus check_proofs_spent(sequence<Proof> proofs);
[Throws=CashuSdkError]
Expand All @@ -312,7 +318,7 @@ interface Wallet {
[Throws=CashuSdkError]
SendProofs send(Amount amount, sequence<Proof> proofs);
[Throws=CashuSdkError]
Melted melt(string quote, sequence<Proof> proofs, Amount fee_reserve);
Melted melt(string quote_id, sequence<Proof> proofs);
[Throws=CashuSdkError]
string proofs_to_token(sequence<Proof> proof, CurrencyUnit? unit, string? memo);
};
Expand Down
9 changes: 4 additions & 5 deletions bindings/cashu-sdk-ffi/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,11 @@ mod ffi {
pub use cashu_ffi::{
Amount, BlindedMessage, BlindedSignature, Bolt11Invoice, CashuError, CheckSpendableRequest,
CheckSpendableResponse, CurrencyUnit, Id, InvoiceStatus, KeyPair, KeySet, KeySetInfo,
KeySetResponse, Keys, KeysResponse, MeltBolt11Request, MeltBolt11Response,
KeySetResponse, Keys, KeysResponse, MeltBolt11Request, MeltBolt11Response, MeltQuote,
MeltQuoteBolt11Request, MeltQuoteBolt11Response, MintBolt11Request, MintBolt11Response,
MintInfo, MintKeySet, MintProof, MintProofs, MintQuoteBolt11Request,
MintQuoteBolt11Response, MintQuoteInfo, MintVersion, Nut05MeltBolt11Request,
Nut05MeltBolt11Response, PreMintSecrets, Proof, PublicKey, Secret, SecretKey, SwapRequest,
SwapResponse, Token,
MintInfo, MintKeySet, MintProof, MintProofs, MintQuote, MintQuoteBolt11Request,
MintQuoteBolt11Response, MintVersion, Nut05MeltBolt11Request, Nut05MeltBolt11Response,
PreMintSecrets, Proof, PublicKey, Secret, SecretKey, SwapRequest, SwapResponse, Token,
};

pub use crate::error::CashuSdkError;
Expand Down
27 changes: 16 additions & 11 deletions bindings/cashu-sdk-ffi/src/wallet.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
use std::ops::Deref;
use std::sync::{Arc, RwLock};

use cashu_ffi::{BlindedSignature, CurrencyUnit, MintQuoteInfo, PreMintSecrets, Proof, Token};
use cashu_ffi::{
BlindedSignature, CurrencyUnit, MeltQuote, MintQuote, PreMintSecrets, Proof, Token,
};
use cashu_sdk::client::minreq_client::HttpClient;
use cashu_sdk::types::ProofsStatus;
use cashu_sdk::url::UncheckedUrl;
Expand All @@ -20,13 +22,22 @@ pub struct Wallet {
}

impl Wallet {
pub fn new(mint_url: &str, mint_keys: Arc<Keys>, quotes: Vec<Arc<MintQuoteInfo>>) -> Self {
pub fn new(
mint_url: String,
mint_keys: Arc<Keys>,
mint_quotes: Vec<Arc<MintQuote>>,
melt_quotes: Vec<Arc<MeltQuote>>,
) -> Self {
let client = HttpClient {};
Self {
inner: WalletSdk::new(
client,
UncheckedUrl::new(mint_url),
quotes
mint_quotes
.into_iter()
.map(|q| q.as_ref().deref().clone())
.collect(),
melt_quotes
.into_iter()
.map(|q| q.as_ref().deref().clone())
.collect(),
Expand Down Expand Up @@ -111,20 +122,14 @@ impl Wallet {
Ok(Arc::new(send_proofs.into()))
}

pub fn melt(
&self,
quote: String,
proofs: Vec<Arc<Proof>>,
fee_reserve: Arc<Amount>,
) -> Result<Arc<Melted>> {
pub fn melt(&self, quote_id: String, proofs: Vec<Arc<Proof>>) -> Result<Arc<Melted>> {
let melted = RUNTIME.block_on(async {
self.inner
.write()
.unwrap()
.melt(
quote,
&quote_id,
proofs.iter().map(|p| p.as_ref().deref().clone()).collect(),
*fee_reserve.as_ref().deref(),
)
.await
})?;
Expand Down
17 changes: 9 additions & 8 deletions bindings/cashu-sdk-js/src/wallet.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,13 @@ impl JsWallet {
let client = HttpClient {};

JsWallet {
inner: Wallet::new(client, mint_url.into(), vec![], mint_keys.deref().clone()),
inner: Wallet::new(
client,
mint_url.into(),
vec![],
vec![],
mint_keys.deref().clone(),
),
}
}

Expand Down Expand Up @@ -123,17 +129,12 @@ impl JsWallet {

/// Melt
#[wasm_bindgen(js_name = melt)]
pub async fn melt(
&self,
quote: String,
proofs: JsValue,
fee_reserve: JsAmount,
) -> Result<JsMelted> {
pub async fn melt(&self, quote: String, proofs: JsValue) -> Result<JsMelted> {
let proofs = serde_wasm_bindgen::from_value(proofs).map_err(into_err)?;

Ok(self
.inner
.melt(quote, proofs, *fee_reserve.deref())
.melt(&quote, proofs)
.await
.map_err(into_err)?
.into())
Expand Down
30 changes: 29 additions & 1 deletion crates/cashu-sdk/src/client/gloo_client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use cashu::nuts::{
};
#[cfg(feature = "nut07")]
use cashu::nuts::{CheckSpendableRequest, CheckSpendableResponse};
use cashu::Amount;
use cashu::{Amount, Bolt11Invoice};
use gloo::net::http::Request;
use serde_json::Value;
use url::Url;
Expand Down Expand Up @@ -118,6 +118,34 @@ impl Client for HttpClient {
}

/// Melt [NUT-05]
async fn post_melt_quote(
&self,
mint_url: Url,
unit: CurrencyUnit,
request: Bolt11Invoice,
) -> Result<MeltQuoteBolt11Response, Error> {
let url = join_url(mint_url, &["v1", "melt", "quote", "bolt11"])?;

let request = MeltQuoteBolt11Request { unit, request };
let res = Request::post(url.as_str())
.json(&request)
.map_err(|err| Error::Gloo(err.to_string()))?
.send()
.await
.map_err(|err| Error::Gloo(err.to_string()))?
.json::<Value>()
.await
.map_err(|err| Error::Gloo(err.to_string()))?;

let response: Result<MeltQuoteBolt11Response, serde_json::Error> =
serde_json::from_value(res.clone());

match response {
Ok(res) => Ok(res),
Err(_) => Err(Error::from_json(&res.to_string())?),
}
}

/// [Nut-08] Lightning fee return if outputs defined
async fn post_melt(
&self,
Expand Down
Loading

0 comments on commit 754936d

Please sign in to comment.