Skip to content

Commit

Permalink
Add Key name as runtime Argument (#23)
Browse files Browse the repository at this point in the history
* Update crates and test sto last state of art

* Path issue on CI/CD

* Add wabt

* nightly tests

* Update tests to last cargo-casper dev version

* // small change consistency KEY_NAME

* Test ci cd

* Trigger CI/CD

* stash change

* Proposition of change to add custome key name

* Add new arg in doc
  • Loading branch information
gRoussac authored Jun 24, 2024
1 parent 45435a6 commit 5322309
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 17 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
23 changes: 14 additions & 9 deletions contract/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
17 changes: 9 additions & 8 deletions tests/src/integration_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand All @@ -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()
Expand All @@ -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());

Expand All @@ -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()
Expand All @@ -75,7 +76,7 @@ mod tests {
.into_t::<String>()
.expect("should be string.");

assert_eq!(result_of_query, VALUE);
assert_eq!(result_of_query, MESSAGE_VALUE);
}

#[test]
Expand Down

0 comments on commit 5322309

Please sign in to comment.