-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathchain.rs
More file actions
130 lines (124 loc) · 4.22 KB
/
chain.rs
File metadata and controls
130 lines (124 loc) · 4.22 KB
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
121
122
123
124
125
126
127
128
129
130
use near_api_types::{BlockHeight, CryptoHash, Reference};
use crate::{
advanced::block_rpc::SimpleBlockRpc,
common::query::{AndThenHandler, PostprocessHandler, RequestBuilder, RpcBlockHandler},
};
/// Chain-related interactions with the NEAR Protocol
///
/// The [`Chain`] struct provides methods to interact with the NEAR blockchain
///
/// # Examples
///
/// ```rust,no_run
/// use near_api::*;
///
/// # async fn example() -> Result<(), Box<dyn std::error::Error>> {
/// let block_number = Chain::block_number().fetch_from_testnet().await?;
/// println!("Current block number: {}", block_number);
/// # Ok(())
/// # }
/// ```
#[derive(Debug, Clone, Copy)]
pub struct Chain;
impl Chain {
/// Set ups a query to fetch the [BlockHeight] of the current block
///
/// ## Fetching the latest block number
///
/// ```rust,no_run
/// use near_api::*;
///
/// # async fn example() -> Result<(), Box<dyn std::error::Error>> {
/// let block_number = Chain::block_number().fetch_from_testnet().await?;
/// println!("Current block number: {}", block_number);
/// # Ok(())
/// # }
/// ```
///
/// ## Fetching the final block number
///
/// ```rust,no_run
/// use near_api::*;
///
/// # async fn example() -> Result<(), Box<dyn std::error::Error>> {
/// let block_number = Chain::block_number().at(Reference::Final).fetch_from_testnet().await?;
/// println!("Final block number: {}", block_number);
/// # Ok(())
/// # }
/// ```
pub fn block_number() -> RequestBuilder<PostprocessHandler<BlockHeight, RpcBlockHandler>> {
RequestBuilder::new(SimpleBlockRpc, Reference::Optimistic, RpcBlockHandler)
.map(|data| data.header.height)
}
/// Set ups a query to fetch the [CryptoHash] of the block
///
/// ## Fetching the latest block hash
///
/// ```rust,no_run
/// use near_api::*;
///
/// # async fn example() -> Result<(), Box<dyn std::error::Error>> {
/// let block_hash = Chain::block_hash().fetch_from_testnet().await?;
/// println!("Current block hash: {}", block_hash);
/// # Ok(())
/// # }
/// ```
///
/// ## Fetching the hash at a specific block number
///
/// ```rust,no_run
/// use near_api::*;
///
/// # async fn example() -> Result<(), Box<dyn std::error::Error>> {
/// let block_hash = Chain::block_hash().at(Reference::AtBlock(1000000)).fetch_from_testnet().await?;
/// println!("Block hash at block number 1000000: {}", block_hash);
/// # Ok(())
/// # }
/// ```
pub fn block_hash() -> RequestBuilder<AndThenHandler<CryptoHash, RpcBlockHandler>> {
RequestBuilder::new(SimpleBlockRpc, Reference::Optimistic, RpcBlockHandler)
.and_then(|data| Ok(CryptoHash::try_from(data.header.hash)?))
}
/// Set ups a query to fetch the [RpcBlockResponse][near_api_types::RpcBlockResponse]
///
/// ## Fetching the latest block
///
/// ```rust,no_run
/// use near_api::*;
///
/// # async fn example() -> Result<(), Box<dyn std::error::Error>> {
/// let block = Chain::block().fetch_from_testnet().await?;
/// println!("Current block: {:?}", block);
/// # Ok(())
/// # }
/// ```
///
/// ## Fetching the block at a specific block number
///
/// ```rust,no_run
/// use near_api::*;
///
/// # async fn example() -> Result<(), Box<dyn std::error::Error>> {
/// let block = Chain::block().at(Reference::AtBlock(1000000)).fetch_from_testnet().await?;
/// println!("Block at block number 1000000: {:?}", block);
/// # Ok(())
/// # }
/// ```
///
/// ## Fetching the block at a specific block hash
///
/// ```rust,no_run
/// use near_api::*;
///
/// # async fn example() -> Result<(), Box<dyn std::error::Error>> {
/// # let block_hash = near_api::types::CryptoHash::default();
/// let block = Chain::block().at(Reference::AtBlockHash(block_hash)).fetch_from_testnet().await?;
/// println!("Block at block hash: {:?}", block);
/// # Ok(())
/// # }
/// ```
pub fn block() -> RequestBuilder<RpcBlockHandler> {
RequestBuilder::new(SimpleBlockRpc, Reference::Optimistic, RpcBlockHandler)
}
// TODO: chunk info
}