A guide to WASM(ink!) implementation for beginners
Summary
- Introduce how to implement ink! that beginners should know.
- Introduce testing methods and useful tools.
Motivation
The Astar Network team is strongly promoting WASM as a platform for future smart contracts. I don’t know much about EVM or WASM at this point, but I support this line of thinking. Therefore, as an ambassador of Astar Network, I would like to support the increase of WASM developers.
Table Of Contents
- Environment
- How to create a project
- What you need to know to implement basic smart contracts
- How to test
- How to debug
- Useful Tools
Environment
Details on how to build the environment for ink! are posted on the official page, so please refer to the following site to build it.
How to create a project
Creating a project is very easy. Execute the following command. In the example below, the project name is “sample_project”.
cargo contract new sample_project
A project is created with the flipper contract implemented by default. Delete unnecessary parts in it and implement your own contract.
What you need to know to implement basic smart contracts
I myself do not have long experience in WASM development. However, I have learned a few things from experienced developers on stackoverflow and stackexchange, so I will write this part based on that experience. There may be some parts that are wrong. I would be very happy if you could point it out to me.
Basic knowledge
- Know where to define what data to store on the blockchain
The part defined by the “#[ink(storage)]” attribute is where you define the data to save.
- Define a constructor
Multiple constructors can be defined. Variables defined in storage, which is a data storage area, must be initialized in the constructor.
- Define a contract function that can be called externally
Functions defined with the #[ink(message)] attribute are externally callable. Notice the arguments of the “flip” function in the example. “&mut self” is defined. This is a declaration to modify the storage area and implies the execution of a transaction. The “get” function will be a function that only references the value in reverse.
- Define an inner function
Functions defined with the “#[inline]” attribute act as internal functions. It will only be available inside the contract.
- Define a payable function
A native token payment is required when the function defined in “payable” is called.
- Others
Other definitions such as “event” are also possible. Please check the ink! documentation for details.
Libraries you should know first
I think the first library you should know as a beginner is “ink_env”. “ink_env” gives you much of the information you need when you want to implement a smart contract. Please refer to the documentation from the link below.
I will give you some examples of how to use it.
- Get the account address that invoked the executed contract.
let caller = self.env().caller();
・ Amount of native tokens transferred during transaction execution
let value = self.env().transferred_value();
There are many other useful functions, so check them out.
Use “String” type and “Vec” type
Use “ink_prelude” as defined in lines 10 and 11. If you import it, you can use it as a type as it is.
Define your own struct
How To Test
At the moment, ink!’s main test is off-chain testing. I don’t know if it’s possible to write test code while on-chain. I’d like to be able to write test code on-chain like EVM’s hardhat and foundry, and hope that WASM’s integrated environment “swanky” will have that capability.
Off-Chain Test
A sample test code is implemented at the bottom of the flipper contract lib.rs file when you create a new project.It can be executed with the following command.
cargo +nightly test
On-Chain Test
I am running it in the following way.
However, to be honest, I’m a little unsure if the method I’m introducing is the best.
First, download the latest version of Astar Network.
Then start it with the following command. Parameters are explained later.
./astar-collator --dev -lruntime=debug
Deploy the contract using the “polkadot/substrate portal” site.
Click the top left icon to connect to your development environment. If you select “Development” — “local node”, you will be connected to the Astar Network node you started locally earlier.
You can deploy a contract to the blockchain from the “Developer” — “Contracts” menu as shown in the image above. This site provides a web interface for deployed contracts. So it’s very useful if you don’t have a frontend implementation.
How To Debug
If you want to debug a smart contract while it is deployed on-chain, it is effective to output debug statements.
Implement debug statements in your smart contract as follows:
ink_env::debug_println!("########## destination caller ############### value is {:?}", caller);
Debugging information is output to the standard output of the node by setting the startup parameters of the node described above and starting it.
--dev -lruntime=debug
Useful Tools
Openbrush
Openbrush is a project that provides a standard implementation like Solidity’s OpenZeppelin. Please refer to the following articles that have been introduced in the past.
Swanky
Swanky is a project to provide an integrated development environment for WASM like the EVM hardhat. I personally have high hopes. I have also introduced this before, so please refer to it.
Next Step
When it comes to WASM development, I am very excited about swanky. It is currently in beta version, but we are looking forward to the release of the product version. At that time, I expect that it will be possible to easily implement test code for on-chain smart contracts. So I plan to keep up with swanky updates.
Personally, I’m most interested in DAOs. I believe DAO has the potential to improve HTTP in the future. So I’m currently developing a DAO tool in WASM. I also introduce them on my medium, so if you are interested, I would be happy if you could read it.
https://realtakahashi-work.medium.com/aiming-for-a-simple-dao-tool-that-anyone-can-use-cheaply-c23c24b99900
About Astar
Astar Network — The Future of Smart Contracts for Multichain.
Astar Network supports the building of dApps with EVM and WASM smart contracts and offers developers true interoperability, with cross-consensus messaging (XCM). We are made by developers and for developers. Astar’s unique Build2Earn model empowers developers to get paid through a dApp staking mechanism for the code they write and dApps they build.
Astar’s vibrant ecosystem has become Polkadot’s leading Parachain globally, supported by all major exchanges and tier 1 VCs. Astar offers the flexibility of all Ethereum and WASM toolings for developers to start building their dApps. To accelerate growth on Polkadot and Kusama Networks, Astar SpaceLabs offers an Incubation Hub for top TVL dApps.