Write, Test, and Build your first smart contract with ink! language
This tutorial is meant for beginners in mind and in return, you will get joy you’ve managed to write, test and build your first smart contract with ink! language.
As Substrate supports WASM smart contracts, it means that any language that can compile to Wasm could be used to write these contracts. ink! is Parity’s answer for writing smart contracts using the Rust programming language. You can learn more about ink! at Substrate Developer Hub.
In this tutorial, we won’t touch the Substrate Node, just focus on setup your environment, write some bytes, test it and build it. This tutorial is designed for OSX and the most popular Linux distributions. Run these commands at your terminal:
curl https://sh.rustup.rs -sSf | sh -s -- -y
rustup target add wasm32-unknown-unknown --toolchain stable
rustup component add rust-src --toolchain nightly
rustup toolchain install nightly-2020-06-01
rustup target add wasm32-unknown-unknown --toolchain nightly-2020-06-01
If it didn’t yield any errors, congratulations, you have prepared Rust language environment and toolchain. Now we will use Rust’s “package manager” to install
cargo-contract . This tool will help us create new ink! projects with templates. Install by running
cargo install --git https://github.com/hicommonwealth/cargo-contract cargo-contract --force
Note: The ink! CLI is under heavy development and some of its commands are not implemented, yet!
Create an ink! project
Now we will generate files for the smart contract project and enter the directory
cargo contract new flipper && cd flipper
The files structure looks like this
It’s the simplest “smart” contract you can build.
The Flipper contract is nothing more than a
boolwhich gets flipped from true to false through the
Testing Your Contract
Now we will verify the functionality of the contract. We can quickly test that his code is functioning as expected using the off-chain test environment that ink! provides
cargo +nightly test
You should see a successful test completion. Now that we are feeling confident things are working, we can actually compile this contract to Wasm.
Finished test [unoptimized + debuginfo] target(s) in 1m 02s
Running target/debug/deps/flipper-469659efe48657dfrunning 2 tests
test flipper::tests::default_works ... ok
test flipper::tests::it_works ... oktest result: ok. 2 passed; 0 failed; 0 ignored; 0 measured; 0 filtered out
Now you will run the build command to compile your smart contract
cargo +nightly contract build
This is actually beautiful because things work as expected! You notice that your WASM binary will be in
Now we need to generate contract metadata which is a sort of contract ABI
cargo +nightly contract generate-metadata
- Registry provides the strings and cutom types used throughout the rest of the JSON.
- Storage defines all the storage items managed by your contract and how to ultimately access them.
- Contract stores information about the callable functions like constructors and messages a user can call to interact with your contract. It also has helpful information like the events that are emitted by the contract or any docs.
If you look close at the constructors and messages, you will also notice a
selector which is a 4-byte hash of the function name and is used to route your contract calls to the correct functions.
ink! is a young language and you might run into non-working cases, but we are here to help! 🙏 Just in time of writing this article ink! 3.0.0 RC 1 was released 🥳 and you may hit fewer problems as a lot of ecosystem components and now adjust to it! Feel free give it a try another example.
You may want to stay up-to-date and follow @edg_developers on Twitter.
ink! — Smart Contract Language
ink! is a Rust-based embedded domain-specific language (eDSL) for writing Wasm smart contracts specifically for the Contracts pallet. The main goals of ink! are user-friendliness, conciseness, and efficiency.
ink! FAQ with common questions you may have when starting to build smart contracts for Substrate.
Why use Substrate?
With Substrate, the runtime is your blockchain canvas, giving you maximum freedom to create and customize your blockchain precisely for your application or business logic. Within the runtime, you can compose any state transition function while utilizing built-in database management, libp2p networking, and the fast and safe consensus protocol GRANDPA.
What is Edgeware?
A self-improving smart contract blockchain
Edgeware is a high-performance, self-upgrading WASM smart contract platform, in the Polkadot ecosystem.
Participants vote, delegate, and fund each other to upgrade the network.
If you are curious about Edgeware, learn more at Edgewa.re
Follow for more updates twitter.com/@heyedgeware
If you want to learn more about the ecosystem, you can find more about Sat awesome Substrate.