Skip to content
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

9 张伟 第五+第六次作业 #477

Open
wants to merge 50 commits into
base: 9-张伟
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
50 commits
Select commit Hold shift + click to select a range
388575e
Merge pull request #1 from linjie-1/master
zli25 Jan 7, 2018
901d828
first homework
seanxwzhang Jan 9, 2018
db44ac5
Revert "first homework"
seanxwzhang Jan 9, 2018
7952c08
第一次作业
BitEveryXW Jan 9, 2018
2874dfb
Merge pull request #2 from linjie-1/master
steven672 Jan 9, 2018
27587f6
Create 第一课作业
steven672 Jan 9, 2018
04d95b4
Delete 第一课作业
steven672 Jan 9, 2018
3040b06
Update yours.sol
steven672 Jan 9, 2018
c6032c2
Merge pull request #1 from linjie-1/master
zl777 Jan 10, 2018
602f1c1
Update yours.sol
zl777 Jan 10, 2018
9ee0a7b
第一次作业 第二次提交 @1/12/18
BitEveryXW Jan 12, 2018
61681d3
Merge pull request #145 from BitEveryXW/XW_dev
BitEveryXW Jan 12, 2018
e164779
Revert "Xw dev"
BitEveryXW Jan 12, 2018
67064fd
Merge pull request #146 from linjie-1/revert-145-XW_dev
BitEveryXW Jan 12, 2018
10b864f
根据要求加了require 函数, 并更新了源代码的基本function。 还请老师在看下~
steven672 Jan 12, 2018
60f2dcd
update
steven672 Jan 12, 2018
8a87868
update
steven672 Jan 12, 2018
9923178
Merge pull request #1 from linjie-1/master
zyzlouis Jan 13, 2018
7852900
Merge pull request #170 from zli25/master
zli25 Jan 13, 2018
d07ab7f
Update yours.sol
zl777 Jan 13, 2018
b99b727
Merge pull request #217 from linjie-1/zl777-patch-1
hezhihua81 Jan 13, 2018
55fac2b
Merge pull request #3 from linjie-1/master
steven672 Jan 13, 2018
5b96d76
revert last mis-checkin...
hezhihua81 Jan 13, 2018
31621b6
Update
steven672 Jan 13, 2018
2e095ae
update hw2
steven672 Jan 13, 2018
dbff9b0
创建第三课目录
lsdlinshunda Jan 14, 2018
78fa6e7
添加第三课说明
lsdlinshunda Jan 14, 2018
4d8f45b
添加第三课作业说明
lsdlinshunda Jan 14, 2018
1010b85
Merge branch 'master' into master
steven672 Jan 17, 2018
80e53bb
delete hw2
steven672 Jan 17, 2018
ec1f212
restore
steven672 Jan 17, 2018
dea4755
Merge remote-tracking branch 'linjie-1/9-张伟' into 9-张伟
Jan 17, 2018
73ef174
Merge remote-tracking branch 'linjie-1/master' into 9-张伟
Jan 17, 2018
2c812b6
第三次作业
Jan 17, 2018
e52488a
Merge pull request #2 from linjie-1/master
zyzlouis Jan 17, 2018
68df86a
Merge pull request #304 from zl777/master
zl777 Jan 17, 2018
bf357cf
Create README.md
linjie-1 Jan 17, 2018
50c15e3
Create README.md
linjie-1 Jan 17, 2018
83e2b9b
Create payroll.sol
linjie-1 Jan 17, 2018
b38d0ff
Create README.md
linjie-1 Jan 17, 2018
953eab2
Create yours.sol
linjie-1 Jan 17, 2018
e28b1b3
Delete yours.sol
linjie-1 Jan 17, 2018
b72fc45
Create yours.sol
linjie-1 Jan 17, 2018
c66e0cc
Update yours.sol
linjie-1 Jan 17, 2018
159bd42
Revert "第3课的代码"
lsdlinshunda Jan 17, 2018
cdece6b
Merge pull request #315 from linjie-1/revert-304-master
lsdlinshunda Jan 17, 2018
15f22fd
Merge pull request #321 from zyzlouis/master
zyzlouis Jan 18, 2018
65029af
第四次作业
Jan 20, 2018
96057e4
第五次作业
Jan 24, 2018
edfa8a0
第五+第六次作业
Jan 27, 2018
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Lesson2/assignment/yours.sol
Original file line number Diff line number Diff line change
Expand Up @@ -279,4 +279,4 @@ execution cost 930 gas
transaction cost 22202 gas
execution cost 930 gas

//gas 不变了,因为calculateRunway现在只是做一个除法
//gas 不变了,因为calculateRunway现在只是做一个除法
16 changes: 16 additions & 0 deletions Lesson3/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
## 硅谷live以太坊智能合约频道官方地址

