Skip to content

Commit

Permalink
Add an endpoint to get the chain tip
Browse files Browse the repository at this point in the history
  • Loading branch information
SupernaviX committed Sep 20, 2024
1 parent 389debd commit 0764fbd
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 0 deletions.
2 changes: 2 additions & 0 deletions firefly-cardanoconnect/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ use config::{load_config, CardanoConnectConfig};
use firefly_server::instrumentation;
use persistence::Persistence;
use routes::{
chain::get_chain_tip,
health::health,
streams::{
create_listener, create_stream, delete_listener, delete_stream, get_listener, get_stream,
Expand Down Expand Up @@ -86,6 +87,7 @@ async fn main() -> Result<()> {
"/api/eventstreams/:streamId/listeners/:listenerId",
get(get_listener).delete(delete_listener),
)
.api_route("/api/chain/tip", get(get_chain_tip))
.route("/api/ws", axum::routing::get(handle_socket_upgrade))
.with_state(state);

Expand Down
1 change: 1 addition & 0 deletions firefly-cardanoconnect/src/routes.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
pub mod chain;
pub mod health;
pub mod streams;
pub mod transaction;
Expand Down
29 changes: 29 additions & 0 deletions firefly-cardanoconnect/src/routes/chain.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
use axum::{extract::State, Json};
use firefly_server::apitypes::{ApiError, ApiResult};
use schemars::JsonSchema;
use serde::Serialize;

use crate::{blockchain::ChainSyncClient, AppState};

#[derive(Serialize, JsonSchema)]
#[serde(rename_all = "camelCase")]
pub struct ChainTip {
pub block_height: Option<u64>,
pub block_slot: Option<u64>,
pub block_hash: String,
}

pub async fn get_chain_tip(
State(AppState { blockchain, .. }): State<AppState>,
) -> ApiResult<Json<ChainTip>> {
let mut sync = blockchain.sync().await?;
let (_, tip) = sync.find_intersect(&[]).await?;
let Some(block) = sync.request_block(&tip).await? else {
return Err(ApiError::not_found("tip of chain not found"));
};
Ok(Json(ChainTip {
block_height: block.block_height,
block_slot: block.block_slot,
block_hash: block.block_hash,
}))
}

0 comments on commit 0764fbd

Please sign in to comment.