Comment on page
Deploy an ink! Smart Contract
In this tutorial, we will learn how to deploy an ink! Smart Contract on CESS blockchain. ink! is developed by Parity, the implementation team responsible for the Polkadot Network, and is compiled into Wasm code for execution. It supports running in the WebAssembly Virtual Machine (Wasm VM), and Rust developers can use their already-familiar toolchain to develop smart contracts instead of learning a new language and toolchain.
- Next, install
cargo-contract, a CLI tool to help setting up and managing WebAssembly smart contracts written in ink! (GitHub repo).# Install an additional component `rust-src`rustup component add rust-src# Install `cargo-contract`cargo install cargo-contract --force --locked cargo-contract# Install `dylint`, a linting toolcargo install cargo-dylint dylink-link
- Verify the installation is successful by running the command:cargo contract --helpThis should display the command help messages similar to the following.Utilities to develop Wasm smart contractsUsage: cargo contract <COMMAND>Commands:new Setup and create a new smart contract projectbuild Compiles the contract, generates metadata, bundles both together in a `<name>.contract` file...
We will create a new smart contract project using the
cargo-contractpackage downloaded in the previous step. Run the following command:
cargo contract new flipper
This command generates the following directory with three files
Take a peek into
Cargo.tomlon the contract dependencies and
lib.rson the contract source code.
Next, we will compile the code and run the test cases inside
lib.rsto check everything work accordingly.
You should see the following output. This means all test cases have passed.
running 2 tests
test flipper::tests::it_works ... ok
test flipper::tests::default_works ... ok
test result: ok. 2 passed; 0 failed; 0 ignored; 0 measured; 0 filtered out; finished in 0.00s
running 0 tests
test result: ok. 0 passed; 0 failed; 0 ignored; 0 measured; 0 filtered out; finished in 0.00s
We can now build the contract code:
cargo contract build
After the build is complete, the output artifacts are stored in
target/ink/directory. There are three key output files:
flipper.wasm- the wasm binary.
flipper.json- a metadata file containing the contract ABI.
flipper.contract- the contract code that bundles the above two files.
You can also make a release build with
cargo contract build --release. The release build has optimization settings to make the code run more efficient in the Wasm VM, but the debug build is good enough for now.
- Run a local CESS node in development mode. Perform the following commands to clone CESS node source code, compile it, and run a local node.# Select the appropriate/latest git tag from the git repositorygit clone https://github.com/CESSProject/cess.git --branch v0.7.4cd cess# The build process will take probably 10 mins depending on your machine speccargo buildtarget/debug/cess-node --devThe node will start running and the console will display something as below.CESS Node Console
- We will use Substrate Contracts UI to deploy and interact with an ink! smart contract, a UI tool developed by Parity. Let's connect Substrate Contracts UI to our local CESS node by:Substrate Contract UI
- Click Upload a new contract.
- In the next screen,
You should see the contract metadata after choosing the right contract file. Then click Next.Upload Contract Bundle
- In Account select box, select alice to deploy the contract from Alice
- Give the Contract Name the value Flipper Contract, and
- Drag or open the file target/ink/flipper.contract in the Upload Contract Bundle.
- Here, note that the contract code upload and instantiation are separated into two steps. In CESS chain you can have one copy of the smart contract code and multiple instances of that smart contract with different initialization configurations, thus saving blockchain space and encouraging code reuse. For example, multiple ERC20 tokens can reuse the same contract code with different units, logos, and symbols.In this screen, we are combining code upload and contract instantiation in one step.
Upload Instantiate 01
- Deployment Constructor: select new(initValue: bool)
- initValue: bool: select false
- Leave the remaining setting unchanged, and click Next
- In the next screen, confirm everything looks good and click Upload and Instantiate.Upload Instantiate 02
- You have successfully instantiated a sample flipper contract in this screen.Try interact with the flipper contract by reading the value from the smart contract and issue a transaction to it!Contract Instantiate Successfully