loom_rpc_handler/handler/
blocks.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
use crate::dto::block::BlockHeader;
use axum::extract::State;
use axum::http::StatusCode;
use axum::Json;
use loom_rpc_state::AppState;
use revm::DatabaseRef;

/// Get latest block
///
/// Get the latest block header
#[utoipa::path(
    get,
    path = "latest_block",
    tag = "block",
    tags = [],
    responses(
    (status = 200, description = "Todo item created successfully", body = BlockHeader),
    )
)]
pub async fn latest_block<DB: DatabaseRef + Send + Sync + Clone + 'static>(
    State(app_state): State<AppState<DB>>,
) -> Result<Json<BlockHeader>, (StatusCode, String)> {
    {
        let block_header_opt = app_state.bc.latest_block().read().await.block_header.clone();
        if let Some(block_header) = block_header_opt {
            let ret = BlockHeader {
                number: block_header.number,
                timestamp: block_header.timestamp,
                base_fee_per_gas: block_header.base_fee_per_gas,
                next_block_base_fee: 0,
            };
            Ok(Json(ret))
        } else {
            Err((StatusCode::INTERNAL_SERVER_ERROR, "No block header found".to_string()))
        }
    }
}