Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
35 commits
Select commit Hold shift + click to select a range
a7ece7d
86-李奇楠 第一课作业
lqn0011 Mar 11, 2018
d138276
upload lesson 2
JASONews Mar 15, 2018
bdbed78
Merge commit 'd138276a7653afea63a240761b76779a1c940cbe' into 86-李奇楠
lqn0011 Mar 16, 2018
5ac3818
version 1 for lesson 2
lqn0011 Mar 16, 2018
98de74a
update lesson 2
lqn0011 Mar 16, 2018
2ac9653
add gas usage table
lqn0011 Mar 16, 2018
1d3647d
Upload lesson 3
JASONews Mar 18, 2018
7dee7bd
Merge commit '1d3647d204b1b832956bd9de31c92269033c1589' into 86-李奇楠
lqn0011 Mar 19, 2018
ba39052
第一第二题
lqn0011 Mar 21, 2018
d6bf474
第一题截图
lqn0011 Mar 21, 2018
da205a6
第三题
lqn0011 Mar 21, 2018
d72a1a5
Upload lesson 4
JASONews Mar 22, 2018
8daed7b
Merge commit 'd72a1a57676f0de6c4aaf782c2a26f0cef359e0c' into 86-李奇楠
lqn0011 Mar 22, 2018
8c5fa1b
update payroll based on react-box
lqn0011 Mar 22, 2018
97f7986
add payroll deploy files
lqn0011 Mar 22, 2018
41473a7
Unit test for addEmployee
lqn0011 Mar 23, 2018
2494932
minor code style
lqn0011 Mar 24, 2018
8abc287
Unit test for removeEmployee
lqn0011 Mar 24, 2018
f71cd7f
Unit test for getPaid
lqn0011 Mar 24, 2018
9b3fc9b
Answers
lqn0011 Mar 24, 2018
cb90858
include gas price when evaluate getPaid ether
lqn0011 Mar 25, 2018
6d358a8
Upload lesson 5
JASONews Mar 25, 2018
ea912ce
Merge commit '6d358a89b9155bf7ef36a0b6a86bb19cf2e22718' into 86-李奇楠
lqn0011 Mar 26, 2018
afcad71
init assignment based on orig
lqn0011 Mar 27, 2018
414ca5e
界面上新增员工和获取员工列表
lqn0011 Mar 27, 2018
57f7092
Employee part UI integration
lqn0011 Mar 27, 2018
6c390b2
addFund use timer to update
lqn0011 Mar 28, 2018
42ebe35
upload zip file
lqn0011 Mar 28, 2018
9ff9164
Add event based UI update for employee and employer
lqn0011 Mar 28, 2018
8817afd
minor
lqn0011 Mar 29, 2018
270b7d7
Upload lesson 6
JASONews Mar 29, 2018
49b046f
Merge commit '270b7d7717904bf44b5c7d190fcf7864617c3ca0' into 86-李奇楠
lqn0011 Mar 29, 2018
f05a7db
Lesson 6 answer based on Lesson 5 work.
lqn0011 Mar 30, 2018
70c791b
Upload lesson 7
JASONews Apr 1, 2018
b3f8439
Merge commit '70c791b5e0c078ae4a6b340f4465f43879f2fa7d' into 86-李奇楠
lqn0011 Apr 2, 2018
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
The table of contents is too big for display.
Diff view
Diff view
  •  
  •  
  •  
26 changes: 26 additions & 0 deletions .gitignore
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
50 changes: 50 additions & 0 deletions Lesson-1/assignment/yours.sol
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);

Copy link
Copy Markdown
Contributor

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.

Copy link
Copy Markdown
Author

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.

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);
}
}
16 changes: 16 additions & 0 deletions Lesson-2/README.md
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
29 changes: 29 additions & 0 deletions Lesson-2/assignment/Gas Usage Record.txt
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 |
10 changes: 10 additions & 0 deletions Lesson-2/assignment/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
## 硅谷live以太坊智能合约 第二课作业
这里是同学提交作业的目录

### 第二课:课后作业
完成今天的智能合约添加100ETH到合约中
- 加入十个员工,每个员工的薪水都是1ETH
每次加入一个员工后调用calculateRunway这个函数,并且记录消耗的gas是多少?Gas变化么?如果有 为什么?
- 如何优化calculateRunway这个函数来减少gas的消耗?
提交:智能合约代码,gas变化的记录,calculateRunway函数的优化

125 changes: 125 additions & 0 deletions Lesson-2/assignment/yours.sol
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);
}
}
104 changes: 104 additions & 0 deletions Lesson-2/assignment/yours_ver1.sol
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);
}
}
Loading