Anatomy Of An ERC20 Contract

by Tyler Farris

June 9th, 2022

What is ERC-20?

Ethereum Request for Comments 20 was proposed by Fabian Vogelsteller in November of 2015. It is the most used and developed smart contract standard on the Ethereum blockchain. It defines a set of rules that developers must adhere to for it to be an ERC20 token. This standard is only for fungible tokens, not NFTs, and can only be exchanged for other ERC20 tokens.

The Tools

Writing, testing, and deploying Ethereum smart contracts has never been easier with the help of tools and libraries like MetaMask, Remix IDE, and OpenZeppelin.

  • MetaMask serves as a crypto wallet for Ethereum based tokens such as ERC20 and ERC721. It connects with other developer tools and DAPPs like Remix IDE, making it ideal for our workflow.

  • Remix IDE is an in-browser IDE that's focused on smart contract development. It offers a rich selection of plugins for debugging and testing. And educational plugins, like LearnEth, for new software developers in the blockchain space.

  • OpenZeppelin supplies an open-source library of high-quality, audited, Ethereum smart contracts. You can import all the base contracts from their GitHub. You could write the contract by hand, but this is prone to errors and not recommended. Let's take advantage of the tools available to us!

The Contract

Let's look at the most basic implementation of an ERC20 contract:

Basic example of an ERC20 contract.

I know what you're thinking. “That's it?” Yep, that’s all we need to deploy our Kickstand contract, let's go through line by line:

  • line 1: This is the SPDX License ID, every contract should have this at the top of the file. Here, we are saying this file is under the MIT license.

  • line 2: The version pragma enables certain compilers and checks. Our contract, will not compile with versions earlier than 0.8.4 or any version starting with 0.9.0 (denoted by the ^).

  • line 4: We are importing the base ERC20 contract from Open Zeppelin. This gives us all the required functions and events that define an ERC20 contract.

  • line 6: If you are familiar with JavaScript or TypeScript classes, this should look familiar. Here, we are saying our contract Kickstand is (or extends) the ERC20 contract that we imported on line 4.

  • line 7: Much like classes, our Kickstand contract needs a constructor. Following this, we supply arguments to our base contract constructor, name, and symbol. The constructors execute once when the contract is deployed.

  • line 8: This function is minting our tokens. It takes two arguments. The first, msg.sender, is the address that deploys the contract and the second, is our contract's total amount of tokens. The equation says we want an initial supply of 10,000 tokens with a decimal value of decimals(). The function decimals() returns 18, which Ethereum and most ERC20 tokens use as a decimal value. So our contract will have an initial supply of 10,000.000000000000000000 tokens and technically be exchangeable with Ethereum. Pretty cool!

Deployment

We are now ready to deploy our Kickstand contract. Select the deploy tab from the panel on the left-hand side of the Remix IDE. Remix supplies test Environments you can deploy to for initial testing and debugging. In our case select JavaScript VM.

Next, you’ll need to select Kickstand.sol from the Contract dropdown and hit the deploy button.

remix deploy and run ui

Remix provides a sandbox for testing your contracts. The buttons are the functions that we are inheriting from the base ERC20 contract. Blue buttoned functions are either pure or view functions, meaning they are just reading properties or returning a value, so they don’t cost any gas (FREE!). Orange buttoned functions cost gas because they are writing to the blockchain. remix function sandbox

Nuts and Bolts

These are the getters, functions, and events our contract is inheriting.

These are the  getters, functions, and events our contract is inheriting.

Getters
  • totalSupply() Returns the amount of the contract tokens that are in existence.

  • balanceOf () Returns the number of tokens of the given account.

  • allowance() Returns the remaining number of tokens that the spender is allowed to spend on behalf of the owner.

Functions
  • transfer() The amount of tokens specified by the function caller is sent to the recipient address. Returns a Boolean dependent on the success of the transfer. Emits the Transfer event.

  • approve() The function caller sets an amount the spender is allowed to transfer. Returns a Boolean dependent on the operation success. Emits the Approval event.

  • transferFrom() Moves the amount of tokens from the sender to the recipient. Returns a Boolean dependent on the operation success.

Events
  • Transfer This event is emitted when the amount, value, of tokens are sent from the from address to the to address.

  • Approval This event is emitted when the amount, value, of tokens is approved by the owner to be used by the spender.

Below is a working contract that extends the ERC20 base contract, but rolls all our functions. We are grabbing the Transfer and Approval events from the base ERC20 from Open Zeppelin.

A working contract that extends the ERC-20 base contract, but rolls all our functions.

Now that we better understand our Kickstand contract, let's deploy it to a public test net.

  1. Download the MetaMask browser extension. Then, sign in or sign up and choose the appropriate test network.

  2. Get the test ether for that network. If you selected Ropsten Network, you’ll need to search for Ropsten faucets. It can take some time for the test ether to show up in your wallet, so you should extract ether from multiple faucets.

  3. Back in the Remix IDE. In Deploy & Run, select Injected Web3 from the Environment pulldown.

  4. Now you should see Kickstand.sol as an option in the Contract selection box, select it and hit the Deploy button.

  5. A MetaMask popup will ask you to confirm the transaction.

MetaMask popup.

And that’s it. You’ve written and deployed an ERC20 contract!

Where To From Here?

The crypto space is growing fast and ERC20 is just one of many different contract standards available to blockchain developers. Here are some awesome resources and tutorial that helped me along the way:

  • CryptoZombies: A gamified tutorial that teaches you blockchain and solidity development through ZOMBIES!!!.

  • Ethernaut: A Web3/Solidity-based wargame. Each level is a smart contract that needs to be 'hacked'. Created by the folks at OpenZeppelin so you know it's gonna be good!

  • OpenZeppelin Docs: The docs have comprehensive guides for all things contract and dapp development.

  • Ethereum Foundation: This is probably the best resource available to solidity developers. You'll find guides for all things Ethereum and Web3 as well as forums where you can interact with others in the community.

And if you are interested in blockchain development or want to have a DAPP built, feel free to drop us a line and we can chat.

Go Back

Kick your software into gear

hello@kickstand.work