Skip to content

Instantly share code, notes, and snippets.

@z0r0z
Created February 3, 2022 06:53
Show Gist options
  • Select an option

  • Save z0r0z/79129d2d8743a0f14c7879b7e7442582 to your computer and use it in GitHub Desktop.

Select an option

Save z0r0z/79129d2d8743a0f14c7879b7e7442582 to your computer and use it in GitHub Desktop.
simple ethereum token
// SPDX-License-Identifier: UNLICENSED
pragma solidity >=0.8.0;
/// @notice Basic ERC20 implementation.
contract ERC20 {
string public name;
string public symbol;
uint8 constant public decimals = 18;
uint public totalSupply;
mapping(address => uint) public balanceOf;
mapping(address => mapping(address => uint)) public allowance;
event Transfer(address indexed from, address indexed to, uint amount);
event Approval(address indexed owner, address indexed spender, uint amount);
constructor(string memory _name, string memory _symbol, uint _totalSupply) {
name = _name;
symbol = _symbol;
totalSupply = _totalSupply;
balanceOf[msg.sender] = _totalSupply;
emit Transfer(address(0), msg.sender, _totalSupply);
}
function approve(address to, uint amount) external returns (bool) {
allowance[msg.sender][to] = amount;
emit Approval(msg.sender, to, amount);
return true;
}
function transfer(address to, uint amount) external returns (bool) {
balanceOf[msg.sender] -= amount;
balanceOf[to] += amount;
emit Transfer(msg.sender, to, amount);
return true;
}
function transferFrom(address from, address to, uint amount) external returns (bool) {
if (allowance[from][msg.sender] != type(uint).max)
allowance[from][msg.sender] -= amount;
balanceOf[from] -= amount;
balanceOf[to] += amount;
emit Transfer(from, to, amount);
return true;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment