-
Notifications
You must be signed in to change notification settings - Fork 0
/
TokenSaleXriba.sol
366 lines (284 loc) · 11.3 KB
/
TokenSaleXriba.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
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
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
pragma solidity ^0.4.19;
/**
* @title SafeMath
* @dev Math operations with safety checks that throw on error
*/
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;
}
}
/**
* @title Ownable
* @dev The Ownable contract has an owner address, and provides basic authorization control
* functions, this simplifies the implementation of "user permissions".
*/
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() internal {
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;
}
}
contract tokenInterface {
function balanceOf(address _owner) public constant returns (uint256 balance);
function transfer(address _to, uint256 _value) public returns (bool);
}
contract rateInterface {
function readRate(string _currency) public view returns (uint256 oneEtherValue);
}
contract RC {
using SafeMath for uint256;
TokenSale tokenSaleContract;
uint256 public startTime;
uint256 public endTime;
uint256 public soldTokens;
uint256 public remainingTokens;
uint256 public oneTokenInUsdWei;
function RC(address _tokenSaleContract, uint256 _oneTokenInUsdWei, uint256 _remainingTokens, uint256 _startTime , uint256 _endTime ) public {
require ( _tokenSaleContract != 0 );
require ( _oneTokenInUsdWei != 0 );
require( _remainingTokens != 0 );
tokenSaleContract = TokenSale(_tokenSaleContract);
tokenSaleContract.addMeByRC();
soldTokens = 0;
remainingTokens = _remainingTokens;
oneTokenInUsdWei = _oneTokenInUsdWei;
setTimeRC( _startTime, _endTime );
}
function setTimeRC(uint256 _startTime, uint256 _endTime ) internal {
if( _startTime == 0 ) {
startTime = tokenSaleContract.startTime();
} else {
startTime = _startTime;
}
if( _endTime == 0 ) {
endTime = tokenSaleContract.endTime();
} else {
endTime = _endTime;
}
}
modifier onlyTokenSaleOwner() {
require(msg.sender == tokenSaleContract.owner() );
_;
}
function setTime(uint256 _newStart, uint256 _newEnd) public onlyTokenSaleOwner {
if ( _newStart != 0 ) startTime = _newStart;
if ( _newEnd != 0 ) endTime = _newEnd;
}
event BuyRC(address indexed buyer, bytes trackID, uint256 value, uint256 soldToken, uint256 valueTokenInUsdWei );
function () public payable {
require( now > startTime );
require( now < endTime );
require( msg.value >= 1*10**18); //1 Ether
require( remainingTokens > 0 );
uint256 tokenAmount = tokenSaleContract.buyFromRC.value(msg.value)(msg.sender, oneTokenInUsdWei, remainingTokens);
remainingTokens = remainingTokens.sub(tokenAmount);
soldTokens = soldTokens.add(tokenAmount);
BuyRC( msg.sender, msg.data, msg.value, tokenAmount, oneTokenInUsdWei );
}
}
contract CardSale {
using SafeMath for uint256;
TokenSale tokenSaleContract;
uint256 public startTime;
uint256 public endTime;
uint256 public soldTokens;
uint256 public remainingTokens;
mapping(address => bool) public rc;
function CardSale(address _tokenSaleContract, uint256 _remainingTokens, uint256 _startTime , uint256 _endTime ) public {
require ( _tokenSaleContract != 0 );
require( _remainingTokens != 0 );
tokenSaleContract = TokenSale(_tokenSaleContract);
tokenSaleContract.addMeByRC();
soldTokens = 0;
remainingTokens = _remainingTokens;
setTimeRC( _startTime, _endTime );
}
function setTimeRC(uint256 _startTime, uint256 _endTime ) internal {
if( _startTime == 0 ) {
startTime = tokenSaleContract.startTime();
} else {
startTime = _startTime;
}
if( _endTime == 0 ) {
endTime = tokenSaleContract.endTime();
} else {
endTime = _endTime;
}
}
function owner() view public returns (address) {
return tokenSaleContract.owner();
}
modifier onlyTokenSaleOwner() {
require(msg.sender == owner() );
_;
}
function setTime(uint256 _newStart, uint256 _newEnd) public onlyTokenSaleOwner {
if ( _newStart != 0 ) startTime = _newStart;
if ( _newEnd != 0 ) endTime = _newEnd;
}
event NewRC(address contr);
function addMeByRC() public {
require(tx.origin == owner() );
rc[ msg.sender ] = true;
NewRC(msg.sender);
}
function newCard(uint256 _oneTokenInUsdWei) onlyTokenSaleOwner public {
new RC(this, _oneTokenInUsdWei, remainingTokens, 0, 0 );
}
function () public payable {
revert();
}
modifier onlyRC() {
require( rc[msg.sender] ); //check if is an authorized rcContract
_;
}
function buyFromRC(address _buyer, uint256 _rcTokenValue, uint256 ) onlyRC public payable returns(uint256) {
uint256 tokenAmount = tokenSaleContract.buyFromRC.value(msg.value)(_buyer, _rcTokenValue, remainingTokens);
remainingTokens = remainingTokens.sub(tokenAmount);
soldTokens = soldTokens.add(tokenAmount);
return tokenAmount;
}
}
contract TokenSale is Ownable {
using SafeMath for uint256;
tokenInterface public tokenContract;
rateInterface public rateContract;
address public wallet;
address public advisor;
uint256 public advisorFee; // 1 = 0,1%
uint256 public constant decimals = 18;
uint256 public endTime; // seconds from 1970-01-01T00:00:00Z
uint256 public startTime; // seconds from 1970-01-01T00:00:00Z
mapping(address => bool) public rc;
function TokenSale(address _tokenAddress, address _rateAddress, uint256 _startTime, uint256 _endTime) public {
tokenContract = tokenInterface(_tokenAddress);
rateContract = rateInterface(_rateAddress);
setTime(_startTime, _endTime);
wallet = msg.sender;
advisor = msg.sender;
advisorFee = 0 * 10**3;
}
function tokenValueInEther(uint256 _oneTokenInUsdWei) public view returns(uint256 tknValue) {
uint256 oneEtherInUsd = rateContract.readRate("usd");
tknValue = _oneTokenInUsdWei.mul(10 ** uint256(decimals)).div(oneEtherInUsd);
return tknValue;
}
modifier isBuyable() {
require( now > startTime ); // check if started
require( now < endTime ); // check if ended
require( msg.value > 0 );
uint256 remainingTokens = tokenContract.balanceOf(this);
require( remainingTokens > 0 ); // Check if there are any remaining tokens
_;
}
event Buy(address buyer, uint256 value, address indexed ambassador);
modifier onlyRC() {
require( rc[msg.sender] ); //check if is an authorized rcContract
_;
}
function buyFromRC(address _buyer, uint256 _rcTokenValue, uint256 _remainingTokens) onlyRC isBuyable public payable returns(uint256) {
uint256 oneToken = 10 ** uint256(decimals);
uint256 tokenValue = tokenValueInEther(_rcTokenValue);
uint256 tokenAmount = msg.value.mul(oneToken).div(tokenValue);
address _ambassador = msg.sender;
uint256 remainingTokens = tokenContract.balanceOf(this);
if ( _remainingTokens < remainingTokens ) {
remainingTokens = _remainingTokens;
}
if ( remainingTokens < tokenAmount ) {
uint256 refund = (tokenAmount - remainingTokens).mul(tokenValue).div(oneToken);
tokenAmount = remainingTokens;
forward(msg.value-refund);
remainingTokens = 0; // set remaining token to 0
_buyer.transfer(refund);
} else {
remainingTokens = remainingTokens.sub(tokenAmount); // update remaining token without bonus
forward(msg.value);
}
tokenContract.transfer(_buyer, tokenAmount);
Buy(_buyer, tokenAmount, _ambassador);
return tokenAmount;
}
function forward(uint256 _amount) internal {
uint256 advisorAmount = _amount.mul(advisorFee).div(10**3);
uint256 walletAmount = _amount - advisorAmount;
advisor.transfer(advisorAmount);
wallet.transfer(walletAmount);
}
event NewRC(address contr);
function addMeByRC() public {
require(tx.origin == owner);
rc[ msg.sender ] = true;
NewRC(msg.sender);
}
function setTime(uint256 _newStart, uint256 _newEnd) public onlyOwner {
if ( _newStart != 0 ) startTime = _newStart;
if ( _newEnd != 0 ) endTime = _newEnd;
}
function withdraw(address to, uint256 value) public onlyOwner {
to.transfer(value);
}
function withdrawTokens(address to, uint256 value) public onlyOwner returns (bool) {
return tokenContract.transfer(to, value);
}
function setTokenContract(address _tokenContract) public onlyOwner {
tokenContract = tokenInterface(_tokenContract);
}
function setWalletAddress(address _wallet) public onlyOwner {
wallet = _wallet;
}
function setAdvisorAddress(address _advisor) public onlyOwner {
advisor = _advisor;
}
function setAdvisorFee(uint256 _advisorFee) public onlyOwner {
advisorFee = _advisorFee;
}
function setRateContract(address _rateAddress) public onlyOwner {
rateContract = rateInterface(_rateAddress);
}
function () public payable {
revert();
}
function newRC(uint256 _oneTokenInUsdWei, uint256 _remainingTokens) onlyOwner public {
new RC(this, _oneTokenInUsdWei, _remainingTokens, 0, 0 );
}
}