ERC20
Solidity is a programming language for writing smart contracts. Solidity is the de facto standard language for Ethereum. Solidity is a statically typed language that is compiled to EVM opcodes that can be run on the Ethereum Virtual Machine (EVM).
In this exercise session, we will create a simple ERC20 cryptocurrency and deploy it on the Rinkeby testnet. If you do not have enough Ethers from the last exercise session, I can send you more.
Exercise 1:
Check Remix IDE/Metamask and make sure you have some test Ethers. If not, send me your address, so that I can send you 1 ETH.
- Question: In case you lost your wallet, how can you recreate your wallet?
We will implement a simplified ERC20 Token. There are tokens that only implement this simplified ERC20 token, for example the GNT token. For a simplified token, we need to implement the following interfaces:
function totalSupply() external view returns (uint256);
function balanceOf(address account) external view returns (uint256);
function transfer(address recipient, uint256 amount) external returns (bool);
event Transfer(address indexed from, address indexed to, uint256 value);
In order to implement the full ERC20 interface, you could implement those methods as well:
function allowance(address owner, address spender) external view returns (uint256);
function approve(address spender, uint256 amount) external returns (bool);
function transferFrom(address sender, address recipient, uint256 amount) external returns (bool);
event Approval(address indexed owner, address indexed spender, uint256 value);
Exercise 2: Implement the mapping of accounts to values. Also think about the minting. The minting process is the process, where you define how much tokens an address gets.
- Question: in the lecture we had a special minting function. Is this required for a simple token?
Implement the balanceOf function. This functions returns the balance from the mapping of accounts.
To prevent uint256 overflows, use SafeMath.sol from OpenZeppelin.
Exercise 3: Implement the transfer function, use SafeMath:
import "./SafeMath.sol";
...
contract X {
using SafeMath for uint256;
- Question: what does SafeMath do?
Give your token a name, the number of decimal points (18), and implement the total supply function:
string public constant name = "Token Name";
string public constant symbol = "SYM";
uint8 public constant decimals = 18; // 18 is the most common number of decimal places
Exercise 4: Implement the rest of the functionality of the simplified ERC20.
- Question: how can you quickly test your implementation?
Since your contract is tested and running, you can deploy it on the Rinkeby testnet.
Exercise 5:
Deploy your contract and send me via MetaMask (0x0CbdF5B0c4E117619631bA4b97dC0d439ADAbD88) some tokens.
- Question: can you see your tokens on Etherscan?
- Question: why can a token become valuable?
Solutions:
pragma solidity ^0.5.8;
import "./SafeMath.sol";
contract Blabla {
using SafeMath for uint256;
mapping (address => uint256) balances;
uint256 total = 1000 * 10**2;
string public constant name = "VSS Token";
string public constant symbol = "Gugus";
uint8 public constant decimals = 2;
event Transfer(address indexed from, address indexed to, uint256 value);
constructor() public {
balances[msg.sender] = total;
}
function balanceOf(address account) external view returns (uint256) {
return balances[account];
}
function totalSupply() external view returns (uint256) {
return total;
}
function transfer(address recipient, uint256 amount) external returns (bool) {
require(balances[msg.sender] >= amount);
balances[msg.sender] = balances[msg.sender].sub(amount);
balances[recipient] = balances[recipient].add(amount);
emit Transfer(msg.sender, recipient, amount);
return true;
}
}