How to Develop ERC20 Token Using Truffle

Navdeep Singh
Codezillas

--

In this blog post we will begin with a short introduction to the Truffle framework and create an ERC20-compatible token using it.

Truffle for Ethereum

Truffle is a framework for development, testing, and asset pipelining for Ethereum. It also has an incorporated package specification using the ERC190 standard. ERC190 is a request for comments, which lays down a set of rules about package specifications for Ethereum smart contracts.

Asset pipelining for Ethereum is a concept that has been borrowed from Ruby on Rails. This feature provides a framework to concatenate, minify, or compress JavaScript, Solidity, and CSS assets in the required sequence of execution. This feature helps the developer to create compressed and pre-processed library-like modules that can be reused by other coding modules, which helps in improving productivity.

Working on a Demo

Step 1: We will start by installing the Truffle framework. The installation is pretty straightforward and can be accomplished in the Git Bash window with the following command:

$ npm install -g truffle
Installation inside a new folder named Truffle

Step 2: After the installation, we need to initiate the framework, which can be done as shown in the next figure.

If you run into any trouble during installation or initiation, please refer to the Ethereum stack exchange or Truffle documentation for the known troubleshooting issues.

It is to be noted that, to initiate Truffle for a new project, we must always use the Truffle init command in a new folder with a custom project name. We cannot initiate Truffle in a project folder where the Truffle framework is already initiated, as this then throws an exception.

This is how it works:

Initiation of Truffle framework

Step 3: Let’s now create a simple ERC20-compatible token. For this, we create a new folder, ERC20_token, inside the private-truffle-project folder and write a solidity file, ScratchToken.sol, inside it.

We place the solidity file, along with the Migration.sol file, inside the contract folder.

Now, we open another Git Bash window and type in testrpc to start the testnet :

Starting testrpc

Step 4: In the old Git Bash window, we change the truffle.js file to point to our localhost:

Truffle.js configuration changes

Step 5: Compile the .sol files, as shown in the next figure; they can now be deployed using the command truffle.cmd deploy in the testnet:

Compiling ScratchToken.sol

This is how we deploy a basic ERC20-compliant token using the Truffle framework. We can add more functionality for incorporating other ERC20-compliant functions that are required for a full-fledged token with security enhancements.

Functions and more info

An ERC20-compliant token must have the following list of functions with the same naming convention:

totalSupply (): This function has no arguments and gets back the total supply of tokens that is in circulation.

For example, if we create an ERC20 token that imitates the bitcoin ideology, then the totalSupply() would fetch 21 million as a number,

balanceOf (owner): This function has an argument inside the parenthesis, which is the address of the owner.

Hence, this balanceOf function needs address of the owner to return back the balance value.

transfer (to, value): This function has two arguments, namely, the address of the to, that is, the recipient’s address, and the value of how many tokens needs to be sent to that address.

transferFrom (from,to,value): This function has three arguments, namely, the sender’s address, the recipient’s address, and the value of the token that needs to be transferred.

approve (spender,value): This function empowers the owner to approve a third-party entity to spend tokens up to a designated maximum value

allowance (owner, spender): This function allows the owner to empower a third-party entity to spend tokens on their behalf, without enforcing any spending limit.

In addition to the previous functions, such a token has two events:

event Transfer(from, to, tokens)

event Approval(tokenOwner, spender, tokens)

For example, if there is a token that has a totalSupplyInCirculation() function, which has the same functionality as totalSupply(), the token is ERC20-non-compliant, because the function name has changed.

Events, on the other hand, are broadcast throughout the network. For example, when a transfer occurs, a transfer event is broadcast throughout the network so that the network is aware of the transaction and the blocks are updated accordingly in the blockchain. Approval events are also broadcast in a similar manner to make the network aware of the change of authority of the spender’s address.

Ethereum Smart Contract Development

If you found this article an interesting read, you can try Ethereum Smart Contract Development to Become an Ethereum Blockchain developer using a blend of concepts and hands-on implementations. This book is your guide to blockchain and Ethereum smart contract development.

Word of Caution

It is advised to store crypto tokens in a hardware wallet like the one shown in the image,

Storage Carrying Case for KeepKey Simple Cryptocurrency BTC Bitcoin Wallet Hardware by Aenllosi (Black)

For other updates you can follow me on Twitter on my twitter handle @NavRudraSambyal

Thanks for reading, please share it if you found it useful 🙂

--

--

Navdeep Singh
Codezillas

Author of Reactive programming with Swift, Engineering Manager — Exploring possibilities with new Tech.