casper_rust_wasm_sdk/sdk/rpcs/
get_era_info.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};
10#[allow(deprecated)]
11use casper_client::{
12 cli::get_era_info as get_era_info_cli, get_era_info as get_era_info_lib,
13 rpcs::results::GetEraInfoResult as _GetEraInfoResult, JsonRpcId, SuccessResponse,
14};
15#[cfg(all(feature = "js", target_arch = "wasm32"))]
16use gloo_utils::format::JsValueSerdeExt;
17use rand::RngExt;
18#[cfg(all(feature = "js", target_arch = "wasm32"))]
19use serde::{Deserialize, Serialize};
20#[cfg(all(feature = "js", target_arch = "wasm32"))]
21use wasm_bindgen::prelude::*;
22
23#[cfg(all(feature = "js", target_arch = "wasm32"))]
24#[derive(Debug, Deserialize, Clone, Serialize)]
25#[cfg_attr(feature = "js", wasm_bindgen)]
26pub struct GetEraInfoResult(_GetEraInfoResult);
27
28#[cfg(all(feature = "js", target_arch = "wasm32"))]
29impl From<GetEraInfoResult> for _GetEraInfoResult {
30 fn from(result: GetEraInfoResult) -> Self {
31 result.0
32 }
33}
34#[cfg(all(feature = "js", target_arch = "wasm32"))]
35impl From<_GetEraInfoResult> for GetEraInfoResult {
36 fn from(result: _GetEraInfoResult) -> Self {
37 GetEraInfoResult(result)
38 }
39}
40
41#[cfg(all(feature = "js", target_arch = "wasm32"))]
42#[cfg_attr(feature = "js", wasm_bindgen)]
43impl GetEraInfoResult {
44 #[cfg_attr(feature = "js", wasm_bindgen(getter))]
45 #[cfg(feature = "js")]
46 pub fn api_version(&self) -> JsValue {
47 JsValue::from_serde(&self.0.api_version).unwrap()
48 }
49
50 #[cfg_attr(feature = "js", wasm_bindgen(getter))]
51 #[cfg(feature = "js")]
52 pub fn era_summary(&self) -> JsValue {
53 JsValue::from_serde(&self.0.era_summary).unwrap()
54 }
55
56 #[cfg_attr(feature = "js", wasm_bindgen(js_name = "toJson"))]
57 #[cfg(feature = "js")]
58 pub fn to_json(&self) -> JsValue {
59 JsValue::from_serde(&self.0).unwrap_or(JsValue::null())
60 }
61}
62
63#[derive(Debug, Deserialize, Clone, Default, Serialize)]
64#[cfg(all(feature = "js", target_arch = "wasm32"))]
65#[cfg_attr(
66 feature = "js",
67 wasm_bindgen(js_name = "getEraInfoOptions", getter_with_clone)
68)]
69pub struct GetEraInfoOptions {
70 pub maybe_block_id_as_string: Option<String>,
71 pub maybe_block_identifier: Option<BlockIdentifier>,
72 pub rpc_address: Option<String>,
73 pub verbosity: Option<Verbosity>,
74}
75
76#[cfg(all(feature = "js", target_arch = "wasm32"))]
77#[cfg_attr(feature = "js", wasm_bindgen)]
78impl SDK {
79 #[deprecated(note = "prefer 'get_era_summary' as it doesn't require a switch block")]
80 #[allow(deprecated)]
81 #[cfg(feature = "js")]
82 pub fn get_era_info_options(&self, options: JsValue) -> Result<GetEraInfoOptions, JsError> {
83 options
84 .into_serde::<GetEraInfoOptions>()
85 .map_err(|err| JsError::new(&format!("Error deserializing options: {err:?}")))
86 }
87
88 #[deprecated(note = "prefer 'get_era_summary' as it doesn't require a switch block")]
89 #[allow(deprecated)]
90 #[cfg_attr(feature = "js", wasm_bindgen(js_name = "get_era_info"))]
91 #[cfg(feature = "js")]
92 pub async fn get_era_info_js_alias(
93 &self,
94 options: Option<GetEraInfoOptions>,
95 ) -> Result<GetEraInfoResult, JsError> {
96 let GetEraInfoOptions {
97 maybe_block_id_as_string,
98 maybe_block_identifier,
99 verbosity,
100 rpc_address,
101 } = options.unwrap_or_default();
102
103 let maybe_block_identifier = if let Some(maybe_block_identifier) = maybe_block_identifier {
104 Some(BlockIdentifierInput::BlockIdentifier(
105 maybe_block_identifier,
106 ))
107 } else {
108 maybe_block_id_as_string.map(BlockIdentifierInput::String)
109 };
110 let result = self
111 .get_era_info(maybe_block_identifier, verbosity, rpc_address)
112 .await;
113 match result {
114 Ok(data) => Ok(data.result.into()),
115 Err(err) => {
116 let err = &format!("Error occurred with {err:?}");
117
118 Err(JsError::new(err))
119 }
120 }
121 }
122
123 #[deprecated(note = "This function is an alias. Please use `get_era_info` instead.")]
125 #[allow(deprecated)]
126 #[cfg(feature = "js")]
127 pub async fn chain_get_era_info_by_switch_block(
128 &self,
129 options: Option<GetEraInfoOptions>,
130 ) -> Result<GetEraInfoResult, JsError> {
131 self.get_era_info_js_alias(options).await
132 }
133}
134
135impl SDK {
136 #[deprecated(note = "prefer 'get_era_summary' as it doesn't require a switch block")]
137 #[allow(deprecated)]
138 pub async fn get_era_info(
139 &self,
140 maybe_block_identifier: Option<BlockIdentifierInput>,
141 verbosity: Option<Verbosity>,
142 rpc_address: Option<String>,
143 ) -> Result<SuccessResponse<_GetEraInfoResult>, SdkError> {
144 let random_id = rand::rng().random::<u64>().to_string();
146 if let Some(BlockIdentifierInput::String(maybe_block_id)) = maybe_block_identifier {
147 get_era_info_cli(
148 &random_id,
149 &self.get_rpc_address(rpc_address),
150 self.get_verbosity(verbosity).into(),
151 &maybe_block_id,
152 )
153 .await
154 .map_err(SdkError::from)
155 } else {
156 let maybe_block_identifier =
157 if let Some(BlockIdentifierInput::BlockIdentifier(maybe_block_identifier)) =
158 maybe_block_identifier
159 {
160 Some(maybe_block_identifier)
161 } else {
162 None
163 };
164 get_era_info_lib(
165 JsonRpcId::from(random_id),
166 &self.get_rpc_address(rpc_address),
167 self.get_verbosity(verbosity).into(),
168 maybe_block_identifier.map(Into::into),
169 )
170 .await
171 .map_err(SdkError::from)
172 }
173 }
174}
175
176#[cfg(test)]
177mod tests {
178 use super::*;
179 use crate::types::{
180 hash::block_hash::BlockHash, identifier::block_identifier::BlockIdentifier,
181 };
182 use sdk_tests::tests::helpers::get_network_constants;
183
184 #[tokio::test]
185 #[allow(deprecated)]
186 async fn test_get_era_info_with_none_values() {
187 let sdk = SDK::new(None, None, None);
189 let error_message = "failed to parse node address as valid URL";
190
191 let result = sdk.get_era_info(None, None, None).await;
193
194 assert!(result.is_err());
196 let err_string = result.err().unwrap().to_string();
197 assert!(err_string.contains(error_message));
198 }
199
200 #[tokio::test]
201 #[allow(deprecated)]
202 async fn test_get_era_info_with_block_id_string() {
203 let sdk = SDK::new(None, None, None);
205 let verbosity = Some(Verbosity::High);
206 let (rpc_address, _, _, _, _) = get_network_constants();
207 let result = sdk
208 .get_block(None, verbosity, Some(rpc_address.clone()))
209 .await;
210 let block_hash = BlockHash::from(
211 *result
212 .unwrap()
213 .result
214 .block_with_signatures
215 .unwrap()
216 .block
217 .hash(),
218 )
219 .to_string();
220 let block_identifier = BlockIdentifierInput::String(block_hash.to_string());
221
222 let result = sdk
224 .get_era_info(Some(block_identifier), verbosity, Some(rpc_address))
225 .await;
226
227 assert!(result.is_ok());
229 }
230
231 #[tokio::test]
232 #[allow(deprecated)]
233 async fn test_get_era_info_with_block_identifier() {
234 let sdk = SDK::new(None, None, None);
236 let block_identifier =
237 BlockIdentifierInput::BlockIdentifier(BlockIdentifier::from_height(1));
238 let verbosity = Some(Verbosity::High);
239 let (rpc_address, _, _, _, _) = get_network_constants();
240
241 let result = sdk
243 .get_era_info(Some(block_identifier), verbosity, Some(rpc_address.clone()))
244 .await;
245
246 assert!(result.is_ok());
248 }
249
250 #[tokio::test]
251 #[allow(deprecated)]
252 async fn test_get_era_info_with_error() {
253 let sdk = SDK::new(Some("http://localhost".to_string()), None, None);
254
255 let error_message = "error sending request";
256
257 let result = sdk.get_era_info(None, None, None).await;
259
260 assert!(result.is_err());
262 let err_string = result.err().unwrap().to_string();
263 assert!(err_string.contains(error_message));
264 }
265}