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        put_deploy(
90            JsonRpcId::from(rand::thread_rng().gen::<u64>().to_string()),
91            &self.get_rpc_address(rpc_address),
92            self.get_verbosity(verbosity).into(),
93            deploy.into(),
94        )
95        .await
96    }
97}
98
99#[cfg(test)]
100#[allow(deprecated)]
101mod tests {
102    use super::*;
103    use crate::{
104        helpers::public_key_from_secret_key,
105        types::deploy_params::{
106            deploy_str_params::DeployStrParams, payment_str_params::PaymentStrParams,
107        },
108    };
109    use sdk_tests::{
110        config::{PAYMENT_TRANSFER_AMOUNT, TRANSFER_AMOUNT},
111        tests::helpers::{get_network_constants, get_user_secret_key},
112    };
113
114    fn get_deploy() -> Deploy {
115        let secret_key = get_user_secret_key(None).unwrap();
116        let account = public_key_from_secret_key(&secret_key).unwrap();
117        let (_, _, _, _, chain_name) = get_network_constants();
118        let deploy_params =
119            DeployStrParams::new(&chain_name, &account, Some(secret_key), None, None, None);
120        let payment_params = PaymentStrParams::default();
121        payment_params.set_payment_amount(PAYMENT_TRANSFER_AMOUNT);
122
123        Deploy::with_transfer(
124            TRANSFER_AMOUNT,
125            &account, // self transfer
126            None,
127            deploy_params,
128            payment_params,
129        )
130        .unwrap()
131    }
132
133    #[tokio::test]
134    async fn test_put_deploy_with_none_values() {
135        // Arrange
136        let sdk = SDK::new(None, None, None);
137        let error_message = "failed to parse node address as valid URL";
138        let deploy = get_deploy();
139
140        // Act
141        let result = sdk.put_deploy(deploy, None, None).await;
142
143        // Assert
144        assert!(result.is_err());
145        let err_string = result.err().unwrap().to_string();
146        assert!(err_string.contains(error_message));
147    }
148
149    #[tokio::test]
150    async fn test_put_deploy() {
151        // Arrange
152        let sdk = SDK::new(None, None, None);
153        let verbosity = Some(Verbosity::High);
154        let (rpc_address, _, _, _, _) = get_network_constants();
155        let deploy = get_deploy();
156
157        // Act
158        let result = sdk.put_deploy(deploy, verbosity, Some(rpc_address)).await;
159
160        // Assert
161        assert!(result.is_ok());
162    }
163
164    #[tokio::test]
165    async fn test_put_deploy_with_error() {
166        // Arrange
167        let sdk = SDK::new(Some("http://localhost".to_string()), None, None);
168        let error_message = "error sending request for url (http://localhost/rpc)";
169        let deploy = get_deploy();
170
171        // Act
172        let result = sdk.put_deploy(deploy, None, None).await;
173
174        // Assert
175        assert!(result.is_err());
176        let err_string = result.err().unwrap().to_string();
177        assert!(err_string.contains(error_message));
178    }
179}