-
Notifications
You must be signed in to change notification settings - Fork 10
86_李奇楠_第六课作业 #2
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
lqn0011
wants to merge
35
commits into
Guigulive:86-李奇楠
Choose a base branch
from
lqn0011:86-李奇楠
base: 86-李奇楠
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.
The head ref may contain hidden characters: "86-\u674E\u5947\u6960"
Open
86_李奇楠_第六课作业 #2
Changes from all commits
Commits
Show all changes
35 commits
Select commit
Hold shift + click to select a range
a7ece7d
86-李奇楠 第一课作业
lqn0011 d138276
upload lesson 2
JASONews bdbed78
Merge commit 'd138276a7653afea63a240761b76779a1c940cbe' into 86-李奇楠
lqn0011 5ac3818
version 1 for lesson 2
lqn0011 98de74a
update lesson 2
lqn0011 2ac9653
add gas usage table
lqn0011 1d3647d
Upload lesson 3
JASONews 7dee7bd
Merge commit '1d3647d204b1b832956bd9de31c92269033c1589' into 86-李奇楠
lqn0011 ba39052
第一第二题
lqn0011 d6bf474
第一题截图
lqn0011 da205a6
第三题
lqn0011 d72a1a5
Upload lesson 4
JASONews 8daed7b
Merge commit 'd72a1a57676f0de6c4aaf782c2a26f0cef359e0c' into 86-李奇楠
lqn0011 8c5fa1b
update payroll based on react-box
lqn0011 97f7986
add payroll deploy files
lqn0011 41473a7
Unit test for addEmployee
lqn0011 2494932
minor code style
lqn0011 8abc287
Unit test for removeEmployee
lqn0011 f71cd7f
Unit test for getPaid
lqn0011 9b3fc9b
Answers
lqn0011 cb90858
include gas price when evaluate getPaid ether
lqn0011 6d358a8
Upload lesson 5
JASONews ea912ce
Merge commit '6d358a89b9155bf7ef36a0b6a86bb19cf2e22718' into 86-李奇楠
lqn0011 afcad71
init assignment based on orig
lqn0011 414ca5e
界面上新增员工和获取员工列表
lqn0011 57f7092
Employee part UI integration
lqn0011 6c390b2
addFund use timer to update
lqn0011 42ebe35
upload zip file
lqn0011 9ff9164
Add event based UI update for employee and employer
lqn0011 8817afd
minor
lqn0011 270b7d7
Upload lesson 6
JASONews 49b046f
Merge commit '270b7d7717904bf44b5c7d190fcf7864617c3ca0' into 86-李奇楠
lqn0011 f05a7db
Lesson 6 answer based on Lesson 5 work.
lqn0011 70c791b
Upload lesson 7
JASONews b3f8439
Merge commit '70c791b5e0c078ae4a6b340f4465f43879f2fa7d' into 86-李奇楠
lqn0011 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
The table of contents is too big for display.
Diff view
Diff view
There are no files selected for viewing
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,26 @@ | ||
| .idea/vcs.xml | ||
| .idea/workspace.xml | ||
| Lesson-4/assignment/payroll/payroll.iml | ||
| Lesson-4/assignment/payroll/.idea/misc.xml | ||
| Lesson-4/assignment/payroll/.idea/modules.xml | ||
| Lesson-4/assignment/payroll/.idea/workspace.xml | ||
| Lesson-5/orgin/orgin.iml | ||
| Lesson-5/orgin/package-lock.json | ||
| Lesson-5/orgin/.idea/misc.xml | ||
| Lesson-5/orgin/.idea/modules.xml | ||
| Lesson-5/orgin/.idea/workspace.xml | ||
| Lesson-5/assignment/payroll/orgin.iml | ||
| Lesson-5/assignment/payroll/package-lock.json | ||
| Lesson-5/assignment/payroll/.idea/misc.xml | ||
| Lesson-5/assignment/payroll/.idea/modules.xml | ||
| Lesson-5/assignment/payroll/.idea/workspace.xml | ||
| Lesson-6/orgin/orgin.iml | ||
| Lesson-6/orgin/package-lock.json | ||
| Lesson-6/orgin/.idea/misc.xml | ||
| Lesson-6/orgin/.idea/modules.xml | ||
| Lesson-6/orgin/.idea/workspace.xml | ||
| Lesson-6/assignment/payroll/orgin.iml | ||
| Lesson-6/assignment/payroll/package-lock.json | ||
| Lesson-6/assignment/payroll/.idea/misc.xml | ||
| Lesson-6/assignment/payroll/.idea/modules.xml | ||
| Lesson-6/assignment/payroll/.idea/workspace.xml |
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 |
|---|---|---|
| @@ -1 +1,51 @@ | ||
| /*作业请提交在这个目录下*/ | ||
| pragma solidity ^0.4.14; | ||
|
|
||
| contract Payroll { | ||
| uint constant payDuration = 10 seconds; | ||
|
|
||
| address owner; | ||
| uint salary; | ||
| address employee; | ||
| uint lastPayday; | ||
|
|
||
| function Payroll() { | ||
| owner = msg.sender; | ||
| } | ||
|
|
||
| function updateEmployeeAddress(address e) { | ||
| require(msg.sender == owner); | ||
|
|
||
| employee = e; | ||
| lastPayday = now; | ||
| } | ||
|
|
||
| function updateEmployeeSalary(uint s) { | ||
| require(msg.sender == owner); | ||
|
|
||
| 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() { | ||
| require(msg.sender == employee); | ||
|
|
||
| uint nextPayday = lastPayday + payDuration; | ||
| assert(nextPayday < now); | ||
|
|
||
| lastPayday = nextPayday; | ||
| employee.transfer(salary); | ||
| } | ||
| } | ||
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/> | ||
| ### 本节知识点 | ||
| 第2课:智能合约设计进阶-多员工薪酬系统 | ||
| - 动态静态数组的不同 | ||
| - 函数输入参数检查 revert | ||
| - 循环与遍历的安全性 | ||
| - 程序运行错误检查和容错:assert与require |
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,29 @@ | ||
| | Version 1 | | | | ||
| |-----------|------------------|----------------| | ||
| | Employee | transaction cost | execution cost | | ||
| |-----------|------------------|----------------| | ||
| | 1 | 22971 | 1699 | | ||
| | 2 | 23759 | 2487 | | ||
| | 3 | 24547 | 3275 | | ||
| | 4 | 25335 | 4063 | | ||
| | 5 | 26123 | 4851 | | ||
| | 6 | 26911 | 5639 | | ||
| | 7 | 27699 | 6427 | | ||
| | 8 | 28487 | 7215 | | ||
| | 9 | 29275 | 8003 | | ||
| | 10 | 30063 | 8791 | | ||
|
|
||
| | Version 2 | | | | ||
| |-----------|------------------|----------------| | ||
| | Employee | transaction cost | execution cost | | ||
| |-----------|------------------|----------------| | ||
| | 1 | 22122 | 850 | | ||
| | 2 | 22122 | 850 | | ||
| | 3 | 22122 | 850 | | ||
| | 4 | 22122 | 850 | | ||
| | 5 | 22122 | 850 | | ||
| | 6 | 22122 | 850 | | ||
| | 7 | 22122 | 850 | | ||
| | 8 | 22122 | 850 | | ||
| | 9 | 22122 | 850 | | ||
| | 10 | 22122 | 850 | |
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,10 @@ | ||
| ## 硅谷live以太坊智能合约 第二课作业 | ||
| 这里是同学提交作业的目录 | ||
|
|
||
| ### 第二课:课后作业 | ||
| 完成今天的智能合约添加100ETH到合约中 | ||
| - 加入十个员工,每个员工的薪水都是1ETH | ||
| 每次加入一个员工后调用calculateRunway这个函数,并且记录消耗的gas是多少?Gas变化么?如果有 为什么? | ||
| - 如何优化calculateRunway这个函数来减少gas的消耗? | ||
| 提交:智能合约代码,gas变化的记录,calculateRunway函数的优化 | ||
|
|
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,125 @@ | ||
| pragma solidity ^0.4.14; | ||
|
|
||
| contract Payroll { | ||
| struct Employee { | ||
| address id; | ||
| uint salary; | ||
| uint lastPayday; | ||
| } | ||
|
|
||
| uint constant payDuration = 10 seconds; | ||
|
|
||
| address owner; | ||
| Employee[] employees; | ||
| uint totalSalary; | ||
|
|
||
| function Payroll() { | ||
| owner = msg.sender; | ||
| } | ||
|
|
||
| function _partialPaid(Employee employee) private { | ||
| if (employee.id != 0x0) { | ||
| uint payment = employee.salary * (now - employee.lastPayday) / payDuration; | ||
| employee.id.transfer(payment); | ||
| } | ||
| } | ||
|
|
||
| function _findEmployee(address e) private constant returns (Employee, uint) { | ||
| for(uint i = 0; i < employees.length; i++) { | ||
| if(employees[i].id == e) { | ||
| return (employees[i], i); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| //For debug only | ||
| function getAllEmployees() returns (address[], uint[], uint[]){ | ||
| require(msg.sender == owner); | ||
| address[] memory addrs = new address[](employees.length); | ||
| uint[] memory salays = new uint[](employees.length); | ||
| uint[] memory lastpds = new uint[](employees.length); | ||
| for (uint i = 0; i < employees.length; i++) { | ||
| Employee storage employee = employees[i]; | ||
| addrs[i] = employee.id; | ||
| salays[i] = employee.salary; | ||
| lastpds[i] = employee.lastPayday; | ||
| } | ||
| return (addrs, salays, lastpds); | ||
| } | ||
| //For debug only | ||
| function getBalance() constant returns (uint){ | ||
| require(msg.sender == owner); | ||
| return this.balance; | ||
| } | ||
| //For debug only | ||
| function getTotalSalary() constant returns (uint){ | ||
| require(msg.sender == owner); | ||
| return totalSalary; | ||
| } | ||
|
|
||
|
|
||
| function addEmployee(address e, uint s) { | ||
| require(msg.sender == owner); | ||
| var (employee, index) = _findEmployee(e); | ||
| assert(employee.id == 0x00); | ||
| employees.push(Employee(e, s * 1 ether, now)); | ||
|
|
||
| totalSalary += s * 1 ether; | ||
| } | ||
|
|
||
| function removeEmployee(address e) { | ||
| require(msg.sender == owner); | ||
| var (employee, index) = _findEmployee(e); | ||
| assert(employee.id != 0x00); | ||
| delete employees[index]; | ||
| employees[index] = employees[employees.length-1]; | ||
| employees.length--; | ||
|
|
||
| totalSalary -= employee.salary; | ||
|
|
||
| _partialPaid(employee); | ||
| } | ||
|
|
||
| function updateEmployee(address e, uint s) { | ||
| require(msg.sender == owner); | ||
| var (employee, index) = _findEmployee(e); | ||
| assert(employee.id != 0x00); | ||
|
|
||
| uint delta_salary = employee.salary - s * 1 ether; | ||
|
|
||
| employees[index].id = e; | ||
| employees[index].salary = s * 1 ether; | ||
| employees[index].lastPayday = now; | ||
|
|
||
| totalSalary -= delta_salary; | ||
|
|
||
| _partialPaid(employee); | ||
| } | ||
|
|
||
| function addFund() payable returns (uint) { | ||
| return this.balance; | ||
| } | ||
|
|
||
| function calculateRunway() constant returns (uint) { | ||
| // uint totalSalary = 0; | ||
| // for (uint i = 0; i < employees.length; i++) { | ||
| // totalSalary += employees[i].salary; | ||
| // } | ||
| return this.balance / totalSalary; | ||
| } | ||
|
|
||
| function hasEnoughFund() constant returns (bool) { | ||
| return calculateRunway() > 0; | ||
| } | ||
|
|
||
| function getPaid() { | ||
| var (employee, index) = _findEmployee(msg.sender); | ||
| assert(employee.id != 0x00); | ||
|
|
||
| uint nextPayday = employee.lastPayday + payDuration; | ||
| assert(nextPayday < now); | ||
|
|
||
| employees[index].lastPayday = nextPayday; | ||
| employees[index].id.transfer(employees[index].salary); | ||
| } | ||
| } |
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,104 @@ | ||
| 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 { | ||
| if (employee.id != 0x0) { | ||
| uint payment = employee.salary * (now - employee.lastPayday) / payDuration; | ||
| employee.id.transfer(payment); | ||
| } | ||
| } | ||
|
|
||
| function _findEmployee(address e) private returns (Employee, uint) { | ||
| for(uint i = 0; i < employees.length; i++) { | ||
| if(employees[i].id == e) { | ||
| return (employees[i], i); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| function getAllEmployees() returns (address[], uint[], uint[]){ | ||
| require(msg.sender == owner); | ||
| address[] memory addrs = new address[](employees.length); | ||
| uint[] memory salays = new uint[](employees.length); | ||
| uint[] memory lastpds = new uint[](employees.length); | ||
|
|
||
| for (uint i = 0; i < employees.length; i++) { | ||
| Employee storage employee = employees[i]; | ||
| addrs[i] = employee.id; | ||
| salays[i] = employee.salary; | ||
| lastpds[i] = employee.lastPayday; | ||
| } | ||
|
|
||
| return (addrs, salays, lastpds); | ||
| } | ||
|
|
||
| function addEmployee(address e, uint s) { | ||
| require(msg.sender == owner); | ||
| var (employee, index) = _findEmployee(e); | ||
| assert(employee.id == 0x00); | ||
| employees.push(Employee(e, s * 1 ether, now)); | ||
| } | ||
|
|
||
| function removeEmployee(address e) { | ||
| require(msg.sender == owner); | ||
| var (employee, index) = _findEmployee(e); | ||
| assert(employee.id != 0x00); | ||
| delete employees[index]; | ||
| employees[index] = employees[employees.length-1]; | ||
| employees.length--; | ||
| } | ||
|
|
||
| function updateEmployee(address e, uint s) { | ||
| require(msg.sender == owner); | ||
| var (employee, index) = _findEmployee(e); | ||
| assert(employee.id != 0x00); | ||
|
|
||
| employees[index].id = e; | ||
| employees[index].salary = s * 1 ether; | ||
| employees[index].lastPayday = now; | ||
|
|
||
| _partialPaid(employee); | ||
| } | ||
|
|
||
| function addFund() payable returns (uint) { | ||
| return this.balance; | ||
| } | ||
|
|
||
| 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) { | ||
| return calculateRunway() > 0; | ||
| } | ||
|
|
||
| function getPaid() { | ||
| var (employee, index) = _findEmployee(msg.sender); | ||
| assert(employee.id != 0x00); | ||
|
|
||
| uint nextPayday = employee.lastPayday + payDuration; | ||
| assert(nextPayday < now); | ||
|
|
||
| employees[index].lastPayday = nextPayday; | ||
| employees[index].id.transfer(employees[index].salary); | ||
| } | ||
| } |
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.
Need to pay the rest due to the employee first before modifying, if the salary is not paid yet.
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.
Thanks updated in lesson two homework with partialPay func.