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 probably wondering how to use these concepts without having a starting balance. In this article, we’ll walk you through the process of using ERC20 tokens when all your balances are 0.
Understanding ERC20
Before we get into the details, let’s quickly recap 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 that has no intrinsic value, meaning its value comes from supply and demand in the market. In other words, tokens like Ether (ETH) or USDC are not backed by any physical assets.
OpenZeppelin ERC20 Implementation
To use ERC20 tokens, you need to familiarize yourself with the OpenZeppelin contract that implements this standard on the Ethereum network. The specific implementation that we will be using is available at [
Step 1: Create an ERC20 token
To start using ERC20 tokens, you 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 need to initialize the token contract using the new
function provided by OpenZeppelin.
pragma solidity ^0.8.0;
contract MyToken {
// ...
constructor() {
_initToken();
}
function _initToken() internal {
MyTokenInitializer.init(_tokenAddress);
}
}
structure MyTokenInitializer {
address _tokenAddress;
}
Step 3: Deploying the Token Contract
Once you have initialized your token contract, you can deploy it to the Ethereum network using the Solidity deployment tool.
pragma solidity ^0.8.0;
contract MyToken {
// ...
function deploy() public return (address) {
address newTokenAddress = MyTokenInitializer._tokenAddress;
return newTokenAddress;
}
}
Step 4: Set the token supply
To set the number of tokens, you need to create a mapping that maps the token name to its total quantity.
pragma solidity ^0.8.0;
contract MyToken {
// ...
public total supply unit;
mapping (string => uint) public nameToSupply;
function setTotalSupply(uint _totalSupply) public {
total supply = _total supply;
for (address tokenSymbol in nameToSupply.values()) {
if (tokenSymbol != address(this)) {
uint supplyAmount = supplyname[tokensymbol] _totalSupply / 10*18;
remove nameToSupply[tokenSymbol];
}
}
}
function getBalanceOf(address_account) public view returns (uint256) {
return total supply quantity - nameSupply[_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 += nameSupply[_account] quantity / 10*18;
return total quantity of supplies – SupplyName[_account];
}
public function remove(uint amount) returns (bool) {
require(totalSupply >= quantity);
totalSupply -= SupplyName[message.sender] quantity / 10*18;
// …