CyberMiles Developer Community

Hello World

Hello World

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 coding...

Before you start, prepare the following tools to develop on the CyberMiles blockchain.

  1. Install Chrome browser.
  2. Install Venus as a Chrome extension
  3. Get 1000 Test CMT on the CMT Testnet Faceut
  4. Enter Europa to start coding

1. Install Chrome browser here.

2. Install Venus

To install Venus, go to this page on cybermiles.io.

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.

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.

3. Get 1000 Test CMT on CMT Testnet Faceut

With your Venus wallet address, you can apply for CMT test token on CyberMiles Travis 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.

4. Go to europa.cybermiles.io

Paste the code here to the Europa. In the later section, you will learn about how to write each line of code.

Compile, Run and Deploy the Smart Contract

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!

Execute the Smart Contract

Click Greet. A "Hello World" message is printed!

Click Owner. This shows the Venus wallet address you used to pay the gas fee.

Terminate the Smart Contract

You can also choose to terminate the smart contract when you've done playing with it.

Click "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.

Let's study the codes

Now it's time to understand how each line of code works!

Version Pragma

This indicates the smart contract programming language and version number. We use Lity as the smart contract programming language.

pragma lity ^1.2.4;

Contract and function

This states the name of the contract "Human".

It also states the only two functions of the smart contract - Greet and Terminate.

contract Human {
    
  function greet() {
  
  }

  function terminate() {
  
  }
}

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.

address public owner;    

    modifier onlyOwner() {        

        assert(msg.sender == owner);  

        _;   

     }    

    constructor () public { 

        owner = msg.sender;   

     }

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.

  function greet() public pure returns (string) {
      return "Hello world";
  }

Clean up the smart contract

This shows how to terminate the smart contract. It defines that only the owner can execute this function.

function terminate() external onlyOwner {    

    selfdestruct(owner);  

}