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
//! Queries data from a specific block on the network.
//!
//! Blocks can be referenced using either;
//! - a [block ID](https://docs.unc.org/docs/api/rpc#using-block_id-param) (block height or block hash) for querying historical blocks
//! - or a [finality specifier](https://docs.unc.org/docs/api/rpc#using-finality-param) (“final” or “optimistic”) for latest blocks.
//!
//! ## Examples
//!
//! - Query historical blocks by using a specific reference (block height or block hash).
//!
//!     - `BlockId::Height`
//!
//!       ```
//!       # use unc_jsonrpc_client::methods;
//!       use unc_primitives::types::{BlockReference, BlockId};
//!
//!       let request = methods::block::RpcBlockRequest {
//!           block_reference: BlockReference::BlockId(BlockId::Height(83975193))
//!       };
//!       ```
//!
//!     - `BlockId::Hash`
//!
//!       ```
//!       # use unc_jsonrpc_client::methods;
//!       # fn main() -> Result<(), Box<dyn std::error::Error + Send + Sync>> {
//!       use unc_primitives::types::{BlockReference, BlockId};
//!
//!       let request = methods::block::RpcBlockRequest {
//!           block_reference: BlockReference::BlockId(BlockId::Hash(
//!               "G1SHrwLp55oV3kz94x3ekrR6r4ihNRWdAVZpckgBx4U4".parse()?,
//!           )),
//!       };
//!       # Ok(())
//!       # }
//!       ```
//!
//! - Query latest blocks.
//!
//!     - `Finality::Final`: Get the most recent, completely finalized block.
//!
//!       References a block that has been validated on at least 66% of the nodes in the network.
//!
//!       ```
//!       # use unc_jsonrpc_client::methods;
//!       use unc_primitives::types::{BlockReference, Finality};
//!
//!       let request = methods::block::RpcBlockRequest {
//!           block_reference: BlockReference::Finality(Finality::Final)
//!       };
//!       ```
//!
//!     - `Finality::None`: Get the most recently submitted block.
//!
//!       Returns the latest block recorded on the node that responded to your query.
//!
//!       ```
//!       # use unc_jsonrpc_client::methods;
//!       use unc_primitives::types::{BlockReference, Finality};
//!
//!       let request = methods::block::RpcBlockRequest {
//!           block_reference: BlockReference::Finality(Finality::None)
//!       };
//!       ```
use super::*;

pub use unc_jsonrpc_primitives::types::blocks::RpcBlockError;
pub use unc_jsonrpc_primitives::types::blocks::RpcBlockRequest;

pub type RpcBlockResponse = unc_primitives::views::BlockView;

impl RpcHandlerResponse for RpcBlockResponse {}

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

impl RpcMethod for RpcBlockRequest {
    type Response = RpcBlockResponse;
    type Error = RpcBlockError;

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

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

impl private::Sealed for RpcBlockRequest {}