-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy patherc20.sol
176 lines (140 loc) · 5.7 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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
//SPDX-License-Identifier: GPL-3.0
pragma solidity >=0.5.0 <0.9.0;
// ------------------------------------------
// EIP-20: ERC-20 Token Standard
// https://eips.ethereum.org/EIPS/eip-20
// ------------------------------------------
interface ERC20Interface {
function totalSupply() external view returns (uint);
function balanceOf(address tokenOwner) external view returns (uint balance);
function transfer(address to, uint tokens) external returns (bool success);
function allowance(address tokenOwner,address spender) external view returns(uint remaining);
function approve(address spender, uint tokens)external returns(bool success);
function transferFrom(address from, address to, uint tokens)external returns(bool success);
event Transfer(address indexed from, address indexed to, uint tokens);
event Approval(address indexed tokenOwner, address indexed spender, uint tokens);
}
contract Cryptos is ERC20Interface {
string public name = "Cryptos";
string public symbol = "CRPT";
uint public decimals = 0; //18
uint public override totalSupply;
address public founder;
mapping(address => uint) public balances;
// balances[0x1111...] = 100;
mapping(address => mapping(address => uint)) allowed;
//0x111... (owner) allows 0x2222... (the spender) ---- 100 tokens
// allowed[0x111][0x222] = 100;
constructor(){
totalSupply = 1000000;
founder = msg.sender;
balances[founder] = totalSupply;
}
function balanceOf(address tokenOwner) public view override returns (uint balance){
return balances[tokenOwner];
}
function transfer(address to, uint tokens) public virtual override returns(bool success){
require(balances[msg.sender] >= tokens);
balances[to] += tokens;
balances[msg.sender] -= tokens;
emit Transfer(msg.sender, to, tokens);
return true;
}
function allowance(address tokenOwner, address spender) view public override returns(uint){
return allowed[tokenOwner][spender];
}
function approve(address spender, uint tokens) public override returns(bool success){
require(balances[msg.sender] >= tokens);
require(tokens > 0);
allowed[msg.sender][spender] = tokens;
emit Approval(msg.sender, spender, tokens);
return true;
}
function transferFrom(address from, address to, uint tokens) public virtual override returns (bool success){
require(allowed[from][msg.semder] >= tokens);
require(balances[from] >= tokens);
balances[from] -= tokens;
allowed[from][msg.sender] -= tokens;
balances[to] += tokens;
emit Transfer(from, to, tokens);
return true;
}
}
//ICO Coding Starts here
contract CryptosICO is Cryptos {
address public admin;
address payable public deposit;
uint tokenPrice = 0.001 ether; // 1ETH = 1000 CRPT, 1CRPT = 0.001ETH
uint public hardCap = 300 ether;
uint public raisedAmount;
uint public saleStart = block.timestamp;
uint public saleEnd = block.timestamp + 604800; // ICO ends in one week
uint public tokenTradeStart = saleEnd + 604800; // Transferable in a week after sale
uint public maxInvestment = 5 ether;
uint public minInvestment = 0.1 ether;
enum State {beforeStart, running, afterEnd, halted}
State public icoState;
constructor(address payable _deposit){
deposit = _deposit;
admin = msg.sender;
icoState = State.beforeStart;
}
modifier onlyAdmin() {
require(msg.msg.sender == admin);
_;
}
function halt() public onlyAdmin{
icoState = State.halted;
}
function resume()public onlyAdmin{
icoState = State.running;
}
function changeDepositAddress(address payable newDeposit) public onlyAdmin{
deposit = newDeposit;
}
function getCurrentState() public view returns(state){
if (icoState == State.halted) {
return State.halted;
}else if(block.timestamp < saleEnd){
return State.beforeStart;
}else if(block.timestamp >= saleStart && block.timestamp <=saleEnd){
return State.running;
}else{
return State.afterEnd;
}
}
event Invest(address inverstor, uint value, uint tokens);
function invest() payable public returns(bool){
icoState = getCurrentState();
require(icoState == State.running);
require(msg.value >= minInvestment && msg.value <= maxInvestment);
raisedAmount += msg.value;
require(raisedAmount <= hardCap);
uint tokens = msg.value / tokenPrice;
balances[msg.sender] += tokens;
balances[founder] -= tokens;
deposit.transfer(msg.value);
emit Invest(msg.sender, msg.value, tokens);
return true;
}
receive() payable external{
invest();
}
function transfer(address to, uint tokens) public override returns(bool success){
require(block.timestamp > tokenTradeStart);
Cryptos.transfer(to, tokens); //same as super.transfer(to, tokens);
return true;
}
function transferFrom(address from, address to, uint tokens) public override returns(bool success){
require(block.timestamp > tokenTradeStart);
Cryptos.transferFrom(from, to, tokens);
return true;
}
function burn() public returns(bool){
icoState = getCurrentState();
require(icoState == State.afterEnd);
balances[founder] = 0;
return true;
}
}
// our ERC 20 TOKEN IS READY TO IMPLEMENT