-
Notifications
You must be signed in to change notification settings - Fork 0
/
ERC20.sol
154 lines (128 loc) · 4.63 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
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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
// SPDX-License-Identifier: MIT
pragma solidity 0.8;
/*
To make scracth ERC20 Token
- define / make interface
- function : totalSupply () external view returns(uint)
- function : balanceOf (address account) external view returns(uint)
- function : transfer(address recipent , uint amount) external view returns(bool)
- function : allowance(address owner , address spender) external view returns(uint)
- function : approve(address spender,uint amount) external view returns(bool)
- function : transferForm(address sender , address recipent , uint amount) external view returns(bool)
- event : Transfer(address indexed from, address indexed to, uint value)
- event : Approval(address indexed owner , address indexed spender , uint value)
- make basic contact
- make token name and symbol
- define founder adr and name
- define decimal : The decimals field in an ERC-20 token contract
defines how divisible a token can be . ex 0.00000000000001
divided into 10^18 smaller units
- make balance mapping for store user balance with has address
- totalSupply are total number of tokens that have been created
- make allowed mapping
*/
interface ERC20 {
// interfaces don't need any define to virtual keyword
// interfaces use to make function that don't have any not implement
function totalSupply() external view returns (uint256);
function balanceOf(address account) external view returns (uint256);
function transfer(address recipent, uint256 amount) external returns (bool);
function allowance(address owner, address spender)
external
returns (uint256);
function approve(address spender, uint256 amount) external returns (bool);
function transferForm(
address sender,
address recipent,
uint256 amount
) external returns (bool);
event Transfer(address indexed from, address indexed to, uint256 value);
event Approval(
address indexed owner,
address indexed spender,
uint256 value
);
/*
indexed : means that the value of that parameter will be logged and can be used for efficient
filtering when querying for past events , retrieval of events when you need to filter them based
on specific criteria
*/
}
contract Giga is ERC20 {
string public name = "kunal";
string public symbol = "KL";
string public founderName = "kunal lokhande";
address public founder;
uint256 public decimal = 16;
mapping(address => uint256) public balance;
uint256 public totalSupply;
mapping(address => mapping(address => uint256)) allowed;
constructor() {
totalSupply = 1000;
founder = msg.sender;
balance[founder] = totalSupply;
}
modifier CheckBalance(uint256 _amount) {
require(_amount > 0, "amount must be greater then zero");
require(balance[msg.sender] >= _amount, "you don't have balance");
_;
}
function balanceOf(address _account)
public
view
override
returns (uint256)
{
return balance[_account];
}
function transfer(address _recipent, uint256 _amount)
public
override
CheckBalance(_amount)
returns (bool)
{
balance[msg.sender] -= _amount;
balance[_recipent] += _amount;
emit Transfer(msg.sender, _recipent, _amount);
return true;
}
function allowance(address _owner, address _spender)
public
view
override
returns (uint256)
{
return allowed[_owner][_spender];
}
function approve(address _spender, uint256 _amount)
public
override
CheckBalance(_amount)
returns (bool)
{
allowed[msg.sender][_spender] = _amount;
emit Approval(msg.sender, _spender, _amount);
return true;
}
function transferForm(
address _sender,
address _recipent,
uint256 _amount
) public override returns (bool) {
require(
allowed[_sender][_recipent] >= _amount,
"recipent don't have authority to spend sender's token"
);
require(balance[_sender] >= _amount, "insufficent balance");
balance[_sender] -= _amount;
balance[_recipent] += _amount;
emit Transfer(msg.sender, _recipent, _amount);
return true;
}
function addSupply(uint256 _amount) public {
totalSupply += _amount;
}
function removeSupply(uint256 _amount) public {
totalSupply -= _amount;
}
}