-
Notifications
You must be signed in to change notification settings - Fork 0
/
ERC20_demo.sol
114 lines (80 loc) · 2.99 KB
/
ERC20_demo.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
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
// SPDX-License-Identifier: MIT
pragma solidity ^0.7.5;
interface IERC20 {
function totalSupply() external view returns (uint256);
function balanceOf(address _owner) external view returns (uint256 );
function transfer(address _to, uint256 _value) external returns (bool );
function transferFrom(address _from, address _to, uint256 _value) external returns (bool );
function approve(address _spender, uint256 _value) external returns (bool success);
function allowance(address _owner, address _spender) external view returns (uint256 remaining);
event Transfer(address indexed _from, address indexed _to, uint256 _value);
event Approval(address indexed _owner, address indexed _spender, uint256 _value);
}
contract MyToken is IERC20 {
string name;
string symbol;
string decimal;
address ownerAddress;
mapping(address => uint256) accounts;
mapping (address => mapping(address => uint256)) allowed;
uint256 total_supply;
modifier onlyAdmin{
require(msg.sender==ownerAddress,"only owner can deposite/mint tokens");
_;
}
constructor(string memory _name,string memory _symbol,string memory _decimal,uint256 _supply)
{
name=_name;
symbol=_symbol;
decimal=_decimal;
total_supply=_supply;
ownerAddress=msg.sender;
accounts[msg.sender]=total_supply;
}
//to get total balance of tokens
function totalSupply() public override view returns (uint256)
{
return total_supply;
}
//to get remaining balance in given address
function balanceOf(address _owner) public override view returns (uint256)
{
return accounts[_owner];
}
function transfer(address _to, uint256 _value) public override returns (bool )
{
require(_value <= accounts[msg.sender]) ;
accounts[msg.sender] = accounts[msg.sender] - _value;
accounts[_to] = accounts[_to] + _value;
emit Transfer(msg.sender ,_to,_value);
return true;
}
function mint(uint256 _tokens) public onlyAdmin returns (uint256)
{
total_supply+= _tokens;
accounts[msg.sender] += _tokens;
return total_supply;
}
//spender is allowed to spend tokens less that equal to _value
function approve(address _spender, uint256 _value) public override returns (bool success)
{
require(_spender != address(0));
allowed [msg.sender][_spender]= _value;
emit Approval(msg.sender,_spender,_value);
return true;
}
//can view allowence given to _spender by _owner (whoever call this funtion is owner)
function allowance(address _owner, address _spender) public override view returns (uint256 remaining)
{
return allowed [_owner][_spender];
}
function transferFrom(address _from, address _to, uint256 _value) public override returns (bool )
{
require(_value <= accounts[_from]) ;
require(_value <= allowed[_from][_to] ) ;
accounts[_from] -= _value;
accounts[_to] += _value;
emit Transfer (_from ,_to,_value);
return true;
}
}