Using ERC20 for the First Time with 0 Balances: A Step-by-Step Guide
As a new user of Ethereum and the ERC20 token standard, you’re likely curious about how to use these concepts without having any initial balances. In this article, we’ll walk you through the process of using ERC20 tokens when all balances are 0.
Understanding ERC20
Before diving in, let’s quickly review what ERC20 is. The ERC20 standard defines a token that can be used as a currency on the Ethereum blockchain. It’s a simple token with no intrinsic value, meaning its value is derived from market demand and supply. In other words, tokens like Ether (ETH) or USDC are not backed by any physical asset.
ERC20 Implementation from OpenZeppelin
To use ERC20 tokens, you’ll need to consult the OpenZeppelin contract that implements this standard on your Ethereum network. The specific implementation we’ll be using is available at [
Step 1: Create an ERC20 Token
To start using ERC20 tokens, you’ll need to create a new token contract. This involves defining the token’s metadata, such as its name and symbol.
pragma solidity ^0.8.0;
contract MyToken {
string public name = "My Token";
string public symbol = "MYT";
}
Step 2: Initialize the Token
Next, you’ll need to initialize your token contract using the new
function provided by OpenZeppelin.
pragma solidity ^0.8.0;
contract MyToken {
// ...
constructor() {
_initToken();
}
function _initToken() internal {
MyTokenInitializer.init(_tokenAddress);
}
}
struct MyTokenInitializer {
address _tokenAddress;
}
Step 3: Deploy the Token Contract
Now that your token contract is initialized, you can deploy it to the Ethereum network using the Solidity deployment tool.
pragma solidity ^0.8.0;
contract MyToken {
// ...
function deploy() public returns (address) {
address newTokenAddress = MyTokenInitializer._tokenAddress;
return newTokenAddress;
}
}
Step 4: Set the Token Supply
To set the token supply, you’ll need to create a mapping that maps the token’s name to its total supply.
pragma solidity ^0.8.0;
contract MyToken {
// ...
uint public totalSupply;
mapping (string => uint) public nameToSupply;
function setTotalSupply(uint _totalSupply) public {
totalSupply = _totalSupply;
for (address tokenSymbol in nameToSupply.values()) {
if (tokenSymbol != address(this)) {
uint supplyAmount = nameToSupply[tokenSymbol] _totalSupply / 10*18;
delete nameToSupply[tokenSymbol];
}
}
}
function getBalanceOf(address _account) public view returns (uint256) {
return totalSupply - nameToSupply[_account];
}
// ...
}
Step 5: Use the Token
Finally, you can use your token as usual by calling the balanceOf
function to check its balance or using it to transfer Ether.
“`solidity
pragma solidity ^0.8.0;
contract MyToken {
// …
constructor() {}
function deposit(address _account, uint amount) public returns (uint256) {
totalSupply += nameToSupply[_account] amount / 10*18;
return totalSupply – nameToSupply[_account];
}
function withdraw(uint amount) public returns (bool) {
require(totalSupply >= amount);
totalSupply -= nameToSupply[msg.sender] amount / 10*18;
// …