casper_rust_wasm_sdk/sdk/contract/
install.rs#[cfg(target_arch = "wasm32")]
use crate::deploy::deploy::PutDeployResult;
use crate::types::deploy_params::{
deploy_str_params::{deploy_str_params_to_casper_client, DeployStrParams},
payment_str_params::{payment_str_params_to_casper_client, PaymentStrParams},
session_str_params::{session_str_params_to_casper_client, SessionStrParams},
};
use crate::{types::sdk_error::SdkError, SDK};
use casper_client::{
cli::make_deploy, rpcs::results::PutDeployResult as _PutDeployResult, SuccessResponse,
};
#[cfg(target_arch = "wasm32")]
use wasm_bindgen::prelude::*;
#[cfg(target_arch = "wasm32")]
#[wasm_bindgen]
impl SDK {
#[wasm_bindgen(js_name = "install")]
pub async fn install_js_alias(
&self,
deploy_params: DeployStrParams,
session_params: SessionStrParams,
payment_amount: &str,
node_address: Option<String>,
) -> Result<PutDeployResult, JsError> {
let result = self
.install(deploy_params, session_params, payment_amount, node_address)
.await;
match result {
Ok(data) => Ok(data.result.into()),
Err(err) => {
let err = &format!("Error occurred with {:?}", err);
Err(JsError::new(err))
}
}
}
}
impl SDK {
pub async fn install(
&self,
deploy_params: DeployStrParams,
session_params: SessionStrParams,
payment_amount: &str,
node_address: Option<String>,
) -> Result<SuccessResponse<_PutDeployResult>, Box<SdkError>> {
let payment_params = PaymentStrParams::default();
payment_params.set_payment_amount(payment_amount);
let deploy = make_deploy(
"",
deploy_str_params_to_casper_client(&deploy_params),
session_str_params_to_casper_client(&session_params),
payment_str_params_to_casper_client(&payment_params),
false,
)
.map_err(|err| Box::new(SdkError::from(err)))?;
self.put_deploy(deploy.into(), None, node_address)
.await
.map_err(|err| Box::new(SdkError::from(err)))
}
}
#[cfg(test)]
mod tests {
use sdk_tests::{
config::{ARGS_SIMPLE, HELLO_CONTRACT, PAYMENT_AMOUNT, TTL, WASM_PATH},
tests::helpers::{get_network_constants, get_user_secret_key, read_wasm_file},
};
use crate::helpers::public_key_from_secret_key;
use super::*;
#[tokio::test]
async fn test_install_with_none_values() {
let sdk = SDK::new(None, None);
let deploy_params = DeployStrParams::new("", "", None, None, None);
let session_params = SessionStrParams::default();
let error_message =
"Invalid argument 'is_session_transfer': requires --session-arg to be present";
let result = sdk.install(deploy_params, session_params, "", None).await;
assert!(result.is_err());
let err_string = result.err().unwrap().to_string();
assert!(err_string.contains(error_message));
}
#[tokio::test]
async fn test_install_with_valid_input() {
let sdk = SDK::new(None, None);
let (node_address, _, chain_name) = get_network_constants();
let secret_key = get_user_secret_key(None).unwrap();
let account = public_key_from_secret_key(&secret_key).unwrap();
let deploy_params =
DeployStrParams::new(&chain_name, &account, Some(secret_key), None, None);
let mut session_params = SessionStrParams::default();
let module_bytes = match read_wasm_file(&format!("{WASM_PATH}{HELLO_CONTRACT}")) {
Ok(module_bytes) => module_bytes,
Err(err) => {
eprintln!("Error reading file: {:?}", err);
return;
}
};
session_params.set_session_bytes(module_bytes.into());
let args_simple: Vec<String> = ARGS_SIMPLE.iter().map(|s| s.to_string()).collect();
session_params.set_session_args(args_simple);
let result = sdk
.install(
deploy_params,
session_params,
PAYMENT_AMOUNT,
Some(node_address),
)
.await;
assert!(result.is_ok());
let deploy_hash = result.unwrap().result.deploy_hash;
assert!(!deploy_hash.to_string().is_empty());
}
#[tokio::test]
async fn test_install_with_invalid_input() {
let sdk = SDK::new(None, None);
let (node_address, _, chain_name) = get_network_constants();
let secret_key = get_user_secret_key(None).unwrap();
let account = public_key_from_secret_key(&secret_key).unwrap();
let error_message =
"Missing a required arg - exactly one of the following must be provided";
let deploy_params = DeployStrParams::new(
&chain_name,
&account,
Some(secret_key.clone()),
None,
Some(TTL.to_string()),
);
let mut session_params = SessionStrParams::default();
let module_bytes = match read_wasm_file(&format!("{WASM_PATH}{HELLO_CONTRACT}")) {
Ok(module_bytes) => module_bytes,
Err(err) => {
eprintln!("Error reading file: {:?}", err);
return;
}
};
session_params.set_session_bytes(module_bytes.into());
let args_simple: Vec<String> = ARGS_SIMPLE.iter().map(|s| s.to_string()).collect();
session_params.set_session_args(args_simple);
let result = sdk
.install(deploy_params, session_params, "", Some(node_address))
.await;
assert!(result.is_err());
let err_string = result.err().unwrap().to_string();
assert!(err_string.contains(error_message));
}
#[tokio::test]
async fn test_install_without_secret_key() {
let sdk = SDK::new(None, None);
let (node_address, _, chain_name) = get_network_constants();
let secret_key = get_user_secret_key(None).unwrap();
let account = public_key_from_secret_key(&secret_key).unwrap();
let error_message = "account authorization invalid at state root hash";
let deploy_params =
DeployStrParams::new(&chain_name, &account, None, None, Some(TTL.to_string()));
let mut session_params = SessionStrParams::default();
let module_bytes = match read_wasm_file(&format!("{WASM_PATH}{HELLO_CONTRACT}")) {
Ok(module_bytes) => module_bytes,
Err(err) => {
eprintln!("Error reading file: {:?}", err);
return;
}
};
session_params.set_session_bytes(module_bytes.into());
let args_simple: Vec<String> = ARGS_SIMPLE.iter().map(|s| s.to_string()).collect();
session_params.set_session_args(args_simple);
let result = sdk
.install(
deploy_params,
session_params,
PAYMENT_AMOUNT,
Some(node_address),
)
.await;
assert!(result.is_err());
let err_string = result.err().unwrap().to_string();
assert!(err_string.contains(error_message));
}
#[tokio::test]
async fn test_install_with_error() {
let sdk = SDK::new(Some("http://localhost".to_string()), None);
let (_, _, chain_name) = get_network_constants();
let secret_key = get_user_secret_key(None).unwrap();
let account = public_key_from_secret_key(&secret_key).unwrap();
let deploy_params =
DeployStrParams::new(&chain_name, &account, Some(secret_key.clone()), None, None);
let error_message = "error sending request for url (http://localhost/rpc)";
let mut session_params = SessionStrParams::default();
let module_bytes = match read_wasm_file(&format!("{WASM_PATH}{HELLO_CONTRACT}")) {
Ok(module_bytes) => module_bytes,
Err(err) => {
eprintln!("Error reading file: {:?}", err);
return;
}
};
session_params.set_session_bytes(module_bytes.into());
let args_simple: Vec<String> = ARGS_SIMPLE.iter().map(|s| s.to_string()).collect();
session_params.set_session_args(args_simple);
let result = sdk
.install(deploy_params, session_params, PAYMENT_AMOUNT, None)
.await;
assert!(result.is_err());
let err_string = result.err().unwrap().to_string();
assert!(err_string.contains(error_message));
}
}