-
Notifications
You must be signed in to change notification settings - Fork 130
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #507 from linjie-1/revert-488-fifth_homework
Revert "54_陈扬帆_第五次作业(冲突已解决)"
- Loading branch information
Showing
78 changed files
with
20 additions
and
10,721 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,89 +1 @@ | ||
/*作业请提交在这个目录下*/ | ||
pragma solidity ^0.4.14; | ||
//因为每次都要循环遍历的缘故,没加一次雇员,需要更多的gas,所以我把totalSalary的运算放在其他会改动totalSalary的函数,以降低gas的消耗 | ||
contract Payroll { | ||
struct Employee { | ||
address id; | ||
uint salary; | ||
uint lastPayday; | ||
} | ||
|
||
uint constant payDuration = 10 seconds; | ||
uint totalSalary; | ||
|
||
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); | ||
totalSalary = totalSalary + salary; | ||
employees.push(Employee(employeeId, salary, now)); | ||
} | ||
|
||
function removeEmployee(address employeeId) { | ||
require(msg.sender == owner); | ||
|
||
var (employee, index) = _findEmployee(employeeId); | ||
assert(employee.id != 0x0); | ||
_partialPaid(employees[index]); | ||
totalSalary = totalSalary - employee.salary; | ||
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); | ||
totalSalary = totalSalary + salary - employee.salary; | ||
employee.salary = salary; | ||
employee.lastPayday = now; | ||
} | ||
|
||
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() { | ||
var (employee, index) = _findEmployee(msg.sender); | ||
assert(employee.id != 0x0); | ||
|
||
uint nextPayday = employee.lastPayday + payDuration; | ||
assert(nextPayday < now); | ||
|
||
employee.lastPayday = nextPayday; | ||
employee.id.transfer(employee.salary); | ||
} | ||
} |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,122 +1 @@ | ||
/*作业请提交在这个目录下*/ | ||
//新增更改员工薪水的支付地址的函数 | ||
function changePaymentAddress(address employeeId, address newEmployeeId) onlyOwner employeeExist(employeeId) { | ||
var employee = employees[employeeId]; | ||
|
||
employees[employeeId].id = newEmployeeId; | ||
} | ||
|
||
//加分题 | ||
L[O] = 0 | ||
|
||
L[A] = A + merge[L[0],0] | ||
= [A,0] | ||
|
||
L[B] = B + merge[L[0],0] | ||
= [B,0] | ||
|
||
L[C] = C + merge[L[0],0] | ||
= [C,0] | ||
|
||
L[K1] = K1 + merge[L[B],L[A],B,A] | ||
= K1 + merge[[B,0],[A,0],[B,A]] | ||
= [K1,B] + merge[[0],[A,0],[A]] | ||
= [K1,B,A,0] | ||
|
||
L[K2] = K2 + merge[L[C],L[A],C,A] | ||
= K2 + merge[[C,0]],[A,0]],[C,A]] | ||
= [K2,C,A,0] | ||
|
||
L[Z] = Z + merge[L[K2],L[K1],[K2,K1]] | ||
= Z + merge[[K2,C,A,0],[K1,B,A,0],[K2,K1]] | ||
= [Z,K2] + merge[[C,A,0],[B,A,0],K1] | ||
= [Z,K2,K1] + merge[[C,A,0],[B,A,0]] | ||
= [Z,K2,K1,C,A,B,0] | ||
|
||
|
||
//源码 | ||
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 onlyOwner { | ||
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) onlyOwner { | ||
var employee = employees[employeeId]; | ||
assert(employee.id == 0x0); | ||
totalSalary += salary * 1 ether; | ||
|
||
employees[employeeId] = Employee(employeeId, salary * 1 ether, now); | ||
} | ||
|
||
function removeEmployee(address employeeId) onlyOwner employeeExist(employeeId) { | ||
|
||
var employee = employees[employeeId]; | ||
|
||
_partialPaid(employee); | ||
totalSalary -= employees[employeeId].salary; | ||
delete employees[employeeId]; | ||
|
||
} | ||
|
||
function updateEmployee(address employeeId, uint salary) onlyOwner 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 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); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
## 硅谷live以太坊智能合约频道官方地址 | ||
|
||
### 第四课《使用Truffle架构进行前后端交互,测试,部署》 | ||
|
||
目录结构 | ||
<br/>| | ||
<br/>|--orgin 课程初始代码 | ||
<br/>| | ||
<br/>|--assignment 课程作业提交代码 | ||
<br/> | ||
### 本节知识点 | ||
第4课:使用Truffle架构进行前后端交互,测试,部署 | ||
- 为什么要用Truffle,Truffle的基本概念 | ||
- Truffle 的command line 功能 | ||
- 初始化项目与Truffle项目目录结构 | ||
- 编译部署合约到testrpc |
Oops, something went wrong.