ERC20 Tokens, Oracles, ICOs, and More!

Jacob Bills

April 04, 2020

In this tutorial, learn how to run a private ethereum blockchain and deploy your very own ERC20 token. We'll also cover getting real world data onto the blockchain and what oracles are. Finally we'll discuss ICO (initial coin offering) basics. We'll merely be scratching the surface of all the complexities involved in the blockchain world and is for beginners just starting out with blockchain development. I would encourage you to continue with your research once you have completed this tutorial


In this blog


Setup ethereum

You can get started running an ethereum node by following the instructions below. Here is the official documentation for what we will be installing.

For CentOS
#!/bin/bash
# download geth and tools
yum install wget -y
wget https://gethstore.blob.core.windows.net/builds/geth-alltools-linux-amd64-1.9.12-b6f1c8dc.tar.gz
mv geth-alltools-linux-amd64-1.9.12-b6f1c8dc /opt
ln -sf /opt/geth-alltools-linux-amd64-1.9.12-b6f1c8dc/geth /etc/alternatives/geth
ln -sf /etc/alternatives/geth /bin/geth
ln -sf /opt/geth-alltools-linux-amd64-1.9.12-b6f1c8dc/bootnode /etc/alternatives/bootnode
ln -sf /etc/alternatives/bootnode /bin/bootnode
rm -f geth-alltools-linux-amd64-1.9.12-b6f1c8dc.tar.gz
firewall-cmd --permanent --add-port=8545/tcp
firewall-cmd --reload
For iMac/OSX
brew tap ethereum/ethereum
brew install ethereum

Run a private ethereum blockchain

1. Setup Bootnode
A bootnode acts as an entry point and helps nodes find other participating nodes in the network. First create a bootnode key and start the bootnode service.
#!/bin/bash
bootnode -genkey bootnode.key
bootnode -nodekey bootnode.key
Make a note of the enode:// uri, we will use this when starting up the first node.

2. Create account(s)
Next we will create some accounts that we can work with on the blockchain. We'll specify the the initial amount for these accounts in the genesis file which initializes the blockchain. I'm using ./datadir to store the contents of the blockchain but you can specify whichever directory you want, just make sure you are consistent with the directory you choose
#!/bin/bash
geth --datadir=./datadir account new
3. Create genesis file and initialize blockchain
The ethereum genesis file will initialize the blockchain. It is here you can set the initial difficulty, start block, gas limit, initial account balances from previous step, and features. In the genesis config you specify the various features which all nodes participating in the blockchain must conform to.

Create a file called genesis.json and add the following contents
Make sure to replace the account address with the address that was generated in the second step. To find the address look in the ./datadir/keystore/ UTC file which was generated during account creation
{
    "config": {
        "chainId": 15,
        "homesteadBlock": 0,
        "constantinopleBlock": 0,
        "byzantiumBlock": 0,
        "eip150Block": 0,
        "eip155Block": 0,
        "eip158Block": 0
    },
    "difficulty": "0x0400",
    "gasLimit": "0x1313131",
    "alloc": {
        "7aFfE50B138E14fafFfD9C5EEAE44e5c69da869a": { "balance": "0x1337000000000000000000" }
    }
}
Once you have created the genesis file initialize the blockchain
#!/bin/bash
geth --datadir ./datadir init genesis.json
4. Start the first ethereum node
Ensure you use the same value in the networkid parameter that you specified in the chainId field of the genesis file, replace datadir where you initialized the blockchain/created the accounts, and replace the enode uri with the uri from the first step. I wont go into full details of the other parameters since they are self explanatory
#!/bin/bash
geth --datadir ./datadir --networkid 15 --bootnodes enode://03c586bbee887bd99a1125d214cbce75d9933fa81005ec7b2fe1923bbdd2e9971e1842af9b72599b28fb43f404b455c114abc40867a0d0d81192b569c248a2bb@127.0.0.1:0?discport=30301 --rpc --rpcaddr 0.0.0.0 --rpcport 8545 --ws --wsaddr 0.0.0.0 --wsport 8546 --allow-insecure-unlock --wsorigins "*"  --rpccorsdomain "*"
5. Open the ethereum console and run a few commands to verify everything is working
Open the console
#!/bin/bash
geth --datadir=./datadir attach
Run a few commands.
> eth.accounts
["0x7affe50b138e14fafffd9c5eeae44e5c69da869a"]
> eth.getBalance(eth.accounts[0])
2.3229356729235784806170624e+25
The geth console will tab complete for you. Enter the following and use tab to get a list of their respective commands. (a full list of commands is here)

