diff --git a/Lesson2/hw-2.sol b/Lesson2/hw-2.sol new file mode 100644 index 000000000..99ec61f12 --- /dev/null +++ b/Lesson2/hw-2.sol @@ -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 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); + } + +} \ No newline at end of file diff --git a/Lesson3/assignment/yours.sol b/Lesson3/assignment/yours.sol index dfdb2c486..189a9cd28 100644 --- a/Lesson3/assignment/yours.sol +++ b/Lesson3/assignment/yours.sol @@ -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; +} diff --git a/lesson1/hw-1.sol b/lesson1/hw-1.sol new file mode 100644 index 000000000..455d868ab --- /dev/null +++ b/lesson1/hw-1.sol @@ -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); + } + +} \ No newline at end of file diff --git "a/\343\200\220\346\265\213\350\257\225\343\200\221\347\254\254\344\270\200\350\257\276\344\275\234\344\270\232" "b/\343\200\220\346\265\213\350\257\225\343\200\221\347\254\254\344\270\200\350\257\276\344\275\234\344\270\232" new file mode 100644 index 000000000..f0ce4777d --- /dev/null +++ "b/\343\200\220\346\265\213\350\257\225\343\200\221\347\254\254\344\270\200\350\257\276\344\275\234\344\270\232" @@ -0,0 +1 @@ +测试·第一课作业