Skip to content

Commit

Permalink
fix: remove unwraps
Browse files Browse the repository at this point in the history
  • Loading branch information
thesimplekid committed Apr 7, 2024
1 parent 36d53c2 commit 0e87d59
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 30 deletions.
11 changes: 8 additions & 3 deletions src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@ use cashu_sdk::lightning_invoice::ParseOrSemanticError;
pub enum Error {
DecodeInvoice,
StatusCode(StatusCode),
_Ln(ln_rs::Error),
Ln(ln_rs::Error),
Custom(String),
}

impl std::error::Error for Error {}
Expand All @@ -20,7 +21,8 @@ impl fmt::Display for Error {
match self {
Self::DecodeInvoice => write!(f, "Failed to decode LN Invoice"),
Self::StatusCode(code) => write!(f, "{}", code),
Self::_Ln(code) => write!(f, "{}", code),
Self::Custom(err) => write!(f, "{}", err),
Self::Ln(code) => write!(f, "{}", code),
}
}
}
Expand Down Expand Up @@ -48,9 +50,12 @@ impl IntoResponse for Error {
match self {
Error::DecodeInvoice => (StatusCode::BAD_REQUEST, self.to_string()).into_response(),
Error::StatusCode(code) => (code, "").into_response(),
Error::_Ln(code) => {
Error::Ln(code) => {
(StatusCode::INTERNAL_SERVER_ERROR, code.to_string()).into_response()
}
Error::Custom(msg) => {
(StatusCode::INTERNAL_SERVER_ERROR, msg.to_string()).into_response()
}
}
}
}
Expand Down
76 changes: 49 additions & 27 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,12 @@ use axum::http::header::{
ACCESS_CONTROL_ALLOW_CREDENTIALS, ACCESS_CONTROL_ALLOW_ORIGIN, AUTHORIZATION, CONTENT_TYPE,
};
use axum::http::StatusCode;
use axum::response::Response;
use axum::response::{IntoResponse, Response};
use axum::routing::{get, post};
use axum::Router;
use bip39::Mnemonic;
use cashu_sdk::amount::Amount;
use cashu_sdk::error::ErrorResponse;
use cashu_sdk::mint::{LocalStore, Mint, RedbLocalStore};
use cashu_sdk::nuts::nut02::Id;
use cashu_sdk::nuts::{
Expand All @@ -25,12 +26,12 @@ use cashu_sdk::nuts::{
};
use cashu_sdk::types::MintQuote;
use clap::Parser;
use error::into_response;
use error::{into_response, Error};
use futures::StreamExt;
use ln_rs::{Bolt11Invoice, Ln};
use tokio::sync::Mutex;
use tower_http::cors::CorsLayer;
use tracing::{debug, warn};
use tracing::warn;
use utils::unix_time;

use crate::config::LnBackend;
Expand Down Expand Up @@ -59,16 +60,18 @@ async fn main() -> anyhow::Result<()> {
None => "./config.toml".to_string(),
};

debug!("Path: {}", config_file_arg);

let settings = config::Settings::new(&Some(config_file_arg));

let db_path = match args.db {
Some(path) => PathBuf::from_str(&path)?,
None => settings.info.clone().db_path,
};

let localstore = RedbLocalStore::new(db_path.to_str().unwrap())?;
let db_path = db_path
.to_str()
.ok_or(Error::Custom("Invalid db_path".to_string()))?;

let localstore = RedbLocalStore::new(db_path)?;
let mint_info = MintInfo::default();
//settings.mint_info.clone();
localstore.set_mint_info(&mint_info).await?;
Expand All @@ -84,23 +87,25 @@ async fn main() -> anyhow::Result<()> {
)
.await?;

println!("Mint created");

let last_pay_path = PathBuf::from_str(&settings.info.last_pay_path.clone())?;

match fs::metadata(&last_pay_path) {
Ok(_) => (),
Err(_e) => {
// Create the parent directory if it doesn't exist
fs::create_dir_all(last_pay_path.parent().unwrap())?;
fs::create_dir_all(
last_pay_path
.parent()
.ok_or(Error::Custom("Invalid last_pay_path".to_string()))?,
)?;

// Attempt to create the file
let mut fs = File::create(&last_pay_path).unwrap();
fs.write_all(&0_u64.to_be_bytes()).unwrap();
let mut fs = File::create(&last_pay_path)?;
fs.write_all(&0_u64.to_be_bytes())?;
}
}

let last_pay = fs::read(&last_pay_path).unwrap();
let last_pay = fs::read(&last_pay_path)?;

let last_pay_index =
u64::from_be_bytes(last_pay.try_into().unwrap_or([0, 0, 0, 0, 0, 0, 0, 0]));
Expand Down Expand Up @@ -334,7 +339,17 @@ async fn get_mint_bolt11_quote(
)
.await;

let invoice = invoice.unwrap();
let invoice = if let Ok(invoice) = invoice {
invoice
} else {
warn!("Could not get ln invoice for mint quote");
let response = ErrorResponse {
code: 99,
error: Some("Could not fetch ln invoice".to_string()),
detail: None,
};
return Err((StatusCode::INTERNAL_SERVER_ERROR, Json(response)).into_response());
};

let quote = state
.mint
Expand Down Expand Up @@ -386,7 +401,11 @@ async fn get_melt_bolt11_quote(
State(state): State<MintState>,
Json(payload): Json<MeltQuoteBolt11Request>,
) -> Result<Json<MeltQuoteBolt11Response>, Response> {
let amount = payload.request.amount_milli_satoshis().unwrap() / 1000;
let amount = payload
.request
.amount_milli_satoshis()
.ok_or(Error::Custom("Invoice amount not defined".to_string()).into_response())?
/ 1000;
assert!(amount > 0);
let quote = state
.mint
Expand All @@ -408,49 +427,51 @@ async fn get_melt_bolt11_quote(
async fn get_check_melt_bolt11_quote(
State(state): State<MintState>,
Path(quote_id): Path<String>,
) -> Result<Json<MeltQuoteBolt11Response>, StatusCode> {
) -> Result<Json<MeltQuoteBolt11Response>, Response> {
let quote = state
.mint
.lock()
.await
.check_melt_quote(&quote_id)
.await
.unwrap();
.map_err(into_response)?;

Ok(Json(quote))
}

async fn post_melt_bolt11(
State(state): State<MintState>,
Json(payload): Json<MeltBolt11Request>,
) -> Result<Json<MeltBolt11Response>, StatusCode> {
) -> Result<Json<MeltBolt11Response>, Response> {
let quote = state
.mint
.lock()
.await
.verify_melt_request(&payload)
.await
.map_err(|_| StatusCode::NOT_ACCEPTABLE)?;
.map_err(into_response)?;

let pre = state
.ln
.ln_processor
.pay_invoice(Bolt11Invoice::from_str(&quote.request).unwrap(), None)
.pay_invoice(
Bolt11Invoice::from_str(&quote.request)
.map_err(|_| Error::DecodeInvoice.into_response())?,
None,
)
.await
.unwrap();
.map_err(|err| Error::Ln(err).into_response())?;

let preimage = pre.payment_preimage;
let preimage = pre
.payment_preimage
.ok_or(Error::DecodeInvoice.into_response())?;
let res = state
.mint
.lock()
.await
.process_melt_request(
&payload,
&preimage.unwrap(),
Amount::from(pre.total_spent.to_sat()),
)
.process_melt_request(&payload, &preimage, Amount::from(pre.total_spent.to_sat()))
.await
.unwrap();
.map_err(into_response)?;

Ok(Json(res))
}
Expand Down Expand Up @@ -493,6 +514,7 @@ async fn post_swap(
.process_swap_request(payload)
.await
.map_err(into_response)?;

Ok(Json(swap_response))
}

Expand Down

0 comments on commit 0e87d59

Please sign in to comment.