-
Notifications
You must be signed in to change notification settings - Fork 13
56_胡闰祺_第三次作业 #46
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
Open
aceRunqi
wants to merge
16
commits into
Guigulive:56-胡闰祺
Choose a base branch
from
aceRunqi:master
base: 56-胡闰祺
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
56_胡闰祺_第三次作业 #46
Changes from all commits
Commits
Show all changes
16 commits
Select commit
Hold shift + click to select a range
c38345b
Update and rename yours.sol to 56_胡闰祺_第二次作业.sol
aceRunqi e199e9d
Upload lesson 3
JASONews de2ed59
Upload lesson 3
JASONews cd1f5f0
Merge pull request #36 from aceRunqi/master
taxic 2e9dc31
Revert "56_胡闰祺_第二次作业"
taxic ea0b6f3
Merge pull request #37 from Guigulive/revert-36-master
taxic e2f74b0
Merge pull request #2 from Guigulive/master
aceRunqi 22bf3ef
Update and rename yours.sol to 56_胡闰祺_第三次作业.sol
aceRunqi d8e85f1
Add files via upload
aceRunqi 9dc46f5
Upload lesson 4
JASONews 798bb92
Merge pull request #3 from Guigulive/master
aceRunqi 9011da3
Upload lesson 5
JASONews d688b89
Merge pull request #4 from Guigulive/master
aceRunqi d774837
Add files via upload
aceRunqi 16cc425
Upload lesson 6
JASONews dba37f0
Merge pull request #5 from Guigulive/master
aceRunqi File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
This file contains hidden or 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 @@ | ||
| /*作业请提交在这个目录下*/ |
This file contains hidden or 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以太坊智能合约频道官方地址 | ||
|
|
||
| ### 第三课《智能合约后端优化和产品化》 | ||
|
|
||
| 目录结构 | ||
| <br/>| | ||
| <br/>|--orgin 课程初始代码 | ||
| <br/>| | ||
| <br/>|--assignment 课程作业提交代码 | ||
| <br/> | ||
| ### 本节知识点 | ||
| 第3课:智能合约后端优化和产品化 | ||
| - 如何通过数据结构优化降低合约执行成本 | ||
| - 合约的继承 | ||
| - 巧用modifier | ||
| - 以太坊函数库的使用和基本介绍 |
This file contains hidden or 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,140 @@ | ||
| /*作业请提交在这个目录下*/ | ||
|
|
||
| /*作业请提交在这个目录下*/ | ||
|
|
||
| ############################################## Codes for Question 1. and Question 2. ######################################### | ||
|
|
||
| pragma solidity ^0.4.14; | ||
|
|
||
| contract Payroll { | ||
|
|
||
| struct Employee { | ||
| address EmployeeAddress; | ||
| uint salary; | ||
| uint lastPayday; | ||
| } | ||
|
|
||
| mapping(address => Employee) public employees; | ||
|
|
||
| uint constant payDuration = 30 seconds; | ||
|
|
||
| address OwnerAddress; | ||
|
|
||
| uint totalSalary = 0; | ||
|
|
||
| address thisAddress = this; | ||
|
|
||
| function Payroll() public { | ||
| OwnerAddress = msg.sender; | ||
| } | ||
|
|
||
| modifier onlyOwner() { | ||
| require(msg.sender == OwnerAddress); | ||
| _; | ||
| } | ||
|
|
||
| modifier employeeExist(address curr_EmployeeAddress) { | ||
|
|
||
| var curr_employee = employees[curr_EmployeeAddress]; | ||
| assert(curr_employee.EmployeeAddress != 0x0); | ||
| _; | ||
|
|
||
| } | ||
|
|
||
| function addEmployee(address curr_EmployeeAddress, uint curr_salary) onlyOwner { | ||
|
|
||
| assert(employees[curr_EmployeeAddress].EmployeeAddress == 0x0); | ||
|
|
||
| uint unitSalary = curr_salary * 1 ether; | ||
| employees[curr_EmployeeAddress] = Employee(curr_EmployeeAddress, unitSalary, now); | ||
|
|
||
| totalSalary += unitSalary; | ||
|
|
||
| } | ||
|
|
||
| modifier partialPay(address curr_EmployeeAddress) { | ||
|
|
||
| _partialPay(employees[curr_EmployeeAddress]); | ||
| _; | ||
|
|
||
| } | ||
| function _partialPay(Employee employee) private { | ||
|
|
||
| uint tempSalary = employee.salary * (now - employee.lastPayday); | ||
| tempSalary = tempSalary / payDuration; | ||
|
|
||
| employee.EmployeeAddress.transfer(tempSalary); | ||
|
|
||
| } | ||
|
|
||
|
|
||
| function removeEmployee(address curr_EmployeeAddress) onlyOwner employeeExist(curr_EmployeeAddress) partialPay(curr_EmployeeAddress) { | ||
|
|
||
| var curr_employee = employees[curr_EmployeeAddress]; | ||
| totalSalary -= curr_employee.salary; | ||
|
|
||
| delete employees[curr_EmployeeAddress]; | ||
|
|
||
| } | ||
|
|
||
| function updateEmployee(address curr_EmployeeAddress, uint new_salary) onlyOwner employeeExist(curr_EmployeeAddress) partialPay(curr_EmployeeAddress) { | ||
|
|
||
| var curr_employee = employees[curr_EmployeeAddress]; | ||
|
|
||
| uint unitSalary = new_salary * 1 ether; | ||
| totalSalary = totalSalary - curr_employee.salary + unitSalary; | ||
|
|
||
| curr_employee.salary = unitSalary; | ||
| curr_employee.lastPayday = now; | ||
|
|
||
| } | ||
|
|
||
| function changePaymentAddress(address curr_EmployeeAddress, address new_EmployeeAddress) onlyOwner employeeExist(curr_EmployeeAddress) partialPay(curr_EmployeeAddress) { | ||
|
|
||
| var curr_employee = employees[curr_EmployeeAddress]; | ||
|
|
||
| curr_employee.EmployeeAddress = new_EmployeeAddress; | ||
|
|
||
| } | ||
|
|
||
| function addFund() payable public returns (uint) { | ||
|
|
||
| return thisAddress.balance; | ||
|
|
||
| } | ||
|
|
||
| function calculateRunway() public view returns (uint) { | ||
|
|
||
| return thisAddress.balance / totalSalary; | ||
|
|
||
| } | ||
|
|
||
| function hasEnoughFund() public view returns (bool) { | ||
|
|
||
| return calculateRunway() > 0; | ||
|
|
||
| } | ||
|
|
||
| function getPaid() employeeExist(msg.sender){ | ||
|
|
||
| var curr_employee = employees[msg.sender]; | ||
|
|
||
| var newPayday = curr_employee.lastPayday + payDuration; | ||
| assert(newPayday < now); | ||
|
|
||
| curr_employee.lastPayday = newPayday; | ||
| curr_employee.EmployeeAddress.transfer(curr_employee.salary); | ||
|
|
||
| } | ||
| } | ||
|
|
||
| ################################################### Snapshots for Question 1. ############################################## | ||
| ### attached ### | ||
|
|
||
| ######################################################### Question 3. ###################################################### | ||
|
|
||
| |-----> B-----| | ||
| | |------> K1------| | ||
| O ---->|-----> A--|--| |-----> Z | ||
| | |---------> K2------| | ||
| |-----> C--| | ||
Binary file not shown.
This file contains hidden or 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,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 |
This file contains hidden or 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,3 @@ | ||
| ## 硅谷live以太坊智能合约 第三课 | ||
|
|
||
| 这里是每一课的初始代码,有需要的同学可以参考 |
Empty file.
This file contains hidden or 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 |
This file contains hidden or 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,12 @@ | ||
| ## 硅谷live以太坊智能合约 第四课作业 | ||
| 这里是同学提交作业的目录 | ||
|
|
||
| ### 第四课:课后作业 | ||
| - 将第三课完成的payroll.sol程序导入truffle工程 | ||
| - 在test文件夹中,写出对如下两个函数的单元测试: | ||
| - function addEmployee(address employeeId, uint salary) onlyOwner | ||
| - function removeEmployee(address employeeId) onlyOwner employeeExist(employeeId) | ||
| - 思考一下我们如何能覆盖所有的测试路径,包括函数异常的捕捉 | ||
| - (加分题,选作) | ||
| - 写出对以下函数的基于solidity或javascript的单元测试 function getPaid() employeeExist(msg.sender) | ||
| - Hint:思考如何对timestamp进行修改,是否需要对所测试的合约进行修改来达到测试的目的? |
This file contains hidden or 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 @@ | ||
| /*作业请提交在这个目录下*/ |
This file contains hidden or 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,3 @@ | ||
| ## 硅谷live以太坊智能合约 第四课 | ||
|
|
||
| 这里是每一课的初始代码,有需要的同学可以参考 |
This file contains hidden or 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 @@ | ||
|
|
This file contains hidden or 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,13 @@ | ||
| ## 硅谷live以太坊智能合约频道官方地址 | ||
|
|
||
| ### 第五课 | ||
|
|
||
| 目录结构 | ||
| <br/>| | ||
| <br/>|--orgin 课程初始代码 | ||
| <br/>| | ||
| <br/>|--assignment 课程作业提交代码 | ||
| <br/> | ||
|
|
||
| ### 本节知识点 | ||
|
|
Binary file not shown.
This file contains hidden or 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,2 @@ | ||
| ## 硅谷live以太坊智能合约 第五课作业 | ||
| 这里是同学提交作业的目录 |
This file contains hidden or 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 @@ | ||
| /*作业请提交在这个目录下*/ |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
function changePaymentAddress(address newEmployeeId) employeeExist(msg.sender) employeeNonExist(newEmployeeId) {
require(newEmployeeId != 0x0);
var oldEmployee = employees[msg.sender];
employees[newEmployeeId] = Employee(newEmployeeId, oldEmployee.salary, oldEmployee.lastPayday);
delete employees[msg.sender];
}
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
(1) require new addr non-zero
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
(2) better for employees to change their addresses