casper_rust_wasm_sdk/sdk/rpcs/
put_deploy.rs

1#[cfg(target_arch = "wasm32")]
2use crate::deploy::deploy::PutDeployResult;
3use crate::types::deploy::Deploy;
4use crate::{types::verbosity::Verbosity, SDK};
5#[allow(deprecated)]
6use casper_client::{
7    put_deploy, rpcs::results::PutDeployResult as _PutDeployResult, Error, JsonRpcId,
8    SuccessResponse,
9};
10use rand::Rng;
11#[cfg(target_arch = "wasm32")]
12use wasm_bindgen::prelude::*;
13
14/// SDK methods for putting a deploy.
15#[cfg(target_arch = "wasm32")]
16#[wasm_bindgen]
17impl SDK {
18    /// Puts a deploy using the provided options.
19    ///
20    /// # Arguments
21    ///
22    /// * `deploy` - The `Deploy` object to be sent.
23    /// * `verbosity` - An optional `Verbosity` level for controlling the output verbosity.
24    /// * `rpc_address` - An optional string specifying the rpc address to use for the request.
25    ///
26    /// # Returns
27    ///
28    /// A `Result` containing either a `PutDeployResult` or a `JsError` in case of an error.
29    ///
30    /// # Errors
31    ///
32    /// Returns a `JsError` if there is an error during the deploy process.
33    #[wasm_bindgen(js_name = "put_deploy")]
34    #[allow(deprecated)]
35    pub async fn put_deploy_js_alias(
36        &self,
37        deploy: Deploy,
38        verbosity: Option<Verbosity>,
39        rpc_address: Option<String>,
40    ) -> Result<PutDeployResult, JsError> {
41        let result = self.put_deploy(deploy, verbosity, rpc_address).await;
42        match result {
43            Ok(data) => Ok(data.result.into()),
44            Err(err) => {
45                let err = &format!("Error occurred with {err:?}");
46                Err(JsError::new(err))
47            }
48        }
49    }
50
51    /// JavaScript Alias for `put_deploy`.
52    #[deprecated(note = "This function is an alias. Please use `put_deploy` instead.")]
53    #[allow(deprecated)]
54    pub async fn account_put_deploy(
55        &self,
56        deploy: Deploy,
57        verbosity: Option<Verbosity>,
58        rpc_address: Option<String>,
59    ) -> Result<PutDeployResult, JsError> {
60        self.put_deploy_js_alias(deploy, verbosity, rpc_address)
61            .await
62    }
63}
64
65impl SDK {
66    /// Puts a deploy based on the provided options.
67    ///
68    /// # Arguments
69    ///
70    /// * `deploy` - The `Deploy` object to be sent.
71    /// * `verbosity` - An optional `Verbosity` level for controlling the output verbosity.
72    /// * `rpc_address` - An optional string specifying the rpc address to use for the request.
73    ///
74    /// # Returns
75    ///
76    /// A `Result` containing either a `_PutDeployResult` or an `Error` in case of an error.
77    ///
78    /// # Errors
79    ///
80    /// Returns an `Error` if there is an error during the deploy process.
81    #[allow(deprecated)]
82    pub async fn put_deploy(
83        &self,
84        deploy: Deploy,
85        verbosity: Option<Verbosity>,
86        rpc_address: Option<String>,
87    ) -> Result<SuccessResponse<_PutDeployResult>, Error> {
88        //log("account_put_deploy!");
89        let random_id = JsonRpcId::from(rand::thread_rng().gen::<u64>().to_string());
90        put_deploy(
91            random_id,
92            &self.get_rpc_address(rpc_address),
93            self.get_verbosity(verbosity).into(),
94            deploy.into(),
95        )
96        .await
97    }
98}
99
100#[cfg(test)]
101#[allow(deprecated)]
102mod tests {
103    use super::*;
104    use crate::{
105        helpers::public_key_from_secret_key,
106        types::deploy_params::{
107            deploy_str_params::DeployStrParams, payment_str_params::PaymentStrParams,
108        },
109    };
110    use sdk_tests::{
111        config::{PAYMENT_TRANSFER_AMOUNT, TRANSFER_AMOUNT},
112        tests::helpers::{get_network_constants, get_user_secret_key},
113    };
114
115    fn get_deploy() -> Deploy {
116        let secret_key = get_user_secret_key(None).unwrap();
117        let account = public_key_from_secret_key(&secret_key).unwrap();
118        let (_, _, _, _, chain_name) = get_network_constants();
119        let deploy_params =
120            DeployStrParams::new(&chain_name, &account, Some(secret_key), None, None, None);
121        let payment_params = PaymentStrParams::default();
122        payment_params.set_payment_amount(PAYMENT_TRANSFER_AMOUNT);
123
124        Deploy::with_transfer(
125            TRANSFER_AMOUNT,
126            &account, // self transfer
127            None,
128            deploy_params,
129            payment_params,
130        )
131        .unwrap()
132    }
133
134    #[tokio::test]
135    async fn test_put_deploy_with_none_values() {
136        // Arrange
137        let sdk = SDK::new(None, None, None);
138        let error_message = "failed to parse node address as valid URL";
139        let deploy = get_deploy();
140
141        // Act
142        let result = sdk.put_deploy(deploy, None, None).await;
143
144        // Assert
145        assert!(result.is_err());
146        let err_string = result.err().unwrap().to_string();
147        assert!(err_string.contains(error_message));
148    }
149
150    #[tokio::test]
151    async fn test_put_deploy() {
152        // Arrange
153        let sdk = SDK::new(None, None, None);
154        let verbosity = Some(Verbosity::High);
155        let (rpc_address, _, _, _, _) = get_network_constants();
156        let deploy = get_deploy();
157
158        // Act
159        let result = sdk.put_deploy(deploy, verbosity, Some(rpc_address)).await;
160
161        // Assert
162        assert!(result.is_ok());
163    }
164
165    #[tokio::test]
166    async fn test_put_deploy_with_error() {
167        // Arrange
168        let sdk = SDK::new(Some("http://localhost".to_string()), None, None);
169        let error_message = "error sending request";
170        let deploy = get_deploy();
171
172        // Act
173        let result = sdk.put_deploy(deploy, None, None).await;
174
175        // Assert
176        assert!(result.is_err());
177        let err_string = result.err().unwrap().to_string();
178        assert!(err_string.contains(error_message));
179    }
180}