-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patherc20 from scratch.sol
63 lines (46 loc) · 1.9 KB
/
erc20 from scratch.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
57
58
59
60
61
62
63
// SPDX-License-Identiifier : MIT
pragma solidity ^0.8.17;
contract MyToken{
event Transfer(address indexed _from, address indexed _to, uint _value);
event Approval(address indexed _from, address indexed _spender, uint _value);
string public name;
string public symbol;
uint public immutable decimals;
uint public immutable totalSupply;
mapping(address => uint) _balances;
mapping(address => mapping(address => uint)) _allowances;
constructor(string memory _name, string memory _symbol, uint _decimals, uint _totalSupply) {
name = _name;
symbol = _symbol;
decimals = _decimals;
totalSupply = _totalSupply;
_balances[msg.sender] = _totalSupply;
}
function balanceOf(address _owner) public view returns(uint){
require(_owner != address(0), "cannot be an address zero");
return _balances[_owner];
}
function transfer(address _to, uint _value) public returns(bool){
require(_balances[msg.sender] >= _value && _balances[msg.sender] != 0, "must have value, and not be zero");
_balances[msg.sender] -= _value;
_balances[_to] += _value;
emit Transfer(msg.sender, _to, _value);
return true;
}
function allowance(address _owner, address _spender) public view returns(uint){
return _allowances[_spender][_owner];
}
function approve (address _spender, uint _value) public returns(bool){
require(_balances[msg.sender] >= _value, "bal must be greater than the value");
_allowances[_spender][msg.sender] = _value;
emit Approval(msg.sender, _spender, _value);
return true;
}
function transferFrom(uint _value, address _from, address _to) public returns(bool) {
_balances[_from] -= _value;
_balances[_to] += _value;
_allowances[msg.sender][_from] = _value;
emit Transfer(msg.sender, _to, _value);
return true;
}
}