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