-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathpayroll.sol
More file actions
61 lines (46 loc) · 1.88 KB
/
Copy pathpayroll.sol
File metadata and controls
61 lines (46 loc) · 1.88 KB
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
pragma solidity ^0.4.0;
contract newPayroll {
//use your own Mist wallet addresses!
address[] employees = [0x1dD364dB3C0352C8C15df4dC5C42ae7158Cbb39e,
0xB77539Cfd9EcAbdc36F5eC8a858f8fB31D7c8d6c,
0x575f8DA2ffa0B77B23d647BB5A39739283d49368];
uint totalReceived = 0;
mapping (address => uint) withdrawnAmounts; //this is like a dictionary in Swift
/* Constructor */ //ASFAIK, only runs at initialization
//need the 'payable' keyword to be able to accept money
function newPayroll() payable {
updateTotalReceived();
}
/*this is the fallback function*/
function() payable {
updateTotalReceived();
}
//keeping a running tally of how much money has been deposited to the contract
function updateTotalReceived() internal {
totalReceived += msg.value;
}
//returns a boolean to ensure only valid addresses are trying to collect pay
modifier canWithdraw() {
bool contains = false;
for(uint i = 0; i < employees.length; i++) {
if (employees[i] == msg.sender) {
contains = true;
}
}
require(contains);
_;
}
//the withdrawal function; this instance divides and disburses to a validated address of a requestor
//but only once
function withdraw() canWithdraw {
//simple equal allocation
uint amountAllocated = totalReceived/employees.length;
//keeping track of who's already withdrawn
uint amountWithdrawn = withdrawnAmounts[msg.sender];
uint amount = amountAllocated - amountWithdrawn;
withdrawnAmounts[msg.sender] = amountWithdrawn + amount;
if (amount > 0) {
msg.sender.transfer(amount);
}
}
}