Creating a currency through ERC20

Jerry Qi
6 min readMar 6, 2021

The power of ERC20 smart contracts allows for representation for many different objects. But. Before getting into ERC20, I would like to reintroduce myself: Hey, I’m Jerry, a 15 year old who is passionate about upcoming technologies and solving real world problems with these tech.

Disclaimer
Some degree of understanding of object oriented programming (classes) would be very beneficial understanding the process (ex. Java, Python, Javascript, etc)

Introductions

Source: Ethereum.org

First thing that we need to know: Money, or goods, are not a definitive physical objects that really does something. If enough people believes in it, money and value comes out of nowhere. This is the concept with cryptocurrencies (ERC20), especially bitcoin. This is because bitcoin is a straight up virtual currency, and as long as people believe that it is “digital gold” there are a sense of value and demand in the currency [Digital Gold: Bitcoin has a mining limit].

Although smart contracts are aimed to create many different things: virtual law contracts and others, smart contracts can also be utilized to create tokens. And, later, allow the tokens to become a fungible and tradable good/currency.

Now. Lets go through the currency development process together…

ERC20 Development Tools

NodeJS

NodeJS is a useful javascript dependency for testing and developing the smart contract through command-line applications.

Truffle Framework

The framework gives us a template of tools for the development of the smart contract in ERC20 form and allow generating files to be a whole lot easier, for testing and deploying, also developing client side decentralized apps. Note that files would be in solidity, ethereum’s native smart contract language.

Ganache

Ganache is a local blockchain useful for testing which runs on your computer’s memory.

Metamask for google chrome

This is a browser extension working on chrome, firefox and other web browsers. Because of the need to connect to the front end token sale, we will have to use metamask on browser.

Token contract

The first part of creating a ERC20 cryptocurrency is creating the token contract: a smart contract class specifying details for the ERC20 token to be created (similar with objects in python and java), and allows the eth networks to manage the tokens on the network once deployed.

Above is the first part of my token contract, all the global variables are declared for access
Through the constructor of the token contract, the initial supply of dirtCoin

The second task on hand is now to type up the different required methods that are specified on the ERC20 standard. They are the required baseline to create a basic ERC20 token. I also talk about the types of methods in the token contract in this presentation that I did about smart contracts, check it out!

The interface for the ERC20 token contract to be implemented

The getters all returns tangible information. The methods, totalSupply, balanceOf, and allowance are all getters in the sense that it would return some information about either the token or a user’s wallet account.

  • totalSupply method returns total tokens that are still “not burned”(coin burning destroys tokens).
  • balanceOf method would return the balance of the wallet account inputted in the parameters
  • allowance method involves a special feature that erc20 tokens have. The ERC-20 standard also allows an “owner” to give an allowance to a “spender” to be able to retrieve tokens from it. This getter returns the remaining number of tokens that the spender will be allowed to spend on behalf of owner.

The functions, are the methods which initiates transactions between users. Which include the transfer, transferFrom, approve methods. From which in the parameters are addresses of wallets. Calling these functions involve the change in financial assets of token account wallets.

  • tranfer method, transfers the an amount of tokens from the current wallet to the other wallet account (recipient) specified in the parameters of the method
  • transferFrom method is the same function as the transfer method, but specifying both sender and recipient plus the amount in the parameters
  • approve method sets the “allowance” that the spender is allowed to transfer from the function caller: “owner”’s balance. This function is related to the allowance getter above

The Events: transfer and Approval, allow clients and dapps to monitor what is happening inside the smart contract, it is also used to record the transactions that happen.

  • Transfer event is called whenever change in financial assets occur in the network (ex. a sends money to b, tranfer event is called)
  • Approval event is called whenever a person grants another person permission to spend some amount of their tokens (ex. a allows b to access/spend 10 of their tokens)

Excerpts from my implementation of the methods:

Events implementation
Transfer method
Approve method
TransferFrom method

ICO contract development

ICO stands for initial coin offering. The second large section of creating tokens. It works together with the token smart contract to distribute tokens through a front-end ICO website [more detail: ICO]. This distribution is through a format where the users will be able to purchase the tokens through Ether.

The dirtCoin ICO token sale frontend

Usually, the ICO token sale smart contract doesn’t have a specific template, but there will be some methods that should be implemented.

The dirtCoinSale interface

Compared to the token contract, the ICO smart contract is quite a bit simpler with only implementable methods.

  • Sell event is called whenever an amount of tokens are sold from the smart contract. Information of buyer and the amount of tokens are emitted.
  • buyTokens method informs the token sale contract of how many tokens are bought from the token sale contract
  • endSale method, when called, ends the ICO token sale and returns the tokens stored in the ICO token sale contract back to the admin account

Here is the version that I implemented:

Implementation for buytokens and endsale

These methods act together with the front-end website, which would receive user input, then call the ICO token sale smart contract of some method, which then would result in the ICO token sale smart contract calling methods of the token contract itself to initiate transactions between user wallets.

Conclusion

The three parts: token contract, ICO token sale contract, and frontend website, works together through their different roles in order to create a virtual currency.

The next steps in initializing an actual token would be to implement extra methods (even incorporate multiple smart contracts!) for originality rather than the ones just in the ERC20 standard.
Be Unique! Then, your device will have to become a node of the Ethereum Mainnet, but in order to deploy contracts onto it there will be a calculated fee.

Check out the completed project here.

Thanks for reading about my ERC20 token project!
Once again, I’m Jerry, a 15 year old passionate to solve problems and improve the world with upcoming technologies. If you want to connect or just chat, feel free to reach out on Linkedin or email. Also, Check out my newsletters!

See you in the next article!

--

--

Jerry Qi

Grade 11 Student at tks.world. Passionate and write about emerging tech.