forked from pkz0313/SmartContract
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path彭坤第三次作业.sol
97 lines (96 loc) · 3.19 KB
/
彭坤第三次作业.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
{\rtf1\ansi\ansicpg936\cocoartf1561\cocoasubrtf200
{\fonttbl\f0\fswiss\fcharset0 Helvetica;}
{\colortbl;\red255\green255\blue255;}
{\*\expandedcolortbl;;}
\paperw11900\paperh16840\margl1440\margr1440\vieww10800\viewh8400\viewkind0
\pard\tx566\tx1133\tx1700\tx2267\tx2834\tx3401\tx3968\tx4535\tx5102\tx5669\tx6236\tx6803\pardirnatural\partightenfactor0
\f0\fs24 \cf0 pragma solidity ^0.4.14;\
import './Ownable.sol';\
import './SafeMath.sol';\
contract Payroll is Ownable\{\
using SafeMath for uint;\
struct Employee\{\
address id;\
uint salary;\
uint lastPayday;\
\}\
uint constant payDuration = 10 seconds;\
address owner; \
uint public totalsalary ;\
mapping(address=>Employee) public employees;\
\
\
modifier employeeNotExist(address employeeID)\{\
var employee = employees[employeeID]; \
assert(employee.id == 0x0);\
_;\
\}\
modifier employeeExist(address employeeID)\{\
var employee = employees[employeeID]; \
assert(employee.id != 0x0);\
_;\
\}\
\
\
function _partialPaid(Employee employee) private\{\
uint payment = employee.salary * (now - employee.lastPayday)/payDuration;\
employee.id.transfer(payment);\
\}\
\
\
\
\
function addEmployee(address employeeID,uint salary) onlyOwner employeeNotExist(employeeID)\{\
employees[employeeID] = Employee(employeeID,salary*1 ether,now);\
totalsalary = totalsalary.add(salary * 1 ether);\
\}\
\
\
function removeEmployee(address employeeID) onlyOwner employeeExist(employeeID)\{\
var employee = employees[employeeID]; \
_partialPaid(employee);\
totalsalary = totalsalary.sub(employee.salary);\
delete employees[employeeID];\
\}\
\
function updateEmployee(address employeeID, uint salary) onlyOwner employeeExist(employeeID)\{\
var employee = employees[employeeID];\
_partialPaid(employee);\
totalsalary = totalsalary.sub(employee.salary);\
employees[employeeID].salary = salary * 1 ether;\
totalsalary.add(employees[employeeID].salary);\
employees[employeeID].lastPayday = now;\
\
\
\}\
function changePaymentAddress(address oldAddress,address newAddress) onlyOwner employeeExist(oldAddress)\{\
var employee = employees[oldAddress];\
uint salary = employee.salary;\
uint payment = salary * (now - employee.lastPayday)/payDuration;\
newAddress.transfer(payment);\
removeEmployee(oldAddress);\
addEmployee(newAddress,salary/1 ether);\
\}\
\
\
\
function addFund() payable returns(uint)\{\
return this.balance; \
\}\
function calculateRunway() returns(uint)\{\
return this.balance/totalsalary;\
\}\
function hasEnough() returns (bool)\{\
return calculateRunway()>0;\
\}\
\
\
\
function getPaid() employeeExist(msg.sender)\{\
var employee = employees[msg.sender];\
uint nextPayday = employee.lastPayday + payDuration;\
assert(nextPayday < now);\
employee.lastPayday = nextPayday;\
employee.id.transfer(employee.salary);\
\}\
\}}