Skip to content

Commit

Permalink
chore: Refresh wallet before getting the balance
Browse files Browse the repository at this point in the history
  • Loading branch information
holzeis committed Jan 20, 2024
1 parent 79dc6a4 commit c075507
Showing 1 changed file with 30 additions and 3 deletions.
33 changes: 30 additions & 3 deletions webapp/src/api.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,35 @@
use crate::subscribers::AppSubscribers;
use anyhow::Result;
use axum::extract::State;
use axum::http::StatusCode;
use axum::response::IntoResponse;
use axum::response::Response;
use axum::Json;
use native::api;
use serde::Serialize;
use std::sync::Arc;

struct AppError(anyhow::Error);

impl IntoResponse for AppError {
fn into_response(self) -> Response {
(
StatusCode::INTERNAL_SERVER_ERROR,
format!("Something went wrong: {}", self.0),
)
.into_response()
}
}

impl<E> From<E> for AppError
where
E: Into<anyhow::Error>,
{
fn from(err: E) -> Self {
Self(err.into())
}
}

#[derive(Serialize)]
pub struct Version {
version: String,
Expand All @@ -27,7 +51,10 @@ pub struct Balance {
off_chain: u64,
}

pub async fn get_balance(State(subscribers): State<Arc<AppSubscribers>>) -> impl IntoResponse {
pub async fn get_balance(
State(subscribers): State<Arc<AppSubscribers>>,
) -> Result<Json<Balance>, AppError> {
ln_dlc::refresh_wallet_info().await?;
let balance = subscribers
.wallet_info()
.map(|wallet_info| Balance {
Expand All @@ -38,6 +65,6 @@ pub async fn get_balance(State(subscribers): State<Arc<AppSubscribers>>) -> impl
on_chain: 0,
off_chain: 0,
});
Json(balance)

Ok(Json(balance))
}

0 comments on commit c075507

Please sign in to comment.