forked from linjie-1/guigulive-operation
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Assignment #3
100 lines (76 loc) · 3.02 KB
/
Assignment #3
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
pragma solidity ^0.4.14;
import './SafeMath.sol';
import './Ownable.sol';
contract Payroll is Ownable {
struct Employee {
address id;
uint salary;
uint lastPayday;
}
uint constant payDuration = 10 seconds;
uint totalSalary = 0 ether;
mapping(address => Employee) public employees;
modifier employeeExist (address employeeId){
var employee = employees[employeeId];
assert(employee.id != 0x0);
_;
}
//only employee have access to update payment address
modifier onlyEmployee (address employeeId) {
require(msg.sender == employees[employeeId].id);
_;
}
function _partialPaid(Employee employee) private {
uint payment = employee.salary * (now - employee.lastPayday) / payDuration;
// employee.id.transfer(payment);
}
function addEmployee(address employeeId, uint salary) onlyOwner {
var employee = employees[employeeId];
assert(employee.id == 0x0);
employees[employeeId] = Employee(employeeId,salary * 1 ether,now);
totalSalary += employees[employeeId].salary;
}
function removeEmployee(address employeeId) onlyOwner employeeExist(employeeId) {
var employee = employees[employeeId];
_partialPaid(employee);
totalSalary -= employees[employeeId].salary;
delete employees[employeeId];
}
//update salary
function changeEmployeeSalary(address employeeId, uint salary) onlyOwner employeeExist(employeeId){
var employee = employees[employeeId];
_partialPaid(employee);
totalSalary -= employees[employeeId].salary;
employees[employeeId].salary = salary * 1 ether;
employees[employeeId].lastPayday = now;
totalSalary += employees[employeeId].salary;
}
//update address
function changePaymentAddress (address employeeId, address employeeIdNew) onlyEmployee (employeeId) employeeExist(employeeId){
var employee = employees[employeeId];
_partialPaid(employee);
employees[employeeIdNew] = Employee(employeeIdNew,employees[employeeId].salary,now);
delete employees[employeeId];
}
function addFund() payable returns (uint) {
return this.balance;
}
function calculateRunway() returns (uint) {
return this.balance / totalSalary;
}
function hasEnoughFund() returns (bool) {
return calculateRunway() > 0;
}
function checkEmployee(address employeeId) returns(uint salary, uint lastPayday){
var employee = employees[employeeId];
salary = employee.salary;
lastPayday = employee.lastPayday;
}
function getPaid() employeeExist(msg.sender){
var employee = employees[msg.sender];
uint nextPayday = employee.lastPayday + payDuration;
assert(nextPayday < now);
employees[msg.sender].lastPayday = nextPayday;
employee.id.transfer(employee.salary);
}
}