Skip to main content

casper_rust_wasm_sdk/sdk/rpcs/
put_deploy.rs

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