### 第三课《智能合约后端优化和产品化》

目录结构
<br/>|
<br/>|--orgin 课程初始代码
<br/>|
<br/>|--assignment 课程作业提交代码
<br/>
### 本节知识点
第3课:智能合约后端优化和产品化
- 如何通过数据结构优化降低合约执行成本
- 合约的继承
- 巧用modifier
- 以太坊函数库的使用和基本介绍
Binary file added Lesson3/assignment/AddEmployee.jpeg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added Lesson3/assignment/AddFund.jpeg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added Lesson3/assignment/CalculateRunaway.jpeg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added Lesson3/assignment/CheckEmployee.jpeg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added Lesson3/assignment/GetEmployee.jpeg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added Lesson3/assignment/GetPaid.jpeg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added Lesson3/assignment/HasEnoughFund.jpeg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
14 changes: 14 additions & 0 deletions Lesson3/assignment/README.md
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
Binary file added Lesson3/assignment/RemoveEmployee.jpeg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added Lesson3/assignment/UpdateEmployee.jpeg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
250 changes: 250 additions & 0 deletions Lesson3/assignment/yours.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,250 @@
/*assignment 1*/
pragma solidity ^0.4.14;

/** @title Payroll contract. */
contract Payroll {
struct Employee {
address wallet;
uint salary;
uint lastPayday;
}

mapping(address => Employee) public employees;
address owner;
uint totalSalary = 0;
uint constant payDuration = 10 seconds;

/** @dev Constructor.
*/
function Payroll() {
owner = msg.sender;
}

modifier onlyOwner {
require(msg.sender == owner);
_;
}

modifier employeeExist(address wallet){
var employee = employees[wallet];
assert(employee.wallet != 0x0);
_;
}

/** @dev Pay employee their rest payment before any change.
* @param employee Employee that need to be paid.
*/
function _payRestSalary(Employee employee) private {
uint restPayment = employee.salary * (now - employee.lastPayday) / payDuration;
employee.wallet.transfer(restPayment);
}

/** @dev Add new employee to contract.
* @param salary Salary of the new employee.
* @param wallet Wallet address of the new employee.
*/
function addEmployee(uint salary, address wallet) onlyOwner {
var employee = employees[wallet];

employees[wallet] = Employee(wallet, salary * 1 ether, now);
totalSalary += employees[wallet].salary;
}

/** @dev Update employee in the contract.
* @param salary Salary of the new employee needs to be updated.
* @param wallet Wallet address of the employee needs to be updated.
*/
function updateEmployee(uint salary, address wallet) onlyOwner employeeExist(wallet){
var employee = employees[wallet];

_payRestSalary(employee);
totalSalary = totalSalary - employees[wallet].salary;
employees[wallet].salary = salary * 1 ether;
employees[wallet].lastPayday = now;
totalSalary = totalSalary + employees[wallet].salary;
}

function checkEmployee(address wallet) returns (uint salary, uint lastPayday) {
var employee = employees[wallet];

salary = employee.salary;
lastPayday = employee.lastPayday;
}

/** @dev Remove employee in the contract.
* @param wallet Wallet address of the employee needs to be removed.
*/
function removeEmployee(address wallet) onlyOwner employeeExist(wallet){
var employee = employees[wallet];

_payRestSalary(employee);
totalSalary -= employees[wallet].salary;
delete employees[wallet];
}

/** @dev add fund to employer.
* @return employer balance after adding fund.
*/
function addFund() payable returns (uint) {
return this.balance;
}

/** @dev calculate how many times the employer can pay the employees
* @return the times that employees can get paid from employer's balance
*/
function calculateRunway() returns (uint) {
return this.balance / totalSalary;
}

/** @dev check if employer has enough money to pay
* @return true means employers has enough money, false means not
*/
function hasEnoughFund() returns (bool) {
return calculateRunway() > 0;
}

/** @dev pay salary to employee
*/
function getPaid() employeeExist(msg.sender){
var employee = employees[msg.sender];

uint nextPayDay = employee.lastPayday + payDuration;
assert(nextPayDay < now);

employees[msg.sender].lastPayday = nextPayDay;
employee.wallet.transfer(employee.salary);
}
}


///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
/*assignment 2*/
pragma solidity ^0.4.14;

