use anyhow::Result;
use axum::extract::ws::WebSocketUpgrade;
use axum::extract::{Json, Path, State};
use axum::http::StatusCode;
use axum::response::{IntoResponse, Response};
use cdk::error::ErrorResponse;
use cdk::nuts::{
CheckStateRequest, CheckStateResponse, Id, KeysResponse, KeysetResponse, MeltBolt11Request,
MeltQuoteBolt11Request, MeltQuoteBolt11Response, MintBolt11Request, MintBolt11Response,
MintInfo, MintQuoteBolt11Request, MintQuoteBolt11Response, RestoreRequest, RestoreResponse,
SwapRequest, SwapResponse,
};
use cdk::util::unix_time;
use paste::paste;
use uuid::Uuid;
use crate::ws::main_websocket;
use crate::MintState;
macro_rules! post_cache_wrapper {
($handler:ident, $request_type:ty, $response_type:ty) => {
paste! {
pub async fn [<cache_ $handler>](
state: State<MintState>,
payload: Json<$request_type>
) -> Result<Json<$response_type>, Response> {
use std::ops::Deref;
let json_extracted_payload = payload.deref();
let State(mint_state) = state.clone();
let cache_key = match mint_state.cache.calculate_key(&json_extracted_payload) {
Some(key) => key,
None => {
return $handler(state, payload).await;
}
};
if let Some(cached_response) = mint_state.cache.get::<$response_type>(&cache_key).await {
return Ok(Json(cached_response));
}
let response = $handler(state, payload).await?;
mint_state.cache.set(cache_key, &response.deref()).await;
Ok(response)
}
}
};
}
post_cache_wrapper!(post_swap, SwapRequest, SwapResponse);
post_cache_wrapper!(
post_mint_bolt11,
MintBolt11Request<Uuid>,
MintBolt11Response
);
post_cache_wrapper!(
post_melt_bolt11,
MeltBolt11Request<Uuid>,
MeltQuoteBolt11Response<Uuid>
);
#[cfg_attr(feature = "swagger", utoipa::path(
get,
context_path = "/v1",
path = "/keys",
responses(
(status = 200, description = "Successful response", body = KeysResponse, content_type = "application/json")
)
))]
pub async fn get_keys(State(state): State<MintState>) -> Result<Json<KeysResponse>, Response> {
let pubkeys = state.mint.pubkeys().await.map_err(|err| {
tracing::error!("Could not get keys: {}", err);
into_response(err)
})?;
Ok(Json(pubkeys))
}
#[cfg_attr(feature = "swagger", utoipa::path(
get,
context_path = "/v1",
path = "/keys/{keyset_id}",
params(
("keyset_id" = String, description = "The keyset ID"),
),
responses(
(status = 200, description = "Successful response", body = KeysResponse, content_type = "application/json"),
(status = 500, description = "Server error", body = ErrorResponse, content_type = "application/json")
)
))]
pub async fn get_keyset_pubkeys(
State(state): State<MintState>,
Path(keyset_id): Path<Id>,
) -> Result<Json<KeysResponse>, Response> {
let pubkeys = state.mint.keyset_pubkeys(&keyset_id).await.map_err(|err| {
tracing::error!("Could not get keyset pubkeys: {}", err);
into_response(err)
})?;
Ok(Json(pubkeys))
}
#[cfg_attr(feature = "swagger", utoipa::path(
get,
context_path = "/v1",
path = "/keysets",
responses(
(status = 200, description = "Successful response", body = KeysetResponse, content_type = "application/json"),
(status = 500, description = "Server error", body = ErrorResponse, content_type = "application/json")
)
))]
pub async fn get_keysets(State(state): State<MintState>) -> Result<Json<KeysetResponse>, Response> {
let keysets = state.mint.keysets().await.map_err(|err| {
tracing::error!("Could not get keysets: {}", err);
into_response(err)
})?;
Ok(Json(keysets))
}
#[cfg_attr(feature = "swagger", utoipa::path(
post,
context_path = "/v1",
path = "/mint/quote/bolt11",
request_body(content = MintQuoteBolt11Request, description = "Request params", content_type = "application/json"),
responses(
(status = 200, description = "Successful response", body = MintQuoteBolt11Response, content_type = "application/json"),
(status = 500, description = "Server error", body = ErrorResponse, content_type = "application/json")
)
))]
pub async fn post_mint_bolt11_quote(
State(state): State<MintState>,
Json(payload): Json<MintQuoteBolt11Request>,
) -> Result<Json<MintQuoteBolt11Response<Uuid>>, Response> {
let quote = state
.mint
.get_mint_bolt11_quote(payload)
.await
.map_err(into_response)?;
Ok(Json(quote))
}
#[cfg_attr(feature = "swagger", utoipa::path(
get,
context_path = "/v1",
path = "/mint/quote/bolt11/{quote_id}",
params(
("quote_id" = String, description = "The quote ID"),
),
responses(
(status = 200, description = "Successful response", body = MintQuoteBolt11Response, content_type = "application/json"),
(status = 500, description = "Server error", body = ErrorResponse, content_type = "application/json")
)
))]
pub async fn get_check_mint_bolt11_quote(
State(state): State<MintState>,
Path(quote_id): Path<Uuid>,
) -> Result<Json<MintQuoteBolt11Response<Uuid>>, Response> {
let quote = state
.mint
.check_mint_quote("e_id)
.await
.map_err(|err| {
tracing::error!("Could not check mint quote {}: {}", quote_id, err);
into_response(err)
})?;
Ok(Json(quote))
}
pub async fn ws_handler(State(state): State<MintState>, ws: WebSocketUpgrade) -> impl IntoResponse {
ws.on_upgrade(|ws| main_websocket(ws, state))
}
#[cfg_attr(feature = "swagger", utoipa::path(
post,
context_path = "/v1",
path = "/mint/bolt11",
request_body(content = MintBolt11Request, description = "Request params", content_type = "application/json"),
responses(
(status = 200, description = "Successful response", body = MintBolt11Response, content_type = "application/json"),
(status = 500, description = "Server error", body = ErrorResponse, content_type = "application/json")
)
))]
pub async fn post_mint_bolt11(
State(state): State<MintState>,
Json(payload): Json<MintBolt11Request<Uuid>>,
) -> Result<Json<MintBolt11Response>, Response> {
let res = state
.mint
.process_mint_request(payload)
.await
.map_err(|err| {
tracing::error!("Could not process mint: {}", err);
into_response(err)
})?;
Ok(Json(res))
}
#[cfg_attr(feature = "swagger", utoipa::path(
post,
context_path = "/v1",
path = "/melt/quote/bolt11",
request_body(content = MeltQuoteBolt11Request, description = "Quote params", content_type = "application/json"),
responses(
(status = 200, description = "Successful response", body = MeltQuoteBolt11Response, content_type = "application/json"),
(status = 500, description = "Server error", body = ErrorResponse, content_type = "application/json")
)
))]
pub async fn post_melt_bolt11_quote(
State(state): State<MintState>,
Json(payload): Json<MeltQuoteBolt11Request>,
) -> Result<Json<MeltQuoteBolt11Response<Uuid>>, Response> {
let quote = state
.mint
.get_melt_bolt11_quote(&payload)
.await
.map_err(into_response)?;
Ok(Json(quote))
}
#[cfg_attr(feature = "swagger", utoipa::path(
get,
context_path = "/v1",
path = "/melt/quote/bolt11/{quote_id}",
params(
("quote_id" = String, description = "The quote ID"),
),
responses(
(status = 200, description = "Successful response", body = MeltQuoteBolt11Response, content_type = "application/json"),
(status = 500, description = "Server error", body = ErrorResponse, content_type = "application/json")
)
))]
pub async fn get_check_melt_bolt11_quote(
State(state): State<MintState>,
Path(quote_id): Path<Uuid>,
) -> Result<Json<MeltQuoteBolt11Response<Uuid>>, Response> {
let quote = state
.mint
.check_melt_quote("e_id)
.await
.map_err(|err| {
tracing::error!("Could not check melt quote: {}", err);
into_response(err)
})?;
Ok(Json(quote))
}
#[cfg_attr(feature = "swagger", utoipa::path(
post,
context_path = "/v1",
path = "/melt/bolt11",
request_body(content = MeltBolt11Request, description = "Melt params", content_type = "application/json"),
responses(
(status = 200, description = "Successful response", body = MeltQuoteBolt11Response, content_type = "application/json"),
(status = 500, description = "Server error", body = ErrorResponse, content_type = "application/json")
)
))]
pub async fn post_melt_bolt11(
State(state): State<MintState>,
Json(payload): Json<MeltBolt11Request<Uuid>>,
) -> Result<Json<MeltQuoteBolt11Response<Uuid>>, Response> {
let res = state
.mint
.melt_bolt11(&payload)
.await
.map_err(into_response)?;
Ok(Json(res))
}
#[cfg_attr(feature = "swagger", utoipa::path(
post,
context_path = "/v1",
path = "/checkstate",
request_body(content = CheckStateRequest, description = "State params", content_type = "application/json"),
responses(
(status = 200, description = "Successful response", body = CheckStateResponse, content_type = "application/json"),
(status = 500, description = "Server error", body = ErrorResponse, content_type = "application/json")
)
))]
pub async fn post_check(
State(state): State<MintState>,
Json(payload): Json<CheckStateRequest>,
) -> Result<Json<CheckStateResponse>, Response> {
let state = state.mint.check_state(&payload).await.map_err(|err| {
tracing::error!("Could not check state of proofs");
into_response(err)
})?;
Ok(Json(state))
}
#[cfg_attr(feature = "swagger", utoipa::path(
get,
context_path = "/v1",
path = "/info",
responses(
(status = 200, description = "Successful response", body = MintInfo)
)
))]
pub async fn get_mint_info(State(state): State<MintState>) -> Result<Json<MintInfo>, Response> {
Ok(Json(state.mint.mint_info().clone().time(unix_time())))
}
#[cfg_attr(feature = "swagger", utoipa::path(
post,
context_path = "/v1",
path = "/swap",
request_body(content = SwapRequest, description = "Swap params", content_type = "application/json"),
responses(
(status = 200, description = "Successful response", body = SwapResponse, content_type = "application/json"),
(status = 500, description = "Server error", body = ErrorResponse, content_type = "application/json")
)
))]
pub async fn post_swap(
State(state): State<MintState>,
Json(payload): Json<SwapRequest>,
) -> Result<Json<SwapResponse>, Response> {
let swap_response = state
.mint
.process_swap_request(payload)
.await
.map_err(|err| {
tracing::error!("Could not process swap request: {}", err);
into_response(err)
})?;
Ok(Json(swap_response))
}
#[cfg_attr(feature = "swagger", utoipa::path(
post,
context_path = "/v1",
path = "/restore",
request_body(content = RestoreRequest, description = "Restore params", content_type = "application/json"),
responses(
(status = 200, description = "Successful response", body = RestoreResponse, content_type = "application/json"),
(status = 500, description = "Server error", body = ErrorResponse, content_type = "application/json")
)
))]
pub async fn post_restore(
State(state): State<MintState>,
Json(payload): Json<RestoreRequest>,
) -> Result<Json<RestoreResponse>, Response> {
let restore_response = state.mint.restore(payload).await.map_err(|err| {
tracing::error!("Could not process restore: {}", err);
into_response(err)
})?;
Ok(Json(restore_response))
}
pub fn into_response<T>(error: T) -> Response
where
T: Into<ErrorResponse>,
{
(
StatusCode::INTERNAL_SERVER_ERROR,
Json::<ErrorResponse>(error.into()),
)
.into_response()
}