Skip to main content

casper_rust_wasm_sdk/sdk/binary_port/
wasm32.rs

1#[cfg(all(feature = "js", target_arch = "wasm32"))]
2use crate::{
3    types::{
4        digest::Digest,
5        era_id::EraId,
6        hash::{block_hash::BlockHash, transaction_hash::TransactionHash},
7        key::Key,
8        public_key::PublicKey,
9        record_id::RecordId,
10        sdk_error::SdkError,
11        transaction::Transaction,
12    },
13    SDK,
14};
15#[cfg(all(feature = "js", target_arch = "wasm32"))]
16use gloo_utils::format::JsValueSerdeExt;
17#[cfg(all(feature = "js", target_arch = "wasm32"))]
18use wasm_bindgen::prelude::*;
19
20#[cfg(all(feature = "js", target_arch = "wasm32"))]
21#[cfg_attr(feature = "js", wasm_bindgen)]
22impl SDK {
23    #[cfg_attr(
24        feature = "js",
25        wasm_bindgen(js_name = "get_binary_latest_switch_block_header")
26    )]
27    #[cfg(feature = "js")]
28    pub async fn get_binary_latest_switch_block_header_js_alias(
29        &self,
30        node_address: Option<String>,
31    ) -> Result<JsValue, JsError> {
32        let result = self
33            .get_binary_latest_switch_block_header(node_address)
34            .await;
35
36        result
37            .and_then(|data| JsValue::from_serde(&data).map_err(SdkError::SerializationError))
38            .map_err(|err| JsError::new(&format!("Error occurred: {err:?}")))
39    }
40
41    #[cfg_attr(
42        feature = "js",
43        wasm_bindgen(js_name = "get_binary_latest_block_header")
44    )]
45    #[cfg(feature = "js")]
46    pub async fn get_binary_latest_block_header_js_alias(
47        &self,
48        node_address: Option<String>,
49    ) -> Result<JsValue, JsError> {
50        let result = self.get_binary_latest_block_header(node_address).await;
51        result
52            .and_then(|data| JsValue::from_serde(&data).map_err(SdkError::SerializationError))
53            .map_err(|err| JsError::new(&format!("Error occurred: {err:?}")))
54    }
55
56    #[cfg_attr(
57        feature = "js",
58        wasm_bindgen(js_name = "get_binary_block_header_by_height")
59    )]
60    #[cfg(feature = "js")]
61    pub async fn get_binary_block_header_by_height_js_alias(
62        &self,
63        height: u64,
64        node_address: Option<String>,
65    ) -> Result<JsValue, JsError> {
66        let result = self
67            .get_binary_block_header_by_height(node_address, height)
68            .await;
69        result
70            .and_then(|data| JsValue::from_serde(&data).map_err(SdkError::SerializationError))
71            .map_err(|err| JsError::new(&format!("Error occurred: {err:?}")))
72    }
73
74    #[cfg_attr(
75        feature = "js",
76        wasm_bindgen(js_name = "get_binary_block_header_by_hash")
77    )]
78    #[cfg(feature = "js")]
79    pub async fn get_binary_block_header_by_hash_js_alias(
80        &self,
81        block_hash: BlockHash,
82        node_address: Option<String>,
83    ) -> Result<JsValue, JsError> {
84        let result = self
85            .get_binary_block_header_by_hash(node_address, block_hash.into())
86            .await;
87        result
88            .and_then(|data| JsValue::from_serde(&data).map_err(SdkError::SerializationError))
89            .map_err(|err| JsError::new(&format!("Error occurred: {err:?}")))
90    }
91
92    #[cfg_attr(
93        feature = "js",
94        wasm_bindgen(js_name = "get_binary_latest_block_with_signatures")
95    )]
96    #[cfg(feature = "js")]
97    pub async fn get_binary_latest_block_with_signatures_js_alias(
98        &self,
99        node_address: Option<String>,
100    ) -> Result<JsValue, JsError> {
101        let result = self
102            .get_binary_latest_block_with_signatures(node_address)
103            .await;
104        result
105            .and_then(|data| JsValue::from_serde(&data).map_err(SdkError::SerializationError))
106            .map_err(|err| JsError::new(&format!("Error occurred: {err:?}")))
107    }
108
109    #[cfg_attr(
110        feature = "js",
111        wasm_bindgen(js_name = "get_binary_block_with_signatures_by_height")
112    )]
113    #[cfg(feature = "js")]
114    pub async fn get_binary_block_with_signatures_by_height_js_alias(
115        &self,
116        height: u64,
117        node_address: Option<String>,
118    ) -> Result<JsValue, JsError> {
119        let result = self
120            .get_binary_block_with_signatures_by_height(node_address, height)
121            .await;
122        result
123            .and_then(|data| JsValue::from_serde(&data).map_err(SdkError::SerializationError))
124            .map_err(|err| JsError::new(&format!("Error occurred: {err:?}")))
125    }
126
127    #[cfg_attr(
128        feature = "js",
129        wasm_bindgen(js_name = "get_binary_block_with_signatures_by_hash")
130    )]
131    #[cfg(feature = "js")]
132    pub async fn get_binary_block_with_signatures_by_hash_js_alias(
133        &self,
134        block_hash: BlockHash,
135        node_address: Option<String>,
136    ) -> Result<JsValue, JsError> {
137        let result = self
138            .get_binary_block_with_signatures_by_hash(node_address, block_hash.into())
139            .await;
140        result
141            .and_then(|data| JsValue::from_serde(&data).map_err(SdkError::SerializationError))
142            .map_err(|err| JsError::new(&format!("Error occurred: {err:?}")))
143    }
144
145    #[cfg_attr(
146        feature = "js",
147        wasm_bindgen(js_name = "get_binary_transaction_by_hash")
148    )]
149    #[cfg(feature = "js")]
150    pub async fn get_binary_transaction_by_hash_js_alias(
151        &self,
152        hash: TransactionHash,
153        with_finalized_approvals: bool,
154        node_address: Option<String>,
155    ) -> Result<JsValue, JsError> {
156        let result = self
157            .get_binary_transaction_by_hash(node_address, hash.into(), with_finalized_approvals)
158            .await;
159        result
160            .and_then(|data| JsValue::from_serde(&data).map_err(SdkError::SerializationError))
161            .map_err(|err| JsError::new(&format!("Error occurred: {err:?}")))
162    }
163
164    #[cfg_attr(feature = "js", wasm_bindgen(js_name = "get_binary_peers"))]
165    #[cfg(feature = "js")]
166    pub async fn get_binary_peers_js_alias(
167        &self,
168        node_address: Option<String>,
169    ) -> Result<JsValue, JsError> {
170        let result = self.get_binary_peers(node_address).await;
171        result
172            .and_then(|data| JsValue::from_serde(&data).map_err(SdkError::SerializationError))
173            .map_err(|err| JsError::new(&format!("Error occurred: {err:?}")))
174    }
175
176    #[cfg_attr(feature = "js", wasm_bindgen(js_name = "get_binary_uptime"))]
177    #[cfg(feature = "js")]
178    pub async fn get_binary_uptime_js_alias(
179        &self,
180        node_address: Option<String>,
181    ) -> Result<JsValue, JsError> {
182        let result = self.get_binary_uptime(node_address).await;
183        result
184            .and_then(|data| JsValue::from_serde(&data).map_err(SdkError::SerializationError))
185            .map_err(|err| JsError::new(&format!("Error occurred: {err:?}")))
186    }
187
188    #[cfg_attr(feature = "js", wasm_bindgen(js_name = "get_binary_last_progress"))]
189    #[cfg(feature = "js")]
190    pub async fn get_binary_last_progress_js_alias(
191        &self,
192        node_address: Option<String>,
193    ) -> Result<JsValue, JsError> {
194        let result = self.get_binary_last_progress(node_address).await;
195        result
196            .and_then(|data| JsValue::from_serde(&data).map_err(SdkError::SerializationError))
197            .map_err(|err| JsError::new(&format!("Error occurred: {err:?}")))
198    }
199
200    #[cfg_attr(feature = "js", wasm_bindgen(js_name = "get_binary_reactor_state"))]
201    #[cfg(feature = "js")]
202    pub async fn get_binary_reactor_state_js_alias(
203        &self,
204        node_address: Option<String>,
205    ) -> Result<JsValue, JsError> {
206        let result = self.get_binary_reactor_state(node_address).await;
207        result
208            .and_then(|data| JsValue::from_serde(&data).map_err(SdkError::SerializationError))
209            .map_err(|err| JsError::new(&format!("Error occurred: {err:?}")))
210    }
211
212    #[cfg_attr(feature = "js", wasm_bindgen(js_name = "get_binary_network_name"))]
213    #[cfg(feature = "js")]
214    pub async fn get_binary_network_name_js_alias(
215        &self,
216        node_address: Option<String>,
217    ) -> Result<JsValue, JsError> {
218        let result = self.get_binary_network_name(node_address).await;
219        result
220            .and_then(|data| JsValue::from_serde(&data).map_err(SdkError::SerializationError))
221            .map_err(|err| JsError::new(&format!("Error occurred: {err:?}")))
222    }
223
224    #[cfg_attr(
225        feature = "js",
226        wasm_bindgen(js_name = "get_binary_consensus_validator_changes")
227    )]
228    #[cfg(feature = "js")]
229    pub async fn get_binary_consensus_validator_changes_js_alias(
230        &self,
231        node_address: Option<String>,
232    ) -> Result<JsValue, JsError> {
233        let result = self
234            .get_binary_consensus_validator_changes(node_address)
235            .await;
236        result
237            .and_then(|data| JsValue::from_serde(&data).map_err(SdkError::SerializationError))
238            .map_err(|err| JsError::new(&format!("Error occurred: {err:?}")))
239    }
240
241    #[cfg_attr(
242        feature = "js",
243        wasm_bindgen(js_name = "get_binary_block_synchronizer_status")
244    )]
245    #[cfg(feature = "js")]
246    pub async fn get_binary_block_synchronizer_status_js_alias(
247        &self,
248        node_address: Option<String>,
249    ) -> Result<JsValue, JsError> {
250        let result = self
251            .get_binary_block_synchronizer_status(node_address)
252            .await;
253        result
254            .and_then(|data| JsValue::from_serde(&data).map_err(SdkError::SerializationError))
255            .map_err(|err| JsError::new(&format!("Error occurred: {err:?}")))
256    }
257
258    #[cfg_attr(
259        feature = "js",
260        wasm_bindgen(js_name = "get_binary_available_block_range")
261    )]
262    #[cfg(feature = "js")]
263    pub async fn get_binary_available_block_range_js_alias(
264        &self,
265        node_address: Option<String>,
266    ) -> Result<JsValue, JsError> {
267        let result = self.get_binary_available_block_range(node_address).await;
268        result
269            .and_then(|data| JsValue::from_serde(&data).map_err(SdkError::SerializationError))
270            .map_err(|err| JsError::new(&format!("Error occurred: {err:?}")))
271    }
272
273    #[cfg_attr(feature = "js", wasm_bindgen(js_name = "get_binary_next_upgrade"))]
274    #[cfg(feature = "js")]
275    pub async fn get_binary_next_upgrade_js_alias(
276        &self,
277        node_address: Option<String>,
278    ) -> Result<JsValue, JsError> {
279        let result = self.get_binary_next_upgrade(node_address).await;
280        result
281            .and_then(|data| JsValue::from_serde(&data).map_err(SdkError::SerializationError))
282            .map_err(|err| JsError::new(&format!("Error occurred: {err:?}")))
283    }
284
285    #[cfg_attr(feature = "js", wasm_bindgen(js_name = "get_binary_consensus_status"))]
286    #[cfg(feature = "js")]
287    pub async fn get_binary_consensus_status_js_alias(
288        &self,
289        node_address: Option<String>,
290    ) -> Result<JsValue, JsError> {
291        let result = self.get_binary_consensus_status(node_address).await;
292        result
293            .and_then(|data| JsValue::from_serde(&data).map_err(SdkError::SerializationError))
294            .map_err(|err| JsError::new(&format!("Error occurred: {err:?}")))
295    }
296
297    #[cfg_attr(
298        feature = "js",
299        wasm_bindgen(js_name = "get_binary_chainspec_raw_bytes")
300    )]
301    #[cfg(feature = "js")]
302    pub async fn get_binary_chainspec_raw_bytes_js_alias(
303        &self,
304        node_address: Option<String>,
305    ) -> Result<JsValue, JsError> {
306        let result = self.get_binary_chainspec_raw_bytes(node_address).await;
307        result
308            .and_then(|data| JsValue::from_serde(&data).map_err(SdkError::SerializationError))
309            .map_err(|err| JsError::new(&format!("Error occurred: {err:?}")))
310    }
311
312    #[cfg_attr(feature = "js", wasm_bindgen(js_name = "get_binary_node_status"))]
313    #[cfg(feature = "js")]
314    pub async fn get_binary_node_status_js_alias(
315        &self,
316        node_address: Option<String>,
317    ) -> Result<JsValue, JsError> {
318        let result = self.get_binary_node_status(node_address).await;
319        result
320            .and_then(|data| JsValue::from_serde(&data).map_err(SdkError::SerializationError))
321            .map_err(|err| JsError::new(&format!("Error occurred: {err:?}")))
322    }
323
324    #[cfg_attr(
325        feature = "js",
326        wasm_bindgen(js_name = "get_binary_validator_reward_by_era")
327    )]
328    #[cfg(feature = "js")]
329    pub async fn get_binary_validator_reward_by_era_js_alias(
330        &self,
331        validator_key: PublicKey,
332        era: EraId,
333        node_address: Option<String>,
334    ) -> Result<JsValue, JsError> {
335        let result = self
336            .get_binary_validator_reward_by_era(node_address, validator_key.into(), era.into())
337            .await;
338        result
339            .and_then(|data| JsValue::from_serde(&data).map_err(SdkError::SerializationError))
340            .map_err(|err| JsError::new(&format!("Error occurred: {err:?}")))
341    }
342
343    #[cfg_attr(
344        feature = "js",
345        wasm_bindgen(js_name = "get_binary_validator_reward_by_block_height")
346    )]
347    #[cfg(feature = "js")]
348    pub async fn get_binary_validator_reward_by_block_height_js_alias(
349        &self,
350        validator_key: PublicKey,
351        block_height: u64,
352        node_address: Option<String>,
353    ) -> Result<JsValue, JsError> {
354        let result = self
355            .get_binary_validator_reward_by_block_height(
356                node_address,
357                validator_key.into(),
358                block_height,
359            )
360            .await;
361        result
362            .and_then(|data| JsValue::from_serde(&data).map_err(SdkError::SerializationError))
363            .map_err(|err| JsError::new(&format!("Error occurred: {err:?}")))
364    }
365
366    #[cfg_attr(
367        feature = "js",
368        wasm_bindgen(js_name = "get_binary_validator_reward_by_block_hash")
369    )]
370    #[cfg(feature = "js")]
371    pub async fn get_binary_validator_reward_by_block_hash_js_alias(
372        &self,
373        validator_key: PublicKey,
374        block_hash: BlockHash,
375        node_address: Option<String>,
376    ) -> Result<JsValue, JsError> {
377        let result = self
378            .get_binary_validator_reward_by_block_hash(
379                node_address,
380                validator_key.into(),
381                block_hash.into(),
382            )
383            .await;
384        result
385            .and_then(|data| JsValue::from_serde(&data).map_err(SdkError::SerializationError))
386            .map_err(|err| JsError::new(&format!("Error occurred: {err:?}")))
387    }
388
389    #[cfg_attr(
390        feature = "js",
391        wasm_bindgen(js_name = "get_binary_delegator_reward_by_era")
392    )]
393    #[cfg(feature = "js")]
394    pub async fn get_binary_delegator_reward_by_era_js_alias(
395        &self,
396        validator_key: PublicKey,
397        delegator_key: PublicKey,
398        era: EraId,
399        node_address: Option<String>,
400    ) -> Result<JsValue, JsError> {
401        let result = self
402            .get_binary_delegator_reward_by_era(
403                node_address,
404                validator_key.into(),
405                delegator_key.into(),
406                era.into(),
407            )
408            .await;
409        result
410            .and_then(|data| JsValue::from_serde(&data).map_err(SdkError::SerializationError))
411            .map_err(|err| JsError::new(&format!("Error occurred: {err:?}")))
412    }
413
414    #[cfg_attr(
415        feature = "js",
416        wasm_bindgen(js_name = "get_binary_delegator_reward_by_block_height")
417    )]
418    #[cfg(feature = "js")]
419    pub async fn get_binary_delegator_reward_by_block_height_js_alias(
420        &self,
421        validator_key: PublicKey,
422        delegator_key: PublicKey,
423        block_height: u64,
424        node_address: Option<String>,
425    ) -> Result<JsValue, JsError> {
426        let result = self
427            .get_binary_delegator_reward_by_block_height(
428                node_address,
429                validator_key.into(),
430                delegator_key.into(),
431                block_height,
432            )
433            .await;
434        result
435            .and_then(|data| JsValue::from_serde(&data).map_err(SdkError::SerializationError))
436            .map_err(|err| JsError::new(&format!("Error occurred: {err:?}")))
437    }
438
439    #[cfg_attr(
440        feature = "js",
441        wasm_bindgen(js_name = "get_binary_delegator_reward_by_block_hash")
442    )]
443    #[cfg(feature = "js")]
444    pub async fn get_binary_delegator_reward_by_block_hash_js_alias(
445        &self,
446        validator_key: PublicKey,
447        delegator_key: PublicKey,
448        block_hash: BlockHash,
449        node_address: Option<String>,
450    ) -> Result<JsValue, JsError> {
451        let result = self
452            .get_binary_delegator_reward_by_block_hash(
453                node_address,
454                validator_key.into(),
455                delegator_key.into(),
456                block_hash.into(),
457            )
458            .await;
459        result
460            .and_then(|data| JsValue::from_serde(&data).map_err(SdkError::SerializationError))
461            .map_err(|err| JsError::new(&format!("Error occurred: {err:?}")))
462    }
463
464    #[cfg_attr(feature = "js", wasm_bindgen(js_name = "get_binary_read_record"))]
465    #[cfg(feature = "js")]
466    pub async fn get_binary_read_record_js_alias(
467        &self,
468        record_id: RecordId,
469        key: Vec<u8>,
470        node_address: Option<String>,
471    ) -> Result<JsValue, JsError> {
472        let result = self
473            .get_binary_read_record(node_address, record_id.into(), &key)
474            .await;
475        result
476            .and_then(|data| JsValue::from_serde(&data).map_err(SdkError::SerializationError))
477            .map_err(|err| JsError::new(&format!("Error occurred: {err:?}")))
478    }
479
480    #[cfg_attr(feature = "js", wasm_bindgen(js_name = "get_binary_global_state_item"))]
481    #[cfg(feature = "js")]
482    pub async fn get_binary_global_state_item_js_alias(
483        &self,
484        key: Key,
485        path: Vec<String>,
486        node_address: Option<String>,
487    ) -> Result<JsValue, JsError> {
488        let result = self
489            .get_binary_global_state_item(node_address, key.into(), path)
490            .await;
491        result
492            .and_then(|data| JsValue::from_serde(&data).map_err(SdkError::SerializationError))
493            .map_err(|err| JsError::new(&format!("Error occurred: {err:?}")))
494    }
495
496    #[cfg_attr(
497        feature = "js",
498        wasm_bindgen(js_name = "get_binary_global_state_item_by_state_root_hash")
499    )]
500    #[cfg(feature = "js")]
501    pub async fn get_binary_global_state_item_by_state_root_hash_js_alias(
502        &self,
503        state_root_hash: Digest,
504        key: Key,
505        path: Vec<String>,
506        node_address: Option<String>,
507    ) -> Result<JsValue, JsError> {
508        let result = self
509            .get_binary_global_state_item_by_state_root_hash(
510                node_address,
511                state_root_hash.into(),
512                key.into(),
513                path,
514            )
515            .await;
516        result
517            .and_then(|data| JsValue::from_serde(&data).map_err(SdkError::SerializationError))
518            .map_err(|err| JsError::new(&format!("Error occurred: {err:?}")))
519    }
520
521    #[cfg_attr(
522        feature = "js",
523        wasm_bindgen(js_name = "get_binary_global_state_item_by_block_hash")
524    )]
525    #[cfg(feature = "js")]
526    pub async fn get_binary_global_state_item_by_block_hash_js_alias(
527        &self,
528        block_hash: BlockHash,
529        key: Key,
530        path: Vec<String>,
531        node_address: Option<String>,
532    ) -> Result<JsValue, JsError> {
533        let result = self
534            .get_binary_global_state_item_by_block_hash(
535                node_address,
536                block_hash.into(),
537                key.into(),
538                path,
539            )
540            .await;
541        result
542            .and_then(|data| JsValue::from_serde(&data).map_err(SdkError::SerializationError))
543            .map_err(|err| JsError::new(&format!("Error occurred: {err:?}")))
544    }
545
546    #[cfg_attr(
547        feature = "js",
548        wasm_bindgen(js_name = "get_binary_global_state_item_by_block_height")
549    )]
550    #[cfg(feature = "js")]
551    pub async fn get_binary_global_state_item_by_block_height_js_alias(
552        &self,
553        block_height: u64,
554        key: Key,
555        path: Vec<String>,
556        node_address: Option<String>,
557    ) -> Result<JsValue, JsError> {
558        let result = self
559            .get_binary_global_state_item_by_block_height(
560                node_address,
561                block_height,
562                key.into(),
563                path,
564            )
565            .await;
566        result
567            .and_then(|data| JsValue::from_serde(&data).map_err(SdkError::SerializationError))
568            .map_err(|err| JsError::new(&format!("Error occurred: {err:?}")))
569    }
570
571    #[cfg_attr(
572        feature = "js",
573        wasm_bindgen(js_name = "get_binary_try_accept_transaction")
574    )]
575    #[cfg(feature = "js")]
576    pub async fn get_binary_try_accept_transaction_js_alias(
577        &self,
578        transaction: Transaction,
579        node_address: Option<String>,
580    ) -> Result<JsValue, JsError> {
581        let result = self
582            .get_binary_try_accept_transaction(node_address, transaction.into())
583            .await;
584
585        match result {
586            Ok(_) => Ok(JsValue::undefined()), // Return an appropriate JsValue for success
587            Err(err) => Err(JsError::new(&format!("Error occurred: {err:?}"))),
588        }
589    }
590
591    #[cfg_attr(
592        feature = "js",
593        wasm_bindgen(js_name = "get_binary_try_speculative_execution")
594    )]
595    #[cfg(feature = "js")]
596    pub async fn get_binary_try_speculative_execution_js_alias(
597        &self,
598        transaction: Transaction,
599        node_address: Option<String>,
600    ) -> Result<JsValue, JsError> {
601        let result = self
602            .get_binary_try_speculative_execution(node_address, transaction.into())
603            .await;
604        result
605            .and_then(|data| JsValue::from_serde(&data).map_err(SdkError::SerializationError))
606            .map_err(|err| JsError::new(&format!("Error occurred: {err:?}")))
607    }
608
609    #[cfg_attr(feature = "js", wasm_bindgen(js_name = "get_binary_protocol_version"))]
610    #[cfg(feature = "js")]
611    pub async fn get_binary_protocol_version_js_alias(
612        &self,
613        node_address: Option<String>,
614    ) -> Result<JsValue, JsError> {
615        let result = self.get_binary_protocol_version(node_address).await;
616        result
617            .and_then(|data| JsValue::from_serde(&data).map_err(SdkError::SerializationError))
618            .map_err(|err| JsError::new(&format!("Error occurred: {err:?}")))
619    }
620}