Basic ink! Contract
Project Structure
Each ink! contract requires its own crate with two essential files:
Cargo.toml
: Project configuration and dependencieslib.rs
: Contract implementation
💡 Tip You can use Swanky Suite to quickly bootstrap a new project. Check out the Swanky CLI guide for details.
Contract Configuration
Cargo.toml Setup
Your Cargo.toml
should include the following sections:
Contract Implementation
Minimum Requirements
Every ink! contract must include:
⚠️ Required Elements
no_std
attribute for non-standard library compilationContract module marked with
#[ink::contract]
Storage struct with
#[ink(storage)]
At least one constructor with
#[ink(constructor)]
At least one message with
#[ink(message)]
Basic Contract Template
Example Contracts
Flipper Contract
The flipper contract is the simplest example provided by the ink! team, perfect for understanding basic contract structure.
Best Practices
Project Organization
Keep one contract per crate
Use meaningful names for contract modules
Organize tests in a separate module
Code Structure
Group related functionality together
Document your code with comments
Follow Rust naming conventions
Testing
Include unit tests
Add integration tests where needed
Use ink!'s testing utilities
Last updated