Deploy your first smart contract / ERC20 token

Solidity is the language in which ethereum smart contracts are developed. To keep things simple, we'll use the node package manager to install the solidity compiler and use truffle for compiling and deploying the contract to the blockchain
npm install -g solc
npm install -g truffle
1. Initialize truffle project
#!/bin/bash
mkdir MyERC20Token
cd MyERC20Token
truffle init
2. Create the contract / ERC20 Token
Create the file contracts/FixedSupplyToken.sol. For this tutorial I am using the code from bokkypoobah's github. At the time of this writing I used git commit id 97950a9e4915596d1ec00887c3c1812cfdb122a2
#!/bin/bash
wget -P contracts/ https://raw.githubusercontent.com/bokkypoobah/Tokens/master/contracts/FixedSupplyToken.sol
3. Create the migration
Create the file migrations/2_deploy_contracts.js
var ercContract = artifacts.require("./FixedSupplyToken.sol");
module.exports = function(deployer) {
 deployer.deploy(ercContract);
};
4. Deploy the contract
You'll need to have at least one ethereum node mining and unlock your ethereum account to deploy the contract. Open your geth console and run
> miner.start()
> personal.unlockAccount(eth.accounts[0])
Once your account is unlocked and you see blocks being mined succesfully, you can deploy the smart contract. You should see something like this before you deploy.


Update the truffle-config.js in the root of the MyERC20Token folder and configure the network settings for your ethereum node. If you have followed through this tutorial, uncomment the development section under networks
...
development: {
     host: "127.0.0.1",     // Localhost (default: none)
     port: 8545,            // Standard Ethereum port (default: none)
     network_id: "15",       // Any network (default: none)
},
...
Verify that truffle can succesfully connect to your ethereum node.
#!/bin/bash
truffle test
If everything checks out, compile and deploy the contract.
#!/bin/bash
truffle compile
truffle migrate

Interacting with smart contracts

We'll interact with the contract from the truffle console. Lets check the total supply, account balance, and transfer some tokens between accounts.
#!/bin/bash
truffle console
truffle(development)> FixedSupplyToken.deployed().then(function(instance){return instance.totalSupply();}).then(function(value){return value.toString(10)});
'1000000000000000000000000'
truffle(development)> FixedSupplyToken.deployed().then(function(instance){return instance.balanceOf("0x7affe50b138e14fafffd9c5eeae44e5c69da869a");}).then(function(value){return value.toString(10)});
'1000000000000000000000000'
truffle(development)> FixedSupplyToken.deployed().then(function(instance){return instance.transfer("0xde99a973229a0654e9e59d4f7b689afce811871e",10);}).then(function(value){return value.toString(10)});
'[object Object]'
truffle(development)> FixedSupplyToken.deployed().then(function(instance){return instance.balanceOf("0x7affe50b138e14fafffd9c5eeae44e5c69da869a");}).then(function(value){return value.toString(10)});
'999999999999999999999990'

An introduction to oracles

Oracles are what brings real world data onto the blockchain and come in many different shapes and sizes. For example, an oracle might be a service running on your computer, an individual, or even an entire organization. Typically organizations or groups of organizations will try to reach a consensus on the data before publishing it to the blockchain, these are known as consensus oracles. The data on the blockchain is only as good as the oracles that generate the data. So while the data on the blockchain data has integrity once its published, we must instill a sense of trust on the oracles generating that data. It's paradoxical to think that for a trustless blockchain system to operate it depends on a trusted data source. This problem is known as the oracle paradox and as of this writing there isn't a bullet proof solution that effectively addresses this. Consensys has been working towards a solution to this problem and you can find out more here. They are one of the many trying to solve this paradox. Many believe that once this problem is solved there will a breakthrough with massive scale adoption of blockchain technologies.

For beginners its typically fine to run an oracle from a single server. Just keep in mind that if that server is compromised then you and others may no longer want to trust the data being published.

Initial coin offering (ICO)

ICOs are one way of raising capital for your project or startup and there are a lot of factors to take into consideration before launch. When you start an ICO you are typically asking for investors to trade fiat, cryptocurrency, or other services in exchange for your coin. Your first order of business should be procuring a whitepaper describing what problem your coin solves and should be digestible to both tech and non-tech savvy audiences. You need to decide whether your coin offering will have a limited supply (think exclusivity) or if it can be minted in limitless amounts (which may/may not devalue your coin). Distribution method(s) and amounts should be well defined so everyone knows when and where the coins are going. When your ICO is ready, a good marketing or advertising campaign will really help your coin take off.

VLOG