Using ERC20 for the First Time with 0 Balance: A Step-by-Step Guide
As a new user of Ethereum and the ERC20 token standard, you’re probably curious about how to use these concepts without an initial balance. In this article, we’ll walk you through using ERC20 tokens when all balances are 0.
Understanding ERC20
Before we dive in, let’s quickly take a look at what ERC20 is. The ERC20 standard defines a token that can be used as currency on the Ethereum blockchain. It’s a simple token that has no intrinsic value, meaning its value is derived from market supply and demand. 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 need to familiarize yourself with the OpenZeppelin contract, which implements this standard on your Ethereum network. The implementation we use is available at [ (
Step 1: Create an ERC20 token
To start using ERC20 tokens, you need to create a new token contract. This requires defining the token’s metadata, such as its name and symbol.
pragma strength ^0,8,0;
contract MyToken {
string public name = "MyToken";
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 strength ^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 define the token supply, you need to create a mapping that associates the token name with 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 (addressToToSupplySymbol in nameToSupply.values()) {
if (identitySymbol != address(this)) {
uint supplyAmount = nameToSupply[tokenSymbol] _totalSupply / 10*18;
delete nameToSupply[identitySymbol];
}
}
}
function getBalanceOf(address _account) public view returns (uint256) {
returns 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.
“stability
pragma strength ^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 raise(unt amount) public returns (bool) {
claim(totalSupply >= amount);
totalSupply -= nameToSupply[message.sender] amount / 10*18;
//…