forked from thakerhriday/SmartContracts
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Cryptos_ICO.sol
204 lines (143 loc) · 6.27 KB
/
Cryptos_ICO.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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
//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);
}
// The Cryptos Token Contract
contract Cryptos is ERC20Interface{
string public name = "Cryptos";
string public symbol = "CRPT";
uint public decimals = 0;
uint public override totalSupply;
address public founder;
mapping(address => uint) public balances;
// balances[0x1111...] = 100;
mapping(address => mapping(address => uint)) allowed;
// 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) public view 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][to] >= tokens);
require(balances[from] >= tokens);
balances[from] -= tokens;
balances[to] += tokens;
allowed[from][to] -= tokens;
return true;
}
}
contract CryptosICO is Cryptos{
address public admin;
address payable public deposit;
uint tokenPrice = 0.001 ether; // 1 ETH = 1000 CRTP, 1 CRPT = 0.001
uint public hardCap = 300 ether;
uint public raisedAmount; // this value will be in wei
uint public saleStart = block.timestamp;
uint public saleEnd = block.timestamp + 604800; //one week
uint public tokenTradeStart = saleEnd + 604800; //transferable in a week after saleEnd
uint public maxInvestment = 5 ether;
uint public minInvestment = 0.1 ether;
enum State { beforeStart, running, afterEnd, halted} // ICO states
State public icoState;
constructor(address payable _deposit){
deposit = _deposit;
admin = msg.sender;
icoState = State.beforeStart;
}
modifier onlyAdmin(){
require(msg.sender == admin);
_;
}
// emergency stop
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 < saleStart){
return State.beforeStart;
}else if(block.timestamp >= saleStart && block.timestamp <= saleEnd){
return State.running;
}else{
return State.afterEnd;
}
}
event Invest(address investor, uint value, uint tokens);
// function called when sending eth to the contract
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;
// adding tokens to the inverstor's balance from the founder's balance
balances[msg.sender] += tokens;
balances[founder] -= tokens;
deposit.transfer(msg.value); // transfering the value sent to the ICO to the deposit address
emit Invest(msg.sender, msg.value, tokens);
return true;
}
// this function is called automatically when someone sends ETH to the contract's address
receive () payable external{
invest();
}
// burning unsold tokens
function burn() public returns(bool){
icoState = getCurrentState();
require(icoState == State.afterEnd);
balances[founder] = 0;
return true;
}
function transfer(address to, uint tokens) public override returns (bool success){
require(block.timestamp > tokenTradeStart); // the token will be transferable only after tokenTradeStart
// calling the transfer function of the base contract
super.transfer(to, tokens); // same as Cryptos.transfer(to, tokens);
return true;
}
function transferFrom(address from, address to, uint tokens) public override returns (bool success){
require(block.timestamp > tokenTradeStart); // the token will be transferable only after tokenTradeStart
Cryptos.transferFrom(from, to, tokens); // same as super.transferFrom(to, tokens);
return true;
}
}