WASM ERC20

Introduction

In this chapter, we will show you how you can build an ERC20 token contract with ink!.
Over the course of the chapter, we will cover:
  • Initial token minting
  • Token transfers
  • Approvals and third party transfers
  • Emitting runtime events through Substrate
But first, we will go over the ERC20 standard for those of you who are not familiar.

ERC20 Standard

The ERC20 token standard defines the interface for the most popular Ethereum smart contract.
1
// ----------------------------------------------------------------------------
2
// ERC Token Standard #20 Interface
3
// https://github.com/ethereum/EIPs/blob/master/EIPS/eip-20.md
4
// ----------------------------------------------------------------------------
5
6
contract ERC20Interface {
7
// Storage Getters
8
function totalSupply() public view returns (uint);
9
function balanceOf(address tokenOwner) public view returns (uint balance);
10
function allowance(address tokenOwner, address spender) public view returns (uint remaining);
11
12
// Public Functions
13
function transfer(address to, uint tokens) public returns (bool success);
14
function approve(address spender, uint tokens) public returns (bool success);
15
function transferFrom(address from, address to, uint tokens) public returns (bool success);
16
17
// Contract Events
18
event Transfer(address indexed from, address indexed to, uint tokens);
19
event Approval(address indexed tokenOwner, address indexed spender, uint tokens);
20
}Copy to clipboardErrorCopied
Copied!
In summary, it allows individuals to deploy their own cryptocurrency on top of an existing smart contract platform. There isn't much magic happening in this contract. Users balances are stored in a HashMap, and a set of APIs are built to allow users to transfer tokens they own or allow a third party to transfer some amount of tokens on their behalf. Most importantly, all of this logic is implemented ensuring that funds are not unintentionally created or destroyed, and that a user's funds are protected from malicious actors.
Note that all the public functions return a bool which specifies if the call was successful or not. We will adhere to that specification.
Last modified 2mo ago
Export as PDF
Copy link