Skip to main content

kora_lib/rpc_server/method/
get_blockhash.rs

1use crate::{error::KoraError, CacheUtil};
2use serde::Serialize;
3use solana_client::nonblocking::rpc_client::RpcClient;
4use utoipa::ToSchema;
5
6#[cfg(not(test))]
7use crate::state::get_config;
8
9#[cfg(test)]
10use crate::tests::config_mock::mock_state::get_config;
11
12#[derive(Debug, Serialize, ToSchema)]
13pub struct GetBlockhashResponse {
14    pub blockhash: String,
15}
16
17pub async fn get_blockhash(rpc_client: &RpcClient) -> Result<GetBlockhashResponse, KoraError> {
18    let config = &get_config()?;
19
20    let blockhash = CacheUtil::get_or_fetch_latest_blockhash(config, rpc_client).await?;
21
22    Ok(GetBlockhashResponse { blockhash: blockhash.to_string() })
23}
24
25#[cfg(test)]
26mod tests {
27    use super::*;
28    use crate::tests::{config_mock::ConfigMockBuilder, rpc_mock::RpcMockBuilder};
29
30    #[tokio::test]
31    async fn test_get_blockhash_success() {
32        let _m = ConfigMockBuilder::new().build_and_setup();
33        let rpc_client = RpcMockBuilder::new().with_blockhash().build();
34
35        let result = get_blockhash(&rpc_client).await;
36
37        assert!(result.is_ok(), "Should successfully get blockhash");
38        let response = result.unwrap();
39        assert!(!response.blockhash.is_empty(), "Blockhash should not be empty");
40    }
41}