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.
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.
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
curl -s -o - https://storage.googleapis.com/golang | egrep -o "[^>]*linux-amd64.tar.gz[^<]*" | sort -r | head -1
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
git clone https://github.com/ethereum/go-ethereum cd go-ethereum PATH=$PATH:/usr/local/go/bin make geth
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.shback to top
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.
/usr/src/go-ethereum/build/bin/geth --testnet --datadir ~/eth-testnet
/usr/src/go-ethereum/build/bin/geth --testnet --datadir ~/eth-testnet account new # set a password for your account
/usr/src/go-ethereum/build/bin/geth --testnet --datadir ~/eth-testnet --mine
/usr/src/go-ethereum/build/bin/geth --testnet --datadir ~/eth-testnet --mine console
/usr/src/go-ethereum/build/bin/geth --testnet --datadir ~/eth-testnet --rpc --rpccorsdomain "http://localhost:8000" --rpcapi eth,web3,personal
personal.newAccount()
web3.fromWei(eth.getBalance(eth.coinbase), 'ether')
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);
eth.getTransaction('0x1786ffd39975833bf1ed2f09657d1585cb3f2cc73f60ac04277eef1');
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
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.
git clone https://github.com/tomlion/vim-solidity.git cd vim-solidity cp -r ftdetect/ ftplugin/ indent/ syntax/ /usr/share/vim/vim80/
pragma solidity ^0.4.13; contract HelloWorldContract { bytes32 public name = 'Hello World'; }
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
It is important to compile the solidity code to generate the ABI and BYTE CODE used during contract creation in the Ethereum network.
/usr/src/solidity/build/solc/solc --abi HelloWorldContract.sol
/usr/src/solidity/build/solc/solc --bin HelloWorldContract.solback to top
# 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
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