-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
1 changed file
with
38 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
#![cfg_attr( | ||
not(target_arch = "wasm32"), | ||
crate_type = "target arch should be wasm32" | ||
)] | ||
#![no_main] | ||
|
||
use casperlabs_contract::{ | ||
contract_api::{runtime, storage}, | ||
unwrap_or_revert::UnwrapOrRevert, | ||
}; | ||
use casperlabs_types::{ApiError, Key, URef}; | ||
|
||
const KEY: &str = "special_value"; | ||
|
||
fn store(value: 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 "special_value" in context-local storage. | ||
runtime::put_key(KEY, value_key); | ||
} | ||
|
||
// All session code must have a `call` entrypoint. | ||
#[no_mangle] | ||
pub extern "C" fn call() { | ||
// Get the optional first argument supplied to the argument. | ||
let value: String = runtime::get_arg(0) | ||
// Unwrap the `Option`, returning an error if there was no argument supplied. | ||
.unwrap_or_revert_with(ApiError::MissingArgument) | ||
// Unwrap the `Result` containing the deserialized argument or return an error if there was | ||
// a deserialization error. | ||
.unwrap_or_revert_with(ApiError::InvalidArgument); | ||
|
||
store(value); | ||
} |