casper_rust_wasm_sdk/sdk/rpcs/
get_deploy.rs

1#[cfg(target_arch = "wasm32")]
2use crate::types::deploy::Deploy;
3use crate::types::hash::deploy_hash::DeployHash;
4use crate::{types::verbosity::Verbosity, SDK};
5use casper_client::{
6    get_deploy, rpcs::results::GetDeployResult as _GetDeployResult, Error, JsonRpcId,
7    SuccessResponse,
8};
9#[cfg(target_arch = "wasm32")]
10use gloo_utils::format::JsValueSerdeExt;
11use rand::Rng;
12#[cfg(target_arch = "wasm32")]
13use serde::{Deserialize, Serialize};
14#[cfg(target_arch = "wasm32")]
15use wasm_bindgen::prelude::*;
16
17// Define a struct to wrap the GetDeployResult
18#[cfg(target_arch = "wasm32")]
19#[derive(Debug, Deserialize, Clone, Serialize)]
20#[wasm_bindgen]
21pub struct GetDeployResult(_GetDeployResult);
22
23#[cfg(target_arch = "wasm32")]
24impl From<GetDeployResult> for _GetDeployResult {
25    fn from(result: GetDeployResult) -> Self {
26        result.0
27    }
28}
29#[cfg(target_arch = "wasm32")]
30impl From<_GetDeployResult> for GetDeployResult {
31    fn from(result: _GetDeployResult) -> Self {
32        GetDeployResult(result)
33    }
34}
35
36#[cfg(target_arch = "wasm32")]
37#[wasm_bindgen]
38impl GetDeployResult {
39    #[wasm_bindgen(getter)]
40    /// Gets the API version as a JavaScript value.
41    pub fn api_version(&self) -> JsValue {
42        JsValue::from_serde(&self.0.api_version).unwrap()
43    }
44
45    #[wasm_bindgen(getter)]
46    /// Gets the deploy information.
47    pub fn deploy(&self) -> Deploy {
48        self.0.deploy.clone().into()
49    }
50
51    #[wasm_bindgen(getter)]
52    /// Gets the execution info as a JavaScript value.
53    pub fn execution_info(&self) -> JsValue {
54        JsValue::from_serde(&self.0.execution_info).unwrap()
55    }
56
57    #[wasm_bindgen(js_name = "toJson")]
58    /// Converts the result to a JSON JavaScript value.
59    pub fn to_json(&self) -> JsValue {
60        JsValue::from_serde(&self.0).unwrap_or(JsValue::null())
61    }
62}
63
64/// Options for the `get_deploy` method.
65#[derive(Debug, Clone, Default, Deserialize, Serialize)]
66#[cfg(target_arch = "wasm32")]
67#[wasm_bindgen(js_name = "getDeployOptions", getter_with_clone)]
68pub struct GetDeployOptions {
69    pub deploy_hash_as_string: Option<String>,
70    pub deploy_hash: Option<DeployHash>,
71    pub finalized_approvals: Option<bool>,
72    pub rpc_address: Option<String>,
73    pub verbosity: Option<Verbosity>,
74}
75
76#[cfg(target_arch = "wasm32")]
77#[wasm_bindgen]
78impl SDK {
79    /// Parses deploy options from a JsValue.
80    ///
81    /// # Arguments
82    ///
83    /// * `options` - A JsValue containing deploy options to be parsed.
84    ///
85    /// # Returns
86    ///
87    /// Parsed deploy options as a `GetDeployOptions` struct.
88    #[deprecated(note = "prefer 'get_transaction_options'")]
89    #[allow(deprecated)]
90    pub fn get_deploy_options(&self, options: JsValue) -> Result<GetDeployOptions, JsError> {
91        let mut options: GetDeployOptions = options.into_serde()?;
92
93        // Handle finalized_approvals
94        if let Some(finalized_approvals) = options.finalized_approvals {
95            options.finalized_approvals =
96                Some(JsValue::from_bool(finalized_approvals) == JsValue::TRUE);
97        }
98
99        Ok(options)
100    }
101
102    /// Retrieves deploy information using the provided options.
103    ///
104    /// # Arguments
105    ///
106    /// * `options` - An optional `GetDeployOptions` struct containing retrieval options.
107    ///
108    /// # Returns
109    ///
110    /// A `Result` containing either a `GetDeployResult` or an error.
111    #[deprecated(note = "prefer 'get_transaction'")]
112    #[allow(deprecated)]
113    #[wasm_bindgen(js_name = "get_deploy")]
114    pub async fn get_deploy_js_alias(
115        &self,
116        options: Option<GetDeployOptions>,
117    ) -> Result<GetDeployResult, JsError> {
118        let GetDeployOptions {
119            deploy_hash_as_string,
120            deploy_hash,
121            finalized_approvals,
122            verbosity,
123            rpc_address,
124        } = options.unwrap_or_default();
125
126        let err_msg = "Error: Missing deploy hash as string or deploy hash".to_string();
127        let deploy_hash = if let Some(deploy_hash_as_string) = deploy_hash_as_string {
128            DeployHash::new(&deploy_hash_as_string)?
129        } else {
130            if deploy_hash.is_none() {
131                return Err(JsError::new(&err_msg));
132            }
133            deploy_hash.unwrap()
134        };
135
136        let result = self
137            .get_deploy(deploy_hash, finalized_approvals, verbosity, rpc_address)
138            .await;
139        match result {
140            Ok(data) => Ok(data.result.into()),
141            Err(err) => {
142                let err = &format!("Error occurred with {:?}", err);
143                Err(JsError::new(err))
144            }
145        }
146    }
147
148    /// Retrieves deploy information using the provided options, alias for `get_deploy`.
149    #[deprecated(note = "This function is an alias. Please use `get_transaction` instead.")]
150    #[allow(deprecated)]
151    pub async fn info_get_deploy(
152        &self,
153        options: Option<GetDeployOptions>,
154    ) -> Result<GetDeployResult, JsError> {
155        self.get_deploy_js_alias(options).await
156    }
157}
158
159impl SDK {
160    /// Retrieves deploy information based on the provided options.
161    ///
162    /// # Arguments
163    ///
164    /// * `deploy_hash` - The deploy hash.
165    /// * `finalized_approvals` - An optional boolean indicating finalized approvals.
166    /// * `verbosity` - An optional verbosity level.
167    /// * `rpc_address` - An optional rpc address.
168    ///
169    /// # Returns
170    ///
171    /// A `Result` containing either a `_GetDeployResult` or an error.
172    #[deprecated(note = "prefer 'get_transaction'")]
173    #[allow(deprecated)]
174    pub async fn get_deploy(
175        &self,
176        deploy_hash: DeployHash,
177        finalized_approvals: Option<bool>,
178        verbosity: Option<Verbosity>,
179        rpc_address: Option<String>,
180    ) -> Result<SuccessResponse<_GetDeployResult>, Error> {
181        //log("get_deploy!");
182        get_deploy(
183            JsonRpcId::from(rand::thread_rng().gen::<u64>().to_string()),
184            &self.get_rpc_address(rpc_address),
185            self.get_verbosity(verbosity).into(),
186            deploy_hash.into(),
187            finalized_approvals.unwrap_or_default(),
188        )
189        .await
190    }
191}
192
193#[cfg(test)]
194mod tests {
195    use super::*;
196    use crate::{
197        helpers::public_key_from_secret_key,
198        types::deploy_params::{
199            deploy_str_params::DeployStrParams, payment_str_params::PaymentStrParams,
200        },
201    };
202    use sdk_tests::{
203        config::{PAYMENT_TRANSFER_AMOUNT, TRANSFER_AMOUNT},
204        tests::helpers::{get_network_constants, get_user_secret_key},
205    };
206
207    #[tokio::test]
208    #[allow(deprecated)]
209    async fn test_get_deploy_with_none_values() {
210        // Arrange
211        let sdk = SDK::new(None, None, None);
212        let deploy_hash = DeployHash::from_digest([1u8; 32].into()).unwrap();
213        let error_message = "failed to parse node address as valid URL";
214
215        // Act
216        let result = sdk.get_deploy(deploy_hash, None, None, None).await;
217
218        // Assert
219        assert!(result.is_err());
220        let err_string = result.err().unwrap().to_string();
221        assert!(err_string.contains(error_message));
222    }
223
224    #[tokio::test]
225    #[allow(deprecated)]
226    async fn test_get_deploy_with_invalid_deploy_hash() {
227        // Arrange
228        let sdk = SDK::new(None, None, None);
229        let deploy_hash = DeployHash::from_digest([1u8; 32].into()).unwrap();
230        let verbosity = Some(Verbosity::High);
231        let (rpc_address, _, _, _, _) = get_network_constants();
232
233        // Act
234        let result = sdk
235            .get_deploy(deploy_hash, None, verbosity, Some(rpc_address))
236            .await;
237
238        // Assert
239        assert!(result.is_err());
240    }
241
242    #[tokio::test]
243    #[allow(deprecated)]
244    async fn test_get_deploy_with_valid_deploy_hash() {
245        // Arrange
246        let sdk = SDK::new(None, None, None);
247        let verbosity = Some(Verbosity::High);
248        let (rpc_address, _, _, _, chain_name) = get_network_constants();
249
250        let secret_key = get_user_secret_key(None).unwrap();
251        let account = public_key_from_secret_key(&secret_key).unwrap();
252
253        let deploy_params =
254            DeployStrParams::new(&chain_name, &account, Some(secret_key), None, None, None);
255        let payment_params = PaymentStrParams::default();
256        payment_params.set_payment_amount(PAYMENT_TRANSFER_AMOUNT);
257        let make_transfer = sdk
258            .transfer(
259                TRANSFER_AMOUNT,
260                &account, // self transfer
261                None,
262                deploy_params,
263                payment_params,
264                verbosity,
265                Some(rpc_address.clone()),
266            )
267            .await
268            .unwrap();
269        let deploy_hash = make_transfer.result.deploy_hash;
270        assert!(!deploy_hash.to_string().is_empty());
271
272        // Act
273        let result = sdk
274            .get_deploy(deploy_hash.into(), None, verbosity, Some(rpc_address))
275            .await;
276
277        // Assert
278        assert!(result.is_ok());
279    }
280
281    #[tokio::test]
282    #[allow(deprecated)]
283    async fn test_get_deploy_with_finalized_approvals() {
284        // Arrange
285        let sdk = SDK::new(None, None, None);
286        let verbosity = Some(Verbosity::High);
287        let (rpc_address, _, _, _, chain_name) = get_network_constants();
288
289        let secret_key = get_user_secret_key(None).unwrap();
290        let account = public_key_from_secret_key(&secret_key).unwrap();
291
292        let deploy_params =
293            DeployStrParams::new(&chain_name, &account, Some(secret_key), None, None, None);
294        let payment_params = PaymentStrParams::default();
295        payment_params.set_payment_amount(PAYMENT_TRANSFER_AMOUNT);
296        let make_transfer = sdk
297            .transfer(
298                TRANSFER_AMOUNT,
299                &account, // self transfer
300                None,
301                deploy_params,
302                payment_params,
303                verbosity,
304                Some(rpc_address.clone()),
305            )
306            .await
307            .unwrap();
308        let deploy_hash = make_transfer.result.deploy_hash;
309        assert!(!deploy_hash.to_string().is_empty());
310        let finalized_approvals = true;
311
312        // Act
313        let result = sdk
314            .get_deploy(
315                deploy_hash.into(),
316                Some(finalized_approvals),
317                verbosity,
318                Some(rpc_address),
319            )
320            .await;
321
322        // Assert
323        assert!(result.is_ok());
324    }
325
326    #[tokio::test]
327    #[allow(deprecated)]
328    async fn test_get_deploy_with_error() {
329        // Arrange
330        let sdk = SDK::new(Some("http://localhost".to_string()), None, None);
331        let deploy_hash = DeployHash::from_digest([1u8; 32].into()).unwrap();
332        let error_message = "error sending request for url (http://localhost/rpc)";
333
334        // Act
335        let result = sdk.get_deploy(deploy_hash, None, None, None).await;
336
337        // Assert
338        assert!(result.is_err());
339        let err_string = result.err().unwrap().to_string();
340        assert!(err_string.contains(error_message));
341    }
342}