Skip to content

Instantly share code, notes, and snippets.

@shahzaintariq
Created February 21, 2020 20:01
Show Gist options
  • Save shahzaintariq/4c277cc483234ffe8d11f29058b93986 to your computer and use it in GitHub Desktop.
Save shahzaintariq/4c277cc483234ffe8d11f29058b93986 to your computer and use it in GitHub Desktop.
ERC20 contract
pragma solidity ^0.6.1;
import {ERC20TokenInterface} from './IERC20.sol';
contract ERC20 is ERC20TokenInterface{
string internal tName;
string internal tSymbol;
uint256 internal tTotalSupply;
uint256 internal tdecimals;
mapping(address => uint256) balances;
mapping(address => mapping(address => uint256)) allowed;
constructor(string memory _tokenName,string memory _symbol,uint256 _totalSupply,uint256 _decimals)public{
tName = _tokenName;
tSymbol = _symbol;
balances[msg.sender] += _totalSupply;
tTotalSupply = _totalSupply;
tdecimals = _decimals;
}
function name() override public view returns(string memory) { return tName;}
function symbol() override public view returns(string memory) { return tSymbol;}
function totalSupply()override public view returns(uint256) { return tTotalSupply;}
function decimals() override public view returns(uint256) { return tdecimals;}
function balanceOf(address tokenOwner) override public view returns(uint256){ return balances[tokenOwner]; }
function transfer(address to, uint token) override public returns(bool success){
require(balances[msg.sender] >= token, "you should have some token");
balances[msg.sender] -= token;
balances[to] += token;
emit Transfer(msg.sender,to,token);
return true;
}
function approve(address spender, uint tokens) override public returns(bool success) {
allowed[msg.sender][spender] += tokens;
emit Approval(msg.sender, spender,tokens);
return true;
}
function allowance(address owner, address spender) override public view returns(uint){
return allowed[owner][spender];
}
function transferFrom(address from, address to, uint tokens) override public returns(bool success) {
require(balances[from] >= tokens);
require(allowed[from][msg.sender] >= tokens);
balances[from] -= tokens;
balances[to] += tokens;
allowned[from][msg.sender] -= tokens;
emit Transfer(from,to,tokens);
return true;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment