WASM: Getting Started
Deploy ink! contracts
Deploy Rust contracts using ink! framework. Better performance for complex logic. Smaller bytecode than EVM.
Requirements
- Rust - Latest stable from rust-lang.org
- cargo-contract - ink! development tool
- Polkadot.js - For deployment and interaction
- Rust knowledge - Ownership, borrowing, types
Setup
# Install Rust
curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh
# Add WASM target
rustup target add wasm32-unknown-unknown
# Install cargo-contract
cargo install cargo-contract --force
Verify: cargo contract --version
Create Contract
cargo contract new my_contract
cd my_contract
Examine lib.rs:
#![cfg_attr(not(feature = "std"), no_std, no_main)]
#[ink::contract]
mod my_contract {
#[ink(storage)]
pub struct MyContract {
value: u32,
}
impl MyContract {
#[ink(constructor)]
pub fn new(init_value: u32) -> Self {
Self { value: init_value }
}
#[ink(message)]
pub fn get(&self) -> u32 {
self.value
}
#[ink(message)]
pub fn increment(&mut self) {
self.value += 1;
}
}
}
Build & Test
Build: cargo contract build
Test:
#[cfg(test)]
mod tests {
use super::*;
#[ink::test]
fn default_works() {
let contract = MyContract::new(0);
assert_eq!(contract.get(), 0);
}
#[ink::test]
fn increment_works() {
let mut contract = MyContract::new(0);
contract.increment();
assert_eq!(contract.get(), 1);
}
}
Run: cargo test
Deploy
Via Polkadot.js Portal:
- Visit https://portal.selendra.org
- Connect to Selendra
- Navigate to "Developer" → "Contracts"
- Click "Upload & deploy code"
- Select
.contractfile fromtarget/ink/ - Set constructor parameters
- Sign and submit
Interact
Use portal interface:
- Call
get()to read value - Call
increment()to modify - View transactions on explorer
Example: Counter
#![cfg_attr(not(feature = "std"), no_std, no_main)]
#[ink::contract]
mod counter {
#[ink(storage)]
pub struct Counter {
value: i32,
}
impl Counter {
#[ink(constructor)]
pub fn new(init_value: i32) -> Self {
Self { value: init_value }
}
#[ink(message)]
pub fn get(&self) -> i32 {
self.value
}
#[ink(message)]
pub fn increment(&mut self) {
self.value += 1;
}
#[ink(message)]
pub fn decrement(&mut self) {
self.value -= 1;
}
}
}
Common Issues
"wasm32-unknown-unknown not installed" - Run rustup target add wasm32-unknown-unknown
cargo-contract install fails - Update Rust: rustup update stable, install build tools: apt install build-essential
Build fails - Check Rust version, verify ink! dependencies
Deployment fails - Check account has SEL tokens, verify network connection
Differences from EVM
- Storage: Direct access, HashMap/Vec supported natively
- Type System: Rust's strong types, compile-time safety
- Gas Model: Weight-based, generally more efficient
- Tooling: cargo-contract and portal, less mature than EVM
When to Use WASM
- New projects: Modern language, better long-term
- Performance critical: Complex calculations
- Rust team: Leverage existing expertise
- Type safety valued: Rust prevents many bugs
Network
Portal: https://portal.selendra.org
RPC: wss://rpc.selendra.org
Chain ID: 1961
Next Steps
ink! Framework | Security Practices
Quick Reference: Build: cargo contract build | Test: cargo test | Deploy: Via Polkadot.js portal
Found an issue or want to contribute?
Help us improve this documentation by editing this page on GitHub.
