Ink! Owner-Only Message - Ink Typing CST Test
Loading…
Ink! Owner-Only Message — Ink Code
A contract where only the owner can update a stored message.
#[ink::contract]
mod owner_message {
#[ink(storage)]
pub struct OwnerMessage {
owner: AccountId,
message: ink::prelude::string::String,
}
impl OwnerMessage {
#[ink(constructor)]
pub fn new() -> Self {
Self {
owner: Self::env().caller(),
message: String::from(""),
}
}
#[ink(message)]
pub fn set_message(&mut self, msg: String) {
assert!(self.env().caller() == self.owner);
self.message = msg;
}
}
}Ink Language Guide
Ink! is a Rust-based eDSL (embedded domain-specific language) for writing smart contracts on the Substrate blockchain framework. It emphasizes safety, efficiency, and tight integration with Polkadot and Substrate ecosystems.
Primary Use Cases
- ▸High-security smart contracts
- ▸DeFi protocols on Substrate
- ▸NFT minting and marketplaces
- ▸On-chain governance modules
- ▸Wasm-based blockchain apps
Notable Features
- ▸Rust-based syntax
- ▸Compile-time type safety
- ▸Wasm target for Substrate
- ▸Macros for contract boilerplate
- ▸Support for storage, events, and messages
Origin & Creator
Ink! was created by Parity Technologies, the team behind Substrate and Polkadot, to provide a safe Rust-based smart contract language for WebAssembly chains. Initial development started around 2018.
Industrial Note
Ink! is primarily used in Substrate-based chains and Polkadot parachains, for high-assurance contracts in DeFi, on-chain governance, NFT minting, and other blockchain-native applications.
Quick Explain
- ▸Ink! uses Rust syntax with domain-specific macros for blockchain smart contracts.
- ▸It provides compile-time safety and strong typing, reducing runtime errors.
- ▸Contracts compile to WebAssembly (Wasm) to run on Substrate-based chains.
- ▸Designed for formal verification and predictable execution.
- ▸Used in DeFi, NFTs, and governance applications within Polkadot and Kusama ecosystems.
Core Features
- ▸#[ink(storage)] for state
- ▸#[ink(event)] for events
- ▸#[ink(message)] for callable functions
- ▸Cross-contract calls
- ▸Metadata generation for contracts
Learning Path
- ▸Learn Rust basics
- ▸Understand Substrate architecture
- ▸Write simple ink! contracts
- ▸Compile and deploy Wasm
- ▸Test, audit, and deploy production contracts
Practical Examples
- ▸Simple key-value store
- ▸ERC-20-like token
- ▸NFT minting and transfer
- ▸Voting and governance module
- ▸Cross-contract calls for DeFi pools
Comparisons
- ▸Ink! vs Solidity: Ink! is Rust-based and Wasm-native; Solidity is EVM-native.
- ▸Ink! vs Vyper: Ink! runs on Substrate/Wasm; Vyper runs on EVM.
- ▸Ink! vs Solidity + EVM: Ink! has strong Rust type safety; Solidity has larger ecosystem.
- ▸Ink! vs Move: Move is resource-based, Ink! is Rust/Wasm based.
- ▸Ink! vs Cairo: Cairo targets STARKs; Ink! targets Substrate/Wasm chains.
Strengths
- ▸Safe, type-checked contracts
- ▸High performance via Wasm
- ▸Strong ecosystem support in Polkadot
- ▸Auditable Rust code
- ▸Predictable gas and storage behavior
Limitations
- ▸Limited to Substrate/Wasm chains
- ▸Smaller ecosystem than Solidity/EVM
- ▸Requires Rust proficiency
- ▸No EVM compatibility
- ▸Less tooling for testing vs EVM chains
When NOT to Use
- ▸EVM-based chains
- ▸Projects requiring Solidity tooling
- ▸Developers unfamiliar with Rust
- ▸Contracts targeting non-Substrate chains
- ▸Projects needing large pre-existing libraries from Solidity
Cheat Sheet
- ▸#[ink(storage)] -> contract state struct
- ▸#[ink(event)] -> blockchain event
- ▸#[ink(message)] -> public function
- ▸Constructor via #[ink(constructor)]
- ▸Traits for cross-contract calls
FAQ
- ▸Is ink! Rust-based?
- ▸Yes - fully built on Rust.
- ▸Can ink! run on EVM?
- ▸No - targets Wasm/Substrate.
- ▸Is ink! safe for DeFi?
- ▸Yes - Rust safety + compile-time checks.
- ▸Does ink! support NFTs?
- ▸Yes - with PSP34 standard.
- ▸Can ink! contracts call each other?
- ▸Yes - via cross-contract trait calls.
30-Day Skill Plan
- ▸Week 1: Rust syntax and ownership
- ▸Week 2: ink! storage & messages
- ▸Week 3: Events and cross-contract calls
- ▸Week 4: Unit testing & simulation
- ▸Week 5: Deploy real-world applications on Substrate
Final Summary
- ▸Ink! is a Rust-based smart contract language for Substrate.
- ▸Safe, high-performance, Wasm-targeted.
- ▸Ideal for DeFi, NFTs, governance, and blockchain-native apps.
- ▸Leverages Rust's type safety and macros for secure contracts.
- ▸Perfect for developers targeting Polkadot/Kusama ecosystems.
Project Structure
- ▸lib.rs - main contract code
- ▸Cargo.toml - Rust project config
- ▸tests/ - Rust unit tests
- ▸target/ - compiled Wasm artifacts
- ▸metadata.json - ABI for frontend interaction
Monetization
- ▸Develop Polkadot DeFi contracts
- ▸Offer audited NFT templates
- ▸Consult on ink! contract development
- ▸Sell governance modules
- ▸Educational workshops
Productivity Tips
- ▸Leverage ink! macros
- ▸Write modular contracts
- ▸Unit-test extensively
- ▸Keep Wasm code minimal
- ▸Use cargo-contract CLI efficiently
Basic Concepts
- ▸Storage struct holds contract state
- ▸Messages are public callable functions
- ▸Events record contract activity
- ▸Cross-contract calls allowed via ink! traits
- ▸Constructor initializes contract state