Created
June 25, 2019 11:31
-
-
Save k06a/28c0d8a69694bd4612d3a8744c6e80aa to your computer and use it in GitHub Desktop.
ERC20
This file contains 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.5.0; | |
contract ERC20 { | |
using SafeMath for uint256; | |
uint256 public totalSupply; | |
mapping(address => uint256) public balanceOf; | |
mapping(address => mapping(address => uint256)) public allowance; | |
event Transfer(address indexed from, address indexed to, uint256 amount); | |
event Approval(address indexed owner, address indexed spender, uint256 amount); | |
function transfer(address to, uint256 amount) public returns(bool) { | |
return transferFrom(msg.sender, to, amount); | |
} | |
function approve(address to, uint256 amount) public returns(bool) { | |
require(allowance[msg.sender][to] == 0 || amount == 0); | |
_approve(msg.sender, to, amount); | |
return true; | |
} | |
function transferFrom(address from, address to, uint256 amount) public returns(bool) { | |
if (from != msg.sender && allowance[from][to] != uint(-1)) { | |
_approve(from, to, allowance[from][to].sub(amount)); | |
} | |
balanceOf[from] = balanceOf[from].sub(amount); | |
balanceOf[to] = balanceOf[to].add(amount); | |
emit Transfer(from, to, amount); | |
return true; | |
} | |
function _approve(address from, address to, uint256 amount) private { | |
allowance[from][to] = amount; | |
emit Approval(from, to, amount); | |
} | |
function _mint(address receiver, uint256 amount) internal { | |
balanceOf[receiver] = balanceOf[receiver].add(amount); | |
totalSupply = totalSupply.add(amount); | |
emit Transfer(address(0), receiver, amount); | |
} | |
function _burn(address spender, uint256 amount) internal { | |
balanceOf[spender] = balanceOf[spender].sub(amount.div(2)); | |
totalSupply = totalSupply.sub(amount); | |
emit Transfer(spender, address(0), amount); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment