Skip to content

Instantly share code, notes, and snippets.

@koxu1996
Last active July 8, 2024 05:46
Show Gist options
  • Save koxu1996/9a01db96c632b41268ed0138dd75d01f to your computer and use it in GitHub Desktop.
Save koxu1996/9a01db96c632b41268ed0138dd75d01f to your computer and use it in GitHub Desktop.
Casper's smart contract example - no bullshit, just stable Rust and no_std

Example of Minimal Smart Contract for Casper Blockchain

No bullshit, no unstable rust, no big-size WASM, no cargo casper.

Getting WASM

Compile to optimal-size WASM with:

cargo build --release --target wasm32-unknown-unknown
wasm-opt -Oz --strip-debug --signext-lowering ./target/wasm32-unknown-unknown/release/casper_example_contract.wasm -o ./casper_example_contract.wasm

Beware of std

It might be included implicitly - even though your code declares #[no_std] - so make sure your code is truly no_std compatible:

rustup target add thumbv7m-none-eabi
cargo build --target thumbv7m-none-eabi

For example, contract that calls Risc0 ZKVM for verification is 20KB only, but it must be done right 😉.

[package]
name = "casper-example-contract"
version = "0.1.0"
edition = "2021"
[lib]
crate-type = ["cdylib"]
[dependencies]
casper-contract = { version = "4.0.0", default-features = false }
casper-types = { version = "4.0.1", default-features = false }
casper-contract-no-std-helpers = { version = "0.1.0", "git" = "https://github.com/koxu1996/casper-contract-no-std-helpers"}
[profile.release]
lto = true
opt-level = 's'
// Content of ./src/lib.rs
#![no_std]
extern crate alloc;
use casper_contract::contract_api;
#[allow(unused)]
use casper_contract_no_std_helpers; // Injects global allocator and panic handler.
#[no_mangle]
pub extern "C" fn increment() {
let _foo = alloc::string::String::new(); // For dynamic sized types, you have to use `alloc`.
contract_api::runtime::get_caller();
//panic!("Fooo");
}
pub extern "C" fn call() {
// install contract here
}
[toolchain]
channel = "stable"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment