Polkadot Fundamentals and Substrate Development
Smart contracts
This tutorial demonstrates how to build a basic smart contract to run on a Substrate-based chain.
In this tutorial, you'll explore using ink! as a programming language for writing Rust-based smart contracts.
Install the CLI tool
We will first update our Rust environment -
- rustup component add rust-src
- Then verify whether we have the WebAssembly target installed -
rustup target add wasm32-unknown-unknown --toolchain nightly
Now let’s go ahead and install the CLI tool -
- Add rust-src compiler component -
rustup component add rust-src
- Install the latest version of cargo-contract -
cargo install --force --locked cargo-contract --version 2.0.0-rc
- Verify the installation and explore the commands available -
cargo contract --help
Create a new smart contract project
Here we are assuming that you already have a precompiled substrate node install
To generate files for an ink! Project -
- Create a new project folder named “flipper” - cargo contract new flipper
- You will see a project generated with the default contract lib.rs
- Now we can test this smart contract with - Cargo Test
- Now we will build it - Cargo build
Deploy contract
Now that we have compiled the contract, we just need to start the node and deploy it
- Start your node using
substrate-contracts-node --log info,runtime::contracts=debug 2>&1
- Go to the flipper project folder.
- Build the contract using cargo contract build.
cargo contract instantiate --constructor new --args "false" --suri //Alice --salt $(date +%s)
Interact with contract
Since the smart contract has been deployed, let’s interact with it -
- We will first use get()
cargo contract call --contract $INSTANTIATED_CONTRACT_ADDRESS --message get --suri //Alice --dry-run
- Then we will use flip()
cargo contract call --contract $INSTANTIATED_CONTRACT_ADDRESS --message flip --suri //Alice
Project
Comments
You need to enroll in the course to be able to comment!