SOLIDITY: A Simple Tutorial

SOLIDITY: A Simple Tutorial

Background and Problem

Imagine a grandfather who wants to pass his property to his family members smoothly after his death. In traditional wills, the estate allocation scheme is implemented in legal documents, and then a judge reviews the file and makes the appropriate decision. However, this process can lead to disputes between family members, affecting the final decision of the judge, and even causing further damage to family relationships. Can we make the distribution of the estate automatically, avoiding these problems?

The Development Environment

To build a solidity contract, the easiest way is to use the online integrated development environment (IDE) Remix, which is officially offered by the Ethereum Square platform. Click here to open Remix, where you can write, compile, and deploy solidity intelligent contracts.

After opening the Remix page, pay attention to the right side of the “runtab” and select “JavaScript VM” from the “environment” drop-down box. This option uses a memory emulator Ethereum Square nodes as the operating platform for your solidity intelligent contracts.

The Solidity Compiler Version

Solidity is still in its early stages, and the language is constantly evolving from grammar to the compiler. The first line of code in solidity must use the “pragma” keyword to declare which version of the compiler is needed.

For example:

pragma solidity ^0.5.7;

Note that the semicolon at the end cannot be omitted.

The First Solidity Contract

Now we can define our first contract. Use the “contract” keyword to define a contract agreement, which is similar to what we are familiar with OOP in the class. The name of the contract usually starts with a capital letter, for example, “Will”.

contract Will {
    // code
}

Solidity Global Variables and Constructors

Before we start writing code, we should first clear the terms of the will. The hypothesis is that the grandfather has 50 Ethereum coins, of which 20 are reserved for his son Conrad, and the remaining 30 left his wife Lisa.

We will complete the following code:

pragma solidity ^0.5.7;

contract Will {
    address public owner;
    uint public fortune;
    bool public isDeceased;

    constructor() public payable {
        owner = msg.sender;
        fortune = msg.value;
        isDeceased = false;
    }
}

In this code, we define a contract with three global variables: owner, fortune, and isDeceased. The constructor function is automatically executed when the contract is deployed, and it sets the initial values of these variables.

Using the Solidity Modifier

In solidity, the modifier (Modifier) may be added as an additional condition logic function. For example, suppose we have a method to turn off the lights, and there is a modifier that requires the light switch to be in the on state. We can declare this additional modifier on the method, to ensure that only the light switch is in the on state, and then call this method.

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

modifier mustBeDeceased() {
    require(isDeceased == true);
    _;
}

Set Allocation Scheme Inheritance

Now we have to continue to complete the assigned task of heritage among family members, which requires their wallet addresses and number assignment. We will create an array to hold their wallet addresses, and then write a method to distribute the estate.

address[] public familyWallets;
mapping (address => uint) inheritance;

function addWallet(address wallet) public onlyOwner {
    familyWallets.push(wallet);
    inheritance[wallet] = 0;
}

function setInheritance(address wallet, uint amount) public onlyOwner {
    inheritance[wallet] = amount;
}

To Achieve Automatic Allocation Heritage

Let’s summarize. We have learned global variables, data types, constructors, such as specific keywords payable and public, built-in global variables such as msg.sender and msg.value, and modifiers require, arrays, and methods of mapping table. We have been setting up the framework contract, now let us integrate the various parts of the final completion of the contract.

As part of this code is the last tutorial, we will automatically assign family heritage.

function payout() private mustBeDeceased {
    for (uint i = 0; i < familyWallets.length; i++) {
        address wallet = familyWallets[i];
        uint amount = inheritance[wallet];
        wallet.transfer(amount);
    }
}

Solidity Contract Deployment and Interaction

Your Remix page should look like this:

Remix on the right page switching to “compileOptions” page, confirm the figure below to select the compiler version, and then click [start to compile].

Make sure “Environment” to select the drop-down box “JavaScript VM”, click on the “account” drop-down menu will display five test accounts, each has 100 Ethereum coins, let us choose the first one.

Not to deploy Ethereum Square blockchain contract free, deployment need to pay the fee, commonly referred to as gas.

Deployment need to pay the fee, commonly referred to as gas. The purpose of this mechanism is to avoid the introduction of chain blocks malicious misuse of computing resources to learn more about gas, you can view this article: one minute to find out Gas / Gas Price / Gas Limit.

gas limitField use the default value on it, let’s not modify it. valueWe want to send to the field indicates the number of Ethereum currency contracts at the time of deployment contracts. Enter the 50, remember we added in the constructor defined payable keywords?

Now continue, click [deploy].

You may notice three things at once. First, check the account balance has now become 49.9999 … because we transferred the contract 50 Ethereum money, but also a little deduction deployment fees.

Bottom of the page will also provide detailed information on the console of the deployment process, you can look at.

Our contracts have been successfully deployed! It generates its own address, and shows the way we define the two contracts.

As the holder of the contract, the first thing we need to do, is to set the number of family members inherit: Conrad (20), Lisa (30).

Suppose we use “account” the pull-down menu in the second as Conrad’s account, with third Lisa. Selecting a second account, click [copied to the clipboard] icon, then input the above setInheritance text entry box behind.

We executed setInheritance before the method, there are several things to keep in mind. Incoming contract Ethereum currency unit is the amount of money wei rather than Ethereum, 1 ETH = 1,000,000,000,000,000,000 WEI, which is a very small unit, the number of properties that we too will need to be converted to the local currency to the value of the unit of WEI.

After scaling the number of properties that, in the figure above write setInheritance back behind the text input box, previously input address, pay attention to use between these two values separated by commas.

And do not forget the “account” first account drop-down box is checked, remember onlyOwner modifiers do? Only holders of personnel contracts can call setInheritance method!

Let us now turn to Lisa Conrad and execution setInheritance method. You should be able to see the success of the console output.

Look at them decoded input: You see, it shows that the data we input. Distribution of estate good, but the bad news came. Grandfather at the age of 73, in an Arctic expedition, unfortunately died of a heart attack. He was always so full of passion and vitality.

While we commemorate the grandfather, at the same time we will call contract deceased() method, complete grandfather’s last wish.