near_jsonrpc_client/methods/
block.rs

1//! Queries data from a specific block on the network.
2//!
3//! Blocks can be referenced using either;
4//! - a [block ID](https://docs.near.org/docs/api/rpc#using-block_id-param) (block height or block hash) for querying historical blocks
5//! - or a [finality specifier](https://docs.near.org/docs/api/rpc#using-finality-param) (“final” or “optimistic”) for latest blocks.
6//!
7//! ## Examples
8//!
9//! - Query historical blocks by using a specific reference (block height or block hash).
10//!
11//!     - `BlockId::Height`
12//!
13//!       ```
14//!       # use near_jsonrpc_client::methods;
15//!       use near_primitives::types::{BlockReference, BlockId};
16//!
17//!       let request = methods::block::RpcBlockRequest {
18//!           block_reference: BlockReference::BlockId(BlockId::Height(83975193))
19//!       };
20//!       ```
21//!
22//!     - `BlockId::Hash`
23//!
24//!       ```
25//!       # use near_jsonrpc_client::methods;
26//!       # fn main() -> Result<(), Box<dyn std::error::Error + Send + Sync>> {
27//!       use near_primitives::types::{BlockReference, BlockId};
28//!
29//!       let request = methods::block::RpcBlockRequest {
30//!           block_reference: BlockReference::BlockId(BlockId::Hash(
31//!               "G1SHrwLp55oV3kz94x3ekrR6r4ihNRWdAVZpckgBx4U4".parse()?,
32//!           )),
33//!       };
34//!       # Ok(())
35//!       # }
36//!       ```
37//!
38//! - Query latest blocks.
39//!
40//!     - `Finality::Final`: Get the most recent, completely finalized block.
41//!
42//!       References a block that has been validated on at least 66% of the nodes in the network.
43//!
44//!       ```
45//!       # use near_jsonrpc_client::methods;
46//!       use near_primitives::types::{BlockReference, Finality};
47//!
48//!       let request = methods::block::RpcBlockRequest {
49//!           block_reference: BlockReference::Finality(Finality::Final)
50//!       };
51//!       ```
52//!
53//!     - `Finality::None`: Get the most recently submitted block.
54//!
55//!       Returns the latest block recorded on the node that responded to your query.
56//!
57//!       ```
58//!       # use near_jsonrpc_client::methods;
59//!       use near_primitives::types::{BlockReference, Finality};
60//!
61//!       let request = methods::block::RpcBlockRequest {
62//!           block_reference: BlockReference::Finality(Finality::None)
63//!       };
64//!       ```
65use super::*;
66
67pub use near_jsonrpc_primitives::types::blocks::RpcBlockError;
68pub use near_jsonrpc_primitives::types::blocks::RpcBlockRequest;
69
70pub type RpcBlockResponse = near_primitives::views::BlockView;
71
72impl RpcHandlerResponse for RpcBlockResponse {}
73
74impl RpcHandlerError for RpcBlockError {
75    fn parse(value: serde_json::Value) -> Result<Self, serde_json::Error> {
76        common::parse_unknown_block!(value => Self)
77    }
78}
79
80impl RpcMethod for RpcBlockRequest {
81    type Response = RpcBlockResponse;
82    type Error = RpcBlockError;
83
84    fn method_name(&self) -> &str {
85        "block"
86    }
87
88    fn params(&self) -> Result<serde_json::Value, io::Error> {
89        Ok(json!(self))
90    }
91}
92
93impl private::Sealed for RpcBlockRequest {}