LogoLogo
WebsiteSubstrate Block ExplorerEVM Block ExplorerFaucet
  • Learn
    • Architecture
    • Staking
      • Proof of stake
    • Smart Contracts
    • Accounts
    • Infrastructure
      • Nodes
      • Agents
      • Models
      • IPFS
    • Security
      • OPOC
      • TSS
      • IPFS Integrity
      • Model Updates Integrity
    • Fees
    • Finney Testnet RPC Endpoints
  • Build
    • Address format
    • ink! Environment
    • Wasm Smart Contracts
      • Smart Contract Stack
      • Domain-Specific Languages (DSLs)
      • ink! Development
      • ask! Development
      • Basic ink! Contract
    • EVM Smart Contracts
      • Introduction to EVM Smart Contracts
      • HardHat
      • Your first EVM Smart Contract
      • Debug EVM Transactions
      • Precompiles
        • SR25519
        • Substrate ECDSA
        • XC20
    • Run a node
      • Run an archive node
        • Binary
      • Run a full node
      • Become a validator
        • Learn about Validators
        • Validator requirements
        • Spin up a validator
        • Set your identity
    • Build an Agent
      • Introduction
      • Development
      • Installing WASP
      • Agents API Reference
      • Available AI Models
Powered by GitBook
On this page
  • Project Structure
  • Contract Configuration
  • Contract Implementation
  • Example Contracts
  • Best Practices
Export as PDF
  1. Build
  2. Wasm Smart Contracts

Basic ink! Contract

Project Structure

Each ink! contract requires its own crate with two essential files:

  • Cargo.toml: Project configuration and dependencies

  • lib.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:

[package]
name = "my_contract"
version = "0.1.0"
authors = ["Your Name <[email protected]>"]
edition = "2021"

[dependencies]
ink = { version = "4.3", default-features = false}
ink_metadata = { version = "4.3", features = ["derive"], optional = true }
scale = { package = "parity-scale-codec", version = "3", default-features = false, features = ["derive"] }
scale-info = { version = "2.5", default-features = false, features = ["derive"], optional = true }

[dev-dependencies]
ink_e2e = { version = "4.3" }

[lib]
path = "lib.rs"

[features]
default = ["std"]
std = [
    "ink/std",
    "scale/std",
    "scale-info/std"
]
ink-as-dependency = []
e2e-tests = []

Contract Implementation

Minimum Requirements

Every ink! contract must include:

⚠️ Required Elements

  1. no_std attribute for non-standard library compilation

  2. Contract module marked with #[ink::contract]

  3. Storage struct with #[ink(storage)]

  4. At least one constructor with #[ink(constructor)]

  5. At least one message with #[ink(message)]

Basic Contract Template

#![cfg_attr(not(feature = "std"), no_std)]

#[ink::contract]
mod my_contract {
    /// Contract storage
    #[ink(storage)]
    pub struct MyContract {}

    impl MyContract {
        /// Contract constructor
        #[ink(constructor)]
        pub fn new() -> Self {
            Self {}
        }

        /// Contract message
        #[ink(message)]
        pub fn do_something(&self) {
            ()
        }
    }
}

Example Contracts

Flipper Contract

Best Practices

  1. Project Organization

    • Keep one contract per crate

    • Use meaningful names for contract modules

    • Organize tests in a separate module

  2. Code Structure

    • Group related functionality together

    • Document your code with comments

    • Follow Rust naming conventions

  3. Testing

    • Include unit tests

    • Add integration tests where needed

    • Use ink!'s testing utilities

Previousask! DevelopmentNextEVM Smart Contracts

Last updated 3 months ago

The is the simplest example provided by the ink! team, perfect for understanding basic contract structure.

flipper contract