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