-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathDividendDistributor.sol
119 lines (97 loc) · 3.21 KB
/
DividendDistributor.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
// https://etherscan.io/address/0x858c9eaf3ace37d2bedb4a1eb6b8805ffe801bba
pragma solidity ^0.4.0;
contract Ownable {
address public owner;
function Ownable() public {
owner = msg.sender;
}
modifier onlyOwner() {
if (msg.sender != owner)
throw;
_;
}
modifier protected() {
if(msg.sender != address(this))
throw;
_;
}
function transferOwnership(address newOwner) public onlyOwner {
if (newOwner == address(0))
throw;
owner = newOwner;
}
}
contract DividendDistributorv3 is Ownable{
event Transfer(
uint amount,
bytes32 message,
address target,
address currentOwner
);
struct Investor {
uint investment;
uint lastDividend;
}
mapping(address => Investor) investors;
uint public minInvestment;
uint public sumInvested;
uint public sumDividend;
function DividendDistributorv3() public{
minInvestment = 0.4 ether;
}
function loggedTransfer(uint amount, bytes32 message, address target, address currentOwner) protected
{
if(! target.call.value(amount)() )
throw;
Transfer(amount, message, target, currentOwner);
}
function invest() public payable {
if (msg.value >= minInvestment)
{
sumInvested += msg.value;
investors[msg.sender].investment += msg.value;
// manually call payDividend() before reinvesting, because this resets dividend payments!
investors[msg.sender].lastDividend = sumDividend;
}
}
function divest(uint amount) public {
if ( investors[msg.sender].investment == 0 || amount == 0)
throw;
// no need to test, this will throw if amount > investment
investors[msg.sender].investment -= amount;
sumInvested -= amount;
this.loggedTransfer(amount, "", msg.sender, owner);
}
function calculateDividend() constant public returns(uint dividend) {
uint lastDividend = investors[msg.sender].lastDividend;
if (sumDividend > lastDividend)
throw;
// no overflows here, because not that much money will be handled
dividend = (sumDividend - lastDividend) * investors[msg.sender].investment / sumInvested;
}
function getInvestment() constant public returns(uint investment) {
investment = investors[msg.sender].investment;
}
function payDividend() public {
uint dividend = calculateDividend();
if (dividend == 0)
throw;
investors[msg.sender].lastDividend = sumDividend;
this.loggedTransfer(dividend, "Dividend payment", msg.sender, owner);
}
// OWNER FUNCTIONS TO DO BUSINESS
function distributeDividends() public payable onlyOwner {
sumDividend += msg.value;
}
function doTransfer(address target, uint amount) public onlyOwner {
this.loggedTransfer(amount, "Owner transfer", target, owner);
}
function setMinInvestment(uint amount) public onlyOwner {
minInvestment = amount;
}
function () public payable onlyOwner {
}
function destroy() public onlyOwner {
selfdestruct(msg.sender);
}
}