Ink! Environment Setup
Configure your development environment for building WebAssembly smart contracts using the ink! framework on Mandala Testnet.
Overview
ink! is a domain-specific language (DSL) for writing smart contracts in Rust that compile to WebAssembly (WASM). It provides a familiar programming model while leveraging Rust's safety and performance characteristics.
Prerequisites
Before setting up ink!, ensure you have:
- Rust toolchain installed (covered in Zombienet Local Setup)
- Polkadot.js account created (Create Polkadot.js Account)
- Connection to Mandala (RPC Endpoints)
Installing ink! CLI
Install cargo-contract
The cargo-contract tool is essential for ink! development:
# Install the latest version
cargo install --force --locked cargo-contract
# Verify installation
cargo contract --version
Expected output:
cargo-contract-contract 5.0.3-unknown-aarch64-apple-darwin
Install Required Components
ink! requires specific Rust components:
# Add WebAssembly target (if not already added)
rustup target add wasm32-unknown-unknown
# Install required components
rustup component add rust-src
Project Structure
Creating Your First Project
Generate a new ink! project:
# Create a new project directory
mkdir ink-projects && cd ink-projects
# Generate flipper contract (basic template)
cargo contract new flipper
# Navigate to project
cd flipper
Project structure:
flipper/
├── Cargo.toml # Project configuration
├── lib.rs # Main contract code
└── .gitignore # Git ignore file
Understanding Cargo.toml
The Cargo.toml
for ink! projects includes specific configurations:
[package]
name = "flipper"
version = "0.1.0"
authors = ["[your_name] <[your_email]>"]
edition = "2021"
[dependencies]
ink = { version = "5.1.1", default-features = false }
[dev-dependencies]
ink_e2e = { version = "5.1.1" }
[lib]
path = "lib.rs"
[features]
default = ["std"]
std = [
"ink/std",
]
ink-as-dependency = []
e2e-tests = []
Building and Testing
Building Contracts
Use cargo-contract to build your ink! contracts:
# Build in debug mode
cargo contract build
# Build optimized release version
cargo contract build --release
Build output location:
target/ink/
├── flipper.contract # Contract bundle
├── flipper.wasm # WebAssembly binary
└── flipper.json # Contract metadata
Understanding Build Artifacts
Generated files:
flipper.wasm:
- Compiled WebAssembly binary
- Deployed to the blockchain
- Contains contract logic
flipper.json:
- Contract metadata and ABI
- Used by frontend applications
- Contains function signatures and types
flipper.contract:
- Bundle containing both WASM and metadata
- Used for deployment via Polkadot.js Apps
Running Tests
ink! supports unit testing within contracts:
# Run all tests
cargo test
# Run tests with output
cargo test -- --nocapture
# Run specific test
cargo test test_function_name
Example test structure:
#[cfg(test)]
mod tests {
use super::*;
#[ink::test]
fn default_works() {
let flipper = Flipper::default();
assert_eq!(flipper.get(), false);
}
#[ink::test]
fn it_works() {
let mut flipper = Flipper::new(false);
assert_eq!(flipper.get(), false);
flipper.flip();
assert_eq!(flipper.get(), true);
}
}
Next Steps
With your ink! environment configured:
- Build your first Flipper contract - Learn ink! basics
- Deploy to Testnet - Test on live network
For comprehensive ink! documentation, visit ink! docs.