[][src]Crate casperlabs_contract

A Rust library for writing smart contracts on the CasperLabs Platform.

no_std

By default, the library is no_std, however you can enable full std functionality by enabling the crate's std feature.

Example

The following example contains session code which persists an integer value under an unforgeable reference. It then stores the unforgeable reference under a name in context-local storage.

#![no_std]

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: i32) {
    // 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: i32 = 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);
}

Writing Smart Contracts

Support for writing smart contracts are contained in the contract_api module and its submodules.

Modules

args_parser

Home of ArgsParser, a trait used for parsing contract arguments from n-ary tuples.

contract_api

Contains support for writing smart contracts.

ext_ffi

Contains low-level bindings for host-side ("external") functions.

handlers

Contains definitions for panic and allocation error handlers, along with other no_std support code.

unwrap_or_revert

Home of UnwrapOrRevert, a convenience trait for unwrapping values.

Statics

ALLOC

An instance of WeeAlloc which allows contracts built as no_std to avoid having to provide a global allocator themselves.