aboutsummaryrefslogtreecommitdiff
path: root/splitShares.sol
blob: cf3a5377f17e810082e3bf044d4470380ad4284f (plain) (blame)
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
pragma solidity ^0.4.0;
contract SplitIncomes {
    mapping(address => uint256) withdrawn;
    mapping(address => bool) inShares;
    uint256 totalWithdrawn;
    uint256 SharesCount;

    function SplitIncomes(address[] addresses) public {
        SharesCount = addresses.length;

        for (uint i = 0; i < SharesCount; i++) {
            inShares[addresses[i]] = true;
        }
    }

    function () public payable {}

    function balance() public view returns (uint256) {
        if (!inShares[msg.sender]) {
            return 0;
        }

        return (address(this).balance + totalWithdrawn) / SharesCount - withdrawn[msg.sender];
    }

    function withdraw() public {
        require(inShares[msg.sender]);
        uint256 available = balance();
        if (available > 0) {
            withdrawn[msg.sender] += available;
            totalWithdrawn += available;
            msg.sender.transfer(available);
        }
    }
}