文件 1 的 1:OyesMatrix.sol
pragma solidity >=0.4.23 <0.6.0;
contract OyesMatrix {
struct User {
uint id;
address referrer;
uint partnersCount;
mapping(uint8 => bool) activeO3Levels;
mapping(uint8 => bool) activeO6Levels;
mapping(uint8 => O3) O3Matrix;
mapping(uint8 => O6) O6Matrix;
}
struct O3 {
address currentReferrer;
address[] referrals;
bool blocked;
uint reinvestCount;
}
struct O6 {
address currentReferrer;
address[] firstLevelReferrals;
address[] secondLevelReferrals;
bool blocked;
uint reinvestCount;
address closedPart;
}
uint8 public constant LAST_LEVEL = 12;
mapping(address => User) public users;
mapping(uint => address) public idToAddress;
mapping(uint => address) public userIds;
mapping(address => uint) public balances;
uint public lastUserId = 2;
address public owner;
mapping(uint8 => uint) public levelPrice;
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] = 0.025 ether;
for (uint8 i = 2; i <= LAST_LEVEL; i++) {
levelPrice[i] = levelPrice[i-1] * 2;
}
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].activeO3Levels[i] = true;
users[ownerAddress].activeO6Levels[i] = true;
}
userIds[1] = ownerAddress;
}
function() external payable {
if(msg.data.length == 0) {
return registration(msg.sender, owner);
}
registration(msg.sender, bytesToAddress(msg.data));
}
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].activeO3Levels[level], "level already activated");
if (users[msg.sender].O3Matrix[level-1].blocked) {
users[msg.sender].O3Matrix[level-1].blocked = false;
}
address freeO3Referrer = findFreeO3Referrer(msg.sender, level);
users[msg.sender].O3Matrix[level].currentReferrer = freeO3Referrer;
users[msg.sender].activeO3Levels[level] = true;
updateO3Referrer(msg.sender, freeO3Referrer, level);
emit Upgrade(msg.sender, freeO3Referrer, 1, level);
} else {
require(!users[msg.sender].activeO6Levels[level], "level already activated");
if (users[msg.sender].O6Matrix[level-1].blocked) {
users[msg.sender].O6Matrix[level-1].blocked = false;
}
address freeO6Referrer = findFreeO6Referrer(msg.sender, level);
users[msg.sender].activeO6Levels[level] = true;
updateO6Referrer(msg.sender, freeO6Referrer, level);
emit Upgrade(msg.sender, freeO6Referrer, 2, level);
}
}
function registration(address userAddress, address referrerAddress) private {
require(msg.value == 0.05 ether, "registration cost 0.05");
require(!isUserExists(userAddress), "user exists");
require(isUserExists(referrerAddress), "referrer not exists");
uint32 size;
assembly {
size := extcodesize(userAddress)
}
require(size == 0, "cannot be a contract");
User memory user = User({
id: lastUserId,
referrer: referrerAddress,
partnersCount: 0
});
users[userAddress] = user;
idToAddress[lastUserId] = userAddress;
users[userAddress].referrer = referrerAddress;
users[userAddress].activeO3Levels[1] = true;
users[userAddress].activeO6Levels[1] = true;
userIds[lastUserId] = userAddress;
lastUserId++;
users[referrerAddress].partnersCount++;
address freeO3Referrer = findFreeO3Referrer(userAddress, 1);
users[userAddress].O3Matrix[1].currentReferrer = freeO3Referrer;
updateO3Referrer(userAddress, freeO3Referrer, 1);
updateO6Referrer(userAddress, findFreeO6Referrer(userAddress, 1), 1);
emit Registration(userAddress, referrerAddress, users[userAddress].id, users[referrerAddress].id);
}
function updateO3Referrer(address userAddress, address referrerAddress, uint8 level) private {
users[referrerAddress].O3Matrix[level].referrals.push(userAddress);
if (users[referrerAddress].O3Matrix[level].referrals.length < 3) {
emit NewUserPlace(userAddress, referrerAddress, 1, level, uint8(users[referrerAddress].O3Matrix[level].referrals.length));
return sendETHDividends(referrerAddress, userAddress, 1, level);
}
emit NewUserPlace(userAddress, referrerAddress, 1, level, 3);
users[referrerAddress].O3Matrix[level].referrals = new address[](0);
if (!users[referrerAddress].activeO3Levels[level+1] && level != LAST_LEVEL) {
users[referrerAddress].O3Matrix[level].blocked = true;
}
if (referrerAddress != owner) {
address freeReferrerAddress = findFreeO3Referrer(referrerAddress, level);
if (users[referrerAddress].O3Matrix[level].currentReferrer != freeReferrerAddress) {
users[referrerAddress].O3Matrix[level].currentReferrer = freeReferrerAddress;
}
users[referrerAddress].O3Matrix[level].reinvestCount++;
emit Reinvest(referrerAddress, freeReferrerAddress, userAddress, 1, level);
updateO3Referrer(referrerAddress, freeReferrerAddress, level);
} else {
sendETHDividends(owner, userAddress, 1, level);
users[owner].O3Matrix[level].reinvestCount++;
emit Reinvest(owner, address(0), userAddress, 1, level);
}
}
function updateO6Referrer(address userAddress, address referrerAddress, uint8 level) private {
require(users[referrerAddress].activeO6Levels[level], "500. Referrer level is inactive");
if (users[referrerAddress].O6Matrix[level].firstLevelReferrals.length < 2) {
users[referrerAddress].O6Matrix[level].firstLevelReferrals.push(userAddress);
emit NewUserPlace(userAddress, referrerAddress, 2, level, uint8(users[referrerAddress].O6Matrix[level].firstLevelReferrals.length));
users[userAddress].O6Matrix[level].currentReferrer = referrerAddress;
if (referrerAddress == owner) {
return sendETHDividends(referrerAddress, userAddress, 2, level);
}
address ref = users[referrerAddress].O6Matrix[level].currentReferrer;
users[ref].O6Matrix[level].secondLevelReferrals.push(userAddress);
uint len = users[ref].O6Matrix[level].firstLevelReferrals.length;
if ((len == 2) &&
(users[ref].O6Matrix[level].firstLevelReferrals[0] == referrerAddress) &&
(users[ref].O6Matrix[level].firstLevelReferrals[1] == referrerAddress)) {
if (users[referrerAddress].O6Matrix[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].O6Matrix[level].firstLevelReferrals[0] == referrerAddress) {
if (users[referrerAddress].O6Matrix[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].O6Matrix[level].firstLevelReferrals[1] == referrerAddress) {
if (users[referrerAddress].O6Matrix[level].firstLevelReferrals.length == 1) {
emit NewUserPlace(userAddress, ref, 2, level, 5);
} else {
emit NewUserPlace(userAddress, ref, 2, level, 6);
}
}
return updateO6ReferrerSecondLevel(userAddress, ref, level);
}
users[referrerAddress].O6Matrix[level].secondLevelReferrals.push(userAddress);
if (users[referrerAddress].O6Matrix[level].closedPart != address(0)) {
if ((users[referrerAddress].O6Matrix[level].firstLevelReferrals[0] ==
users[referrerAddress].O6Matrix[level].firstLevelReferrals[1]) &&
(users[referrerAddress].O6Matrix[level].firstLevelReferrals[0] ==
users[referrerAddress].O6Matrix[level].closedPart)) {
updateO6(userAddress, referrerAddress, level, true);
return updateO6ReferrerSecondLevel(userAddress, referrerAddress, level);
} else if (users[referrerAddress].O6Matrix[level].firstLevelReferrals[0] ==
users[referrerAddress].O6Matrix[level].closedPart) {
updateO6(userAddress, referrerAddress, level, true);
return updateO6ReferrerSecondLevel(userAddress, referrerAddress, level);
} else {
updateO6(userAddress, referrerAddress, level, false);
return updateO6ReferrerSecondLevel(userAddress, referrerAddress, level);
}
}
if (users[referrerAddress].O6Matrix[level].firstLevelReferrals[1] == userAddress) {
updateO6(userAddress, referrerAddress, level, false);
return updateO6ReferrerSecondLevel(userAddress, referrerAddress, level);
} else if (users[referrerAddress].O6Matrix[level].firstLevelReferrals[0] == userAddress) {
updateO6(userAddress, referrerAddress, level, true);
return updateO6ReferrerSecondLevel(userAddress, referrerAddress, level);
}
if (users[users[referrerAddress].O6Matrix[level].firstLevelReferrals[0]].O6Matrix[level].firstLevelReferrals.length <=
users[users[referrerAddress].O6Matrix[level].firstLevelReferrals[1]].O6Matrix[level].firstLevelReferrals.length) {
updateO6(userAddress, referrerAddress, level, false);
} else {
updateO6(userAddress, referrerAddress, level, true);
}
updateO6ReferrerSecondLevel(userAddress, referrerAddress, level);
}
function updateO6(address userAddress, address referrerAddress, uint8 level, bool x2) private {
if (!x2) {
users[users[referrerAddress].O6Matrix[level].firstLevelReferrals[0]].O6Matrix[level].firstLevelReferrals.push(userAddress);
emit NewUserPlace(userAddress, users[referrerAddress].O6Matrix[level].firstLevelReferrals[0], 2, level, uint8(users[users[referrerAddress].O6Matrix[level].firstLevelReferrals[0]].O6Matrix[level].firstLevelReferrals.length));
emit NewUserPlace(userAddress, referrerAddress, 2, level, 2 + uint8(users[users[referrerAddress].O6Matrix[level].firstLevelReferrals[0]].O6Matrix[level].firstLevelReferrals.length));
users[userAddress].O6Matrix[level].currentReferrer = users[referrerAddress].O6Matrix[level].firstLevelReferrals[0];
} else {
users[users[referrerAddress].O6Matrix[level].firstLevelReferrals[1]].O6Matrix[level].firstLevelReferrals.push(userAddress);
emit NewUserPlace(userAddress, users[referrerAddress].O6Matrix[level].firstLevelReferrals[1], 2, level, uint8(users[users[referrerAddress].O6Matrix[level].firstLevelReferrals[1]].O6Matrix[level].firstLevelReferrals.length));
emit NewUserPlace(userAddress, referrerAddress, 2, level, 4 + uint8(users[users[referrerAddress].O6Matrix[level].firstLevelReferrals[1]].O6Matrix[level].firstLevelReferrals.length));
users[userAddress].O6Matrix[level].currentReferrer = users[referrerAddress].O6Matrix[level].firstLevelReferrals[1];
}
}
function updateO6ReferrerSecondLevel(address userAddress, address referrerAddress, uint8 level) private {
if (users[referrerAddress].O6Matrix[level].secondLevelReferrals.length < 4) {
return sendETHDividends(referrerAddress, userAddress, 2, level);
}
address[] memory O6 = users[users[referrerAddress].O6Matrix[level].currentReferrer].O6Matrix[level].firstLevelReferrals;
if (O6.length == 2) {
if (O6[0] == referrerAddress ||
O6[1] == referrerAddress) {
users[users[referrerAddress].O6Matrix[level].currentReferrer].O6Matrix[level].closedPart = referrerAddress;
} else if (O6.length == 1) {
if (O6[0] == referrerAddress) {
users[users[referrerAddress].O6Matrix[level].currentReferrer].O6Matrix[level].closedPart = referrerAddress;
}
}
}
users[referrerAddress].O6Matrix[level].firstLevelReferrals = new address[](0);
users[referrerAddress].O6Matrix[level].secondLevelReferrals = new address[](0);
users[referrerAddress].O6Matrix[level].closedPart = address(0);
if (!users[referrerAddress].activeO6Levels[level+1] && level != LAST_LEVEL) {
users[referrerAddress].O6Matrix[level].blocked = true;
}
users[referrerAddress].O6Matrix[level].reinvestCount++;
if (referrerAddress != owner) {
address freeReferrerAddress = findFreeO6Referrer(referrerAddress, level);
emit Reinvest(referrerAddress, freeReferrerAddress, userAddress, 2, level);
updateO6Referrer(referrerAddress, freeReferrerAddress, level);
} else {
emit Reinvest(owner, address(0), userAddress, 2, level);
sendETHDividends(owner, userAddress, 2, level);
}
}
function findFreeO3Referrer(address userAddress, uint8 level) public view returns(address) {
while (true) {
if (users[users[userAddress].referrer].activeO3Levels[level]) {
return users[userAddress].referrer;
}
userAddress = users[userAddress].referrer;
}
}
function findFreeO6Referrer(address userAddress, uint8 level) public view returns(address) {
while (true) {
if (users[users[userAddress].referrer].activeO6Levels[level]) {
return users[userAddress].referrer;
}
userAddress = users[userAddress].referrer;
}
}
function usersActiveO3Levels(address userAddress, uint8 level) public view returns(bool) {
return users[userAddress].activeO3Levels[level];
}
function usersActiveO6Levels(address userAddress, uint8 level) public view returns(bool) {
return users[userAddress].activeO6Levels[level];
}
function usersO3Matrix(address userAddress, uint8 level) public view returns(address, address[] memory, bool) {
return (users[userAddress].O3Matrix[level].currentReferrer,
users[userAddress].O3Matrix[level].referrals,
users[userAddress].O3Matrix[level].blocked);
}
function usersO6Matrix(address userAddress, uint8 level) public view returns(address, address[] memory, address[] memory, bool, address) {
return (users[userAddress].O6Matrix[level].currentReferrer,
users[userAddress].O6Matrix[level].firstLevelReferrals,
users[userAddress].O6Matrix[level].secondLevelReferrals,
users[userAddress].O6Matrix[level].blocked,
users[userAddress].O6Matrix[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].O3Matrix[level].blocked) {
emit MissedEthReceive(receiver, _from, 1, level);
isExtraDividends = true;
receiver = users[receiver].O3Matrix[level].currentReferrer;
} else {
return (receiver, isExtraDividends);
}
}
} else {
while (true) {
if (users[receiver].O6Matrix[level].blocked) {
emit MissedEthReceive(receiver, _from, 2, level);
isExtraDividends = true;
receiver = users[receiver].O6Matrix[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])) {
return address(uint160(receiver)).transfer(address(this).balance);
}
if (isExtraDividends) {
emit SentExtraEthDividends(_from, receiver, matrix, level);
}
}
function bytesToAddress(bytes memory bys) private pure returns (address addr) {
assembly {
addr := mload(add(bys, 20))
}
}
}