What can you do with 5 CMTS?

Very sad news is that in the cold environment of the currency circle, according to the current currency price, I can only buy a bottle of mineral water.

But these five CMTS are very useful in the hands of developers! Check out the CyberMiles Developer Community’s 2-hour Learn how to write smart contracts Workshop101 on November 25.

Hello World

Hello World is one of the simplest programs, and the first that most programmers write. Workshop 101 also started with the basic Hello World smart contract.

Special mention should be made to Yang Le, the speaker of Hello World. She is CyberMiles’ product manager and a top English literature student at the University of Hong Kong, who, like most of the attendees, had no prior programming experience. Welcome to the developer community class if you want.

Getting back to the point, we need to install MetaMask for CMT on our computer before we can formally deploy the Hello World smart contract. This is a wallet tool installed in Chrome browser. Its main function is to manage the CyberMiles account private key, to pay CMTs (including gas) directly from the web.

Installation process is simple, go to the website (https://www.cybermiles.io/metamask/) to download the plugin, then in Chrome extension mode, open the developers will be downloaded metamask4cmt. Contents drag and drop to the expansion of the Chrome program page.

After dragging and dropping, if Metamask for CMT doesn’t work properly, try restarting the browser. If it still doesn’t work, the programmers who attended the workshop came up with a new method that saved many people’s computers that day.

Download metamask4cmt. CRX suffix changed to.rar, unzip. On the Chrome extensions page, click “Load unzipped Extension” and add the unzipped Metamask4cmt.crx.

In addition, if you already have another version of MetaMask installed in Chrome, it is best to temporarily stop using the other version of MetaMask and leave only MetaMask for CMT active.

Once MetaMask for CMT is installed, a new CyberMiles account is generated to store CMTs. Select Main CyberMiles Network at the top. Because the organizer will give each person five real CMTS to pay for gas to execute the smart contract and participate in the mystery.

The transfer speed of CMT is very fast, the account is received in about 10 seconds, and there is no gas fee to pay. The transfer experience is very good!

With 5 CMTS and MetaMask for CMT, we are ready to start our Hello World!

Open the Remix for CMT page in Chrome and place the Hello World smart Contract code in the middle of the code editing area, or you can write your own code.

Then click Start to compile on the right, and the compiler automatically checks this code for routine errors. If there is no problem, the compiler will automatically recognize the Greet after the Contract. If there is a problem, the compiler prompts you to modify the code and compile it again.

After checking the code, deploy the Greet smart contract to the CyberMiles blockchain. Click Run on the top right, and then click Deploy. The MetaMask for CMT is called to pay for the gas required to deploy the smart contract.

Under normal circumstances, MetaMask’s payment page will automatically pop up, and you can just click Submit to pay. If it doesn’t pop up automatically, just click the plug-in manually. As you can see, it only costs 0.000489 CMT to deploy this contract on the chain.

Deployed Contracts with gas paid you can see that our contract has been Deployed on the CyberMiles chain at bottom right.

Terminate in red terminates the contract, but only the contract owner can terminate the contract, which also supports a small gas fee.

Greet shows what the smart contract says, in this case Hello World. Of course, the word Hello World is a programmer’s choice. As a developer of smart contracts, you can tell greet() to return any character — a song, an article, a book, or an encrypted text.

Owner displays the address where the contract is owned. The account address where you paid the gas deployment fee for this contract is the owner of the contract.

You can also query the transaction address information with the block browser (cmttracking.io/) and see the address of the contract that was just created. If your friend has the contract Address, enter the contract Address in the At Address field and click At Address to receive the unique smart contract greetings!

We saw above how to store a message on the blockchain with the Hello World smart contract and let anyone in the World see it. So, how does this work in code?

Pragma lity ^ 1.Copy the code

This is the code language and version of the smart contract.

contract Greet {
    
  function greet() {}function terminate() {}}Copy the code

This code introduces the smart contract name Greet and two functions, Greet and Terminate.

 address public owner;

    modifier onlyOwner() {
        assert(msg.sender == owner);
        _;
    }

    constructor () public {
        owner = msg.sender;
    }
Copy the code

The code above sets the owner of the contract, whose address information is public. The sender of the message, who paid the gas fee to enable the contract to be deployed on the chain, is the owner, and defines that only the owner can modify the contract.

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

This code shows that when you call greet(), Hello World appears. This feature just reads the data from the blockchain, so there is no gas charge. Here you can change Hello World to any text.

 function terminate() external onlyOwner {
    selfdestruct(owner);
  }
Copy the code

This is the termination function defined at the beginning of the smart contract, and only the Owner can perform this function. Selfdestruct is the destructor used to destroy data and terminate the contract.

This is the end of the Hello World code, which is a very complete piece of code.

The basic contract not only sets the Owner of the contract, but also sets the Owner to destroy the contract. When selfdesruct is executed, the money in the contract will also be returned to the Owner.

Because the contract address is very similar to the wallet address, and the length is the same, if you accidentally put the money to the contract address, under normal circumstances, it is impossible to find. But if you know who the owner of the contract is, you can contact the owner of the contract and get your assets back when the contract is destroyed.

This is a small personal loss, but if the project side does not take the code seriously, the loss of assets can be very serious. Typically, in July of this year, more than 513,774.16 ETH in Polkadot were frozen in Parity wallets because the owner in the contract was set to null, so no one could terminate the contract and get the money back. For now, the money is still locked up in contracts, permanently frozen.

Being careful and scrutinizing every piece of code, especially if it involves money, is a quality every programmer should have.

CyberMiles ensures the integrity of even the most basic smart contract code, which is worth learning from.

Bet game

An advanced version of the workshop is a guessing game based on smart contracts, with smart contracts acting as judges, which is completely fair.

The code is more complicated, I will not explain one by one, interested friends can go to Github to study.

Github.com/CyberMiles/…

After the Owner deploys the smart contract on the CyberMiles blockchain, the player can enter the contract address in Remix for CMT, invoke the contract, and participate in the guessing game.

The rule of the game is that everyone attending the workshop first chooses a random integer from 2 to 12 and pays a certain amount of CMT for the number they choose as a bet. If you guess correctly, you’ll get equal proportions of CMT.

After the rule is published, the Owner chooses startGame and the game begins! Everyone at the scene eager to try, someone loudly said “all in”, someone to respond, the scene temporarily abnormal joy. This is also the secret use of 5 CMTS!

After successfully calling the contract, fill in the integer of your choice at placeBet in the lower right, select CMT in Value in the upper right, and fill in how much you intend to spend. Fill it out, pay gas through Metamask for CMT, and come down and wait for the lottery!

Yang rolled two random dice on wechat to confirm the correct answer. In the dice “dust settled” process, a quiet, quietly waiting for the final answer, it seems that everyone is immersed in the game.

When the correct answer comes out, the contract Owner fills in the number 8 at endGame and pays the gas fee.

The next step is to claim the prize. According to the rules of the smart contract, the participant who has filled in the number 8 at placeBet will split proportionately all the CMT bets collected.

The winner clicks payme, pays the gas fee, and the contract automatically transfers the allocated CMT to the winner’s CMT account, which is calculated and executed by the code.

There were two winners, one took 5.3 CMT with 0.5 CMT and one took 21.2CMT with 2CMT. Of course, some students who guessed correctly did not give up paying me, but because they were not the winner, they got nothing and paid the gas fee for nothing. Don’t believe in smart contracts.

When the two winners take their prizes, the Owner chooses Terminate and the game is over!

Writing smart contracts with 5 CMTS, discussing with friends how to interpret the Code, having guidance from the CMT development community, and finally participating in an interesting game and witnessing what “Code is law” means.

Many thanks to the CyberMiles developer community for organizing such an event. It was a great experience and part of the charm of blockchain technology!