near_jsonrpc_client/methods/
validators.rs

1//! Queries active validators on the network.
2//!
3//! Returns details and the state of validation on the blockchain.
4//!
5//! ## Examples
6//!
7//! - Get the validators for a specified epoch.
8//!
9//!   ```
10//!   use near_jsonrpc_client::{methods, JsonRpcClient};
11//!   use near_primitives::types::{EpochReference, EpochId, BlockReference, Finality};
12//!
13//!   # #[tokio::main]
14//!   # async fn main() -> Result<(), Box<dyn std::error::Error + Send + Sync>> {
15//!   let client = JsonRpcClient::connect("https://archival-rpc.testnet.fastnear.com");
16//!
17//!   let request = methods::validators::RpcValidatorRequest {
18//!       epoch_reference: EpochReference::EpochId(
19//!           "ECjBFyob3eKjDnDWDrbX4pkiSbGRAGyiHbS4ZVY8dxvD".parse()?,
20//!       )
21//!   };
22//!
23//!   let response = client.call(request).await?;
24//!
25//!   assert!(matches!(
26//!       response,
27//!       methods::validators::RpcValidatorResponse { .. }
28//!   ));
29//!   # Ok(())
30//!   # }
31//!   ```
32//!
33//! - Get the validators for the latest block.
34//!
35//!   ```
36//!   use near_jsonrpc_client::{methods, JsonRpcClient};
37//!   use near_primitives::types::{EpochReference, EpochId, BlockId};
38//!
39//!   # #[tokio::main]
40//!   # async fn main() -> Result<(), Box<dyn std::error::Error>> {
41//!   let client = JsonRpcClient::connect("https://archival-rpc.testnet.fastnear.com");
42//!
43//!   let request = methods::validators::RpcValidatorRequest {
44//!       epoch_reference: EpochReference::Latest
45//!   };
46//!
47//!   let response = client.call(request).await?;
48//!
49//!   assert!(matches!(
50//!       response,
51//!       methods::validators::RpcValidatorResponse { .. }
52//!   ));
53//!   # Ok(())
54//!   # }
55//!   ```
56use super::*;
57
58pub use near_jsonrpc_primitives::types::validator::{RpcValidatorError, RpcValidatorRequest};
59
60pub type RpcValidatorResponse = near_primitives::views::EpochValidatorInfo;
61
62impl RpcHandlerResponse for RpcValidatorResponse {}
63
64impl RpcMethod for RpcValidatorRequest {
65    type Response = RpcValidatorResponse;
66    type Error = RpcValidatorError;
67
68    fn method_name(&self) -> &str {
69        "validators"
70    }
71
72    fn params(&self) -> Result<serde_json::Value, io::Error> {
73        Ok(json!(self))
74    }
75}
76
77impl private::Sealed for RpcValidatorRequest {}