#ffi #contract #smart

nightly casperlabs-contract-ffi

Library for developing CasperLabs smart contracts

18 breaking releases

✓ Uses Rust 2018 edition

0.22.0 Jan 14, 2020
0.21.0 Dec 17, 2019
0.20.0 Nov 7, 2019
0.10.0 Jul 12, 2019
0.3.0 Mar 29, 2019

#277 in Cryptocurrencies

Download history 119/week @ 2020-01-11 66/week @ 2020-01-18 1/week @ 2020-01-25 4/week @ 2020-02-01 25/week @ 2020-02-08 39/week @ 2020-02-15 127/week @ 2020-02-22 42/week @ 2020-03-07 21/week @ 2020-03-14 105/week @ 2020-03-21 4/week @ 2020-03-28 22/week @ 2020-04-04 136/week @ 2020-04-11 2/week @ 2020-04-18 2/week @ 2020-04-25

181 downloads per month

Apache-2.0 and MPL-2.0 licenses

210KB
5K SLoC


lib.rs:

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

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.

use casperlabs_contract_ffi::contract_api::{storage, runtime, Error, TURef};
use casperlabs_contract_ffi::key::Key;
use casperlabs_contract_ffi::unwrap_or_revert::UnwrapOrRevert;
use casperlabs_contract_ffi::uref::URef;

const KEY: &str = "special_value";

fn store(value: i32) {
    // Store `value` under a new unforgeable reference.
    let value_ref: TURef<i32> = storage::new_turef(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(Error::MissingArgument)
        // Unwrap the `Result` containing the deserialized argument or return an error
        // if there was a deserialization error.
        .unwrap_or_revert_with(Error::InvalidArgument);

    store(value);
}

Writing Smart Contracts

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

Dependencies

~2–2.6MB
~50K SLoC