i Crypto! Creating My First ERC-20 Token · Dark Matter Industries

Crypto! Creating My First ERC-20 Token

What do you do when your government has locked you down due to the Pandemic? Well, you try to learn more about cryptocurrencies and Web 3.0…

I have been experimenting with Ethereum, trying my hand at solidity, and attempting to create my own token. Because who doesn’t want to have their own token?1

Infrastructure

First off, there is a whole bunch of infrastructure that needs to be installed and set up on your system (I tried this both on my local Mac and a VM on Google Cloud Platform running Debian):

  1. npm. Nodejs is the glue that binds everything together.
  2. solidity. solc is the solidity compiler. I initially installed it though brew on my Mac but had all sorts of problems. Install using npm instead per the documentation.
  3. Truffle. This is a god-send. It’s a suite of tools to make your life creating contracts much easier.
  4. Ganache. Allows you to set up a personal, local blockchain for testing purposes.
  5. OpenZeppelin. This is an ERC-20 compatible library which provides security features. You want to use this if you are going to deploy your token on a public chain.

OpenZeppelin in particular contains a Standard Token that should provide basic but robust security. You install it with:

$ npm install openzeppelin-solidity

The contract is located at ~/node_modules/openzeppelin-solidity/contracts/token/ERC20/StandardToken.sol

Now set up a project directory and initialise.

$ mkdir BigDawgToken && cd $_
$ truffle init

BigDawgToken

Now that we have initialised truffle in our BigDawgToken directory, it should have the following structure:

$ tree
.
├── contracts
│   └── Migrations.sol
├── migrations
│   └── 1_initial_migration.js
├── test
└── truffle-config.js

3 directories, 3 files

At this point if you want to deploy the token on public Ethereum test networks you need to take care to include your test private keys. This is done by modifying truffle-config.js to require a file containing your keys, which could be placed at the root level. It’s actually quite involved but since we are only going to deploy it on a Ganache instance, we can ignore all that.

Next, in contracts I created this ERC20 token:

pragma solidity ^0.6.2;

import 'openzeppelin-solidity/contracts/token/ERC20/StandardToken.sol';

contract BigDawgToken is StandardToken {
	string public constant name = 'Big Dawg Token';
	string public constant symbol = 'BDT';
	uint8 public constant decimals = 2;
	uint constant _initial_supply = 10000000;

	function BigDawgToken() public {
		totalSupply = _initial_supply;
		balances[msg.sender] = _initial_supply;
		emit Transfer(address(0), msg.sender, _initial_supply);
	}
}

Here we defined the token name and symbol and set the initial supply to 10 million tokens. With a 2 decimal fraction (that is the tokens can be measured up to 2 decimal places), we essentially have 1 billion units.

In the constructor function below we allocate all of the initial supply to the sender account.

Compile

Now to compile the contract.

First we need to check to see which version of solc truffle has been set to use:

$ truffle version
Truffle v5.1.58 (core: 5.1.58)
Solidity v0.5.16 (solc-js)
Node v10.23.0
Web3.js v1.2.9

The default is set to 0.5.16 but I want to use 0.6.2, so we need to modify the truffle-config.js. If you look at the file, you want to change the version on line 85 and uncomment.

82   // Configure your compilers
83   compilers: {
84     solc: {
85       // version: "0.5.1",    // Fetch exact version from solc-bin (default: truffle's version)
86       // docker: true,        // Use "0.5.1" you've installed locally with docker (default: false)
87       // settings: {          // See the solidity docs for advice about optimization and evmVersion
88       //  optimizer: {
89       //    enabled: false,

Now check:

$ truffle version      
Truffle v5.1.58 (core: 5.1.58)
Solidity - 0.6.2 (solc-js)
Node v10.23.0
Web3.js v1.2.9

And compile:

$ truffle compile

Compiling your contracts...
===========================
✔ Fetching solc version list from solc-bin. Attempt #1
✔ Downloading compiler. Attempt #1.
> Compiling ./contracts/Migrations.sol
> Compiling ./contracts/BigDawgToken.sol
> Compiling openzeppelin-solidity/contracts/math/SafeMath.sol
> Compiling openzeppelin-solidity/contracts/token/ERC20/ERC20.sol
> Compiling openzeppelin-solidity/contracts/token/ERC20/IERC20.sol
> Compiling openzeppelin-solidity/contracts/token/ERC20/StandardToken.sol
> Compiling openzeppelin-solidity/contracts/utils/Context.sol
> Artifacts written to /Users/<me>/Documents/ethereum/BigDawgToken/build/contracts
> Compiled successfully using:
   - solc: 0.6.2+commit.bacdbe57.Emscripten.clang

This creates several files (artefacts) in ./build/contracts/.

Deploy

Since we are deploying onto a local Ganache blockchain for testing this is very easy. The command to deploy on a public test network or on the main chain itself is similar, but note as mentioned above, you need to take care of authenticating yourself and it requires adding code to the truffle-config.js and adding another file with your keys.

But for now, we just need to make sure the Ganache chain is running. I use the GUI on the Mac, but on my Linux instances I needed access through a cli tool (npm install -g gnache-cli).

Then to deploy we “migrate” the contract over to the chain:

$ truffle migrate --network ganache
Using network 'ganache'.

Running migration: 1_initial_migration.js
  Deploying Migrations...
  ... 0xb2e90a056dc6ad8e654683d31bee32fa3796a03b89df6760ec1dbcdba53084eb
  Migrations: 0x8cdaf0cd259887258bc13a92cf4ad292698644c0
Saving successful migration to network...
  ... 0xd7bc86d56dc6ad9a3988f1c1eabce403a1b5d570340a3a9cdba53a472ee8c956
Saving artifacts...
Running migration: 2_deploy_contracts.js
  Deploying BigDawgToken...
  ... 0xbe9290d59678b412e60ed6aef70340a34f4ad2977cfb2076b9b8ad415c5dc9f0
  METoken: 0x345ca3e01678b412a488057592ee47305d9b3e10
Saving successful migration to network...
  ... 0xf36163615f41ef7ed8f4a8f192149a0bf633fe1a23678b4129344c43dc7bdda0
Saving artifacts...

That’s it. Now you can inspect the transactions in Ganache.

OK, now back to trying to source some toilet rolls…


  1. To learn, I deployed my token on a local Ganache blockchain and stopped short of deploying on the main chain because this code is so primitive that I have not thought through safety and security. Also, I did not want to pay any gas fees. ↩︎