1+ // SPDX-License-Identifier: MIT
2+ pragma solidity ^ 0.8.23 ;
3+
4+ contract ERC20 {
5+ string public name;
6+ string public symbol;
7+ uint8 public constant decimals = 18 ;
8+
9+ uint256 public totalSupply;
10+ mapping (address => uint256 ) public balanceOf;
11+ mapping (address => mapping (address => uint256 )) public allowance;
12+
13+ event Transfer (address indexed from , address indexed to , uint256 value );
14+ event Approval (address indexed owner , address indexed spender , uint256 value );
15+
16+ constructor (string memory _name , string memory _symbol , uint256 _initialSupply ) {
17+ name = _name;
18+ symbol = _symbol;
19+ _mint (msg .sender , _initialSupply);
20+ }
21+
22+ function transfer (address to , uint256 amount ) external returns (bool ) {
23+ _transfer (msg .sender , to, amount);
24+ return true ;
25+ }
26+
27+ function approve (address spender , uint256 amount ) external returns (bool ) {
28+ allowance[msg .sender ][spender] = amount;
29+ emit Approval (msg .sender , spender, amount);
30+ return true ;
31+ }
32+
33+ function transferFrom (address from , address to , uint256 amount ) external returns (bool ) {
34+ uint256 allowed = allowance[from][msg .sender ];
35+ require (allowed >= amount, "ERC20: insufficient allowance " );
36+ unchecked {
37+ allowance[from][msg .sender ] = allowed - amount;
38+ }
39+ _transfer (from, to, amount);
40+ return true ;
41+ }
42+
43+ function _transfer (address from , address to , uint256 amount ) internal {
44+ require (to != address (0 ), "ERC20: transfer to zero " );
45+ uint256 fromBalance = balanceOf[from];
46+ require (fromBalance >= amount, "ERC20: insufficient balance " );
47+ unchecked {
48+ balanceOf[from] = fromBalance - amount;
49+ balanceOf[to] += amount;
50+ }
51+ emit Transfer (from, to, amount);
52+ }
53+
54+ function _mint (address to , uint256 amount ) internal {
55+ require (to != address (0 ), "ERC20: mint to zero " );
56+ totalSupply += amount;
57+ balanceOf[to] += amount;
58+ emit Transfer (address (0 ), to, amount);
59+ }
60+ }
0 commit comments