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

assignment 4 #493

Open
wants to merge 3 commits into
base: master
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
Binary file added Lesson3/assignment/shotcut.zip
Binary file not shown.
93 changes: 93 additions & 0 deletions Lesson3/assignment/yours.sol
Original file line number Diff line number Diff line change
@@ -1 +1,94 @@
/*作业请提交在这个目录下*/
pragma solidity ^0.4.14;

contract Payroll {
struct Employee {
address id;
uint salary;
uint lastPayday;
}

uint constant payDuration = 10 seconds;
uint totalSalary = 0;

address owner;
mapping (address => Employee) private employees;

modifier onlyOwner() {
require(owner == msg.sender);
_;
}

modifier onlySelf(address employeeId) {
require(employeeId == msg.sender);
_;
}

function Payroll() public{
owner = msg.sender;
}

function _partialPaid(Employee employee) private {
uint amount = (now - employee.lastPayday) / payDuration * employee.salary;
employee.id.transfer(amount);
}

function addEmployee(address employeeId, uint salary) public onlyOwner {
var employee = employees[employeeId];
assert(employee.id == 0x0);
employees[employeeId] = Employee(employeeId, salary, now);
totalSalary += salary * 1 ether;
}

function removeEmployee(address employeeId) public onlyOwner {
var employee = employees[employeeId];
assert(employee.id != 0x0);
_partialPaid(employees[employeeId]);
delete employees[employeeId];
totalSalary -= employees[employeeId].salary * 1 ether;
}

function updateEmployee(address employeeId, uint salary) public onlyOwner {
var employee = employees[employeeId];
assert(employee.id != 0x0);
_partialPaid(employee);
totalSalary -= employees[employeeId].salary;
employees[employeeId].salary = salary * 1 ether;
employees[employeeId].lastPayday = now;
totalSalary += salary * 1 ether;
}

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

function calculateRunway() public view returns (uint) {
assert(totalSalary > 0);
return this.balance / totalSalary;
}

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

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

function getPaid() public {
var employee = employees[msg.sender];
assert(employee.id != 0x0);
uint nextPayday = employees[msg.sender].lastPayday + payDuration;
assert(nextPayday < now);
employees[msg.sender].lastPayday = nextPayday;
employee.id.transfer(employees[msg.sender].salary);
}

function changePaymentAddress(address EmployeeId, address newAddress) public onlySelf(EmployeeId){
var employee = employees[msg.sender];
assert(employee.id != 0x0);
employees[msg.sender].id = newAddress;
}
}
12 changes: 0 additions & 12 deletions Lesson4/assignment/README.md

This file was deleted.

Loading