near_api/common/query/
validator_rpc.rs1use near_api_types::EpochReference;
2use near_openapi_client::Client;
3use near_openapi_client::types::{
4 BlockId, EpochId, JsonRpcRequestForValidators, JsonRpcRequestForValidatorsMethod,
5 JsonRpcResponseForRpcValidatorResponseAndRpcError, RpcError, RpcValidatorRequest,
6 RpcValidatorResponse,
7};
8
9use crate::{
10 NetworkConfig, advanced::RpcType, common::utils::is_critical_validator_error,
11 config::RetryResponse, errors::SendRequestError,
12};
13
14#[derive(Clone, Debug)]
15pub struct SimpleValidatorRpc;
16
17#[async_trait::async_trait]
18impl RpcType for SimpleValidatorRpc {
19 type RpcReference = EpochReference;
20 type Response = RpcValidatorResponse;
21 type Error = RpcError;
22 async fn send_query(
23 &self,
24 client: &Client,
25 _network: &NetworkConfig,
26 reference: &EpochReference,
27 ) -> RetryResponse<RpcValidatorResponse, SendRequestError<RpcError>> {
28 let request = match reference {
29 EpochReference::Latest => RpcValidatorRequest::Latest,
30 EpochReference::AtEpoch(epoch) => {
31 RpcValidatorRequest::EpochId(EpochId((*epoch).into()))
32 }
33 EpochReference::AtBlock(block) => {
34 RpcValidatorRequest::BlockId(BlockId::BlockHeight(*block))
35 }
36 EpochReference::AtBlockHash(block_hash) => {
37 RpcValidatorRequest::BlockId(BlockId::CryptoHash((*block_hash).into()))
38 }
39 };
40 let response = client
41 .validators(&JsonRpcRequestForValidators {
42 id: "0".to_string(),
43 jsonrpc: "2.0".to_string(),
44 method: JsonRpcRequestForValidatorsMethod::Validators,
45 params: request,
46 })
47 .await
48 .map(|r| r.into_inner());
49 match response {
50 Ok(JsonRpcResponseForRpcValidatorResponseAndRpcError::Variant0 { result, .. }) => {
51 RetryResponse::Ok(result)
52 }
53 Ok(JsonRpcResponseForRpcValidatorResponseAndRpcError::Variant1 { error, .. }) => {
54 if is_critical_validator_error(&error) {
55 RetryResponse::Critical(SendRequestError::ServerError(error))
56 } else {
57 RetryResponse::Retry(SendRequestError::ServerError(error))
58 }
59 }
60 Err(err) => RetryResponse::Critical(SendRequestError::ClientError(err)),
61 }
62 }
63}