Write, Test, and Build your first smart contract with ink! language

Matej Nemček 🌱🌍
Commonwealth Labs
Published in
5 min readOct 12, 2020

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.

ink! is a Rust-based eDSL for writing Wasm smart contracts specifically for the Contracts module. It is designed for correctness, conciseness and efficiency.

Are you curious about the difference between ink! and Solidity?

Environment Setup

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

flipper 
|
.
├── Cargo.toml
└── lib.rs

You can view the whole contract Flipper Example Source Code.

It’s the simplest “smart” contract you can build.

The Flipper contract is nothing more than a bool which gets flipped from true to false through the flip() function.

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
Testing our bool() basic contract written in ink! version 3

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-469659efe48657df
running 2 tests
test flipper::tests::default_works ... ok
test flipper::tests::it_works ... ok
test 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
building WASM binary

This is actually beautiful because things work as expected! You notice that your WASM binary will be in target/flipper.wasm

Contract Metadata

Now we need to generate contract metadata which is a sort of contract ABI

cargo +nightly contract generate-metadata
Contract Metadata a.k.a. contract ABI in Solidity

When you open target/metadata.json and take a look at the structure, you can see that this file describes all the interfaces that can be used to interact with your contract.

  • 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.

Conclusion

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.

Author

I’m Matej and I’m taking care of developer relations at Edgewa.re. For more upcoming content follow @edg_developers on Twitter.

If you want to ask questions, feel free to show up at the Edgeware builders channel on Element or Telegram Edgeware Developers

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.

Learn more about Substrate in recent Parity Technologies post www.parity.io/substrate-2-0-is-here/

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.

--

--