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

ยท

What is Ethereum?

Ethereum is an open-source, public blockchain platform with smart contract functionality. Its native cryptocurrency, Ether (ETH), powers a decentralized virtual machine (Ethereum Virtual Machine) that processes peer-to-peer contracts.

First conceptualized in 2013-2014 by programmer Vitalik Buterin, Ethereum was designed as a "next-generation cryptocurrency and decentralized application platform." Development began after a 2014 ICO crowdfunding campaign. Today, Ether is the second-largest cryptocurrency by market capitalization, trailing only Bitcoin.

Key Components of Ethereum Blockchain

  1. Data Storage
    Every transaction on the network is stored permanently on the immutable blockchain. Contract deployments and function executions are recorded as public transactions verifiable by anyone.
  2. Code Execution
    Developers write logic in Solidity (or other languages), compiling it into Ethereum bytecode for deployment. The blockchain stores and executes this code via the Ethereum Virtual Machine (EVM).

Prerequisites for Ethereum Development

Before diving in, you should understand:

๐Ÿ‘‰ Get started with Web3.js, the essential JavaScript library for building DApps (Decentralized Applications) that interact with Ethereum.

Example: Building a Voting DApp

We'll create a decentralized voting application where:

This demonstrates core Ethereum functionalities like contract deployment and interaction.

Setting Up Your Development Environment

Linux (Ubuntu 16.04 Example)

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

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 Contract Features

Compiling the Contract

Use Node.js console to compile:

> 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)

Critical outputs:

  1. Bytecode: The deployable EVM code (compiledCode.contracts[':Voting'].bytecode)
  2. ABI: Application Binary Interface defining contract methods (compiledCode.contracts[':Voting'].interface)

๐Ÿ‘‰ Master Ethereum development with advanced smart contract patterns and optimization techniques.

FAQ

What's the difference between Ethereum and Bitcoin?

While both use blockchain technology, Bitcoin is primarily a digital currency, whereas Ethereum is a programmable platform for building decentralized applications.

Why use Solidity for smart contracts?

Solidity is specifically designed for Ethereum with syntax similar to JavaScript, making it accessible for web developers while providing blockchain-specific features.

How much Ether do I need to start developing?

Test environments like Ganache provide free test Ether. For mainnet deployment, small amounts (often < $10 worth) suffice for initial contract deployments.

Can I update a deployed smart contract?

No. Deployed contracts are immutable, which is why thorough testing is crucial. Upgrades require deploying new contract instances.

What's Gas in Ethereum?

Gas measures computational effort for transactions. Users pay Ether to cover gas costs, preventing infinite loops and spam on the network.