WebAssembly (WASM) Basics
Understanding WebAssembly for smart contract execution.
Advanced⏱️ 50 min📚 Prerequisites: 1
WebAssembly (WASM) Basics
WebAssembly is a binary instruction format that allows running code at near-native speed. Many blockchains use WASM for smart contract execution.
Why WASM for Smart Contracts?
- Performance: Near-native execution speed
- Security: Sandboxed execution environment
- Portability: Runs on any platform
- Language agnostic: Write in Rust, C++, etc.
- Deterministic: Same input = same output
WASM in Blockchain
- Polkadot/Substrate: Uses WASM for runtime
- NEAR Protocol: Smart contracts compile to WASM
- Ethereum 2.0: eWASM for contract execution
- Cosmos: Supports WASM contracts
Compiling Rust to WASM
BASH# Install wasm target rustup target add wasm32-unknown-unknown # Compile to WASM cargo build --target wasm32-unknown-unknown --release
WASM Contract Structure
RUST// Cargo.toml [package] name = "my_contract" version = "0.1.0" [lib] crate-type = ["cdylib"] [dependencies] # Contract framework dependencies
Contract Entry Points
RUST#[no_mangle] pub extern "C" fn init() { // Contract initialization } #[no_mangle] pub extern "C" fn call() { // Contract execution }
WASM Execution
- Deploy: Contract code uploaded to blockchain
- Instantiate: Create contract instance
- Execute: Call contract functions
- Query: Read contract state (no state change)
Advantages
- Fast: Compiled code runs efficiently
- Safe: Sandboxed, can't access host system
- Deterministic: Critical for consensus
- Multi-language: Not limited to one language
Code Examples
WASM Contract Structure
Basic WASM contract structure
RUST
struct ContractState {
owner: String,
value: u64,
}
#[no_mangle]
pub extern "C" fn init() -> u32 {
0
}
#[no_mangle]
pub extern "C" fn execute() -> u32 {
0
}
#[no_mangle]
pub extern "C" fn query() -> u64 {
100
}
fn main() {
println!("WASM contract structure");
println!("Init: {}", init());
println!("Execute: {}", execute());
println!("Query: {}", query());
}Explanation:
WASM contracts have entry points for initialization, execution, and querying. The #[no_mangle] attribute ensures function names are preserved for the WASM interface.
WASM Benefits
Why WASM is ideal for smart contracts
RUST
fn demonstrate_performance() {
println!("WASM executes at near-native speed");
}
fn demonstrate_security() {
println!("WASM runs in isolated sandbox");
println!("Cannot access host system directly");
}
fn demonstrate_determinism(input: u64) -> u64 {
input * 2
}
fn demonstrate_portability() {
println!("WASM runs on any platform");
println!("No platform-specific code needed");
}
fn main() {
demonstrate_performance();
demonstrate_security();
println!("Deterministic: {}", demonstrate_determinism(5));
demonstrate_portability();
}Explanation:
WASM's performance, security, determinism, and portability make it ideal for smart contract execution in blockchain environments.
Exercises
WASM Entry Point
Create a simple WASM contract entry point!
Starter Code:
RUST
fn main() {
}