What Ink! is?
It's a domain-specific language (DSL) embedded within Rust programming language, designed specifically for writing WebAssembly (Wasm) smart contracts on Polkadot. By using Rust, developers benefit from its strong memory safety, advanced type system, and comprehensive development tools. Rust’s ownership model ensures that typical issues like null pointer dereferencing and buffer overflows are things of the past. Wasm offers fast execution speeds, platform independence, and enhanced security through sandboxed environments.
Why Astar Parachain?
Polkadot’s Relay Chain is designed for security and interoperability, not smart contract execution. To build a smart contract, you need a parachain like Astar, which supports the “contracts” pallet. For Cardano developers: this is like building on Cardano’s mainnet, but instead of deploying directly to the base layer, you deploy to a specialized chain (Astar) that handles smart contracts and connects to Polkadot’s shared security. You could spin your parachain and deploy your smart contracts on it, but we are following an easy path for educational purposes.
# Install Rust & Cargo (if not already installed)
curl --proto '=https' --tlsv1.2 -sSf <https://sh.rustup.rs> | sh
# Add WASM compilation target
rustup target add wasm32-unknown-unknown
# Install ink! CLI
cargo install --force --locked cargo-contract
cargo contract new my_first_dapp
cd my_first_dapp
#![cfg_attr(not(feature = "std"), no_std, no_main)]
#[ink::contract]
mod my_first_dapp {
#[ink(storage)]
pub struct MyContract {
value: i32,
}
impl MyContract {
#[ink(constructor)]
pub fn new(init_value: i32) -> Self {
Self { value: init_value }
}
#[ink(message)]
pub fn get(&self) -> i32 {
self.value
}
#[ink(message)]
pub fn increment(&mut self) {
// Use checked_add to prevent overflow
self.value = self.value
.checked_add(1)
.expect("Overflow in increment operation");
}
}
}
- #[ink(storage)]: Defines the contract’s state (like a Cardano datum).
- #[ink(message)]: Public functions callable by users (similar to Cardano’s endpoints).
- #[ink(constructor)]: Initializes the contract (like a Plutus validator script).
cargo contract build
This generates:
- my_first_dapp.wasm (compiled contract).
- metadata.json (ABI for frontend interaction).
- Connect to Shibuya Testnet:
- Visit Astar Portal (Shibuya Testnet)
- Click "Connect Wallet" (use Talisman wallet for simplicity)
- Use the Faucet:
- Scroll to the SBY Token section
- Click "Faucet" and request SBY free tokens
- Note: SBY is the testnet token for Shibuya
- Go to https://ui.use.ink/ →select Astar Shibuya
- Select Add new contract
- Click Upload new contract code.
- Set the name (e.g. my_first_dapp)
- Upload Contract Bundle (which is my_first_dapp.contract)
- Next and sign
- Call get: Retrieve the current value.
- Call increment: Update the value (requires a small gas fee).
- Go to Polkadot-JS Apps → Shibuya Testnet.
- Navigate to Developer → Contracts.
- Click Upload & deploy code.
- Upload my_first_dapp.wasm and metadata.json.
- Set the initial value (e.g., 42) during deployment.
- Call get: Retrieve the current value.
- Call increment: Update the value (requires a small gas fee).