/** @title Payroll contract. */
contract Payroll {
struct Employee {
address wallet;
uint salary;
uint lastPayday;
}

mapping(address => Employee) public employees;
address owner;
uint totalSalary = 0;
uint constant payDuration = 10 seconds;

/** @dev Constructor.
*/
function Payroll() {
owner = msg.sender;
}

modifier onlyOwner {
require(msg.sender == owner);
_;
}

modifier employeeExist(address wallet){
var employee = employees[wallet];
assert(employee.wallet != 0x0);
_;
}

modifier employeeNotExist(address wallet){
assert(employees[wallet].wallet == 0x0);
_;
}

/** @dev Pay employee their rest payment before any change.
* @param employee Employee that need to be paid.
*/
function _payRestSalary(Employee employee) private {
uint restPayment = employee.salary * (now - employee.lastPayday) / payDuration;
employee.wallet.transfer(restPayment);
}

/** @dev Add new employee to contract.
* @param salary Salary of the new employee.
* @param wallet Wallet address of the new employee.
*/
function addEmployee(uint salary, address wallet) onlyOwner employeeNotExist(wallet){
var employee = employees[wallet];

employees[wallet] = Employee(wallet, salary * 1 ether, now);
totalSalary += employees[wallet].salary;
}

/** @dev Update employee in the contract.
* @param salary Salary of the new employee needs to be updated.
* @param wallet Wallet address of the employee needs to be updated.
*/
function updateEmployee(uint salary, address wallet) onlyOwner employeeExist(wallet) {
var employee = employees[wallet];

_payRestSalary(employee);
totalSalary = totalSalary - employees[wallet].salary;
employees[wallet].salary = salary * 1 ether;
employees[wallet].lastPayday = now;
totalSalary = totalSalary + employees[wallet].salary;
}

function checkEmployee(address wallet) returns (uint salary, uint lastPayday) {
var employee = employees[wallet];

salary = employee.salary;
lastPayday = employee.lastPayday;
}

function changePaymentAddress(address oldWallet, address newWallet) onlyOwner employeeExist(oldWallet) employeeNotExist(newWallet){
var employee = employees[oldWallet];
_payRestSalary(employee);
var oldSalary = employee.salary;
removeEmployee(oldWallet);
addEmployee(oldSalary / 1 ether, newWallet);
}

/** @dev Remove employee in the contract.
* @param wallet Wallet address of the employee needs to be removed.
*/
function removeEmployee(address wallet) onlyOwner employeeExist(wallet){
var employee = employees[wallet];

_payRestSalary(employee);
totalSalary -= employees[wallet].salary;
delete employees[wallet];
}

/** @dev add fund to employer.
* @return employer balance after adding fund.
*/
function addFund() payable returns (uint) {
return this.balance;
}

/** @dev calculate how many times the employer can pay the employees
* @return the times that employees can get paid from employer's balance
*/
function calculateRunway() returns (uint) {
return this.balance / totalSalary;
}

/** @dev check if employer has enough money to pay
* @return true means employers has enough money, false means not
*/
function hasEnoughFund() returns (bool) {
return calculateRunway() > 0;
}

/** @dev pay salary to employee
*/
function getPaid() employeeExist(msg.sender){
var employee = employees[msg.sender];

uint nextPayDay = employee.lastPayday + payDuration;
assert(nextPayDay < now);

employees[msg.sender].lastPayday = nextPayDay;
employee.wallet.transfer(employee.salary);
}
}
3 changes: 3 additions & 0 deletions Lesson3/orgin/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
## 硅谷live以太坊智能合约 第三课

这里是每一课的初始代码,有需要的同学可以参考
Empty file added Lesson3/orgin/payroll.sol
Empty file.
16 changes: 16 additions & 0 deletions Lesson4/README.md
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
12 changes: 12 additions & 0 deletions Lesson4/assignment/README.md
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进行修改,是否需要对所测试的合约进行修改来达到测试的目的?
32 changes: 32 additions & 0 deletions Lesson4/assignment/TestPayroll.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
var Payroll = artifacts.require("./Payroll.sol");
const ether = 1000000000000000000;

contract('Payroll', function(accounts) {

it("Employee with wallet: test account[8] and salary: 1 ether should be stored.", function() {
return Payroll.deployed().then(function(instance) {
payrollInstance = instance;

return payrollInstance.addEmployee(1, accounts[8]);
}).then(function() {
return payrollInstance.isEmployeeExist.call(accounts[8]);
}).then(function(isExist) {
assert.equal(isExist, true, "The test account[9] with 1 ether salary was not stored.");
});
});

it("Employee with wallet: test account[9] and salary: 1 ether should be stored and then removed.", function() {
return Payroll.deployed().then(function(instance) {
payrollInstance = instance;

return payrollInstance.addEmployee(1, accounts[9]);
}).then(function() {
return payrollInstance.removeEmployee(accounts[9]);
}).then(function(){
return payrollInstance.isEmployeeExist.call(accounts[9]);
}).then(function(isExist) {
assert.deepEqual(isExist, false, "The test account[9] with 1 ether salary was not deleted.");
});
});

});
Loading