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

第一课作业 #12

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
32 changes: 32 additions & 0 deletions 第一课代码
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
pragma solidity ^0.4.14;

contract Payroll{
uint salary = 1 ether;
address frank = 0xca35b7d915458ef540ade6068dfe2f44e8fa733c;
uint constant payDuration = 10 seconds;
uint lastPayday = now;

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

function calculateRunaway() returns(uint) {
return this.balance / salary;
}

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

function getPaid() {
if (msg.sender != frank) {
revert();
}
uint nextPayDay = lastPayday + payDuration;
if (nextPayDay > now) {
revert();
}
lastPayday = nextPayDay;
frank.transfer(salary);
}
}
41 changes: 41 additions & 0 deletions 第一课作业
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
pragma solidity ^0.4.14;

contract Payroll{
uint salary = 1 ether;
address Lisa;
address Employee;
uint constant payDuration = 8 seconds;
uint lastPayday = now;

function set(uint new salary) {
salary = newsalary * 200 wei;
}

function set(address Employee) {
Lisa = Employee;
}

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

function calculateRunaway() returns(uint) {
return this.balance / salary;
}

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

function getPaid() {
if (msg.sender != frank) {
revert();
}
uint nextPayDay = lastPayday + payDuration;
if (nextPayDay > now) {
revert();
}
lastPayday = nextPayDay;
frank.transfer(salary);
}
}
95 changes: 95 additions & 0 deletions 第三课作业
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
pragma solidity ^0.4.14;

contract Payroll {

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

uint constant payDuration = 10 seconds;
uint totalSalary;
address owner;
mapping(address => Employee) public employees;

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

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

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) onlyOwener {
var employee = employees[employeeId];
assert(employee.id == 0x0);

employees[employeeId] = Employee(employeeId, salary * 1 ether, now);
totalSalary += employees[employeeId].salary;
}

function removeEmployee(address employeeId) onlyOwener employeeExist(employeeId) {
var employee = employees[employeeId];

_partialPaid(employee);
totalSalary -= employees[employeeId].salary;
delete employees[employeeId];
}

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

_partialPaid(employee);
totalSalary -= employees[employeeId].salary;
employees[employeeId].salary = salary * 1 ether;
totalSalary += employees[employeeId].salary;
employees[employeeId].lastPayday = now;
}

function changePaymentaddress(address employeeId) onlyOwener employeeExist(employeeId) {
var employee = employees[employeeId];

_partialPaid(employee);
totalSalary -= employees[employeeId].salary;
delete employees[employeeId];
assert(employee.id == 0x0);

employees[employeeId] = Employee(employeeId, salary * 1 ether, now);
totalSalary += employees[employeeId].salary;

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

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

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

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);
}
}