Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
27 commits
Select commit Hold shift + click to select a range
3f90a82
41_王硕_第二次课后作业
Mar 16, 2018
b24a1cb
Merge pull request #1 from Guigulive/master
Mar 20, 2018
74f11ce
41_王硕_第三次作业
Mar 20, 2018
466e24f
Create 41_王硕_第三次作业
Mar 20, 2018
535a0d2
完成今天所完成的合约产品化内容,使用Remix调用每一个函数,提交函数调用截图。
Mar 20, 2018
b1a3eac
Create 41_王硕_第三次作业
Mar 20, 2018
2a1cc9f
41_王硕_第四次作业
Mar 24, 2018
e8be9b7
Merge pull request #2 from cxinping/master
Mar 24, 2018
0187883
Merge pull request #3 from cxinping/41-王硕
Mar 24, 2018
08c05c8
Merge pull request #4 from cxinping/master
Mar 24, 2018
587285e
删除垃圾文件
Mar 24, 2018
6886cce
Merge pull request #5 from cxinping/master
Mar 24, 2018
bf48126
41_王硕_第四次作业
Mar 24, 2018
8778f0b
Merge pull request #6 from cxinping/master
Mar 24, 2018
8345c08
修改代码
Mar 24, 2018
9bacb54
Merge pull request #7 from cxinping/master
Mar 24, 2018
1c55275
41_王硕_第四次作业
Mar 25, 2018
6ba1a9e
Merge pull request #8 from cxinping/master
Mar 25, 2018
c987d45
get new version codes
Mar 26, 2018
560dbe7
41_王硕_第五次作业
Mar 26, 2018
ae27c21
Merge pull request #9 from cxinping/master
Mar 26, 2018
3d14395
优化代码
Mar 27, 2018
24354f1
Merge pull request #10 from cxinping/master
Mar 27, 2018
d2db5e9
41_王硕_第六次作业
Mar 29, 2018
715340e
Merge pull request #11 from cxinping/master
Mar 29, 2018
a43d736
41_王硕_第六次作业
Mar 30, 2018
2ed7270
Merge pull request #12 from cxinping/master
Mar 30, 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
108 changes: 108 additions & 0 deletions Lesson-2/assignment/payRoll.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
1, 完成今天的智能合约添加100 Ethan到合约中,加入十个员工,每个员工的薪水都是1 Eth。

2, 每次加入一个员工后调用calculateRunway这个函数并且记录消耗的gas是多少?
消耗 2 gas.


3, Gas 变化吗?如果有,为什么?
有变化,因为调用了智能合约的函数 calculateRunway。
4, 如何优化 calculateRunway这个函数来减少gas的消耗?
在函数中避免使用 this关键字。

5, 提交:智能合约代码,gais的变化记录,calculateRunway函数的优化。
pragma solidity ^0.4.14;
contract Payroll {
struct Employee{
address id;
uint salary;
uint lastPayday;

}

uint constant payDuration = 5 seconds;
address owner;
Employee[] employees;

function Payroll(){
//当前创建合约的人
owner = msg.sender;
}

function _partialPaid(Employee employee) private{
uint payment = employee.salary * (now - employee.lastPayday) / payDuration ;
employee.id.transfer(payment);
}

function _findEmployee(address employeeId ) private returns (Employee,uint ){
for(uint i = 0; i < employees.length; i++ ){
if( employees[i].id == employeeId ){
return (employees[i], i );
}
}
}

function addEmployee(address employeeId, uint salary){
require(msg.sender == owner);
var (employee, index) = _findEmployee(employeeId);
//保证你想要加入的这个员工是之前没有的,如果员工已经存在,就没有必要再加一遍了
assert(employee.id == 0x0) ;

employees.push(Employee( employeeId, salary * 1 ether, now ));
}

function removeEmployee(address employeeId){
require(msg.sender == owner);
var (employee, index) = _findEmployee(employeeId);
assert(employee.id != 0x0) ;

//否则快付工资的时候老板直接把你remove掉,你这个月不是白干了嘛,防止黑心老板,删除员工信息前,如果没有到payDuration规定的结算日,但也做了工作,就要先结算清楚
_partialPaid( employee );
delete employees[index];
employees[index ] = employees[ employees.length -1];
employees.length -= 1 ;
}

function updateEmployee(address employeeId ,uint salary) {
require(msg.sender == owner);
var (employee, index) = _findEmployee(employeeId);
assert(employee.id != 0x0) ;

_partialPaid( employee );
employees[index].salary = salary * 1 ether;
employees[index].lastPayday = now;
}

//往智能合约里加钱
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 + totalSalary;
}
//当前可以支付多少次薪水
return this.balance / totalSalary ;
}

//查看在contract中工资的钱能否支持一个月的钱
function hasEnoughFund() returns(bool){
return calculateRunway() > 0;
}

