#100DaysOfSolidity ๐Ÿš€ Creating Your Own ERC20 Token โ€” A Technical Guide ๐Ÿš€

Solidity Academy
4 min readJul 26, 2023

๐Ÿ’ก Welcome to the 50th edition of the #100DaysOfSolidity series! In this medium article, weโ€™ll delve into the exciting world of ERC20 tokens on the Ethereum blockchain. ERC20 is a widely adopted standard for creating fungible tokens, which means each token is interchangeable with another of the same value. These tokens play a pivotal role in decentralized finance (DeFi) and various other blockchain-based applications.

#100DaysOfSolidity ๐Ÿš€ Creating Your Own ERC20 Token โ€” A Technical Guide ๐Ÿš€

๐Ÿ† What is an ERC20 Token?

An ERC20 token is a smart contract that follows the ERC20 standard, a set of rules and interfaces that defines how tokens on the Ethereum network should behave. These tokens offer various functionalities, such as transferring tokens between addresses and allowing third parties to transfer tokens on behalf of the token holder.

๐ŸŒŸ ERC20 Interface

Hereโ€™s the interface for ERC20 tokens, showcasing the required functions and events:

// SPDX-License-Identifier: MIT
pragma solidity ^0.8.17;
interface IERC20 {
function totalSupply() external view returns (uint);
function balanceOf(address account) external view returns (uint);
function transfer(address recipient, uint amount) external returns (bool);
function allowance(address owner, address spender) external view returns (uint);
function approve(address spender, uint amount) external returns (bool);
function transferFrom(address sender, address recipient, uint amount) external returns (bool);
event Transfer(address indexed from, address indexed to, uint value);
event Approval(address indexed owner, address indexed spender, uint value);
}

๐Ÿ“ Example ERC20 Token Contract

To get a hands-on experience with ERC20 tokens, letโ€™s look at a simple example of an ERC20 token contract:

// SPDX-License-Identifier: MIT
pragma solidity ^0.8.17;
import "./IERC20.sol";
contract ERC20 is IERC20 {
uint public totalSupply;
mapping(address => uint) public balanceOf;
mapping(address => mapping(address => uint)) public allowance;
string public name = "Solidity by Example";
string public symbol = "SOLBYEX";
uint8 public decimals = 18;
// Function to transfer tokens from the sender's account to a recipient's account
function transfer(address recipient, uint amount) external returns (bool) {
balanceOf[msg.sender] -= amount;
balanceOf[recipient] += amount;
emit Transfer(msg.sender, recipient, amount);
return true;
}
// Function to approve a spender to spend tokens on behalf of the sender
function approve(address spender, uint amount) external returns (bool) {
allowance[msg.sender][spender] = amount;
emit Approval(msg.sender, spender, amount);
return true;
}
// Function to transfer tokens from a specific account to another account
function transferFrom(address sender, address recipient, uint amount) external returns (bool) {
allowance[sender][msg.sender] -= amount;
balanceOf[sender] -= amount;
balanceOf[recipient] += amount;
emit Transfer(sender, recipient, amount);
return true;
}
// Function to mint new tokens
function mint(uint amount) external {
balanceOf[msg.sender] += amount;
totalSupply += amount;
emit Transfer(address(0), msg.sender, amount);
}
// Function to burn tokens
function burn(uint amount) external {
balanceOf[msg.sender] -= amount;
totalSupply -= amount;
emit Transfer(msg.sender, address(0), amount);
}
}

๐Ÿ› ๏ธ Create Your Own ERC20 Token

With the OpenZeppelin library, creating your own ERC20 token becomes a breeze. Letโ€™s see how you can do it:

// SPDX-License-Identifier: MIT
pragma solidity ^0.8.17;
import "https://github.com/OpenZeppelin/openzeppelin-contracts/blob/v4.0.0/contracts/token/ERC20/ERC20.sol";
contract MyToken is ERC20 {
constructor(string memory name, string memory symbol) ERC20(name, symbol) {
// Mint 100 tokens to the contract deployer (msg.sender)
_mint(msg.sender, 100 * 10 ** uint(decimals()));
}
}

This code demonstrates a token contract named `MyToken` that you can deploy on the Ethereum network with a specified name and symbol. In this example, the contractโ€™s constructor will mint 100 tokens to the deployerโ€™s address.

๐Ÿ”„ Swapping Tokens Using a Smart Contract

Suppose you want to trade ERC20 tokens between different addresses. In that case, you can use a smart contract like `TokenSwap` to facilitate the exchange. Letโ€™s take a look:

// SPDX-License-Identifier: MIT
pragma solidity ^0.8.17;
import "https://github.com/OpenZeppelin/openzeppelin-contracts/blob/v4.0.0/contracts/token/ERC20/IERC20.sol";
contract TokenSwap {
IERC20 public token1;
address public owner1;
uint public amount1;
IERC20 public token2;
address public owner2;
uint public amount2;
constructor(
address _token1,
address _owner1,
uint _amount1,
address _token2,
address _owner2,
uint _amount2
) {
token1 = IERC20(_token1);
owner1 = _owner1;
amount1 = _amount1;
token2 = IERC20(_token2);
owner2 = _owner2;
amount2 = _amount2;
}
function swap() public {
require(msg.sender == owner1 || msg.sender == owner2, "Not authorized");
require(token1.allowance(owner1, address(this)) >= amount1, "Token 1 allowance too low");
require(token2.allowance(owner2, address(this)) >= amount2, "Token 2 allowance too low");
_safeTransferFrom(token1, owner1, owner2, amount1);
_safeTransferFrom(token2, owner2, owner1, amount2);
}
function _safeTransferFrom(IERC20 token, address sender, address recipient, uint amount) private {
bool sent = token.transferFrom(sender, recipient, amount);
require(sent, "Token transfer failed");
}
}

The `TokenSwap` contract facilitates the swapping of two different ERC20 tokens between two specified addresses. Before the swap can occur, both parties need to approve the contract to spend their respective tokens.

๐Ÿ Conclusion

In this extensive guide, we explored the fascinating world of ERC20 tokens. We learned about the ERC20 standard, how to create your own ERC20 token using the OpenZeppelin library, and even built a smart contract to facilitate token swaps between addresses.

Now that you have a solid understanding of ERC20 tokens and how to interact with them in Solidity, you can dive deeper into the world of decentralized finance and other innovative blockchain applications.

Keep coding, stay curious, and ๐Ÿš€ happy tokenizing! ๐Ÿš€

--

--

Solidity Academy

Your go-to resource for mastering Solidity programming. Learn smart contract development and blockchain integration in depth. https://heylink.me/solidity/