Skip to content
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
57 changes: 57 additions & 0 deletions Lesson-1/assignment/54_于宏学_第一课作业.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
/*作业请提交在这个目录下*/
pragma solidity ^0.4.14;

contract Payroll{

uint constant payDuration = 2 seconds;
address owner;
uint salary;
address employee;
uint lastPayday;

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

function updateEmployee(address e,uint s){

if (employee != 0x0)
{
uint payment = salary * (now - lastPayday)/payDuration;
employee.transfer(payment);
}

employee = e;
salary = s * 1 ether;
lastPayday = now;
}

function addFund() payable 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 != employee)
{
revert();
}

uint nextPayday = lastPayday+payDuration;
if(nextPayday>now){
revert();
}
lastPayday = nextPayday;
employee.transfer(salary);
}
}
1 change: 0 additions & 1 deletion Lesson-1/assignment/yours.sol

This file was deleted.

Binary file added Lesson-3/addEmployee.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added Lesson-3/addfund.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
167 changes: 167 additions & 0 deletions Lesson-3/assignment/lesson3.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,167 @@
/*作业请提交在这个目录下*/

pragma solidity ^0.4.14;

contract Payroll{

struct Employee{
address id;
uint salary;
uint lastPayday;
}
uint constant payDuration = 10 seconds;
address owner;
uint totalSalary = 0;

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

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

//Employee[] employees;
//构造函数
function Payroll(){
owner = msg.sender;
}

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


//mapping(address => Employee) employees;
mapping(address => Employee) public employees;


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

}
}*/

//onlyOwner use
function addEmployee(address employeeId,uint salary) onlyOwner
{
//require(msg.sender == owner);
//var (employee,index) = findEmployee(employeeId);
var employee = employees[employeeId];
//solidity循环
/*for(uint i= 0; i< employees.length; i++){
if(employees[i].id == employee){
revert();
}
}*/
//Employee memory employee = findEmployee(employeeId);
assert(employee.id == 0x0);
totalSalary+=salary * 1 ether;
employees[employeeId]=Employee(employeeId,salary* 1 ether,now);
//employees.push((employee,salary,now));
}


/*
function addEmployee(address employeeId,uint salary)
{
require(msg.sender == owner);
//var (employee,index) = findEmployee(employeeId);
var employee = employees[employeeId];
//solidity循环
for(uint i= 0; i< employees.length; i++){
if(employees[i].id == employee){
revert();
}
}
//Employee memory employee = findEmployee(employeeId);
//assert(employee.id == 0x0);
//totalSalary+=salary * 1 ether;
//employees[employeeId]=Employee(employeeId,salary* 1 ether,now);
//employees.push((employee,salary,now));
}
*/

function removeEmployee(address employeeId)
{
require(msg.sender == owner);
//var (employee,index) = findEmployee(employeeId);
var employee = employees[employeeId];
assert(employeeId != 0x0);
//partialPaid(employee[index]);
partialPaid(employee);
//delete employee[index];
totalSalary-=employees[employeeId].salary;
delete employees[employeeId];
/* employees[index] = employees[employees.length-1];
employees.length -=1;*/
}


function updateEmployee(address employeeId,uint salary){
require(msg.sender==owner);
var employee = employees[employeeId];
//var (employee,index) = findEmployee(employeeId);
assert(employee.id != 0x0);
/*
partialPaid(employees[index]);
employees[index].salary = salary;
employees[index].lastPayday = now;*/
partialPaid(employee);
totalSalary-=employees[employeeId].salary;
employees[employeeId].salary = salary * 1 ether;
totalSalary+=employees[employeeId].salary;
employees[employeeId].lastPayday = now;
}

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

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

function hasEnoughFund() returns(bool)
{
return calculateRunway()>0;
}
//员工信息
/*
function checkEmployee(address employeeId) returns(uint,uint)
{
var employee = employees[employeeId];
return(employee.salary,employee.lastPayday);
}*/

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

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

}
1 change: 0 additions & 1 deletion Lesson-3/assignment/yours.sol

This file was deleted.

Binary file added Lesson-3/calculateRunway.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added Lesson-3/checkEmployee.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added Lesson-3/employees.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added Lesson-3/hasEnoughFund.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added Lesson-3/removeEmployee.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.