Skip to content

Commit

Permalink
feat: return multiple quotes for onchain-melt
Browse files Browse the repository at this point in the history
  • Loading branch information
ngutech21 committed Feb 13, 2024
1 parent ac0feea commit 9cfb83c
Show file tree
Hide file tree
Showing 8 changed files with 45 additions and 28 deletions.
13 changes: 11 additions & 2 deletions moksha-cli/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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{}",
Expand All @@ -161,7 +168,7 @@ async fn main() -> anyhow::Result<()> {
return Ok(());
}

let PostMeltOnchainResponse { paid, txid } = wallet.pay_onchain(&quote).await?;
let PostMeltOnchainResponse { paid, txid } = wallet.pay_onchain(quote).await?;
println!("Created transaction: {}\n", &txid);

let mut lock = stdout().lock();
Expand Down Expand Up @@ -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;
Expand Down
6 changes: 4 additions & 2 deletions moksha-core/src/primitives.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -358,9 +359,10 @@ pub struct PostMeltOnchainResponse {
pub txid: String,
}

impl From<OnchainMeltQuote> 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,
Expand Down
3 changes: 3 additions & 0 deletions moksha-mint/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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),

Expand Down
40 changes: 23 additions & 17 deletions moksha-mint/src/routes/btconchain.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};
Expand All @@ -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<Mint>,
Json(request): Json<PostMintQuoteOnchainRequest>,
Expand Down Expand Up @@ -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<String>,
State(mint): State<Mint>,
Expand Down Expand Up @@ -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<Mint>,
Json(request): Json<PostMintOnchainRequest>,
Expand Down Expand Up @@ -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<PostMeltQuoteOnchainResponse>])
),
)]
#[instrument(skip(mint))]
pub async fn post_melt_quote_btconchain(
State(mint): State<Mint>,
Json(melt_request): Json<PostMeltQuoteOnchainRequest>,
) -> Result<Json<PostMeltQuoteOnchainResponse>, MokshaMintError> {
) -> Result<Json<Vec<PostMeltQuoteOnchainResponse>>, MokshaMintError> {
let PostMeltQuoteOnchainRequest {
address,
amount,
Expand Down Expand Up @@ -185,27 +189,21 @@ 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,
fee_sat_per_vbyte: fee_response.sat_per_vbyte,
expiry: quote_onchain_expiry(),
paid: false,
};
Ok(Json(quote.into()))

mint.db.add_onchain_melt_quote(&quote).await?;

Ok(Json(vec![("1 sat per vbyte".to_owned(), quote).into()])) // FIXME return correct comment
}

#[utoipa::path(
Expand All @@ -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<String>,
State(mint): State<Mint>,
Expand All @@ -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(
Expand All @@ -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<Mint>,
Json(melt_request): Json<PostMeltOnchainRequest>,
Expand All @@ -259,7 +265,6 @@ pub async fn post_melt_btconchain(
.await?;

let txid = mint.melt_onchain(&quote, &melt_request.inputs).await?;

let paid = is_onchain_paid(&mint, &quote).await?;

mint.db
Expand All @@ -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<String>,
State(mint): State<Mint>,
Expand Down
5 changes: 1 addition & 4 deletions moksha-mint/src/routes/default.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 >>>>>>>>>>>>>> : {:#?}",
&quote
);
info!("post_melt_bolt11 fee_reserve: {:#?}", &quote);

let (paid, payment_preimage, change) = mint
.melt_bolt11(
Expand Down
2 changes: 1 addition & 1 deletion moksha-wallet/src/client/crossplatform.rs
Original file line number Diff line number Diff line change
Expand Up @@ -185,7 +185,7 @@ impl CashuClient for CrossPlatformHttpClient {
address: String,
amount: u64,
unit: CurrencyUnit,
) -> Result<PostMeltQuoteOnchainResponse, MokshaWalletError> {
) -> Result<Vec<PostMeltQuoteOnchainResponse>, MokshaWalletError> {
let body = PostMeltQuoteOnchainRequest {
address,
amount,
Expand Down
2 changes: 1 addition & 1 deletion moksha-wallet/src/client/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ pub trait CashuClient {
address: String,
amount: u64,
unit: CurrencyUnit,
) -> Result<PostMeltQuoteOnchainResponse, MokshaWalletError>;
) -> Result<Vec<PostMeltQuoteOnchainResponse>, MokshaWalletError>;

async fn get_melt_quote_onchain(
&self,
Expand Down
2 changes: 1 addition & 1 deletion moksha-wallet/src/wallet.rs
Original file line number Diff line number Diff line change
Expand Up @@ -341,7 +341,7 @@ where
&self,
address: String,
amount: u64,
) -> Result<PostMeltQuoteOnchainResponse, MokshaWalletError> {
) -> Result<Vec<PostMeltQuoteOnchainResponse>, MokshaWalletError> {
self.client
.post_melt_quote_onchain(&self.mint_url, address, amount, CurrencyUnit::Sat)
.await
Expand Down

0 comments on commit 9cfb83c

Please sign in to comment.