diff --git a/README.md b/README.md index 525ae69..9acdeec 100644 --- a/README.md +++ b/README.md @@ -90,6 +90,7 @@ casper-client put-deploy \ --payment-amount 5000000000000 \ --session-path [CONTRACT PATH]/contract.wasm \ --session-arg "message:string='hello world'" + --session-arg "key-name:string='my-custom_key-name'" ``` After the deploy is successful, you can view the new NamedKey `special_value` in the faucet account details. diff --git a/contract/src/main.rs b/contract/src/main.rs index f0ec1c6..44e1138 100644 --- a/contract/src/main.rs +++ b/contract/src/main.rs @@ -7,25 +7,30 @@ use alloc::string::String; use casper_contract::contract_api::{runtime, storage}; use casper_types::{Key, URef}; -const KEY_NAME: &str = "my-key-name"; -const RUNTIME_ARG_NAME: &str = "message"; +const RUNTIME_ARG_KEY_NAME: &str = "key-name"; +const RUNTIME_ARG_MESSAGE: &str = "message"; -fn store(value: String) { +fn store(value: String, key_name: String) { // Store `value` under a new unforgeable reference. let value_ref: URef = storage::new_uref(value); // Wrap the unforgeable reference in a value of type `Key`. let value_key: Key = value_ref.into(); - // Store this key under the name "my-key-name" in caller context - runtime::put_key(KEY_NAME, value_key); + // Remove the key if already exists in account entity context + runtime::remove_key(&key_name); + + // Store this key under the name "key-name" in caller context + runtime::put_key(&key_name, value_key); } // All session code must have a `call` entrypoint. #[no_mangle] pub extern "C" fn call() { - // This contract expects a single runtime argument to be provided. The arg is named "message" - // and will be of type `String`. - let value: String = runtime::get_named_arg(RUNTIME_ARG_NAME); - store(value); + // This contract expectstwo runtime arguments to be provided. + // The arg is named "message"and will be of type `String`. + let message: String = runtime::get_named_arg(RUNTIME_ARG_MESSAGE); + // The arg is named "key-name" and will be of type `String`. + let key_name: String = runtime::get_named_arg(RUNTIME_ARG_KEY_NAME); + store(message, key_name); } diff --git a/tests/src/integration_tests.rs b/tests/src/integration_tests.rs index 8ed8e9a..36e5138 100644 --- a/tests/src/integration_tests.rs +++ b/tests/src/integration_tests.rs @@ -14,10 +14,10 @@ mod tests { use casper_types::{runtime_args, ApiError, GenesisAccount, Key, Motes, RuntimeArgs, U512}; - // Define `KEY_NAME` constant to match that in the contract. - const KEY_NAME: &str = "my-key-name"; - const VALUE: &str = "hello world"; - const RUNTIME_ARG_NAME: &str = "message"; + const RUNTIME_ARG_KEY_NAME: &str = "key-name"; + const KEY_NAME_VALUE: &str = "my-custom_key-name"; + const RUNTIME_ARG_MESSAGE: &str = "message"; + const MESSAGE_VALUE: &str = "hello world"; const CONTRACT_WASM: &str = "contract.wasm"; #[test] @@ -36,7 +36,8 @@ mod tests { // absolute paths. let session_code = PathBuf::from(CONTRACT_WASM); let session_args = runtime_args! { - RUNTIME_ARG_NAME => VALUE, + RUNTIME_ARG_MESSAGE => MESSAGE_VALUE, + RUNTIME_ARG_KEY_NAME => KEY_NAME_VALUE }; let deploy_item = DeployItemBuilder::new() @@ -54,7 +55,7 @@ mod tests { let result_of_query = builder.query( None, Key::Account(*DEFAULT_ACCOUNT_ADDR), - &[KEY_NAME.to_string()], + &[KEY_NAME_VALUE.to_string()], ); assert!(result_of_query.is_err()); @@ -66,7 +67,7 @@ mod tests { .query( None, Key::Account(*DEFAULT_ACCOUNT_ADDR), - &[KEY_NAME.to_string()], + &[KEY_NAME_VALUE.to_string()], ) .expect("should be stored value.") .as_cl_value() @@ -75,7 +76,7 @@ mod tests { .into_t::() .expect("should be string."); - assert_eq!(result_of_query, VALUE); + assert_eq!(result_of_query, MESSAGE_VALUE); } #[test]