casper_rust_wasm_sdk/sdk/rpcs/
get_era_summary.rs1#[cfg(all(feature = "js", target_arch = "wasm32"))]
2use crate::types::identifier::block_identifier::BlockIdentifier;
3use crate::{
4 types::{
5 identifier::block_identifier::BlockIdentifierInput, sdk_error::SdkError,
6 verbosity::Verbosity,
7 },
8 SDK,
9};
10use casper_client::{
11 cli::get_era_summary as get_era_summary_cli, get_era_summary as get_era_summary_lib,
12 rpcs::results::GetEraSummaryResult as _GetEraSummaryResult, JsonRpcId, SuccessResponse,
13};
14#[cfg(all(feature = "js", target_arch = "wasm32"))]
15use gloo_utils::format::JsValueSerdeExt;
16use rand::RngExt;
17#[cfg(all(feature = "js", target_arch = "wasm32"))]
18use serde::{Deserialize, Serialize};
19#[cfg(all(feature = "js", target_arch = "wasm32"))]
20use wasm_bindgen::prelude::*;
21
22#[cfg(all(feature = "js", target_arch = "wasm32"))]
24#[derive(Debug, Deserialize, Clone, Serialize)]
25#[cfg_attr(feature = "js", wasm_bindgen)]
26pub struct GetEraSummaryResult(_GetEraSummaryResult);
27
28#[cfg(all(feature = "js", target_arch = "wasm32"))]
29impl From<GetEraSummaryResult> for _GetEraSummaryResult {
30 fn from(result: GetEraSummaryResult) -> Self {
31 result.0
32 }
33}
34
35#[cfg(all(feature = "js", target_arch = "wasm32"))]
36impl From<_GetEraSummaryResult> for GetEraSummaryResult {
37 fn from(result: _GetEraSummaryResult) -> Self {
38 GetEraSummaryResult(result)
39 }
40}
41
42#[cfg(all(feature = "js", target_arch = "wasm32"))]
43#[cfg_attr(feature = "js", wasm_bindgen)]
44impl GetEraSummaryResult {
45 #[cfg_attr(feature = "js", wasm_bindgen(getter))]
47 #[cfg(feature = "js")]
48 pub fn api_version(&self) -> JsValue {
49 JsValue::from_serde(&self.0.api_version).unwrap()
50 }
51
52 #[cfg_attr(feature = "js", wasm_bindgen(getter))]
54 #[cfg(feature = "js")]
55 pub fn era_summary(&self) -> JsValue {
56 JsValue::from_serde(&self.0.era_summary).unwrap()
57 }
58
59 #[cfg_attr(feature = "js", wasm_bindgen(js_name = "toJson"))]
61 #[cfg(feature = "js")]
62 pub fn to_json(&self) -> JsValue {
63 JsValue::from_serde(&self.0).unwrap_or(JsValue::null())
64 }
65}
66
67#[derive(Debug, Deserialize, Clone, Default, Serialize)]
69#[cfg(all(feature = "js", target_arch = "wasm32"))]
70#[cfg_attr(
71 feature = "js",
72 wasm_bindgen(js_name = "getEraSummaryOptions", getter_with_clone)
73)]
74pub struct GetEraSummaryOptions {
75 pub maybe_block_id_as_string: Option<String>,
76 pub maybe_block_identifier: Option<BlockIdentifier>,
77 pub rpc_address: Option<String>,
78 pub verbosity: Option<Verbosity>,
79}
80
81#[cfg(all(feature = "js", target_arch = "wasm32"))]
82#[cfg_attr(feature = "js", wasm_bindgen)]
83impl SDK {
84 #[cfg(feature = "js")]
94 pub fn get_era_summary_options(
95 &self,
96 options: JsValue,
97 ) -> Result<GetEraSummaryOptions, JsError> {
98 options
99 .into_serde::<GetEraSummaryOptions>()
100 .map_err(|err| JsError::new(&format!("Error deserializing options: {err:?}")))
101 }
102
103 #[cfg_attr(feature = "js", wasm_bindgen(js_name = "get_era_summary"))]
117 #[cfg(feature = "js")]
118 pub async fn get_era_summary_js_alias(
119 &self,
120 options: Option<GetEraSummaryOptions>,
121 ) -> Result<GetEraSummaryResult, JsError> {
122 let GetEraSummaryOptions {
123 maybe_block_id_as_string,
124 maybe_block_identifier,
125 verbosity,
126 rpc_address,
127 } = options.unwrap_or_default();
128
129 let maybe_block_identifier = if let Some(maybe_block_identifier) = maybe_block_identifier {
130 Some(BlockIdentifierInput::BlockIdentifier(
131 maybe_block_identifier,
132 ))
133 } else {
134 maybe_block_id_as_string.map(BlockIdentifierInput::String)
135 };
136
137 let result = self
138 .get_era_summary(maybe_block_identifier, verbosity, rpc_address)
139 .await;
140 match result {
141 Ok(data) => Ok(data.result.into()),
142 Err(err) => {
143 let err = &format!("Error occurred with {err:?}");
144 Err(JsError::new(err))
145 }
146 }
147 }
148
149 #[cfg_attr(feature = "js", wasm_bindgen(js_name = "chain_get_era_summary"))]
151 #[deprecated(note = "This function is an alias. Please use `get_era_summary` instead.")]
152 #[allow(deprecated)]
153 #[cfg(feature = "js")]
154 pub async fn chain_get_era_summary(
155 &self,
156 options: Option<GetEraSummaryOptions>,
157 ) -> Result<GetEraSummaryResult, JsError> {
158 self.get_era_summary_js_alias(options).await
159 }
160}
161
162impl SDK {
163 pub async fn get_era_summary(
179 &self,
180 maybe_block_identifier: Option<BlockIdentifierInput>,
181 verbosity: Option<Verbosity>,
182 rpc_address: Option<String>,
183 ) -> Result<SuccessResponse<_GetEraSummaryResult>, SdkError> {
184 let random_id = rand::rng().random::<u64>().to_string();
186 if let Some(BlockIdentifierInput::String(maybe_block_id)) = maybe_block_identifier {
187 get_era_summary_cli(
188 &random_id,
189 &self.get_rpc_address(rpc_address),
190 self.get_verbosity(verbosity).into(),
191 &maybe_block_id,
192 )
193 .await
194 .map_err(SdkError::from)
195 } else {
196 let maybe_block_identifier =
197 if let Some(BlockIdentifierInput::BlockIdentifier(maybe_block_identifier)) =
198 maybe_block_identifier
199 {
200 Some(maybe_block_identifier)
201 } else {
202 None
203 };
204 get_era_summary_lib(
205 JsonRpcId::from(random_id),
206 &self.get_rpc_address(rpc_address),
207 self.get_verbosity(verbosity).into(),
208 maybe_block_identifier.map(Into::into),
209 )
210 .await
211 .map_err(SdkError::from)
212 }
213 }
214}
215
216#[cfg(test)]
217mod tests {
218 use super::*;
219 use crate::types::{
220 hash::block_hash::BlockHash, identifier::block_identifier::BlockIdentifier,
221 };
222 use sdk_tests::tests::helpers::get_network_constants;
223
224 #[tokio::test]
225 async fn test_get_era_summary_with_none_values() {
226 let sdk = SDK::new(None, None, None);
228 let error_message = "failed to parse node address as valid URL";
229
230 let result = sdk.get_era_summary(None, None, None).await;
232
233 assert!(result.is_err());
235 let err_string = result.err().unwrap().to_string();
236 assert!(err_string.contains(error_message));
237 }
238
239 #[tokio::test]
240 async fn test_get_era_summary_with_block_id_string() {
241 let sdk = SDK::new(None, None, None);
243 let verbosity = Some(Verbosity::High);
244 let (rpc_address, _, _, _, _) = get_network_constants();
245 let result = sdk
246 .get_block(None, verbosity, Some(rpc_address.clone()))
247 .await;
248 let block_hash = BlockHash::from(
249 *result
250 .unwrap()
251 .result
252 .block_with_signatures
253 .unwrap()
254 .block
255 .hash(),
256 )
257 .to_string();
258
259 let block_identifier = BlockIdentifierInput::String(block_hash.to_string());
260
261 let result = sdk
263 .get_era_summary(Some(block_identifier), verbosity, Some(rpc_address))
264 .await;
265
266 assert!(result.is_ok());
268 }
269
270 #[tokio::test]
271 async fn test_get_era_summary_with_block_identifier() {
272 let sdk = SDK::new(None, None, None);
274 let block_identifier =
275 BlockIdentifierInput::BlockIdentifier(BlockIdentifier::from_height(11));
276 let verbosity = Some(Verbosity::High);
277 let (rpc_address, _, _, _, _) = get_network_constants();
278
279 let result = sdk
281 .get_era_summary(Some(block_identifier), verbosity, Some(rpc_address.clone()))
282 .await;
283 assert!(result.is_ok());
285 }
286
287 #[tokio::test]
288 async fn test_get_era_summary_with_error() {
289 let sdk = SDK::new(Some("http://localhost".to_string()), None, None);
290
291 let error_message = "error sending request";
292
293 let result = sdk.get_era_summary(None, None, None).await;
295
296 assert!(result.is_err());
298 let err_string = result.err().unwrap().to_string();
299 assert!(err_string.contains(error_message));
300 }
301}