-
Notifications
You must be signed in to change notification settings - Fork 0
/
BTC20Crowdsale
290 lines (236 loc) · 8.52 KB
/
BTC20Crowdsale
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
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
pragma solidity ^0.4.11;
/**
* @title Crowdsale
* @dev Crowdsale is a base contract for managing a token 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.
*/
library SafeMath {
function mul(uint256 a, uint256 b) internal pure returns (uint256) {
uint256 c = a * b;
assert(a == 0 || c / a == b);
return c;
}
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;
}
function sub(uint256 a, uint256 b) internal pure returns (uint256) {
assert(b <= a);
return a - b;
}
function add(uint256 a, uint256 b) internal pure returns (uint256) {
uint256 c = a + b;
assert(c >= a);
return c;
}
}
contract Ownable {
address public owner;
event OwnershipTransferred(address indexed previousOwner, address indexed newOwner);
/**
* @dev The Ownable constructor sets the original `owner` of the contract to the sender
* account.
*/
function Ownable() public {
owner = msg.sender;
}
/**
* @dev Throws if called by any account other than the owner.
*/
modifier onlyOwner() {
require(msg.sender == owner);
_;
}
/**
* @dev Allows the current owner to transfer control of the contract to a newOwner.
* @param newOwner The address to transfer ownership to.
*/
function transferOwnership(address newOwner) onlyOwner public {
require(newOwner != address(0));
OwnershipTransferred(owner, newOwner);
owner = newOwner;
}
}
/**
* @title ERC20Basic
* @dev Simpler version of ERC20 interface
* @dev see https://github.com/ethereum/EIPs/issues/179
*/
contract ERC20Basic {
uint256 public totalSupply;
function balanceOf(address who) constant public returns (uint256);
function transfer(address to, uint256 value) public returns (bool);
event Transfer(address indexed from, address indexed to, uint256 value);
}
/**
* @title Basic token
* @dev Basic version of StandardToken, with no allowances.
*/
contract BasicToken is ERC20Basic {
using SafeMath for uint256;
mapping(address => uint256) tokenBalances;
/**
* @dev transfer token for a specified address
* @param _to The address to transfer to.
* @param _value The amount to be transferred.
*/
function transfer(address _to, uint256 _value) public returns (bool) {
require(tokenBalances[msg.sender]>=_value);
tokenBalances[msg.sender] = tokenBalances[msg.sender].sub(_value);
tokenBalances[_to] = tokenBalances[_to].add(_value);
Transfer(msg.sender, _to, _value);
return true;
}
/**
* @dev Gets the balance of the specified address.
* @param _owner The address to query the the balance of.
* @return An uint256 representing the amount owned by the passed address.
*/
function balanceOf(address _owner) constant public returns (uint256 balance) {
return tokenBalances[_owner];
}
}
contract BTC20Token is BasicToken,Ownable {
using SafeMath for uint256;
//TODO: Change the name and the symbol
string public constant name = "BTC20";
string public constant symbol = "BTC20";
uint256 public constant decimals = 18;
uint256 public constant INITIAL_SUPPLY = 21000000;
event Debug(string message, address addr, uint256 number);
/**
* @dev Contructor that gives msg.sender all of existing tokens.
*/
function BTC20Token(address wallet) public {
owner = msg.sender;
totalSupply = INITIAL_SUPPLY * 10 ** 18;
tokenBalances[wallet] = totalSupply; //Since we divided the token into 10^18 parts
}
function mint(address wallet, address buyer, uint256 tokenAmount) public onlyOwner {
require(tokenBalances[wallet] >= tokenAmount); // checks if it has enough to sell
tokenBalances[buyer] = tokenBalances[buyer].add(tokenAmount); // adds the amount to buyer's balance
tokenBalances[wallet] = tokenBalances[wallet].sub(tokenAmount); // subtracts amount from seller's balance
Transfer(wallet, buyer, tokenAmount);
}
function showMyTokenBalance(address addr) public view returns (uint tokenBalance) {
tokenBalance = tokenBalances[addr];
}
}
contract BTC20Crowdsale {
using SafeMath for uint256;
// The token being sold
BTC20Token public token;
// start and end timestamps where investments are allowed (both inclusive)
uint256 public startTime;
uint256 public endTime;
// address where funds are collected
// address where tokens are deposited and from where we send tokens to buyers
address public wallet;
// how many token units a buyer gets per wei
uint256 public ratePerWei = 50000;
// amount of raised money in wei
uint256 public weiRaised;
uint256 TOKENS_SOLD;
uint256 maxTokensToSale = 15000000 * 10 ** 18;
uint256 minimumContribution = 5 * 10 ** 16; //0.05 is the minimum contribution
/**
* 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);
function BTC20Crowdsale(uint256 _startTime, address _wallet) public
{
startTime = _startTime;
endTime = startTime + 14 days;
require(endTime >= startTime);
require(_wallet != 0x0);
wallet = _wallet;
token = createTokenContract(wallet);
}
// creates the token to be sold.
function createTokenContract(address wall) internal returns (BTC20Token) {
return new BTC20Token(wall);
}
// fallback function can be used to buy tokens
function () public payable {
buyTokens(msg.sender);
}
//determine the rate of the token w.r.t. time elapsed
function determineBonus(uint tokens) internal view returns (uint256 bonus) {
uint256 timeElapsed = now - startTime;
uint256 timeElapsedInWeeks = timeElapsed.div(7 days);
if (timeElapsedInWeeks == 0)
{
bonus = tokens.mul(50); //50% bonus
bonus = bonus.div(100);
}
else if (timeElapsedInWeeks == 1)
{
bonus = tokens.mul(25); //25% bonus
bonus = bonus.div(100);
}
else
{
bonus = 0; //No tokens to be transferred - ICO time is over
}
}
// low level token purchase function
// Minimum purchase can be of 1 ETH
function buyTokens(address beneficiary) public payable {
require(beneficiary != 0x0);
require(validPurchase());
require(msg.value>= minimumContribution);
require(TOKENS_SOLD<maxTokensToSale);
uint256 weiAmount = msg.value;
// calculate token amount to be created
uint256 tokens = weiAmount.mul(ratePerWei);
uint256 bonus = determineBonus(tokens);
tokens = tokens.add(bonus);
require(TOKENS_SOLD+tokens<=maxTokensToSale);
// update state
weiRaised = weiRaised.add(weiAmount);
token.mint(wallet, beneficiary, tokens);
TokenPurchase(msg.sender, beneficiary, weiAmount, tokens);
TOKENS_SOLD = TOKENS_SOLD.add(tokens);
forwardFunds();
}
// 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
function validPurchase() internal constant returns (bool) {
bool withinPeriod = now >= startTime && now <= endTime;
bool nonZeroPurchase = msg.value != 0;
return withinPeriod && nonZeroPurchase;
}
// @return true if crowdsale event has ended
function hasEnded() public constant returns (bool) {
return now > endTime;
}
function changeEndDate(uint256 endTimeUnixTimestamp) public returns(bool) {
require (msg.sender == wallet);
endTime = endTimeUnixTimestamp;
}
function changeStartDate(uint256 startTimeUnixTimestamp) public returns(bool) {
require (msg.sender == wallet);
startTime = startTimeUnixTimestamp;
}
function setPriceRate(uint256 newPrice) public returns (bool) {
require (msg.sender == wallet);
ratePerWei = newPrice;
}
function changeMinimumContribution(uint256 minContribution) public returns (bool) {
require (msg.sender == wallet);
minimumContribution = minContribution * 10 ** 15;
}
}