casper_contract/lib.rs
1//! A Rust library for writing smart contracts on the
2//! [Casper Platform](https://docs.casper.network/dapp-dev-guide).
3//!
4//! # `no_std`
5//!
6//! The library is `no_std`, but uses the `core` and `alloc` crates.
7//!
8//! # Example
9//!
10//! The following example contains session code which persists an integer value under an unforgeable
11//! reference. It then stores the unforgeable reference under a name in context-local storage.
12//!
13//! # Writing Smart Contracts
14//!
15//! ```no_run
16//! #![no_std]
17//! #![no_main]
18//!
19//! use casper_contract::contract_api::{runtime, storage};
20//! use casper_types::{Key, URef};
21//!
22//! const KEY: &str = "special_value";
23//! const ARG_VALUE: &str = "value";
24//!
25//! fn store(value: i32) {
26//! // Store `value` under a new unforgeable reference.
27//! let value_ref: URef = storage::new_uref(value);
28//!
29//! // Wrap the unforgeable reference in a value of type `Key`.
30//! let value_key: Key = value_ref.into();
31//!
32//! // Store this key under the name "special_value" in context-local storage.
33//! runtime::put_key(KEY, value_key);
34//! }
35//!
36//! // All session code must have a `call` entrypoint.
37//! #[no_mangle]
38//! pub extern "C" fn call() {
39//! // Get the optional first argument supplied to the argument.
40//! let value: i32 = runtime::get_named_arg(ARG_VALUE);
41//! store(value);
42//! }
43//! ```
44//!
45//! Support for writing smart contracts are contained in the [`contract_api`] module and its
46//! submodules.
47
48#![cfg_attr(not(test), no_std)]
49#![cfg_attr(all(not(test), feature = "no-std-helpers"), allow(internal_features))]
50#![cfg_attr(
51 all(not(test), feature = "no-std-helpers"),
52 feature(alloc_error_handler, core_intrinsics, lang_items)
53)]
54#![doc(html_root_url = "https://docs.rs/casper-contract/5.1.1")]
55#![doc(
56 html_favicon_url = "https://raw.githubusercontent.com/casper-network/casper-node/blob/dev/images/Casper_Logo_Favicon_48.png",
57 html_logo_url = "https://raw.githubusercontent.com/casper-network/casper-node/blob/dev/images/Casper_Logo_Favicon.png"
58)]
59#![warn(missing_docs)]
60
61extern crate alloc;
62
63pub mod contract_api;
64pub mod ext_ffi;
65#[cfg(all(not(test), feature = "no-std-helpers", not(feature = "std")))]
66mod no_std_handlers;
67pub mod unwrap_or_revert;
68
69/// An instance of [`WeeAlloc`](https://docs.rs/wee_alloc) which allows contracts built as `no_std`
70/// to avoid having to provide a global allocator themselves.
71#[cfg(all(not(test), feature = "no-std-helpers"))]
72#[global_allocator]
73pub static ALLOC: wee_alloc::WeeAlloc = wee_alloc::WeeAlloc::INIT;