Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

2_崔迪_第六课作业 #76

Open
wants to merge 11 commits into
base: 2-崔迪
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions Lesson-6/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
## 硅谷live以太坊智能合约频道官方地址

### 第六课
135 changes: 135 additions & 0 deletions Lesson-6/assignment/Payroll.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,135 @@
pragma solidity ^0.4.14;

import './SafeMath.sol';
import './Ownable.sol';

contract Payroll is Ownable {
using SafeMath for uint;
struct Employee {
address id;
uint salary;
uint lastPayday;
}

uint constant payDuration = 10 seconds;

uint totalSalary;
uint totalEmployee;
address[] employeeList;
mapping(address => Employee) public employees;

event NewEmployee(
address employee
);

event UpdateEmployee(
address employee
);

event RemoveEmployee(
address employee
);

event NewFund(
uint balance
);

event GetPaid(
address employee
);

modifier employeeExit(address employeeId) {
var employee = employees[employeeId];
assert(employee.id != 0x0);
_;
}

function _partialPaid(Employee employee) private {
uint payment = employee.salary
.mul(now.sub(employee.lastPayday))
.div(payDuration);
employee.id.transfer(payment);
}

function checkEmployee(uint index) returns (address employeeId, uint salary, uint lastPayday) {
employeeId = employeeList[index];
var employee = employees[employeeId];
salary = employee.salary;
lastPayday = employee.lastPayday;
}

function addEmployee(address employeeId, uint salary) onlyOwner {
var employee = employees[employeeId];
assert(employee.id == 0x0);

employees[employeeId] = Employee(employeeId, salary.mul(1 ether), now);
totalSalary = totalSalary.add(employees[employeeId].salary);
totalEmployee = totalEmployee.add(1);
employeeList.push(employeeId);

NewEmployee(employeeId);
}

function removeEmployee(address employeeId) onlyOwner employeeExit(employeeId) {
var employee = employees[employeeId];

_partialPaid(employee);
totalSalary = totalSalary.sub(employee.salary);
delete employees[employeeId];
totalEmployee = totalEmployee.sub(1);

for(uint i = 0; i < employeeList.length; i++){
if(employeeList[i] == employeeId) {
delete employeeList[i];
employeeList[i] = employeeList[employeeList.length - 1];
}
}

RemoveEmployee(employeeId);
}

function updateEmployee(address employeeId, uint salary) onlyOwner employeeExit(employeeId) {
var employee = employees[employeeId];

_partialPaid(employee);
totalSalary = totalSalary.sub(employee.salary);
employee.salary = salary.mul(1 ether);
employee.lastPayday = now;
totalSalary = totalSalary.add(employee.salary);

UpdateEmployee(employeeId);
}

function addFund() payable returns (uint) {
return this.balance;
}

function calculateRunway() returns (uint) {
return this.balance.div(totalSalary);
}

function hasEnoughFund() returns (bool) {
return calculateRunway() > 0;
}

function getPaid() employeeExit(msg.sender) {
var employee = employees[msg.sender];

uint nextPayday = employee.lastPayday.add(payDuration);
assert(nextPayday < now);

employee.lastPayday = nextPayday;
employee.id.transfer(employee.salary);

GetPaid(msg.sender);
}

function checkInfo() returns (uint balance, uint runway, uint employeeCount) {
balance = this.balance;
employeeCount = totalEmployee;

if (totalSalary > 0) {
runway = calculateRunway();
}
}
}
4 changes: 4 additions & 0 deletions Lesson-6/assignment/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
## 硅谷live以太坊智能合约 第六课作业
这里是同学提交作业的目录

### 第六课:课后作业
3 changes: 3 additions & 0 deletions Lesson-6/orgin/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
## 硅谷live以太坊智能合约 第六课

这里是每一课的初始代码,有需要的同学可以参考