“Hello World” is often the first program those learning to code write. It’s no exception for building your first blockchain smart contract. This is a step-by-step guide of how to develop a “Hello World” smart contract with Lity.
Before you start, prepare the following tools to develop on the CyberMiles blockchain.
Install Chrome browser
The CyberMiles visual developer tools work on the Chrome browser platform. Download and install Chrome browser.
Install the Venus wallet as a Chrome extension
Venus is an extension for accessing CyberMiles enabled Dapps in your Chrome browser. It works as a wallet and allows you to sign CMT transactions on the webpage. Download and install Venus wallet
When you finish installing, click the chrome extension icon to set up the wallet according to the instruction.
Then, switch the tab to CMT Test Network.
Get 1000 Test CMTs from the CMT Testnet Faucet
With your Venus wallet address, you can get CMT test tokens from the CMT Testnet Faucet.
Input the wallet address you just generated with the Venus.
By clicking “Send me 1000 CMT and 1000 TEST”, you will find 1000 CMT test token in your Venus wallet. You will find this useful later for paying gas free in the development process.
Load the Europa IDE
Load the Europa IDE in your Chrome browser to start coding.
Work with the Smart Contract
Copy and paste the following code into your Europa IDE code editing window.
Click on “Start to compile”. If it’s compiled successfully, it will show the green bar with the name of the contract. Europa will check the code automatically for the most common coding errors.
If the codes work well, you can deploy it to the smart contract, On the Run tab, click “Deploy”.
Use the test tokens in your Venus wallet to pay the gas fee. Most of the time, the Venus payment page will pop up automatically. If it doesn’t, you just need to click the extension icon.
After submitting the transaction, you will see the name of the smart contract, together with the address shown on the Deployed Contracts. That is, you have successfully deployed your first smart contract on the CyberMiles blockchain! Hooray!
Click on “Greet”. A “Hello World” message is printed!
Click on “Owner”. This shows the Venus wallet address you used to pay the gas fee.
You can also choose to terminate the smart contract when you’ve done playing with it.
Click on “terminate” and pay for the gas fee.
After completing the payment, the smart contract address will turn to zero. The Greet content is no longer displayed. That is, the smart contract is not functioning and successfully terminated.
Learn to code
Now it’s time to understand how each line of code works!
This indicates the smart contract programming language and version number. We use Lity as the smart contract programming language.
Contract and function
This states the name of the contract
It also states the only two functions of the smart contract -
Set Owner of the contract
This sets the owner of the smart contract. First, the address of the owner is a public one. Second, it defines the one who pays the deploying the smart contract is owner. Third, it asserts that only the owner can modify the function of this smart contract.
Implement the greet function
This indicates that when you call
greet, it will return “Hello World”. And you can change “Hello world” to any text you want for sure.
Clean up the smart contract
This shows how to terminate the smart contract. It defines that only the owner can execute this function.