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

91_HW-3 #316

Merged
merged 9 commits into from
Jan 20, 2018
Merged
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
95 changes: 95 additions & 0 deletions Lesson2/hw-2.sol
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;

address owner;
Employee[] employees;

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

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

function _findEmployee(address employeeId) private returns (Employee, uint){
for(uint i=0; i<employees.length;i++){
if(employees[i].id == employeeId){
return (employees[i], i);
}
}
}

function addEmployee(address employeeId, uint salary){
require(msg.sender == owner);

var (employee, index) = _findEmployee(employeeId);
assert(employee.id == 0x0);

employees.push(Employee(employeeId, salary, now));
}

function removeEmployee(address employeeId){
require(msg.sender == owner);

var (employee, index) = _findEmployee(employeeId);

assert(employee.id == 0x0);
_partialPaid(employee);

delete employees[index];
employees[index] = employees[employees.length -1];
employees.length -= 1;

}

function updateEmployee(address employeeId, uint salary) {
require(msg.sender == owner);

var (employee, index) = _findEmployee(employeeId);

assert(employee.id == 0x0);
_partialPaid(employee);
employees[index].salary = salary;
employees[index].lastPayday = now;

}

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

function calculateRunway() returns (uint) {
uint totalSalary = 0;
for(uint i=0; i<employees.length;i++){
totalSalary += employees[i].salary;
}
return this.balance / totalSalary;
}

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

function getPaid() {
var (employee, index) = _findEmployee(msg.sender);
assert(employee.id == 0x0);


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

employees[index].lastPayday = nextPayday;
employees[index].id.transfer(employee.salary);
}

}
103 changes: 103 additions & 0 deletions Lesson3/assignment/yours.sol
Original file line number Diff line number Diff line change
@@ -1 +1,104 @@
/*作业请提交在这个目录下*/

//q1

pragma solidity ^0.4.14;

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

uint constant payDuration = 10 seconds;

address owner;
uint totalSalary;
mapping(address => Employee) employees;

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

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

function addEmployee(address employeeId, uint salary){
require(msg.sender == owner);

var employee = employees[employeeId];
assert(employee.id == 0x0);
totalSalary += salary * 1 ether;
employees[employeeId] = (Employee(employeeId, salary * 1 ether, now));
}

function removeEmployee(address employeeId){
require(msg.sender == owner);

var employee = employees[employeeId];

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

}

function updateEmployee(address employeeId, uint salary) {
require(msg.sender == owner);

var employee = employees[employeeId];

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

}

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

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

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

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

function getPaid() {
var employee = employees[msg.sender];
assert(employee.id == 0x0);


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

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

}


/// q2
function changePaymentAddress(address employeeId, address newEmployeeId) onlyOwner employeeExist(employeeId) {
var employee = employees[employeeId];

_partialPaid(employee);
employees[employeeId].id = newEmployeeId;
employees[newEmployeeId].lastPayday = now;
}
38 changes: 38 additions & 0 deletions lesson1/hw-1.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
pragma solidity ^0.4.14;

contract payRoll{
uint salary = 1 ether;
address wallet;
uint constant payDuration = 30 days;
uint lastPayday = now;

function getWallet() {
wallet = msg.sender;
}

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

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

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

function getPaid() {
if(msg.sender != wallet){
revert();
}

uint nextPayday = lastPayday + payDuration;
if(nextPayday > now){
revert();
}
lastPayday = nextPayday;
wallet.transfer(salary);
}

}
1 change: 1 addition & 0 deletions 【测试】第一课作业
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
测试·第一课作业