Created
July 1, 2020 10:26
-
-
Save alexroan/54ffe407e311e7f082d89a853bcb73d0 to your computer and use it in GitHub Desktop.
Tree.sol
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
pragma solidity ^0.5.0; | |
import "@openzeppelin/contracts/math/SafeMath.sol"; | |
contract Tree { | |
using SafeMath for uint; | |
struct User { | |
address payable inviter; | |
address payable self; | |
} | |
mapping(address => User) public tree; | |
address payable public top; | |
constructor() public { | |
tree[msg.sender] = User(msg.sender, msg.sender); | |
top = msg.sender; | |
} | |
function enter(address payable inviter) external payable { | |
require(msg.value >= 1 ether, "Must be at least 1 ether"); | |
require(tree[msg.sender].inviter == address(0), "Sender can't already exist in tree"); | |
require(tree[inviter].self == inviter, "Inviter must exist"); | |
tree[msg.sender] = User(inviter, msg.sender); | |
address payable current = inviter; | |
uint amount = msg.value; | |
while(current != top) { | |
amount = amount.div(2); | |
current.transfer(amount); | |
current = tree[current].inviter; | |
} | |
top.transfer(amount); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment