From 9cfb83c19d7989fa116f43d7d748354c9c9931fa Mon Sep 17 00:00:00 2001 From: ngutech21 Date: Tue, 13 Feb 2024 16:41:59 +0100 Subject: [PATCH] feat: return multiple quotes for onchain-melt --- moksha-cli/src/main.rs | 13 ++++++-- moksha-core/src/primitives.rs | 6 ++-- moksha-mint/src/error.rs | 3 ++ moksha-mint/src/routes/btconchain.rs | 40 +++++++++++++---------- moksha-mint/src/routes/default.rs | 5 +-- moksha-wallet/src/client/crossplatform.rs | 2 +- moksha-wallet/src/client/mod.rs | 2 +- moksha-wallet/src/wallet.rs | 2 +- 8 files changed, 45 insertions(+), 28 deletions(-) diff --git a/moksha-cli/src/main.rs b/moksha-cli/src/main.rs index b6cf1956..cce1dec5 100644 --- a/moksha-cli/src/main.rs +++ b/moksha-cli/src/main.rs @@ -143,7 +143,14 @@ async fn main() -> anyhow::Result<()> { return Ok(()); } - let quote = wallet.pay_onchain_quote(address.clone(), amount).await?; + let quotes = wallet.pay_onchain_quote(address.clone(), amount).await?; + + if quotes.is_empty() { + println!("Error: No quotes found"); + return Ok(()); + } + + let quote = quotes.first().expect("No quotes found"); println!( "Create onchain transaction to melt tokens: amount {} + fee {} = {} (sat)\n\n{}", @@ -161,7 +168,7 @@ async fn main() -> anyhow::Result<()> { return Ok(()); } - let PostMeltOnchainResponse { paid, txid } = wallet.pay_onchain("e).await?; + let PostMeltOnchainResponse { paid, txid } = wallet.pay_onchain(quote).await?; println!("Created transaction: {}\n", &txid); let mut lock = stdout().lock(); @@ -257,6 +264,8 @@ async fn main() -> anyhow::Result<()> { continue; } + // FIXME store quote in db and add option to retry minting later + let mint_result = wallet .mint_tokens(&payment_method, amount.into(), quote.clone()) .await; diff --git a/moksha-core/src/primitives.rs b/moksha-core/src/primitives.rs index 50f31e36..3c39883a 100644 --- a/moksha-core/src/primitives.rs +++ b/moksha-core/src/primitives.rs @@ -340,6 +340,7 @@ pub struct PostMeltQuoteOnchainRequest { #[derive(Deserialize, Serialize, Debug, Clone, ToSchema)] pub struct PostMeltQuoteOnchainResponse { pub quote: String, + pub description: String, pub amount: u64, pub fee: u64, pub paid: bool, @@ -358,9 +359,10 @@ pub struct PostMeltOnchainResponse { pub txid: String, } -impl From for PostMeltQuoteOnchainResponse { - fn from(quote: OnchainMeltQuote) -> Self { +impl From<(String, OnchainMeltQuote)> for PostMeltQuoteOnchainResponse { + fn from((description, quote): (String, OnchainMeltQuote)) -> Self { Self { + description, quote: quote.quote_id.to_string(), amount: quote.amount, fee: quote.fee_total, diff --git a/moksha-mint/src/error.rs b/moksha-mint/src/error.rs index 0ad7a16d..16f468ac 100644 --- a/moksha-mint/src/error.rs +++ b/moksha-mint/src/error.rs @@ -47,6 +47,9 @@ pub enum MokshaMintError { #[error("Lightning invoice not paid yet.")] InvoiceNotPaidYet, + #[error("BTC-Onchain not paid yet.")] + BtcOnchainNotPaidYet, + #[error("Proof already used {0}")] ProofAlreadyUsed(String), diff --git a/moksha-mint/src/routes/btconchain.rs b/moksha-mint/src/routes/btconchain.rs index 86f08aba..e9cb0f2f 100644 --- a/moksha-mint/src/routes/btconchain.rs +++ b/moksha-mint/src/routes/btconchain.rs @@ -8,7 +8,7 @@ use moksha_core::primitives::{ PostMeltQuoteOnchainResponse, PostMintOnchainRequest, PostMintOnchainResponse, PostMintQuoteOnchainRequest, PostMintQuoteOnchainResponse, }; -use tracing::info; +use tracing::{info, instrument}; use uuid::Uuid; use crate::{error::MokshaMintError, mint::Mint}; @@ -23,6 +23,7 @@ use std::str::FromStr; (status = 200, description = "post mint quote", body = [PostMintQuoteOnchainResponse]) ), )] +#[instrument(skip(mint))] pub async fn post_mint_quote_btconchain( State(mint): State, Json(request): Json, @@ -78,6 +79,7 @@ pub async fn post_mint_quote_btconchain( ("quote_id" = String, Path, description = "quote id"), ) )] +#[instrument(skip(mint))] pub async fn get_mint_quote_btconchain( Path(quote_id): Path, State(mint): State, @@ -113,6 +115,7 @@ pub async fn get_mint_quote_btconchain( (status = 200, description = "post mint", body = [PostMintOnchainResponse]) ), )] +#[instrument(skip(mint))] pub async fn post_mint_btconchain( State(mint): State, Json(request): Json, @@ -145,13 +148,14 @@ pub async fn post_mint_btconchain( path = "/v1/melt/quote/btconchain", request_body = PostMeltQuoteOnchainRequest, responses( - (status = 200, description = "post mint quote", body = [PostMeltQuoteOnchainResponse]) + (status = 200, description = "post mint quote", body = [Vec]) ), )] +#[instrument(skip(mint))] pub async fn post_melt_quote_btconchain( State(mint): State, Json(melt_request): Json, -) -> Result, MokshaMintError> { +) -> Result>, MokshaMintError> { let PostMeltQuoteOnchainRequest { address, amount, @@ -185,19 +189,10 @@ pub async fn post_melt_quote_btconchain( .estimate_fee(&address, amount) .await?; - info!( - "post_melt_quote_onchain fee_reserve >>>>>>>>>>>>>> : {:#?}", - &fee_response - ); + info!("post_melt_quote_onchain fee_reserve: {:#?}", &fee_response); - println!( - "post_melt_quote_onchain fee_reserve >>>>>>>>>>>>>> : {:#?}", - &fee_response - ); - - let key = Uuid::new_v4(); let quote = OnchainMeltQuote { - quote_id: key, + quote_id: Uuid::new_v4(), address, amount, fee_total: fee_response.fee_in_sat, @@ -205,7 +200,10 @@ pub async fn post_melt_quote_btconchain( expiry: quote_onchain_expiry(), paid: false, }; - Ok(Json(quote.into())) + + mint.db.add_onchain_melt_quote("e).await?; + + Ok(Json(vec![("1 sat per vbyte".to_owned(), quote).into()])) // FIXME return correct comment } #[utoipa::path( @@ -218,6 +216,7 @@ pub async fn post_melt_quote_btconchain( ("quote_id" = String, Path, description = "quote id"), ) )] +#[instrument(skip(mint))] pub async fn get_melt_quote_btconchain( Path(quote_id): Path, State(mint): State, @@ -238,7 +237,13 @@ pub async fn get_melt_quote_btconchain( .await?; } - Ok(Json(OnchainMeltQuote { paid, ..quote }.into())) + Ok(Json( + ( + "1 sat per vbyte".to_owned(), // FIXME return correct comment + OnchainMeltQuote { paid, ..quote }, + ) + .into(), + )) } #[utoipa::path( @@ -249,6 +254,7 @@ pub async fn get_melt_quote_btconchain( (status = 200, description = "post melt", body = [PostMeltOnchainResponse]) ), )] +#[instrument(skip(mint))] pub async fn post_melt_btconchain( State(mint): State, Json(melt_request): Json, @@ -259,7 +265,6 @@ pub async fn post_melt_btconchain( .await?; let txid = mint.melt_onchain("e, &melt_request.inputs).await?; - let paid = is_onchain_paid(&mint, "e).await?; mint.db @@ -279,6 +284,7 @@ pub async fn post_melt_btconchain( ("tx_id" = String, Path, description = "Bitcoin onchain transaction-id"), ) )] +#[instrument(skip(mint))] pub async fn get_melt_btconchain( Path(tx_id): Path, State(mint): State, diff --git a/moksha-mint/src/routes/default.rs b/moksha-mint/src/routes/default.rs index f861fb3c..da32110d 100644 --- a/moksha-mint/src/routes/default.rs +++ b/moksha-mint/src/routes/default.rs @@ -228,10 +228,7 @@ pub async fn post_melt_bolt11( .get_bolt11_melt_quote(&Uuid::from_str(melt_request.quote.as_str())?) .await?; - println!( - "post_melt_bolt11 fee_reserve >>>>>>>>>>>>>> : {:#?}", - "e - ); + info!("post_melt_bolt11 fee_reserve: {:#?}", "e); let (paid, payment_preimage, change) = mint .melt_bolt11( diff --git a/moksha-wallet/src/client/crossplatform.rs b/moksha-wallet/src/client/crossplatform.rs index 9161c641..3e1295d6 100644 --- a/moksha-wallet/src/client/crossplatform.rs +++ b/moksha-wallet/src/client/crossplatform.rs @@ -185,7 +185,7 @@ impl CashuClient for CrossPlatformHttpClient { address: String, amount: u64, unit: CurrencyUnit, - ) -> Result { + ) -> Result, MokshaWalletError> { let body = PostMeltQuoteOnchainRequest { address, amount, diff --git a/moksha-wallet/src/client/mod.rs b/moksha-wallet/src/client/mod.rs index 47a6f813..d4c5e4dd 100644 --- a/moksha-wallet/src/client/mod.rs +++ b/moksha-wallet/src/client/mod.rs @@ -118,7 +118,7 @@ pub trait CashuClient { address: String, amount: u64, unit: CurrencyUnit, - ) -> Result; + ) -> Result, MokshaWalletError>; async fn get_melt_quote_onchain( &self, diff --git a/moksha-wallet/src/wallet.rs b/moksha-wallet/src/wallet.rs index 7dca7285..517b7634 100644 --- a/moksha-wallet/src/wallet.rs +++ b/moksha-wallet/src/wallet.rs @@ -341,7 +341,7 @@ where &self, address: String, amount: u64, - ) -> Result { + ) -> Result, MokshaWalletError> { self.client .post_melt_quote_onchain(&self.mint_url, address, amount, CurrencyUnit::Sat) .await