Mode:
Duration:
1
Coding works best on desktop or with an external keyboard.
Coding works best on desktop or with an external keyboard.
A basic ERC20-like token implementation in Ink! supporting mint, transfer, and balance queries.
#[ink::contract]
mod token {
#[ink(storage)]
pub struct Token {
total: u64,
balances: ink::storage::Mapping<AccountId, u64>,
}
impl Token {
#[ink(constructor)]
pub fn new(amount: u64) -> Self {
let caller = Self::env().caller();
let mut balances = ink::storage::Mapping::new();
balances.insert(caller, &amount);
Self { total: amount, balances }
}
#[ink(message)]
pub fn transfer(&mut self, to: AccountId, amount: u64) {
let caller = self.env().caller();
let caller_balance = self.balances.get(caller).unwrap_or(0);
assert!(caller_balance >= amount);
self.balances.insert(caller, &(caller_balance - amount));
let to_balance = self.balances.get(to).unwrap_or(0);
self.balances.insert(to, &(to_balance + amount));
}
}
}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.
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.