near_jsonrpc_client/methods/
chunk.rs

1//! Returns details of a specific chunk.
2//!
3//! You can use the [`block`](crate::methods::block) RPC method to get a valid chunk hash.
4//!
5//! ## Examples
6//!
7//! Chunks can be queried using one of two `ChunkReference` variants: `BlockShardId` or `ChunkHash`.
8//!
9//! - `BlockShardId`: Query a chunk by specifying its block ID (block height or block hash) and shard ID.
10//!
11//!     - `BlockId::Hash`
12//!
13//!       ```
14//!       use near_jsonrpc_client::{methods, JsonRpcClient};
15//!       use near_jsonrpc_primitives::types::chunks;
16//!       use near_primitives::types::{BlockId, ShardId};
17//!
18//!       # #[tokio::main]
19//!       # async fn main() -> Result<(), Box<dyn std::error::Error + Send + Sync>> {
20//!       let client = JsonRpcClient::connect("https://archival-rpc.mainnet.fastnear.com");
21//!
22//!       let request = methods::chunk::RpcChunkRequest {
23//!           chunk_reference: chunks::ChunkReference::BlockShardId {
24//!               block_id: BlockId::Hash("6atGq4TUTZerVHU9qWoYfzXNBg3K4C4cca15TE6KfuBr".parse()?),
25//!               shard_id: ShardId::from(0),
26//!           }
27//!       };
28//!
29//!       let response = client.call(request).await?;
30//!
31//!       assert!(matches!(
32//!           response,
33//!           methods::chunk::RpcChunkResponse { .. }
34//!       ));
35//!       # Ok(())
36//!       # }
37//!       ```
38//!
39//!     - `BlockId::Height`
40//!
41//!       ```
42//!       use near_jsonrpc_client::{methods, JsonRpcClient};
43//!       use near_jsonrpc_primitives::types::chunks;
44//!       use near_primitives::types::{BlockId, ShardId};
45//!
46//!       # #[tokio::main]
47//!       # async fn main() -> Result<(), Box<dyn std::error::Error>> {
48//!       let client = JsonRpcClient::connect("https://archival-rpc.mainnet.fastnear.com");
49//!
50//!       let request = methods::chunk::RpcChunkRequest {
51//!           chunk_reference: chunks::ChunkReference::BlockShardId {
52//!               block_id: BlockId::Height(61512623),
53//!               shard_id: ShardId::from(3),
54//!           }
55//!       };
56//!
57//!       let response = client.call(request).await?;
58//!
59//!       assert!(matches!(
60//!           response,
61//!           methods::chunk::RpcChunkResponse { .. }
62//!       ));
63//!       # Ok(())
64//!       # }
65//!       ```
66//!
67//!
68//! - `ChunkHash`: Query a chunk by a specific reference via it's associated chunk hash.
69//!
70//!   ```
71//!   use near_jsonrpc_client::{methods, JsonRpcClient};
72//!   use near_jsonrpc_primitives::types::chunks;
73//!
74//!   # #[tokio::main]
75//!   # async fn main() -> Result<(), Box<dyn std::error::Error + Send + Sync>> {
76//!   let client = JsonRpcClient::connect("https://archival-rpc.mainnet.fastnear.com");
77//!
78//!   let request = methods::chunk::RpcChunkRequest{
79//!       chunk_reference: chunks::ChunkReference::ChunkHash {
80//!           chunk_id: "6GTgCQ5genLEEiPspEvdZEJooBzgWRrUnur9eGSdeTTD".parse()?,
81//!       }
82//!   };
83//!
84//!   let response = client.call(request).await?;
85//!
86//!   assert!(matches!(
87//!       response,
88//!       methods::chunk::RpcChunkResponse { .. }
89//!   ));
90//!   # Ok(())
91//!   # }
92//!   ```
93use super::*;
94
95pub use near_jsonrpc_primitives::types::chunks::{ChunkReference, RpcChunkError, RpcChunkRequest};
96
97pub type RpcChunkResponse = near_primitives::views::ChunkView;
98
99impl RpcHandlerResponse for RpcChunkResponse {}
100
101impl RpcHandlerError for RpcChunkError {
102    fn parse(value: serde_json::Value) -> Result<Self, serde_json::Error> {
103        common::parse_unknown_block!(value => Self)
104    }
105}
106
107impl RpcMethod for RpcChunkRequest {
108    type Response = RpcChunkResponse;
109    type Error = RpcChunkError;
110
111    fn method_name(&self) -> &str {
112        "chunk"
113    }
114
115    fn params(&self) -> Result<serde_json::Value, io::Error> {
116        Ok(json!(self))
117    }
118}
119
120impl private::Sealed for RpcChunkRequest {}