Getting Started with Ethereum Smart Contract Development

In this tutorial, you will get you will get a taste with developing with Ethereum Smart Contract.

An Ethereum contract consists of a code string and data on a specific account address of an Ethereum blockchain. Contract accounts not only can communicate with each other, but they can also execute almost of all of the Turing Complete computations available now. The contract code on an Ethereum blockchain is in a specific binary form, which is known as Ethereum Virtual Machine (EVM) binary code.

However, contracts are usually developed and written in some sort of advanced language, which is translated into binary code and uploaded to the blockchain. Existing Ethereum advanced development languages are JavaScript-like Solidity, Python-like Serpent, and Lisp-like LLL. This article will be using the first of these language, Solidity.

Solidity is an object-oriented advanced Ethereum development language. Influenced by C++, Python, and JavaScript, it supports static types, inheritance, class libraries, and complex custom features. A typical contract development process includes writing, compiling, publishing, and calling.

The Eth Command Line Tool Geth

The Ethereum network command line client provides the Go-based Geth and C++-based Eth command-line tools. Geth is mainly used for Web development and Dapp front-end construction, while Eth is mainly used for GPU mining. But besides these differences, the two functions are almost identical. This article will use Geth because it is both easy-to-use and relatively widely used.

To begin this tutorial, let's first install and start ethereum. You can install ethereum using with the following actions and using brew with the following command.

brew tap ethereum/ethereum
brew install ethereum

Then, run ethereum with following command:

geth console

By default, you will be connected to the Ethereum main network. The main network is slow and expensive, and transfers, the deployment of contracts, and call to contracts consume Ether (ETH), so it cannot be directly used for development. As a result, you'll want to build a private test network. A private test network needs to be set up, of which you are the only member. You are responsible for all the tasks, such as block generation, transaction verification, and smart contract execution. This helps to develop contracts at a low cost, simply and quickly.

You can use genesis.json to initialize the Genesis block, and set the datadir Directory:

From the above we can see that two contracts, Greeter and Mortal, exist. Greeter inherits from Mortal. In addition to the Kill function of Mortal, it has also added Greet function. It should be noted that the contract in Ethereum is immortal and uncontrolled by default. That is, once the contract is deployed on the blockchain, even the author of the contract no longer has the privilege to control the contract. The contract is defined by the author, but the execution of the contract and the services it provides are supported by the entire Ethereum network. As long as the entire network still exists, the contract will continue to exist and run. The contract will not disappear unless a self-destruction program is written and triggered.

The next step is to compile. Currently, the most convenient way to develop a contract is to use online IDE REMIX. After you paste in the code, it is automatically compiled as follows:

Pay attention to the ABI, ByteCode, and other key information in the Details of the compilation results on the right. The ABI (Application Binary Interface) is as follows:

This indicates that the contract creation transaction has been submitted, but the blockchain has not yet been mined at this time, so the transaction has not been packaged into the blockchain, and the entire blockchain network cannot be seen. Here, you must manually start mining again (mining is carried out continuously on the main network without stopping. The private test network here only has one node. For a clear demonstration, you need to manually start or stop mining each time). It can be seen in a few seconds:

This call process does not change the blockchain state, so it returns immediately and does not consume gas.

The last step is to clean up. The contract code will be permanently stored on the blockchain unless it is self-destroyed. Therefore, abandoned contracts need to be destroyed, which requires sending transactions to the blockchain and paying for execution. For example:

Summary

Through learning about the general process of Ethereum contract development in this blog, you now have a understanding of the general process of Ethereum contracts, and share a basic understanding. For more in-depth tutorials, see Ethereum official documentation.