1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119
|
pragma solidity >=0.8.2< 0.9.0;
import "./IERC20.sol";
import "@openzeppelin/contracts/utils/math/SafeMath.sol";
contract ERC20 is IERC20 { using SafeMath for uint256; string public ercName; string public ercSymbol; uint8 public ercDecimals = 18; uint256 public ercTotalSupply; mapping (address => uint256) public ercBalances; mapping (address => mapping (address => uint256)) public ercAllowances; address private owner;
constructor(string memory _name, string memory _symbol, uint8 _decimals) { ercName = _name; ercSymbol = _symbol; ercDecimals = _decimals; owner = msg.sender; }
function mint(address _to, uint256 amount) external { require(msg.sender == owner, "caller is not owner"); require(_to != address(0), "to is a zero address"); require(amount > 0, "amount must > 0");
ercBalances[_to] = ercBalances[_to].add(amount); ercTotalSupply = ercTotalSupply.add(amount);
emit Transfer(address(0), _to, amount); }
function burn(address _to, uint256 amount) external { require(msg.sender == owner, "caller is not owner"); require(_to != address(0), "to is a zero address"); require(amount > 0, "amount must > 0");
ercBalances[_to] = ercBalances[_to].sub(amount); ercTotalSupply = ercTotalSupply.sub(amount);
emit Transfer(_to, address(0), amount); }
function name() override external view returns (string memory) { return ercName; } function symbol() override external view returns (string memory) { return ercSymbol; }
function decimals() override external view returns (uint8) { return ercDecimals; }
function totalSupply() override external view returns (uint256) { return ercTotalSupply; }
function balanceOf(address _owner) override external view returns (uint256 balance) { return ercBalances[_owner]; }
function transfer(address _to, uint256 _value) override external returns (bool success) { require(_value > 0, "_value must > 0"); require(_to != address(0), "to is zero address"); require(ercBalances[msg.sender] >= _value, "user balance not enough");
ercBalances[msg.sender] = ercBalances[msg.sender].sub(_value); ercBalances[_to] = ercBalances[_to].add(_value);
emit Transfer(msg.sender, _to, _value); return true; }
function transferFrom(address _from, address _to, uint256 _value) override external returns (bool success) { require(ercBalances[_from] >= _value, "user balance not enough"); require(ercAllowances[msg.sender][_from] >= _value, "approve balance not enough"); require(_value > 0, "_value must > 0"); require(_to != address(0), "_to is zero address");
ercBalances[_from] = ercBalances[_from].sub(_value); ercBalances[_to] = ercBalances[_to].add(_value); ercAllowances[_from][msg.sender] = ercAllowances[_from][msg.sender].sub(_value); emit Transfer(_from, _to, _value); return true; }
function approve(address _spender, uint256 _value) override external returns (bool success) { require(_value > 0, "_value must > 0"); require(_spender != address(0), "_spender is zero address"); require(ercBalances[msg.sender] >= _value, "user balance not enough");
ercAllowances[msg.sender][_spender] = _value;
emit Approval(msg.sender, _spender, _value); return true; }
function allowance(address _owner, address _spender) override external view returns (uint256 remaining) { return ercAllowances[_owner][_spender]; } }
|