SHARE
TWEET

Untitled

MyOnAsSalat Jan 20th, 2020 71 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. pragma solidity ^0.5.14;
  2. contract PonzyGame{
  3.     mapping(address => Node) public nodes;
  4.     uint[] prices = [1 ether, 2 ether, 7 ether, 29 ether];
  5.     address payable public owner;
  6.     event LevelUp(address indexed user, address indexed seller, uint8 level);
  7.     event Joined(address indexed user, address indexed parent);
  8.     event SaleSkiped(address indexed seller, address user);
  9.     struct Node {
  10.         uint8 level;
  11.         address payable parent;
  12.         address payable descendant1;
  13.         address payable descendant2;
  14.         address payable descendant3;
  15.     }
  16.     constructor(address payable descendant1, address payable descendant2, address payable descendant3) public {
  17.         owner = msg.sender;
  18.         nodes[owner]         = Node(4, owner, descendant1,         descendant2,         descendant3         );  
  19.         nodes[descendant1]   = Node(4, owner, address(uint160(0)), address(uint160(0)), address(uint160(0)) );  
  20.         nodes[descendant2]   = Node(4, owner, address(uint160(0)), address(uint160(0)), address(uint160(0)) );  
  21.         nodes[descendant3]   = Node(4, owner, address(uint160(0)), address(uint160(0)), address(uint160(0)) );  
  22.     }
  23.     function joinTo(address payable _parent) payable public{
  24.         require(msg.value == prices[0],"wrong ether amount");
  25.         require(nodes[msg.sender].level == 0, "already connected");
  26.         address payable parent = findAvailableNode(_parent);
  27.         Node storage node = nodes[parent];
  28.         if(node.descendant1 == address(0)){
  29.             node.descendant1 = msg.sender;
  30.         } else if(node.descendant2 == address(0)) {
  31.             node.descendant2 = msg.sender;
  32.         } else if(node.descendant3 == address(0)) {
  33.             node.descendant3 = msg.sender;
  34.         } else revert("there are no empty descendant");
  35.         parent.transfer(1e18);
  36.         nodes[msg.sender] = Node(1, parent, address(0),address(0),address(0));
  37.    
  38.         emit Joined(msg.sender, parent);
  39.     }
  40.     function findAvailableNode(address payable _parent) public view returns (address payable){
  41.        
  42.         address payable[] memory queue = new address payable[](13);
  43.         uint current = 0;
  44.         uint last = 1;
  45.         if(nodes[_parent].level == 0) queue[0] = owner;
  46.         else queue[0] = _parent;
  47.         while(true){
  48.             address payable currentAddress = queue[current++];
  49.             Node memory currentNode = nodes[currentAddress];
  50.             require(currentNode.level > 0, "tree crashed");
  51.             if(currentNode.descendant1 == address(0)) return currentAddress;
  52.             if(currentNode.descendant2 == address(0)) return currentAddress;
  53.             if(currentNode.descendant3 == address(0)) return currentAddress;
  54.            
  55.             if(queue.length - last < 3){
  56.                 address payable[] memory tmp = new address payable[](queue.length * 3 + 1);
  57.                 for(uint i = current; i < last;i++)
  58.                     tmp[i - current] = queue[i];
  59.                 queue = tmp;
  60.                 last -=current;
  61.                 current = 0;
  62.             }
  63.             queue[last++] = currentNode.descendant1;
  64.             queue[last++] = currentNode.descendant2;
  65.             queue[last++] = currentNode.descendant3;
  66.         }
  67.     }
  68.     function levelUp() payable public {
  69.         Node storage it = nodes[msg.sender];
  70.         require(it.level > 0, "you are not in PonzyGame");
  71.         require(prices[it.level] > 0, "last level");
  72.         require(msg.value == prices[it.level], "wrong ether amount");
  73.         address payable parent = it.parent;
  74.         for(uint i = 0; i < it.level; i++)
  75.             parent = nodes[parent].parent;
  76.         while(nodes[parent].level < it.level + 1){
  77.             emit SaleSkiped(parent, msg.sender);
  78.             parent = nodes[parent].parent;
  79.         }
  80.         parent.transfer(prices[it.level]);
  81.         it.level++;
  82.         emit LevelUp(msg.sender,parent,it.level);
  83.     }
  84. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
Top