//到了领工资日,就可以点击领取工资
function getPaid(){
var (employee, index) = _findEmployee(msg.sender);
assert(employee.id != 0x0) ;

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

employees[index].lastPayday = nextPayDay ;
employees[index].id.transfer(employee.salary);
}

}
95 changes: 95 additions & 0 deletions Lesson-3/assignment/41_王硕_第三次作业
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
pragma solidity ^0.4.14;

contract Payroll {
struct Employee{
address id;
uint salary;
uint lastPayday;
}

uint constant payDuration = 5 seconds;
address owner;
mapping(address => Employee) employees ;

uint totalSalary = 0 ;

function Payroll(){
//当前创建合约的人
owner = msg.sender;
}

function _partialPaid(Employee employee) private{
uint payment = employee.salary * (now - employee.lastPayday) / payDuration ;
employee.id.transfer(payment);
}

function addEmployee(address employeeId, uint salary){
require(msg.sender == owner);
var employee = employees[employeeId];
//保证你想要加入的这个员工是之前没有的,如果员工已经存在,就没有必要再加一遍了
assert(employee.id == 0x0) ;

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

function removeEmployee(address employeeId){
require(msg.sender == owner);
var employee = employees[employeeId];
assert(employee.id != 0x0) ;

//否则快付工资的时候老板直接把你remove掉,你这个月不是白干了嘛,防止黑心老板,删除员工信息前,如果没有到payDuration规定的结算日,但也做了工作,就要先结算清楚
_partialPaid( employee );
totalSalary -= employees[employeeId].salary;

delete employees[employeeId];
}

function updateEmployee(address employeeId ,uint salary) {
require(msg.sender == owner);
var employee = employees[employeeId];
assert(employee.id != 0x0) ;

_partialPaid( employee );
employees[employeeId].salary = salary * 1 ether;
employees[employeeId].lastPayday = now;
}

function changePaymentAddress(address oldEmployeeId, address newEmployeeId){
require(msg.sender == owner);
var employee = employees[oldEmployeeId];
assert(employee.id != 0x0) ;

employees[oldEmployeeId].id = newEmployeeId;
}

//往智能合约里加钱
function addFund() payable returns (uint) {
return this.balance;
}

// 返回钱能支付工资的次数
function calculateRunway() returns (uint){

//当前可以支付多少次薪水
return this.balance / totalSalary ;
}

//查看在contract中工资的钱能否支持一个月的钱
function hasEnoughFund() returns(bool){
return calculateRunway() > 0;
}

//到了领工资日,就可以点击领取工资
function getPaid(){
var employee = employees[msg.sender] ;
assert(employee.id != 0x0) ;

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

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

}
Binary file added Lesson-3/assignment/pic1.png
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 Lesson-3/assignment/pic2.png
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 Lesson-3/assignment/pic3.png
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 Lesson-3/assignment/pic4.png
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 Lesson-3/assignment/pic5.png
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 Lesson-3/assignment/pic6.png
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 Lesson-3/assignment/pic7.png
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 Lesson-3/assignment/pic8.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
35 changes: 35 additions & 0 deletions Lesson-4/assignment/41_王硕_第四次作业
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
var Payroll = artifacts.require("./Payroll.sol");

contract('Payroll', function(accounts) {

it("add employee successed !", function() {
return Payroll.deployed().then(function(instance) {
payrollInstance = instance;
return payrollInstance.addEmployee( accounts[1] , 1);
}).then(function() {

return payrollInstance.employees.call(accounts[1]);
}).then(function(employee){
//console.log("*** employee.id => " + employee[0] );
employee1 = employee[0] ;
}).then(function(){
assert.equal(employee1, accounts[1]);
})
});

it("remove employee successed !", function() {
return Payroll.deployed().then(function(instance) {
payrollInstance = instance;
//console.log("*** accounts[1] => " + accounts[1] );
return payrollInstance.removeEmployee( accounts[1] );
}).then(function() {
return payrollInstance.employees.call(accounts[1]);
}).then(function(employee){
removedEmployeeId = employee[0] ;
// console.log("*** removed employee.id => " + employee[0] );
}).then(function(){
assert.equal(removedEmployeeId, 0 );
})
});

});
Empty file added Lesson-4/assignment/README.md
Empty file.
13 changes: 13 additions & 0 deletions Lesson-5/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
## 硅谷live以太坊智能合约频道官方地址

### 第五课

目录结构
<br/>|
<br/>|--orgin 课程初始代码
<br/>|
<br/>|--assignment 课程作业提交代码
<br/>

### 本节知识点

2 changes: 2 additions & 0 deletions Lesson-5/assignment/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
## 硅谷live以太坊智能合约 第五课作业
这里是同学提交作业的目录
Loading