-
Notifications
You must be signed in to change notification settings - Fork 3
/
trx.sol
484 lines (388 loc) · 20.8 KB
/
trx.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
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
pragma solidity 0.5.10;
/**
*
* ,d8888b
* 88P'
*d888888P
* ?88' d8888b 88bd88b .d888b, d888b8b d888b8b d8888b
* 88P d8P' ?88 88P' ` ?8b, d8P' ?88 d8P' ?88 d8b_,dP
* d88 88b d88 d88 `?8b 88b ,88b 88b ,88b 88b
*d88' `?8888P'd88' `?888P' `?88P'`88b`?88P'`88b`?888P'
* )88
* ,88P
* `?8888P
*
*
* FORSAGE TRX COMMUNITY
* https://trx.forsage.io
* https://tronscan.org/#/contract/TREbha3Jj6TrpT7e6Z5ukh3NRhyxHsmMug/code
**/
contract XGOLD {
function deposit(address sender, address referrer) public payable;
}
contract FORSAGE_TRX_COMMUNITY {
struct User {
uint id;
address referrer;
uint partnersCount;
mapping(uint8 => bool) activeX3Levels;
mapping(uint8 => bool) activeX6Levels;
mapping(uint8 => X3) x3Matrix;
mapping(uint8 => X6) x6Matrix;
}
struct X3 {
address currentReferrer;
address[] referrals;
bool blocked;
uint reinvestCount;
}
struct X6 {
address currentReferrer;
address[] firstLevelReferrals;
address[] secondLevelReferrals;
bool blocked;
uint reinvestCount;
address closedPart;
}
uint8 public currentStartingLevel = 1;
uint8 public constant LAST_LEVEL = 15;
mapping(address => User) public users;
mapping(uint => address) public idToAddress;
uint public lastUserId = 2;
address public owner;
mapping(uint8 => uint) public levelPrice;
XGOLD public xGOLD;
event Registration(address indexed user, address indexed referrer, uint indexed userId, uint referrerId);
event Reinvest(address indexed user, address indexed currentReferrer, address indexed caller, uint8 matrix, uint8 level);
event Upgrade(address indexed user, address indexed referrer, uint8 matrix, uint8 level);
event NewUserPlace(address indexed user, address indexed referrer, uint8 matrix, uint8 level, uint8 place);
event MissedEthReceive(address indexed receiver, address indexed from, uint8 matrix, uint8 level);
event SentExtraEthDividends(address indexed from, address indexed receiver, uint8 matrix, uint8 level);
constructor(address ownerAddress) public {
levelPrice[1] = 100 trx;
levelPrice[2] = 200 trx;
levelPrice[3] = 400 trx;
levelPrice[4] = 800 trx;
levelPrice[5] = 1600 trx;
levelPrice[6] = 3200 trx;
levelPrice[7] = 6400 trx;
levelPrice[8] = 12800 trx;
levelPrice[9] = 25600 trx;
levelPrice[10] = 51200 trx;
levelPrice[11] = 102400 trx;
levelPrice[12] = 204800 trx;
levelPrice[13] = 358400 trx;
levelPrice[14] = 627200 trx;
levelPrice[15] = 1097600 trx;
owner = ownerAddress;
User memory user = User({
id: 1,
referrer: address(0),
partnersCount: uint(0)
});
users[ownerAddress] = user;
idToAddress[1] = ownerAddress;
for (uint8 i = 1; i <= LAST_LEVEL; i++) {
users[ownerAddress].activeX3Levels[i] = true;
users[ownerAddress].activeX6Levels[i] = true;
}
}
function() external payable {
if(msg.data.length == 0) {
return registration(msg.sender, owner);
}
registration(msg.sender, bytesToAddress(msg.data));
}
function setXGold(address xGoldAddress) public {
require(msg.sender == 0x606527eCB96eD08f776c5220aFE8f41474772934, "onlyOwner");
require(address(xGOLD) == address(0));
xGOLD = XGOLD(xGoldAddress);
}
function withdrawLostTRXFromBalance() public {
require(msg.sender == 0x606527eCB96eD08f776c5220aFE8f41474772934, "onlyOwner");
0x606527eCB96eD08f776c5220aFE8f41474772934.transfer(address(this).balance);
}
function registrationExt(address referrerAddress) external payable {
registration(msg.sender, referrerAddress);
}
function buyNewLevel(uint8 matrix, uint8 level) external payable {
require(isUserExists(msg.sender), "user is not exists. Register first.");
require(matrix == 1 || matrix == 2, "invalid matrix");
require(msg.value == levelPrice[level], "invalid price");
require(level > 1 && level <= LAST_LEVEL, "invalid level");
if (matrix == 1) {
require(users[msg.sender].activeX3Levels[level-1], "buy previous level first");
require(!users[msg.sender].activeX3Levels[level], "level already activated");
if (users[msg.sender].x3Matrix[level-1].blocked) {
users[msg.sender].x3Matrix[level-1].blocked = false;
}
address freeX3Referrer = findFreeX3Referrer(msg.sender, level);
users[msg.sender].x3Matrix[level].currentReferrer = freeX3Referrer;
users[msg.sender].activeX3Levels[level] = true;
updateX3Referrer(msg.sender, freeX3Referrer, level);
emit Upgrade(msg.sender, freeX3Referrer, 1, level);
} else {
require(users[msg.sender].activeX6Levels[level-1], "buy previous level first");
require(!users[msg.sender].activeX6Levels[level], "level already activated");
if (users[msg.sender].x6Matrix[level-1].blocked) {
users[msg.sender].x6Matrix[level-1].blocked = false;
}
address freeX6Referrer = findFreeX6Referrer(msg.sender, level);
users[msg.sender].activeX6Levels[level] = true;
updateX6Referrer(msg.sender, freeX6Referrer, level);
emit Upgrade(msg.sender, freeX6Referrer, 2, level);
}
}
function registration(address userAddress, address referrerAddress) private {
require(!isUserExists(userAddress), "user exists");
require(isUserExists(referrerAddress), "referrer not exists");
uint32 size;
assembly {
size := extcodesize(userAddress)
}
require(size == 0, "cannot be a contract");
if (address(xGOLD) != address(0)) {
xGOLD.deposit(userAddress, referrerAddress);
require(msg.value == levelPrice[currentStartingLevel] * 3, "invalid registration cost");
} else {
require(msg.value == levelPrice[currentStartingLevel] * 2, "invalid registration cost");
}
User memory user = User({
id: lastUserId,
referrer: referrerAddress,
partnersCount: 0
});
users[userAddress] = user;
idToAddress[lastUserId] = userAddress;
users[userAddress].referrer = referrerAddress;
users[userAddress].activeX3Levels[1] = true;
users[userAddress].activeX6Levels[1] = true;
lastUserId++;
users[referrerAddress].partnersCount++;
address freeX3Referrer = findFreeX3Referrer(userAddress, 1);
users[userAddress].x3Matrix[1].currentReferrer = freeX3Referrer;
updateX3Referrer(userAddress, freeX3Referrer, 1);
updateX6Referrer(userAddress, findFreeX6Referrer(userAddress, 1), 1);
emit Registration(userAddress, referrerAddress, users[userAddress].id, users[referrerAddress].id);
}
function updateX3Referrer(address userAddress, address referrerAddress, uint8 level) private {
users[referrerAddress].x3Matrix[level].referrals.push(userAddress);
if (users[referrerAddress].x3Matrix[level].referrals.length < 3) {
emit NewUserPlace(userAddress, referrerAddress, 1, level, uint8(users[referrerAddress].x3Matrix[level].referrals.length));
return sendETHDividends(referrerAddress, userAddress, 1, level);
}
emit NewUserPlace(userAddress, referrerAddress, 1, level, 3);
//close matrix
users[referrerAddress].x3Matrix[level].referrals = new address[](0);
if (!users[referrerAddress].activeX3Levels[level+1] && level != LAST_LEVEL) {
users[referrerAddress].x3Matrix[level].blocked = true;
}
//create new one by recursion
if (referrerAddress != owner) {
//check referrer active level
address freeReferrerAddress = findFreeX3Referrer(referrerAddress, level);
if (users[referrerAddress].x3Matrix[level].currentReferrer != freeReferrerAddress) {
users[referrerAddress].x3Matrix[level].currentReferrer = freeReferrerAddress;
}
users[referrerAddress].x3Matrix[level].reinvestCount++;
emit Reinvest(referrerAddress, freeReferrerAddress, userAddress, 1, level);
updateX3Referrer(referrerAddress, freeReferrerAddress, level);
} else {
sendETHDividends(owner, userAddress, 1, level);
users[owner].x3Matrix[level].reinvestCount++;
emit Reinvest(owner, address(0), userAddress, 1, level);
}
}
function updateX6Referrer(address userAddress, address referrerAddress, uint8 level) private {
require(users[referrerAddress].activeX6Levels[level], "500. Referrer level is inactive");
if (users[referrerAddress].x6Matrix[level].firstLevelReferrals.length < 2) {
users[referrerAddress].x6Matrix[level].firstLevelReferrals.push(userAddress);
emit NewUserPlace(userAddress, referrerAddress, 2, level, uint8(users[referrerAddress].x6Matrix[level].firstLevelReferrals.length));
//set current level
users[userAddress].x6Matrix[level].currentReferrer = referrerAddress;
if (referrerAddress == owner) {
return sendETHDividends(referrerAddress, userAddress, 2, level);
}
address ref = users[referrerAddress].x6Matrix[level].currentReferrer;
users[ref].x6Matrix[level].secondLevelReferrals.push(userAddress);
uint len = users[ref].x6Matrix[level].firstLevelReferrals.length;
if ((len == 2) &&
(users[ref].x6Matrix[level].firstLevelReferrals[0] == referrerAddress) &&
(users[ref].x6Matrix[level].firstLevelReferrals[1] == referrerAddress)) {
if (users[referrerAddress].x6Matrix[level].firstLevelReferrals.length == 1) {
emit NewUserPlace(userAddress, ref, 2, level, 5);
} else {
emit NewUserPlace(userAddress, ref, 2, level, 6);
}
} else if ((len == 1 || len == 2) &&
users[ref].x6Matrix[level].firstLevelReferrals[0] == referrerAddress) {
if (users[referrerAddress].x6Matrix[level].firstLevelReferrals.length == 1) {
emit NewUserPlace(userAddress, ref, 2, level, 3);
} else {
emit NewUserPlace(userAddress, ref, 2, level, 4);
}
} else if (len == 2 && users[ref].x6Matrix[level].firstLevelReferrals[1] == referrerAddress) {
if (users[referrerAddress].x6Matrix[level].firstLevelReferrals.length == 1) {
emit NewUserPlace(userAddress, ref, 2, level, 5);
} else {
emit NewUserPlace(userAddress, ref, 2, level, 6);
}
}
return updateX6ReferrerSecondLevel(userAddress, ref, level);
}
users[referrerAddress].x6Matrix[level].secondLevelReferrals.push(userAddress);
if (users[referrerAddress].x6Matrix[level].closedPart != address(0)) {
if ((users[referrerAddress].x6Matrix[level].firstLevelReferrals[0] ==
users[referrerAddress].x6Matrix[level].firstLevelReferrals[1]) &&
(users[referrerAddress].x6Matrix[level].firstLevelReferrals[0] ==
users[referrerAddress].x6Matrix[level].closedPart)) {
updateX6(userAddress, referrerAddress, level, true);
return updateX6ReferrerSecondLevel(userAddress, referrerAddress, level);
} else if (users[referrerAddress].x6Matrix[level].firstLevelReferrals[0] ==
users[referrerAddress].x6Matrix[level].closedPart) {
updateX6(userAddress, referrerAddress, level, true);
return updateX6ReferrerSecondLevel(userAddress, referrerAddress, level);
} else {
updateX6(userAddress, referrerAddress, level, false);
return updateX6ReferrerSecondLevel(userAddress, referrerAddress, level);
}
}
if (users[referrerAddress].x6Matrix[level].firstLevelReferrals[1] == userAddress) {
updateX6(userAddress, referrerAddress, level, false);
return updateX6ReferrerSecondLevel(userAddress, referrerAddress, level);
} else if (users[referrerAddress].x6Matrix[level].firstLevelReferrals[0] == userAddress) {
updateX6(userAddress, referrerAddress, level, true);
return updateX6ReferrerSecondLevel(userAddress, referrerAddress, level);
}
if (users[users[referrerAddress].x6Matrix[level].firstLevelReferrals[0]].x6Matrix[level].firstLevelReferrals.length <=
users[users[referrerAddress].x6Matrix[level].firstLevelReferrals[1]].x6Matrix[level].firstLevelReferrals.length) {
updateX6(userAddress, referrerAddress, level, false);
} else {
updateX6(userAddress, referrerAddress, level, true);
}
updateX6ReferrerSecondLevel(userAddress, referrerAddress, level);
}
function updateX6(address userAddress, address referrerAddress, uint8 level, bool x2) private {
if (!x2) {
users[users[referrerAddress].x6Matrix[level].firstLevelReferrals[0]].x6Matrix[level].firstLevelReferrals.push(userAddress);
emit NewUserPlace(userAddress, users[referrerAddress].x6Matrix[level].firstLevelReferrals[0], 2, level, uint8(users[users[referrerAddress].x6Matrix[level].firstLevelReferrals[0]].x6Matrix[level].firstLevelReferrals.length));
emit NewUserPlace(userAddress, referrerAddress, 2, level, 2 + uint8(users[users[referrerAddress].x6Matrix[level].firstLevelReferrals[0]].x6Matrix[level].firstLevelReferrals.length));
//set current level
users[userAddress].x6Matrix[level].currentReferrer = users[referrerAddress].x6Matrix[level].firstLevelReferrals[0];
} else {
users[users[referrerAddress].x6Matrix[level].firstLevelReferrals[1]].x6Matrix[level].firstLevelReferrals.push(userAddress);
emit NewUserPlace(userAddress, users[referrerAddress].x6Matrix[level].firstLevelReferrals[1], 2, level, uint8(users[users[referrerAddress].x6Matrix[level].firstLevelReferrals[1]].x6Matrix[level].firstLevelReferrals.length));
emit NewUserPlace(userAddress, referrerAddress, 2, level, 4 + uint8(users[users[referrerAddress].x6Matrix[level].firstLevelReferrals[1]].x6Matrix[level].firstLevelReferrals.length));
//set current level
users[userAddress].x6Matrix[level].currentReferrer = users[referrerAddress].x6Matrix[level].firstLevelReferrals[1];
}
}
function updateX6ReferrerSecondLevel(address userAddress, address referrerAddress, uint8 level) private {
if (users[referrerAddress].x6Matrix[level].secondLevelReferrals.length < 4) {
return sendETHDividends(referrerAddress, userAddress, 2, level);
}
address[] memory x6 = users[users[referrerAddress].x6Matrix[level].currentReferrer].x6Matrix[level].firstLevelReferrals;
if (x6.length == 2) {
if (x6[0] == referrerAddress ||
x6[1] == referrerAddress) {
users[users[referrerAddress].x6Matrix[level].currentReferrer].x6Matrix[level].closedPart = referrerAddress;
} else if (x6.length == 1) {
if (x6[0] == referrerAddress) {
users[users[referrerAddress].x6Matrix[level].currentReferrer].x6Matrix[level].closedPart = referrerAddress;
}
}
}
users[referrerAddress].x6Matrix[level].firstLevelReferrals = new address[](0);
users[referrerAddress].x6Matrix[level].secondLevelReferrals = new address[](0);
users[referrerAddress].x6Matrix[level].closedPart = address(0);
if (!users[referrerAddress].activeX6Levels[level+1] && level != LAST_LEVEL) {
users[referrerAddress].x6Matrix[level].blocked = true;
}
users[referrerAddress].x6Matrix[level].reinvestCount++;
if (referrerAddress != owner) {
address freeReferrerAddress = findFreeX6Referrer(referrerAddress, level);
emit Reinvest(referrerAddress, freeReferrerAddress, userAddress, 2, level);
updateX6Referrer(referrerAddress, freeReferrerAddress, level);
} else {
emit Reinvest(owner, address(0), userAddress, 2, level);
sendETHDividends(owner, userAddress, 2, level);
}
}
function findFreeX3Referrer(address userAddress, uint8 level) public view returns(address) {
while (true) {
if (users[users[userAddress].referrer].activeX3Levels[level]) {
return users[userAddress].referrer;
}
userAddress = users[userAddress].referrer;
}
}
function findFreeX6Referrer(address userAddress, uint8 level) public view returns(address) {
while (true) {
if (users[users[userAddress].referrer].activeX6Levels[level]) {
return users[userAddress].referrer;
}
userAddress = users[userAddress].referrer;
}
}
function usersActiveX3Levels(address userAddress, uint8 level) public view returns(bool) {
return users[userAddress].activeX3Levels[level];
}
function usersActiveX6Levels(address userAddress, uint8 level) public view returns(bool) {
return users[userAddress].activeX6Levels[level];
}
function usersX3Matrix(address userAddress, uint8 level) public view returns(address, address[] memory, bool) {
return (users[userAddress].x3Matrix[level].currentReferrer,
users[userAddress].x3Matrix[level].referrals,
users[userAddress].x3Matrix[level].blocked);
}
function usersX6Matrix(address userAddress, uint8 level) public view returns(address, address[] memory, address[] memory, bool, address) {
return (users[userAddress].x6Matrix[level].currentReferrer,
users[userAddress].x6Matrix[level].firstLevelReferrals,
users[userAddress].x6Matrix[level].secondLevelReferrals,
users[userAddress].x6Matrix[level].blocked,
users[userAddress].x6Matrix[level].closedPart);
}
function isUserExists(address user) public view returns (bool) {
return (users[user].id != 0);
}
function findEthReceiver(address userAddress, address _from, uint8 matrix, uint8 level) private returns(address, bool) {
address receiver = userAddress;
bool isExtraDividends;
if (matrix == 1) {
while (true) {
if (users[receiver].x3Matrix[level].blocked) {
emit MissedEthReceive(receiver, _from, 1, level);
isExtraDividends = true;
receiver = users[receiver].x3Matrix[level].currentReferrer;
} else {
return (receiver, isExtraDividends);
}
}
} else {
while (true) {
if (users[receiver].x6Matrix[level].blocked) {
emit MissedEthReceive(receiver, _from, 2, level);
isExtraDividends = true;
receiver = users[receiver].x6Matrix[level].currentReferrer;
} else {
return (receiver, isExtraDividends);
}
}
}
}
function sendETHDividends(address userAddress, address _from, uint8 matrix, uint8 level) private {
(address receiver, bool isExtraDividends) = findEthReceiver(userAddress, _from, matrix, level);
if (!address(uint160(receiver)).send(levelPrice[level])) {
address(uint160(owner)).send(address(this).balance);
return;
}
if (isExtraDividends) {
emit SentExtraEthDividends(_from, receiver, matrix, level);
}
}
function bytesToAddress(bytes memory bys) private pure returns (address addr) {
assembly {
addr := mload(add(bys, 20))
}
}
}