DEV Community

Cover image for 10N polkadot substrate : your first contract
565.ee
565.ee

Posted on

10N polkadot substrate : your first contract

introduce

Tutorial objectives

Update your Rust environment

Install the Substrate contracts node

Create a new smart contract project

Test the default contract

Build the contract

Start the Substrate smart contracts node

Deploy the contract

Create an instance on the blockchain

Call the smart contract

Substrate Tutorials , Substrate 教程

Contact 联系方式

• introduce

As you learned in Blockchain basics decentralized applications are most often written as smart contracts. Although Substrate is primarily a framework and toolkit for building custom blockchains, it can also provide a platform for 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.

• Tutorial objectives

By completing this tutorial, you will accomplish the following objectives:

Learn how to create a smart contract project.
Build and test a smart contract using the ink! smart contract language.
Deploy a smart contract on a local Substrate node.
Interact with a smart contract through a browser.

• Update your Rust environment

For this tutorial, you need to add some Rust source code to your Substrate development environment.

To update your development environment:

• Open a terminal shell on your computer.
• Change to the root directory where you compiled the Substrate node template.
• Update your Rust environment by running the following command:

rustup component add rust-src --toolchain nightly
Enter fullscreen mode Exit fullscreen mode

• Verify that you have the WebAssembly target installed by running the following command:

rustup target add wasm32-unknown-unknown --toolchain nightly
Enter fullscreen mode Exit fullscreen mode

• Install the Substrate contracts node

To simplify this tutorial, you can download a precompiled Substrate node for Linux or macOS. The precompiled binary includes the FRAME pallet for smart contracts by default. Alternatively, you can build the preconfigured contracts-node manually by running cargo install contracts-node on your local computer.

If you can't download the precompiled node, you can compile it locally with a command similar to the following:

cargo install contracts-node --git https://github.com/paritytech/substrate-contracts-node.git --tag <latest-tag> --force --locked
Enter fullscreen mode Exit fullscreen mode

• Create a new smart contract project

You are now ready to start developing a new smart contract project.

To generate the files for a smart contract project:

• Open a terminal shell on your computer.
• Create a new project folder named flipper by running the following command:

cargo contract new flipper
Enter fullscreen mode Exit fullscreen mode

• Change to the new project folder by running the following command:

cd flipper/
Enter fullscreen mode Exit fullscreen mode

• Test the default contract

At the bottom of the lib.rs source code file, there are simple test cases to verify the functionality of the contract. You can test whether this code is functioning as expected using the offchain test environment.

To test the contract:

• Open a terminal shell on your computer, if needed.
• Verify that you are in the flipper project folder, if needed.
• Use the test subcommand and nightly toolchain to execute the default tests for the flipper contract by running the following command:

cargo +nightly test
Enter fullscreen mode Exit fullscreen mode

The command should display output similar to the following to indicate successful test completion:

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
Enter fullscreen mode Exit fullscreen mode

• Build the contract

After testing the default contract, you are ready to compile this project to WebAssembly.

To build the WebAssembly for this smart contract:

• Open a terminal shell on your computer, if needed.
• Verify that you are in the flipper project folder.
• Compile the flipper smart contract by running the following command:

cargo +nightly contract build
Enter fullscreen mode Exit fullscreen mode

This command builds a WebAssembly binary for the flipper project, a metadata file that contains the contract Application Binary Interface (ABI), and a .contract file that you use to deploy the contract. For example, you should see output similar to the following:

• Start the Substrate smart contracts node

If you have successfully installed substrate-contracts-node, you can start a local blockchain node for your smart contract.

To start the preconfigured contracts-node:

• Open a terminal shell on your computer, if needed.
• Start the contracts node in local development mode by running the following command:

substrate-contracts-node --dev
Enter fullscreen mode Exit fullscreen mode

• Navigate to the Contracts UI in a web browser, then click Yes allow this application access.
• Select Local Node.

• Deploy the contract

However, deploying a smart contract on Substrate is a little different than deploying on traditional smart contract platforms. For most smart contract platforms, you must deploy a completely new blob of the smart contract source code each time you make a change. For example, the standard ERC20 token has been deployed to Ethereum thousands of times. Even if a change is minimal or only affects some initial configuration setting, each change requires a full redeployment of the code. Each smart contract instance consume blockchain resources equivalent to the full contract source code, even if no code was actually changed.

In Substrate, the contract deployment process is split into two steps:

• Upload the contract code to the blockchain.
• Create an instance of the contract.

Upload the contract code
For this tutorial, you use the Contracts UI front-end to deploy the flipper contract on the Substrate chain.

To upload the smart contract source code:

• Open to the Contracts UI in a web browser.
• Verify that you are connected to the Local Node.
• Click Add New Contract.
• Click Upload New Contract Code.
• Select an Account to use to create a contract instance.

You can select any existing account, including a predefined account such as alice.

• Type a descriptive Name for the smart contract, for example, Flipper Contract.
• Browse and select or drag and drop the flipper.contract file that contains the bundled Wasm blob and metadata into the upload section.
image

• Create an instance on the blockchain

Smart contracts exist as an extension of the account system on the Substrate blockchain. When you create an instance of this smart contract, Substrate creates a new AccountId to store any balance managed by the smart contract and to allow you to interact with the contract.

After you upload the smart contract and click Next, the Contracts UI displays information about the content of the smart contract.

To create the instance:

• Review and accept the default Deployment Constructor options for the initial version of the smart contract.
• Review and accept the default Max Gas Allowed of 200000.
image

• Click Next.

The transaction is now queued. If you needed to make changes, you could click Go Back to modify the input.
image

• Click Upload and Instantiate.

Depending on the account you used, you might be prompted for the account password. If you used a predefined account, you won't need to provide a password.
image

• Call the smart contract

Now that your contract has been deployed on the blockchain, you can interact with it. The default flipper smart contract has two functions—flip() and get()—and you can use the Contracts UI to try them out.

get() function
You set the initial value of the flipper contract value to false when you instantiated the contract. You can use the get() function to verify the current value is false.

To test the get() function:

• • Select any account from the Account list.

• This contract doesn't place restrictions on who is allowed to send the get() request.

• Select get(): bool from the Message to Send list.
• Click Read.
• Verify that the value false is returned in the Call Results.
image

flip() function
The flip() function changes the value from false to true.

To test the flip() function:

• • Select any predefined account from the Account list.

• The flip() function is a transaction that changes the chain state and requires an account with funds to be used to execute the call. Therefore, you should select an account that has a predefined account balance, such as the alice account.

• Select flip() from the Message to Send list.
• Click Call.
• Verify that the transaction is successful in the Call Results.
• Verify the new value is true in the Call Results.
image

• Substrate Tutorials , Substrate 教程

CN 中文 Github Substrate 教程 : github.com/565ee/Substrate_CN

CN 中文 CSDN Substrate 教程 : blog.csdn.net/wx468116118

EN 英文 Github Substrate Tutorials : github.com/565ee/Substrate_EN

EN 英文 dev.to Substrate Tutorials : dev.to/565ee

• Contact 联系方式

Homepage : 565.ee

GitHub : github.com/565ee

Email : 565.eee@gmail.com

Facebook : facebook.com/565.ee

Twitter : twitter.com/565_eee

Telegram : t.me/ee_565

Top comments (0)