Ethereum Tutorial: Setting Up Development Environment and Writing Your First Smart Contract

ยท

What is Ethereum?

Ethereum is an open-source, public blockchain platform with smart contract functionality. It uses its native cryptocurrency, Ether (ETH), to power decentralized applications (dApps) through the Ethereum Virtual Machine (EVM).

Key Features of Ethereum

Understanding Ethereum Blockchain

The Ethereum blockchain consists of two core components:

  1. Data Storage

    • Records all transactions permanently
    • Maintains transparent, verifiable history
    • Uses Proof-of-Work (PoW) consensus mechanism
  2. Code Execution

    • Smart contracts written in Solidity
    • Compiled to EVM bytecode
    • Executed across all network nodes

Prerequisites for Ethereum Development

Before starting, you should have basic knowledge of:

Recommended Tools

Example: Building a Voting dApp

We'll create a decentralized voting application that:

๐Ÿ‘‰ Learn more about blockchain applications

Setting Up 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
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
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
  2. Install Windows SDK
  3. Install Python 2.7
  4. Install Git
  5. Install OpenSSL
  6. Install Node v8.1.2
  7. Run: npm install ganache-cli [email protected] solc

Writing Your First Smart Contract

Here's our Voting.sol contract:

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;
    }
}

Key Components Explained

  1. Mapping: Stores vote counts per candidate
  2. Constructor: Initializes candidate list
  3. Methods: Vote counting and validation
  4. Visibility: Public vs private functions
  5. View Modifier: Marks read-only functions

Compiling the Smart Contract

  1. Run Node console
  2. Initialize Web3
  3. Compile contract
> Web3 = require('web3')
> web3 = new Web3(new Web3.providers.HttpProvider("http://localhost:8545"))
> code = fs.readFileSync('Voting.sol').toString()
> solc = require('solc')
> compiledCode = solc.compile(code)

Important Outputs

๐Ÿ‘‰ Explore more blockchain development tools

FAQ

What's the difference between Ethereum and Bitcoin?

While both use blockchain technology, Ethereum enables smart contracts and dApp development, whereas Bitcoin is primarily a digital currency.

How long does it take to deploy a smart contract?

Deployment time varies based on network congestion, but typically takes seconds to minutes.

Can I update a deployed smart contract?

No, smart contracts are immutable once deployed. You must deploy a new version.

What's Gas in Ethereum?

Gas refers to the computational fees required to execute operations on the Ethereum network.

How secure are smart contracts?

They're highly secure due to blockchain immutability, but vulnerabilities can exist in contract code.

Do I need Ether to develop Ethereum apps?

You only need Ether for deploying to mainnet. For development, testnets provide free Ether.