-
Notifications
You must be signed in to change notification settings - Fork 6
/
ERC20.sol
56 lines (43 loc) · 1.77 KB
/
ERC20.sol
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
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.24;
import { IERC20 } from '../interfaces/IERC20.sol';
/**
* @title ERC20 Token
* @author Game7 Engineering Team - [email protected]
* @dev Adapted from WETH9: https://etherscan.io/address/0xc02aaa39b223fe8d0a0e5c4f27ead9083c756cc2#code
*/
contract ERC20 is IERC20 {
string public name;
string public symbol;
uint8 public decimals;
uint256 public totalSupply;
mapping(address => uint256) public balanceOf;
mapping(address => mapping(address => uint256)) public allowance;
constructor(string memory _tokenName, string memory _symbol, uint8 _decimals, uint256 _totalSupply) {
name = _tokenName;
symbol = _symbol;
decimals = _decimals;
balanceOf[msg.sender] = _totalSupply;
totalSupply = _totalSupply;
emit Transfer(address(0), msg.sender, _totalSupply);
}
function approve(address spender, uint256 amount) external returns (bool) {
allowance[msg.sender][spender] = amount;
emit Approval(msg.sender, spender, amount);
return true;
}
function transfer(address to, uint256 amount) external returns (bool) {
return transferFrom(msg.sender, to, amount);
}
function transferFrom(address from, address to, uint256 amount) public returns (bool) {
require(balanceOf[from] >= amount, 'ERC20: transfer amount exceeds balance');
if (msg.sender != from && allowance[from][msg.sender] != type(uint256).max) {
require(allowance[from][msg.sender] >= amount, 'ERC20: insufficient allowance');
allowance[from][msg.sender] -= amount;
}
balanceOf[from] -= amount;
balanceOf[to] += amount;
emit Transfer(from, to, amount);
return true;
}
}