kabu_rpc_handler/handler/
blocks.rs

1use crate::dto::block::BlockHeader;
2use axum::Json;
3use axum::extract::State;
4use axum::http::StatusCode;
5use kabu_rpc_state::AppState;
6use revm::{DatabaseCommit, DatabaseRef};
7
8/// Get latest block
9///
10/// Get the latest block header
11#[utoipa::path(
12    get,
13    path = "latest_block",
14    tag = "block",
15    tags = [],
16    responses(
17    (status = 200, description = "Todo item created successfully", body = BlockHeader),
18    )
19)]
20pub async fn latest_block<DB: DatabaseRef + DatabaseCommit + Send + Sync + Clone + 'static>(
21    State(app_state): State<AppState<DB>>,
22) -> Result<Json<BlockHeader>, (StatusCode, String)> {
23    {
24        // Get latest block from block history
25        let block_history_guard = app_state.state.block_history();
26        let block_history = block_history_guard.read().await;
27        let latest_block_number = block_history.latest_block_number;
28
29        if let Some(block_hash) = block_history.get_block_hash_for_block_number(latest_block_number)
30            && let Some(entry) = block_history.get_block_history_entry(&block_hash)
31        {
32            let ret = BlockHeader {
33                number: entry.header.number,
34                timestamp: entry.header.timestamp,
35                base_fee_per_gas: entry.header.base_fee_per_gas,
36                next_block_base_fee: 0,
37            };
38            return Ok(Json(ret));
39        }
40
41        Err((StatusCode::INTERNAL_SERVER_ERROR, "No block header found".to_string()))
42    }
43}