Photo by Kanchanara on Unsplash
An Introduction to ERC-20 Tokens
Understanding and Creating Ethereum-Based Tokens
It's fairly simple to create an ERC-20 token in Solidity, but it does require some familiarity with the Ethereum network and the Solidity programming language.
In this article, we'll explain how to create an ERC-20 token, complete with examples and code snippets to make the process easier to understand.
Let's look at a proper definition of an ERC-20 token since that is the first question that comes to mind.
ERC-20 is the technical standard for fungible tokens created using the Ethereum blockchain. A fungible token is one that is interchangeable with another token. ERC-20 allows different smart-contract-enabled tokens a way to be exchanged.
An ERC-20 token can be defined by specifying the name
, symbol
and totalSupply.
This can be done by creating a new Solidity contract and specifying the appropriate variables.
pragma solidity ^0.8.0;
contract MyToken {
string public name = "My Token";
string public symbol = "MYT";
uint256 public totalSupply;
constructor() public {
totalSupply = 1000000;
}
}
The token must comply with the ERC-20 standard to be compatible with other Ethereum contracts and wallets. This can be done by defining a set of functions such as totalSupply
, balanceOf
, and transfer
, and making sure they adhere to the ERC-20 specification.
pragma solidity ^0.8.0;
contract MyToken {
string public name = "My Token";
string public symbol = "MYT";
uint256 public totalSupply;
mapping(address => uint256) public balanceOf;
constructor() public {
totalSupply = 1000000;
balanceOf[msg.sender] = totalSupply;
}
function transfer(address _to, uint256 _value) public {
require(balanceOf[msg.sender] >= _value);
require(_to != address(0));
balanceOf[msg.sender] -= _value;
balanceOf[_to] += _value;
}
}
The above contract is just the basic implementation of the ERC-20 token; to create a more robust and secured token, we'd have to make use of the openzeppelin library or any other library that leverages the ERC-20 standard.
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.7;
import "@openzeppelin/contracts/token/ERC20/ERC20.sol";
contract Token is ERC20 {
constructor(uint _amount) ERC20("My Token", "MYT") {
_mint(msg.sender, _amount);
}
function mint(uint _amount) public {
_mint(msg.sender, _amount);
}
}
Using the openzeppelin library gives us the standard functions required to create an ERC-20 token i.e approve
, _mint
, allowance
and so on. You can check the OpenZeppelin docs to get the complete documentation on the different functions it provides.
If we run the above contract on the Remix editor and deploy it, we would be able to see the functions provided by the ERC20
contract. It will look like the image below when deployed.
We can take it further by modifying the mint
function and make it payable
so that the contract can receive ether.
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.7;
import "@openzeppelin/contracts/token/ERC20/ERC20.sol";
error Token__NotEnoughEtherPassed(uint expectedETH, uint ethGotten);
contract Token is ERC20 {
uint tokenPrice = 0.05 ether;
constructor() ERC20("My Token", "MYT") {
_mint(msg.sender, _amount);
}
function mint(uint _amount) public payable {
if(msg.value < tokenPrice){
revert Token__NotEnoughEtherPassed(tokenPrice, msg.value);
}
_mint(msg.sender, _amount);
}
}
Conclusion
In this article, we have been able to create an ERC-20 token using the ERC-20 standard, we also made use of the Open Zeppelin library so as not to reinvent the wheel, and then we took it a step further in making the mint
function payable so that the contract was able to receive Ether whenever someone tries to mint a token.
Overall, creating an ERC-20 token in Solidity is a relatively straightforward process that can be accomplished with a basic understanding of the Solidity programming language and the Ethereum network. However, it's important to thoroughly test and audit your contract before deployment to ensure it is secured from attackers.
Finally, If you found this article useful, please consider sharing it with your friends and colleagues. If you have any questions or want to discuss anything related to the topic, please feel free to reach out to me on Twitter or LinkedIn, I would be happy to chat with you.
Thank you for reading.