Ethereum Tutorial: Setting Up the Environment and Writing/Compiling a Smart Contract

·

What is Ethereum?

Ethereum is an open-source, public blockchain platform with smart contract functionality. It provides a decentralized virtual machine (the Ethereum Virtual Machine or EVM) that processes peer-to-peer contracts using its native cryptocurrency, Ether (ETH).

The concept of Ethereum was first proposed between 2013-2014 by programmer Vitalik Buterin, inspired by Bitcoin. It was developed as "the next-generation cryptocurrency and decentralized application platform" through an ICO crowdfunding campaign. Currently, Ether is the second-largest cryptocurrency by market capitalization,仅次于 Bitcoin.

What is the Ethereum Blockchain?

The Ethereum blockchain has two main components:

  1. Data Storage: Every transaction in the network is stored on the blockchain. When you deploy a contract, it's recorded as a transaction. When you execute contract functions, those are also recorded as separate transactions. All transactions are public and verifiable by anyone. This data is immutable. To ensure all nodes maintain identical copies of the data and prevent invalid data from being written, Ethereum uses a proof-of-work algorithm to secure the network.
  2. Code: Ethereum allows you to write logic/application code (smart contracts) using Solidity. The Solidity compiler compiles this code into Ethereum bytecode, which gets deployed to the blockchain. While other languages can write contracts, Solidity remains the most popular and beginner-friendly choice.

In simple terms, the Ethereum blockchain stores both data and code, executing the latter within the EVM (Ethereum Virtual Machine).

Prerequisites for Ethereum Development

To begin Ethereum development, you should have basic knowledge of:

For building decentralized applications (DApps), Ethereum offers web3.js, a convenient JavaScript library that can be integrated into frameworks like React, Angular, or Vue.

Example: An Ethereum Voting Application

In this tutorial, we'll build a simple decentralized voting application. A DApp exists not just on one centralized server but across hundreds of computers, making it nearly impossible to crash. You'll create a voting app where you can:

You'll learn how to write a voting contract, deploy it to the blockchain, and interact with it. Essentially, a blockchain functions like a distributed database that maintains a growing list of records. If you're familiar with relational databases, imagine processing data in batches (e.g., 100 rows per batch) and linking these batches together—this forms a blockchain!

Setting Up the Ethereum Development Environment

Linux (Ubuntu 16.04)

$ sudo apt-get update
$ curl -sL https://deb.nodesource.com/setup_7.x -o nodesource_setup.sh
$ sudo bash nodesource_setup.sh
$ sudo apt-get install nodejs
$ node --version
v7.4.0
$ npm --version
4.0.5
$ mkdir -p ethereum_voting_dapp/chapter1
$ cd ethereum_voting_dapp/chapter1
$ npm install ganache-cli [email protected] solc
$ node_modules/.bin/ganache-cli

macOS

$ brew update
$ brew install nodejs
$ node --version
v7.10.0
$ npm --version
4.2.0
$ mkdir -p ethereum_voting_dapp/chapter1
$ cd ethereum_voting_dapp/chapter1
$ npm install ganache-cli [email protected] solc
$ node_modules/.bin/ganache-cli

Windows

  1. Install Visual Studio Community Edition (ensure Visual C++ is included).
  2. Install Windows SDK.
  3. Install Python 2.7 and add it to PATH.
  4. Install Git and add it to PATH.
  5. Install OpenSSL (full version only).
  6. Install Node.js v8.1.2.
  7. Run: npm install ganache-cli [email protected] solc.

Writing a Solidity Contract

Here’s a Voting contract written in Solidity:

pragma solidity ^0.4.18;
contract Voting {
    mapping (bytes32 => uint8) public votesReceived;
    bytes32[] public candidateList;

    function Voting(bytes32[] candidateNames) public {
        candidateList = candidateNames;
    }

    function totalVotesFor(bytes32 candidate) view public returns (uint8) {
        require(validCandidate(candidate));
        return votesReceived[candidate];
    }

    function voteForCandidate(bytes32 candidate) public {
        require(validCandidate(candidate));
        votesReceived[candidate] += 1;
    }

    function validCandidate(bytes32 candidate) view public returns (bool) {
        for(uint i = 0; i < candidateList.length; i++) {
            if (candidateList[i] == candidate) {
                return true;
            }
        }
        return false;
    }
}

Save this as Voting.sol.

Compiling the Smart Contract

To compile the contract, use the Solidity compiler (solc):

> Web3 = require('web3')
> web3 = new Web3(new Web3.providers.HttpProvider("http://localhost:8545"));
> web3.eth.accounts
> code = fs.readFileSync('Voting.sol').toString()
> solc = require('solc')
> compiledCode = solc.compile(code)

Key outputs:

  1. compiledCode.contracts[':Voting'].bytecode: The bytecode to deploy to the blockchain.
  2. compiledCode.contracts[':Voting'].interface: The ABI definition, which describes the contract's methods.

👉 Learn more about Ethereum development

FAQ

What is the difference between Ethereum and Bitcoin?

Ethereum is a programmable blockchain that supports smart contracts, while Bitcoin is primarily a digital currency.

Why use Solidity for smart contracts?

Solidity is the most widely used language for Ethereum smart contracts due to its simplicity and extensive documentation.

Can I update a deployed smart contract?

No, deployed contracts are immutable. Updates require deploying a new instance.

👉 Explore advanced Ethereum tutorials