diff --git "a/Lesson-1/assignment/54_\344\272\216\345\256\217\345\255\246_\347\254\254\344\270\200\350\257\276\344\275\234\344\270\232.sol" "b/Lesson-1/assignment/54_\344\272\216\345\256\217\345\255\246_\347\254\254\344\270\200\350\257\276\344\275\234\344\270\232.sol" new file mode 100644 index 0000000..5a5ba7d --- /dev/null +++ "b/Lesson-1/assignment/54_\344\272\216\345\256\217\345\255\246_\347\254\254\344\270\200\350\257\276\344\275\234\344\270\232.sol" @@ -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); + } +} diff --git a/Lesson-2/README.md b/Lesson-2/README.md new file mode 100644 index 0000000..1fdc5b3 --- /dev/null +++ b/Lesson-2/README.md @@ -0,0 +1,16 @@ +## 硅谷live以太坊智能合约频道官方地址 + +### 第二课《智能合约设计进阶-多员工薪酬系统》 + +目录结构 +
| +
|--orgin 课程初始代码 +
| +
|--assignment 课程作业提交代码 +
+### 本节知识点 +第2课:智能合约设计进阶-多员工薪酬系统 +- 动态静态数组的不同 +- 函数输入参数检查 revert +- 循环与遍历的安全性 +- 程序运行错误检查和容错:assert与require diff --git a/Lesson-2/assignment/README.md b/Lesson-2/assignment/README.md new file mode 100644 index 0000000..a1fa2d0 --- /dev/null +++ b/Lesson-2/assignment/README.md @@ -0,0 +1,10 @@ +## 硅谷live以太坊智能合约 第二课作业 +这里是同学提交作业的目录 + +### 第二课:课后作业 +完成今天的智能合约添加100ETH到合约中 +- 加入十个员工,每个员工的薪水都是1ETH +每次加入一个员工后调用calculateRunway这个函数,并且记录消耗的gas是多少?Gas变化么?如果有 为什么? +- 如何优化calculateRunway这个函数来减少gas的消耗? +提交:智能合约代码,gas变化的记录,calculateRunway函数的优化 + diff --git a/Lesson-1/assignment/yours.sol b/Lesson-2/assignment/yours.sol similarity index 100% rename from Lesson-1/assignment/yours.sol rename to Lesson-2/assignment/yours.sol diff --git a/Lesson-2/orgin/README.md b/Lesson-2/orgin/README.md new file mode 100644 index 0000000..0309d94 --- /dev/null +++ b/Lesson-2/orgin/README.md @@ -0,0 +1,3 @@ +## 硅谷live以太坊智能合约 第二课《智能合约设计进阶-多员工薪酬系统》 + +这里是每一课的初始代码,有需要的同学可以参考 diff --git a/Lesson-2/orgin/payroll.sol b/Lesson-2/orgin/payroll.sol new file mode 100644 index 0000000..62e380e --- /dev/null +++ b/Lesson-2/orgin/payroll.sol @@ -0,0 +1,50 @@ +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 { + } + + function _findEmployee(address employeeId) private returns (Employee, uint) { + } + + function addEmployee(address employeeId, uint salary) { + } + + function removeEmployee(address employeeId) { + } + + function updateEmployee(address employeeId, uint salary) { + } + + function addFund() payable returns (uint) { + } + + 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) { + } + + function getPaid() { + } +} diff --git a/Lesson-3/README.md b/Lesson-3/README.md new file mode 100644 index 0000000..ba26ced --- /dev/null +++ b/Lesson-3/README.md @@ -0,0 +1,16 @@ +## 硅谷live以太坊智能合约频道官方地址 + +### 第三课《智能合约后端优化和产品化》 + +目录结构 +
| +
|--orgin 课程初始代码 +
| +
|--assignment 课程作业提交代码 +
+### 本节知识点 +第3课:智能合约后端优化和产品化 +- 如何通过数据结构优化降低合约执行成本 +- 合约的继承 +- 巧用modifier +- 以太坊函数库的使用和基本介绍 diff --git a/Lesson-3/addEmployee.png b/Lesson-3/addEmployee.png new file mode 100644 index 0000000..76da527 Binary files /dev/null and b/Lesson-3/addEmployee.png differ diff --git a/Lesson-3/addfund.png b/Lesson-3/addfund.png new file mode 100644 index 0000000..daa948e Binary files /dev/null and b/Lesson-3/addfund.png differ diff --git a/Lesson-3/assignment/README.md b/Lesson-3/assignment/README.md new file mode 100644 index 0000000..01011eb --- /dev/null +++ b/Lesson-3/assignment/README.md @@ -0,0 +1,14 @@ +## 硅谷live以太坊智能合约 第三课作业 +这里是同学提交作业的目录 + +### 第三课:课后作业 +- 第一题:完成今天所开发的合约产品化内容,使用Remix调用每一个函数,提交函数调用截图 +- 第二题:增加 changePaymentAddress 函数,更改员工的薪水支付地址,思考一下能否使用modifier整合某个功能 +- 第三题(加分题):自学C3 Linearization, 求以下 contract Z 的继承线 +- contract O +- contract A is O +- contract B is O +- contract C is O +- contract K1 is A, B +- contract K2 is A, C +- contract Z is K1, K2 diff --git a/Lesson-3/assignment/lesson3.sol b/Lesson-3/assignment/lesson3.sol new file mode 100644 index 0000000..7790fb6 --- /dev/null +++ b/Lesson-3/assignment/lesson3.sol @@ -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