Last active
September 18, 2018 09:32
-
-
Save jtakalai/c0f470732b888cf395496f699199dc92 to your computer and use it in GitHub Desktop.
Fully ERC20 compliant Ethereum token
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
pragma solidity ^0.4.24; | |
// Token standard interface | |
contract ERC20 { | |
// Basic token features: book of balances and transfer | |
uint public totalSupply = 0; | |
mapping (address => uint256) public balanceOf; | |
function transfer(address to, uint tokens) public returns (bool success); | |
// Advanced features: An account can approve another account to spend its funds | |
mapping(address => mapping (address => uint256)) public allowance; | |
function approve(address spender, uint tokens) public returns (bool success); | |
function transferFrom(address from, address to, uint tokens) public returns (bool success); | |
event Transfer(address indexed from, address indexed to, uint tokens); | |
event Approval(address indexed tokenOwner, address indexed spender, uint tokens); | |
} | |
// Token implementing the ERC20 interface | |
contract TestToken is ERC20 { | |
// metadata for wallets | |
string public constant name = "My Test Token"; | |
string public constant symbol = "TEST"; | |
uint8 public constant decimals = 18; | |
function mint(address to, uint256 value) public { | |
require(totalSupply + value > totalSupply, "Overflow"); | |
balanceOf[to] += value; | |
emit Transfer(0, to, value); | |
totalSupply += value; | |
} | |
function transfer(address to, uint256 value) public returns (bool success) { | |
require(balanceOf[msg.sender] >= value, "Not enough tokens"); | |
require(value > 0, "Zero transfer"); | |
require(balanceOf[to] + value > balanceOf[to], "Overflow"); | |
balanceOf[msg.sender] -= value; | |
balanceOf[to] += value; | |
emit Transfer(msg.sender, to, value); | |
return true; | |
} | |
function approve(address spender, uint tokens) public returns (bool success) { | |
allowance[msg.sender][spender] = tokens; | |
emit Approval(msg.sender, spender, tokens); | |
return true; | |
} | |
function transferFrom(address from, address to, uint value) public returns (bool success) { | |
require(balanceOf[from] >= value, "Not enough tokens"); | |
require(value > 0, "Zero transfer"); | |
require(balanceOf[to] + value > balanceOf[to], "Overflow"); | |
require(allowance[from][msg.sender] >= value, "Not enough allowance"); | |
balanceOf[from] -= value; | |
allowance[from][msg.sender] -= value; | |
balanceOf[to] += value; | |
emit Transfer(from, to, value); | |
return true; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment