-
Notifications
You must be signed in to change notification settings - Fork 1
/
WINKCrowdsale tiered.sol
240 lines (191 loc) · 7.56 KB
/
WINKCrowdsale tiered.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
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
pragma solidity ^0.4.18;
contract Token {
function SetupToken(string tokenName, string tokenSymbol, uint256 tokenSupply) public;
function balanceOf(address _owner) public constant returns (uint256 balance);
function transfer(address _to, uint256 _amount) public returns (bool success);
function transferFrom(address _from, address _to, uint256 _amount) public returns (bool success);
function approve(address _spender, uint256 _amount) public returns (bool success);
function allowance(address _owner, address _spender) public constant returns (uint256 remaining);
}
pragma solidity ^0.4.18;
/**
* @title SafeMath
* @dev Math operations with safety checks that throw on error
*/
library SafeMath {
/**
* @dev Multiplies two numbers, throws on overflow.
*/
function mul(uint256 a, uint256 b) internal pure returns (uint256) {
if (a == 0) {
return 0;
}
uint256 c = a * b;
assert(c / a == b);
return c;
}
/**
* @dev Integer division of two numbers, truncating the quotient.
*/
function div(uint256 a, uint256 b) internal pure returns (uint256) {
// assert(b > 0); // Solidity automatically throws when dividing by 0
uint256 c = a / b;
// assert(a == b * c + a % b); // There is no case in which this doesn't hold
return c;
}
/**
* @dev Substracts two numbers, throws on overflow (i.e. if subtrahend is greater than minuend).
*/
function sub(uint256 a, uint256 b) internal pure returns (uint256) {
assert(b <= a);
return a - b;
}
/**
* @dev Adds two numbers, throws on overflow.
*/
function add(uint256 a, uint256 b) internal pure returns (uint256) {
uint256 c = a + b;
assert(c >= a);
return c;
}
}
/**
* @title Crowdsale *Modded*
* @dev Crowdsale is a base contract for managing a token crowdsale.
* Author: https://github.com/OpenZeppelin/zeppelin-solidity/tree/master/contracts/crowdsale
* Crowdsales have a start and end timestamps, where investors can make
* token purchases and the crowdsale will assign them tokens based
* on a token per ETH rate. Funds collected are forwarded to a wallet
* as they arrive.
* Modded to use preminted token contract, and graft in capped crowdsale code from the openZepplin github
*/
contract WinkIfYouLikeIt {
using SafeMath for uint256;
Token token;
address public owner;
// public cap in wei : when initialized, its per ether
uint256 public cap;
// start and end timestamps where investments are allowed (both inclusive)
uint256 public startTime;
uint256 public endTime;
// address where funds are collected
address public wallet;
// how many token units a buyer gets per wei : when initialized, its per ether
uint256 public rate;
// amount of raised money in wei
uint256 public weiRaised;
// amount of raised money in current tier in wei
uint256 public tierTotal;
//tier count
uint256 public tierNum = 0;
/* Funding Tiers
* Tier Rave/ETH Rave Limit Eth Limit
* One 6000 2.000.000 333,3333
* Two 5500 5.000.000 909,0909
* Three 5000 9.000.000 1800,0000
* Four 4500 14.000.000 3111,1111
* Five 4000 20.000.000 5000,0000
*/
uint256[5] fundingRate = [100000, 20000, 5000, 2000, 1000]; //Rave per Eth
//uint256[5] fundingRate = [6000, 5500, 5000, 4500, 4000]; //Rave per Eth
uint256[5] fundingLimit = [100000, 20000, 5000, 2000, 1000]; //Max Rave Available per tier
//uint256[5] fundingLimit = [2000000, 5000000, 9000000, 14000000, 20000000]; //Max Rave Available per tier
/**
* event for token purchase logging
* @param purchaser who paid for the tokens
* @param beneficiary who got the tokens
* @param value weis paid for purchase
* @param amount amount of tokens purchased
*/
event TokenPurchase(address indexed purchaser, address indexed beneficiary, uint256 value, uint256 amount);
event FailedTransfer(address indexed to, uint256 value);
event initialCrowdsale(uint256 _startTime, uint256 _endTime, uint256 _cap, uint256 cap, uint256 _rate, uint256 rate, address _wallet);
function WinkIfYouLikeIt(uint256 _startTime, uint256 _endTime, uint256 _cap, address _wallet) public {
require(_startTime >= now);
require(_endTime >= _startTime);
require(_cap > 0);
require(_wallet != address(0));
owner = msg.sender;
address _tokenAddr = 0x47fCa57ECb412BAC972466486ADdA1704EEEBeD7; //Token Contract Address
token = Token(_tokenAddr);
startTime = _startTime;
endTime = _endTime;
rate = fundingRate[tierNum];
cap = _cap.mul(1 ether);
wallet = _wallet;
initialCrowdsale(_startTime, _endTime, _cap, cap, fundingRate[tierNum], rate, _wallet);
}
// fallback function can be used to buy tokens
function () external payable {
buyTokens(msg.sender);
}
// low level token purchase function
function buyTokens(address beneficiary) public payable {
require(beneficiary != address(0));
require(validPurchase());
uint256 weiAmount = msg.value;
// calculate token amount to be sent in wei
uint256 tokens = getTokenAmount(weiAmount);
// update state
weiRaised = weiRaised.add(weiAmount);
tierTotal = tierTotal.add(weiAmount);
// Check balance of contract
token.transfer(beneficiary, tokens);
TokenPurchase(msg.sender, beneficiary, weiAmount, tokens);
forwardFunds();
//upgrade rate tier check
rateUpgrade(tierTotal);
}
// @return true if crowdsale event has ended & limit has not been reached
function hasEnded() public view returns (bool) {
bool capReached = weiRaised >= cap;
bool timeLimit = now > endTime;
return capReached || timeLimit;
}
// If weiAmountRaised is over tier thresholds, then upgrade rave per eth
function rateUpgrade(uint256 tierAmount) internal {
uint256 tierEthLimit = fundingLimit[tierNum].div(fundingRate[tierNum]);
uint256 tierWeiLimit = tierEthLimit.mul(1 ether);
if(tierAmount >= tierWeiLimit) {
tierNum = tierNum.add(1); //increment tier number
rate = fundingRate[tierNum]; // set new rate in wei
tierTotal = 0; //reset to 0 wei
}
}
// Override this method to have a way to add business logic to your crowdsale when buying
function getTokenAmount(uint256 weiAmount) internal view returns(uint256) {
return weiAmount.mul(rate);
}
// send ether to the fund collection wallet
// override to create custom fund forwarding mechanisms
function forwardFunds() internal {
wallet.transfer(msg.value);
}
// @return true if the transaction can buy tokens & within cap & nonzero
function validPurchase() internal view returns (bool) {
bool withinCap = weiRaised.add(msg.value) <= cap;
bool withinPeriod = now >= startTime && now <= endTime;
bool nonZeroPurchase = msg.value != 0;
return withinPeriod && withinCap && nonZeroPurchase;
}
function tokensAvailable() public onlyOwner constant returns (uint256) {
return token.balanceOf(this);
}
function getRate() public onlyOwner constant returns(uint256) {
return rate;
}
function getWallet() public onlyOwner constant returns(address) {
return wallet;
}
function destroy() public onlyOwner payable {
uint256 balance = tokensAvailable();
if(balance > 0) {
token.transfer(owner, balance);
}
selfdestruct(owner);
}
modifier onlyOwner() {
require(msg.sender == owner);
_;
}
}