Ethereum smart contracts in a nutshell for hackers

Learn how to program Ethereum smart contracts using solidity language

This article will guide you through the steps of setting up your own Ethereum development environment and basic smart contract life cycle on Ethereum network.

As the article title states, this will be done in a nutshell and to support the process, Debian GNU/Linux version 9 (stretch) and CLI interface will be used.

All examples will be performed against Ethereum testnet.

Installing requirements (golang, cmake, geth, solc)

Before start, make sure to have all software and dependencies installed, the list bellow will be used in this guide.

Compile the latest version of each software listed, the recommended location is under /usr/src. Golang is pre compiled.

Some of the commands used during installation need to be performed as super user, you can use sudo in front of them.

golang

curl -O https://storage.googleapis.com/golang/go1.8.linux-amd64.tar.gz
tar -C /usr/local -zxf go1.8.linux-amd64.tar.gz
mkdir -p ~/go
echo "export GOPATH=$HOME/go" >> ~/.bashrc
echo "export PATH=$PATH:$HOME/go/bin:/usr/local/go/bin" >> ~/.bashrc
source ~/.bashrc
discover golang latest version for linux amd64
curl -s -o - https://storage.googleapis.com/golang | egrep -o "[^>]*linux-amd64.tar.gz[^<]*" | sort -r | head -1

cmake

curl -O https://cmake.org/files/v3.9/cmake-3.9.0.tar.gz
tar -zxf cmake-3.9.0.tar.gz 
cd cmake-3.9.0
./bootstrap && make && make install

geth

git clone https://github.com/ethereum/go-ethereum
cd go-ethereum
PATH=$PATH:/usr/local/go/bin make geth

solidity and solc

git clone --recursive https://github.com/ethereum/solidity.git
cd solidity
# here you can checkout the latest tag
# so the compiled version is valid on etherescan
git checkout v0.4.13
#./scripts/install_deps.sh
# install_deps did not work for Debian9 but line bellow did
apt-get -y install build-essential g++ gcc libboost-all-dev unzip
# build script was unable to download jsoncpp-1.7.7
# download manually
mkdir -p deps/downloads 
curl -L -o deps/downloads/jsoncpp-1.7.7.tar.gz https://github.com/open-source-parsers/jsoncpp/archive/1.7.7.tar.gz
./scripts/build.sh
back to top

Using geth and geth js console

Geth or Go Ethereum is the official implementation of the Ethereum protocol, it is writen in go and provides a powerful javascript console.

Using the console, you can interact with the Ethereum network, by for example checking account balance or sending a transaction.

While running, geth will store information about the block inside the datadir, use one datadir per network is recommendable.

start node and blockchain syncing

/usr/src/go-ethereum/build/bin/geth --testnet --datadir ~/eth-testnet

create first account (wallet)

/usr/src/go-ethereum/build/bin/geth --testnet --datadir ~/eth-testnet account new
# set a password for your account

mine ethers in testnet

/usr/src/go-ethereum/build/bin/geth --testnet --datadir ~/eth-testnet --mine

syncing, mining with access to console

/usr/src/go-ethereum/build/bin/geth --testnet --datadir ~/eth-testnet --mine console

serve dapp running on localhost:8000 via RPC

/usr/src/go-ethereum/build/bin/geth --testnet --datadir ~/eth-testnet --rpc --rpccorsdomain "http://localhost:8000" --rpcapi eth,web3,personal

js console create new account

personal.newAccount()

js console check main account balance

web3.fromWei(eth.getBalance(eth.coinbase), 'ether')

js console send transaction

var from = eth.accounts[0];
var to = '0x92b7f127ca5b074274132012cd5f5508bf392643';
var amount = web3.toWei(30, 'ether');
web3.personal.unlockAccount(from, 'YOUR_ACCOUNT_PASSWORD');
eth.sendTransaction({from: from, to: to, value: amount});
web3.personal.lockAccount(from);

js console transaction info

eth.getTransaction('0x1786ffd39975833bf1ed2f09657d1585cb3f2cc73f60ac04277eef1');

js console deploy simple contract

var abi = ABI_JSON_GOES_HERE;
var code = "0xBYTE_CODE_GOES_HERE";
web3.eth.contract(abi).new({ from: web3.eth.accounts[0], data: code, gas: 1000000 });
back to top

Writing solidity smart contract

Smart contracts are written using solidity which is a contract oriented language designed to target the Ethereum Virtual Machine.

The contract code needs to be compiled using solidity compiler or solc to generate the ABI and BYTE CODE used to deploy the contract on the network.

Install solidity syntax for vim

git clone https://github.com/tomlion/vim-solidity.git
cd vim-solidity
cp -r ftdetect/ ftplugin/ indent/ syntax/ /usr/share/vim/vim80/

Hello World contract

pragma solidity ^0.4.13;

contract HelloWorldContract {
    bytes32 public name = 'Hello World';
}

Simple owned contract

pragma solidity ^0.4.13;

contract OwnedContract {
    bytes32 public name = 'Owned Contract';
    address public owner;

    function OwnedContract() {
        owner = msg.sender;
    }

    modifier isOwner() {
        require(msg.sender == owner);
        _;
    }

    function OwnedFunction() isOwner {
        // do something only owner can do
        // the owner is the contract creator account
    }
}
back to top

Compiling contract source code

It is important to compile the solidity code to generate the ABI and BYTE CODE used during contract creation in the Ethereum network.

Generate the ABI

/usr/src/solidity/build/solc/solc --abi HelloWorldContract.sol

Generate the BYTE CODE

/usr/src/solidity/build/solc/solc --bin HelloWorldContract.sol
back to top

Testing on Test Network

Deploy contract via geth js console

# open geth console
/usr/src/go-ethereum/build/bin/geth --testnet --datadir ~/eth-testnet console

# in the console create the contract using ABI and BYTE CODE result from compilation
var abi = [{"constant":true,"inputs":[],"name":"name","outputs":[{"name":"","type":"bytes32"}],"payable":false,"type":"function"}];
var code = '0x60606040527f48656c6c6f20576f726c64000000000000000000000000000000000000000000600090600019169055341561003957600080fd5b5b609d806100486000396000f30060606040526000357c0100000000000000000000000000000000000000000000000000000000900463ffffffff16806306fdde0314603d575b600080fd5b3415604757600080fd5b604d606b565b60405180826000191660001916815260200191505060405180910390f35b600054815600a165627a7a72305820f4c510e24a238337d5334b5b38a44e88ea53ef40f26aeb96eba4609cb72827cd0029';
web3.personal.unlockAccount(eth.accounts[0], 'YOUR_ACCOUNT_PASSWORD');
var contract = web3.eth.contract(abi).new({ from: eth.accounts[0], data: code, gas: 1000000 });
web3.personal.lockAccount(eth.accounts[0]);

# call the method in the contract
contract.name()
back to top

Example lottery and demo

To finalize the article, here goes the link to a working lottery contract example and a 100% working dapp using metamask on Ethereum main net.

back to top