Skip to content

Commit

Permalink
fix: cashu endpoints for fedimint 0.3
Browse files Browse the repository at this point in the history
  • Loading branch information
Kodylow committed Mar 20, 2024
1 parent 3f8cfbb commit cfb23e8
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 9 deletions.
2 changes: 1 addition & 1 deletion fedimint-clientd/src/router/handlers/cashu/info.rs
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ pub async fn handle_info(

let response = CashuNUT06InfoResponse {
name: config.global.federation_name().unwrap().to_string(),
pubkey: config.global.federation_id().to_string(),
pubkey: config.global.calculate_federation_id().to_string(),
version: format!("{:?}", config.global.consensus_version),
description: "Cashu <-> Fedimint Soon (tm)".to_string(),
description_long: "Cashu <-> Fedimint Soon (tm)".to_string(),
Expand Down
22 changes: 19 additions & 3 deletions fedimint-clientd/src/router/handlers/cashu/melt/method.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ use fedimint_wallet_client::{WalletClientModule, WithdrawState};
use futures_util::StreamExt;
use lightning_invoice::Bolt11Invoice;
use serde::{Deserialize, Serialize};
use tracing::info;
use tracing::{error, info};

use crate::error::AppError;
use crate::router::handlers::cashu::{Method, Unit};
Expand Down Expand Up @@ -69,7 +69,23 @@ pub async fn melt_bolt11(
amount_msat: Amount,
) -> Result<PostMeltQuoteMethodResponse, AppError> {
let lightning_module = client.get_first_module::<LightningClientModule>();
lightning_module.select_active_gateway().await?;
let gateway_id = match lightning_module.list_gateways().await.first() {
Some(gateway_announcement) => gateway_announcement.info.gateway_id,
None => {
error!("No gateways available");
return Err(AppError::new(
StatusCode::INTERNAL_SERVER_ERROR,
anyhow!("No gateways available"),
))
}
};
let gateway = lightning_module.select_gateway(&gateway_id).await.ok_or_else(|| {
error!("Failed to select gateway");
AppError::new(
StatusCode::INTERNAL_SERVER_ERROR,
anyhow!("Failed to select gateway"),
)
})?;

let bolt11 = Bolt11Invoice::from_str(&request)?;
let bolt11_amount = Amount::from_msats(
Expand All @@ -93,7 +109,7 @@ pub async fn melt_bolt11(
payment_type,
contract_id: _,
fee,
} = lightning_module.pay_bolt11_invoice(bolt11, ()).await?;
} = lightning_module.pay_bolt11_invoice(Some(gateway), bolt11, ()).await?;

let operation_id = payment_type.operation_id();
info!("Gateway fee: {fee}, payment operation id: {operation_id}");
Expand Down
28 changes: 23 additions & 5 deletions fedimint-clientd/src/router/handlers/cashu/mint/method.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,9 @@ use fedimint_core::time::now;
use fedimint_core::Amount;
use fedimint_ln_client::LightningClientModule;
use fedimint_wallet_client::WalletClientModule;
use lightning_invoice::{Bolt11InvoiceDescription, Description};
use serde::{Deserialize, Serialize};
use tracing::error;

use crate::error::AppError;
use crate::router::handlers::cashu::{Method, Unit};
Expand Down Expand Up @@ -63,18 +65,34 @@ pub async fn mint_bolt11(
client: ClientHandleArc,
amount_msat: Amount,
) -> Result<PostMintQuoteMethodResponse, AppError> {
let lightning_module = client.get_first_module::<LightningClientModule>();
lightning_module.select_active_gateway().await?;

let valid_until = now() + Duration::from_secs(DEFAULT_MINT_EXPIRY_OFFSET);
let expiry_time = crate::utils::system_time_to_u64(valid_until)?;
let lightning_module = client.get_first_module::<LightningClientModule>();
let gateway_id = match lightning_module.list_gateways().await.first() {
Some(gateway_announcement) => gateway_announcement.info.gateway_id,
None => {
error!("No gateways available");
return Err(AppError::new(
StatusCode::INTERNAL_SERVER_ERROR,
anyhow!("No gateways available"),
))
}
};
let gateway = lightning_module.select_gateway(&gateway_id).await.ok_or_else(|| {
error!("Failed to select gateway");
AppError::new(
StatusCode::INTERNAL_SERVER_ERROR,
anyhow!("Failed to select gateway"),
)
})?;

let (operation_id, invoice) = lightning_module
let (operation_id, invoice, _) = lightning_module
.create_bolt11_invoice(
amount_msat,
format!("{}, method={:?}", DEFAULT_MINT_DESCRIPTION, Method::Bolt11),
Bolt11InvoiceDescription::Direct(&Description::new(DEFAULT_MINT_DESCRIPTION.to_string())?),
Some(expiry_time),
(),
Some(gateway),
)
.await?;

Expand Down

0 comments on commit cfb23e8

Please sign in to comment.