1. Home
  2. /
  3. Ink
  4. /
  5. Ink! Timelock Contract

Ink! Timelock Contract - Ink Typing CST Test

Loading…

Ink! Timelock Contract — Ink Code

Funds can be withdrawn only after a specific block timestamp.

#[ink::contract]
mod timelock {
	#[ink(storage)]
	pub struct Timelock {
		unlock: u64,
	}

	impl Timelock {
		#[ink(constructor)]
		pub fn new(unlock: u64) -> Self { Self { unlock } }

		#[ink(message)]
		pub fn withdraw(&mut self) {
		assert!(self.env().block_timestamp() >= self.unlock);
		self.env().transfer(self.env().caller(), self.env().balance()).unwrap();
		}
	}
}

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

Official Docs

  • ▸https://paritytech.github.io/ink-docs/
  • ▸https://github.com/paritytech/ink

More Ink Typing Exercises

Ink! Simple Counter ContractInk! Owner-Only MessageInk! Simple ERC20 TokenInk! Simple Vault (Deposit + Withdraw)Ink! Simple VotingInk! Whitelist AccessInk! Event LoggerInk! Immutable Config ExampleInk! Simple Multiplier

Practice Other Languages

CReactPythonC++RustTypeScriptKotlinPHPJavaC#RubyMqlCqlN1qlCypher