ink! is an eDSL designed to write smart contracts in Rust for blockchains built on the Substrate framework. ink! contracts are compiled to WebAssembly.
Table of Contents
- Play with It
- Usage
- Hello, World! ‒ The Flipper
- Examples
- How it Works
- ink! Macros & Attributes Overview
- Developer Documentation
- Community Badges
- Contributing
- License
Play with It
The best way to start is to check out the Getting Started page in our documentation. If you want a local setup, you can use our substrate-contracts-node for a quickstart. It’s a simple Substrate blockchain that includes the contracts pallet.
You can also try out the contracts on Rococo testnet, a Substrate-based parachain that supports ink! smart contracts. For more details, refer to our documentation.
The Contracts UI can be used to instantiate and interact with your contracts.
Usage
A prerequisite for compiling smart contracts is to have Rust and Cargo installed. Here’s a detailed installation guide. Additionally, we recommend installing cargo-contract as it helps manage WebAssembly smart contracts written with ink!:
cargo install cargo-contract --force
To initialize a new ink! project, use:
cargo contract new flipper
This creates a folder named flipper containing a scaffold Cargo.toml
and lib.rs
. To build your contract, navigate into the flipper directory and run:
cargo contract build
The build process will produce compilation artifacts in the target folder.
Hello, World! ‒ The Flipper
The Flipper contract is a simple contract managing a single boolean value. Think of it as a light switch:
- If the switch is on (true), the light is on; if off (false), the light is off.
- You can “flip” the switch to change its state.
Here’s a glimpse at the code:
#[ink::contract]
mod flipper {
#[ink(storage)]
pub struct Flipper {
value: bool,
}
#[ink(constructor)]
pub fn new(init_value: bool) -> Self {
Self { value: init_value }
}
#[ink(message)]
pub fn flip(&mut self) {
self.value = !self.value;
}
#[ink(message)]
pub fn get(&self) -> bool {
self.value
}
#[cfg(test)]
mod tests {
use super::*;
#[ink::test]
fn it_works() {
let mut flipper = Flipper::new(false);
assert_eq!(flipper.get(), false);
flipper.flip();
assert_eq!(flipper.get(), true);
}
}
}
This code sets up the Flipper contract, allowing it to be flipped and queried.
Examples
In the examples repository, you’ll find a treasure trove of practical applications:
basic_contract_ref
‒ Cross-contract calling interface.trait-erc20
‒ Implements the ERC20 token standard.erc721
‒ Represents a simple implementation of ERC721 NFTs.
To build a single example, navigate to its root directory and run:
cargo contract build
Follow instructions from the earlier sections for deploying them.
How it Works
The foundation of ink! lies in Substrate’s FRAME, which equips smart contracts with essential functionalities through the contracts pallet. An ink! contract is compiled into a Wasm blob that can be uploaded to the blockchain, allowing interactions as defined in its interface.
ink! Macros & Attributes Overview
The ink! framework utilizes various macros to streamline contract development:
#[ink(storage)]
to define the contract’s state.#[ink(message)]
marks methods that can be called externally.#[ink(constructor)]
to define how contracts are instantiated.
For a complete overview, check the ink! documentation on these attributes.
Developer Documentation
For any intricate detail about ink!, the comprehensive documentation is available at use.ink.
Community Badges
Feel free to share your ink! projects with our community badges. Choose between normal and flat designs available here.
Contributing
Interested in contributing? Check our contribution guidelines for more information.
License
The code within this repository is licensed under the Apache License 2.0.
Troubleshooting
If you encounter issues while using ink!, consider these tips:
- Ensure that Rust and Cargo are correctly installed and updated.
- Make sure you have the required dependencies by running
cargo update
. - Check if any error messages provide specific guidance on issues to address.
For more insights, updates, or to collaborate on AI development projects, stay connected with fxis.ai.
At fxis.ai, we believe that such advancements are crucial for the future of AI, as they enable more comprehensive and effective solutions. Our team is continually exploring new methodologies to push the envelope in artificial intelligence, ensuring that our clients benefit from the latest technological innovations.