How to Develop an Ethereum Smart Contract

Smart Contract is a powerful scheme, running on the Ethereum Blockchain, but is also considered as the backend application of the Ethereum Blockchain. Understanding how the scheme itself works as well as the process for developing such a contract is necessary for Ethereum Blockchain App development. This tutorial will explain development environments, toolsets, and some basic Solidity code used in smart contracts. Before reading this tutorial, we recommend that you have a basic knowledge regarding Blockchain and Ethereum.

Development Environment

Ethereum Networks

Ethereum Mainnet—short for Ethereum main network—is the original and functional Ethereum network. Each transaction on the Mainnet requires a real transaction fee, where transaction on the Mainnet costs a dollar amount to execute. It is the network in which the ‘end-product’ will be deployed.

The Ethereum Testnet—short for Ethereum test network—is an Ethereum network that uses the same technology as the Mainnet; however, Testnet is used for testing smart contracts and DApps. The Ethereum coins in the Testnet do not have real value outside of the Testnet. Because the Testnet uses the same technology as the Mainnet, Smart Contract acts the same on both networks. The only difference is that developers use Testnet for testing purposes only.

Ethereum Emulator

You could also install your private Ethereum network on local computer network, using an Ethereum Emulator, such as Ganache & Ganache-cli, or testrpc.

  • Ganache is an emulator Blockchain for Ethereum development. You can use it to run tests, execute commands, and inspect Blockchain state easily.
  • Ganache-cli is the command line version of Ganache. It is faster, easier, and safer than Ganache.
  • TestRPC is a customizable Blockchain emulator. It is the most minimal of the Ethereum network and only includes basic function, as compared with other networks.
Figure 1: Ganache UI
Figure 2: Ganache-CLI daemon
Figure 3: TestRPC daemon

Fundamental Ethereum

Ethereum Wallets

Application wallets are applications running on desktops (Atomic, Mist, Exodus, etc.) or smart phones (Jaxx, Coinomi, TrustWallet, etc.), and support user payment on the Ethereum network. Metamask is a browser add-on Ethereum wallet run on Chrome, Firefox, and Opera browser. Its usage is easier than Web wallets (such as MyEtherWallet), which works like accounts on a web server. As a result, they are not secure, because the web server is able to access your accounts and wallets.

Figure 4: A MetaMask wallet
Figure 5: My EtherWallet – a web-based Ethereum wallet

Ethereum Programming Language

Smart contracts are compiled to bytecode and run on Ethereum Virtual Machines in the Blockchain. They can be written in Solidity, Serpent, LLL, etc., but Solidity is the most popular and supported. Solidity is an object-oriented and high-level programming language. You can learn more about Solidity here.

Setup Environment for Smart Contract Development

Set Up Network

There are 3 popular test-nets: Rinkeby, Ropsten, and Kovan; all of them should be used in the system testing stage. We recommend using a local network (ganache-cli or testrpc) to setup a smart contract development environment. However, in this tutorial, we will be using Rinkeby testnet to illustrate the development of a smart contract.

Wallet

While application wallets (such as Mist) are simple to install, we demonstrate executing Ethereum transactions using Metamask, a browser add-on wallet available for Chrome and FireFox.

  • Install Metamask add-on for Firefox here.
  • Follow the instructions to create a Wallet (or Import if you already had a Wallet).
Figure 6: Create a MetaMask wallet
  • Choose the Rinkeby Test Network and copy the Wallet Address.
Figure 7: Switch to Rinkeby Test Network
  • Post this address on Facebook, Twitter, or Google Plus. Then, copy the posted URL and paste to the textbox on https://faucet.rinkeby.io/ to get some free Ether for test.

Solidity Compiler

For convenience, we use an online IDE for coding, debugging, and testing our smart contract. One of the most famous is Remix online. You can also use the offline version of Remix for better performance.

Develop and Test the First Smart Contract

Write your First Solidity Code

  • Write your first Solidity code.
pragma solidity ^0.5.1;

contract HelloWorld {

    int myNumber;

    constructor() public  {
        myNumber = 9;
    }

    function increaseMyNumber() public {
        myNumber++;
    }

    function addMyNumber(int s) public {
        myNumber += s;
    }
    
    function getMyNumber() public view returns(int number) {
        return myNumber;
   }

}
  • Choose your compiler version, then press Start to compile.
Figure 8: Compile your contract

Deploy your Contract to Testnet and Invoke it

  • Choose Run tab on Remix, the Environment as Rinke by Injected Web3, the Account as Metamask wallet, and the HelloWorld Contract. Then, press Deploy.
Figure 9: Deploy your contract
  • The Metamask Wallet popups and asks to use Ether to deploy the Contract. Press Confirm to Deploy the Contract.
Figure 10: Pay for the deployment
  • Wait for the deploying of the Contract to Testnet. Then, you can see the contract was deployed successfully with contract address. You could invoke the contract’s function from Remix and see the results.
Figure 11: Mannually invole your contract’s method
  • You can copy the contract’s address, find it on Etherscan, and view its transactions and events.
Figure 12: Inspect your contract on the Test Network
  • That’s it! You already wrote, deployed, and invoked your first Smart Contract, using Remix online compiler and Metamask. You can also do it with other compilers and Ethereum networks.

Debug and Troubleshooting Smart Contract

Use Event

  • Add event to log the value that you want to check.
pragma solidity ^0.5.1;

contract HelloWorld {

    int myNumber;
	  event LogValue(int);

    constructor() public  {
        myNumber = 9;
    }

    function increaseMyNumber() public {
        myNumber++;
	      emit LogValue(myNumber);
    }

    function addMyNumber(int s) public {
        myNumber += s;
    }
    
    function getMyNumber() public view returns(int number) {
        return myNumber;
   }

}
  • Then, view the log on Remix console or on Etherscan.
Figure 13: Trace your contract flow
Figure 14: Trace your contract flow on the Test Network

The Remix Integrated Debugger

  • The debugger could debug transactions step-by-step, or show information on the log screen.
Figure 15: Debug your contract
Thong Nguyen
Thong Nguyen joined LogiGear in 2009 as a software developer and has been leading LogiGear’s researching as a senior technology manager. He was responsible for setting up the innovation of TestArchitect, a leading tool in test automation. Adapting to Blockchain technology since 2018, Thong started leading a MOWEDE development team to deliver Blockchain projects. Thong also teaches testing domain and cryptography at the University of Sciences and is actively involved in LogiGear's internal technical seminars.
Thong Nguyen
Thong Nguyen joined LogiGear in 2009 as a software developer and has been leading LogiGear’s researching as a senior technology manager. He was responsible for setting up the innovation of TestArchitect, a leading tool in test automation. Adapting to Blockchain technology since 2018, Thong started leading a MOWEDE development team to deliver Blockchain projects. Thong also teaches testing domain and cryptography at the University of Sciences and is actively involved in LogiGear's internal technical seminars.