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

pub use unc_jsonrpc_primitives::types::chunks::{RpcChunkError, RpcChunkRequest};

pub type RpcChunkResponse = unc_primitives::views::ChunkView;

impl RpcHandlerResponse for RpcChunkResponse {}

impl RpcHandlerError for RpcChunkError {
    fn parse(value: serde_json::Value) -> Result<Self, serde_json::Error> {
        common::parse_unknown_block!(value => Self)
    }
}

impl RpcMethod for RpcChunkRequest {
    type Response = RpcChunkResponse;
    type Error = RpcChunkError;

    fn method_name(&self) -> &str {
        "chunk"
    }

    fn params(&self) -> Result<serde_json::Value, io::Error> {
        Ok(json!(self))
    }
}

impl private::Sealed for RpcChunkRequest {}