Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
30 commits
Select commit Hold shift + click to select a range
8a9dc92
Update yours.sol
newjueqi Mar 13, 2018
b6a228f
修正require 和assert的用法
newjueqi Mar 13, 2018
0e67468
Update yours.sol
newjueqi Mar 14, 2018
23a28fb
Update and rename yours.sol to 56_胡闰祺_第一次作业.sol
aceRunqi Mar 14, 2018
bbaf120
Merge remote-tracking branch 'upstream/master'
Mar 15, 2018
33729c4
Merge pull request #1 from Guigulive/master
aceRunqi Mar 15, 2018
06831bf
Update yours.sol
newjueqi Mar 16, 2018
211fba5
Update README.md
newjueqi Mar 16, 2018
8efa7ed
Update README.md
newjueqi Mar 16, 2018
119f3f1
Create answer.sol
newjueqi Mar 16, 2018
7096cd2
Update README.md
newjueqi Mar 16, 2018
50a188d
Update yours.sol
newjueqi Mar 16, 2018
c38345b
Update and rename yours.sol to 56_胡闰祺_第二次作业.sol
aceRunqi Mar 17, 2018
cd1f5f0
Merge pull request #36 from aceRunqi/master
taxic Mar 19, 2018
2e9dc31
Revert "56_胡闰祺_第二次作业"
taxic Mar 19, 2018
ea0b6f3
Merge pull request #37 from Guigulive/revert-36-master
taxic Mar 19, 2018
54bc506
Merge remote-tracking branch 'upstream/master'
Mar 19, 2018
baef76e
Merge branch 'master' of github.com:newjueqi/Team-F
Mar 19, 2018
8f7d92c
Update answer.sol
newjueqi Mar 20, 2018
a23520a
finish class
Mar 20, 2018
24ca4fe
Merge branch 'master' of github.com:newjueqi/Team-F
Mar 20, 2018
64da731
Merge remote-tracking branch 'upstream/master'
Mar 22, 2018
96bd01a
finish
newjueqi Mar 22, 2018
3df0247
Merge remote-tracking branch 'upstream/master'
Mar 26, 2018
cfa10ff
Merge branch 'master' of github.com:newjueqi/Team-F
Mar 26, 2018
ba3a789
Create 笔记.md
newjueqi Mar 26, 2018
b14a9a7
finish lesson 5
Mar 28, 2018
0018997
Merge remote-tracking branch 'upstream/master'
Mar 29, 2018
6e6aaf4
finish class 6
Mar 29, 2018
e157224
finish class 5
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
65 changes: 65 additions & 0 deletions Lesson-1/assignment/yours.sol
Original file line number Diff line number Diff line change
@@ -1 +1,66 @@
/*作业请提交在这个目录下*/

pragma solidity ^0.4.14;

contract Payroll {
uint salary = 1 ether; //工资
address employee; //员工地址
address owner; //老板地址
uint lastPayday ; // 上次发薪水的时间

uint constant payDuration = 10 seconds;// 发薪水的时间间隔

//设置老板地址
function Payroll() public {
owner = msg.sender;
}


function updateEmployeeAddressSalary(address e, uint s){
require(msg.sender == owner);
require(s>0);

//修改前先结余
if(employee != 0x0 ){
uint shouldPay = salary*(now-lastPayday)/payDuration;
employee.transfer(shouldPay);
}

employee = e;
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);

}
}
29 changes: 29 additions & 0 deletions Lesson-2/assignment/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,32 @@
- 如何优化calculateRunway这个函数来减少gas的消耗?
提交:智能合约代码,gas变化的记录,calculateRunway函数的优化

### 回答:
加入员工时,calculateRunway这个函数gas变化如下:

transaction cost, execution cost

22966 1694

23747 2475

23528 3256

25309 4037

26090 4818

26871 5599

27652 6380

28433 7161

29214 7942

29995 8723


可以看到,在calculateRunway函数中,因为employees数组的长度不断增大,遍历这个数组所需要的gas也不断增大,所以需要把计算salary放在别的地方。

优化后的程序写在answer.sol
119 changes: 119 additions & 0 deletions Lesson-2/assignment/answer.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,119 @@
/*
加入员工时,calculateRunway这个函数gas变化如下:

transaction cost, execution cost

22966 1694

23747 2475

23528 3256

25309 4037

26090 4818

26871 5599

27652 6380

28433 7161

29214 7942

29995 8723

可以看到,在calculateRunway函数中,因为employees数组的长度不断增大,遍历这个数组所需要的gas也不断增大,所以需要把计算salary放在别的地方。

*/

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 =0;

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( employeeId == employees[i].id ){
return (employees[i],i);
}
}
}

function addEmployee(address employeeId, uint salary) {
require(owner == msg.sender);
var (employee,index) = _findEmployee(employeeId);
assert(employee.id == 0x0);
totalSalary += salary* 1 ether;
employees.push(Employee(employeeId, salary* 1 ether, now));

}

function removeEmployee(address employeeId) {
require(owner == msg.sender);
var (employee,index) = _findEmployee(employeeId);
assert(employee.id != 0x0);
totalSalary -= employee.salary* 1 ether;
_partialPaid(employee);
delete employees[index];
employees[index] = employees[employees.length-1];
employees.length -=1;

}

function updateEmployee(address employeeId, uint salary) {
require(owner == msg.sender);
var (employee,index) = _findEmployee(employeeId);
assert(employee.id != 0x0);
totalSalary -= employee.salary* 1 ether;
_partialPaid(employee);
totalSalary += salary * 1 ether;
employee.lastPayday = now;
employee.salary = salary;
}

function addFund() payable returns (uint) {
return this.balance;
}

function calculateRunway() returns (uint) {
return this.balance / totalSalary;
}

function hasEnoughFund() returns (bool) {
return calculateRunway()>0;
}

function getPaid() {

var (employee,index) = _findEmployee(msg.sender);
require(employee.id == msg.sender);

uint nextPayday = employee.lastPayday+payDuration;
require(nextPayday<now);

employee.lastPayday = nextPayday;
employee.id.transfer(employee.salary);

}
}
89 changes: 89 additions & 0 deletions Lesson-2/assignment/yours.sol
Original file line number Diff line number Diff line change
@@ -1 +1,90 @@
/*作业请提交在这个目录下*/

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 {
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( employeeId == employees[i].id ){
return (employees[i],i);
}
}
}

function addEmployee(address employeeId, uint salary) {
require(owner == msg.sender);
var (employee,index) = _findEmployee(employeeId);
assert(employee.id == 0x0);
employees.push(Employee(employeeId, salary* 1 ether, now));
}

function removeEmployee(address employeeId) {
require(owner == msg.sender);
var (employee,index) = _findEmployee(employeeId);
assert(employee.id != 0x0);
_partialPaid(employee);
delete employees[index];
employees[index] = employees[employees.length-1];
employees.length -=1;

}

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

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);
require(employee.id == msg.sender);

uint nextPayday = employee.lastPayday+payDuration;
require(nextPayday<now);

employee.lastPayday = nextPayday;
employee.id.transfer(employee.salary);

}
}
23 changes: 23 additions & 0 deletions Lesson-3/assignment/Ownable.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
pragma solidity ^0.4.14;

contract Ownable {
address public owner;

event OwnershipTransferred(address indexed previousOwner, address indexed newOwner);

function Ownable() public {
owner = msg.sender;
}

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

function transferOwnership(address newOwner) public onlyOwner {
require(newOwner != address(0));
OwnershipTransferred(owner, newOwner);
owner = newOwner;
}

}
48 changes: 48 additions & 0 deletions Lesson-3/assignment/SafeMath.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
pragma solidity ^0.4.14;


/**
* @title SafeMath
* @dev Math operations with safety checks that throw on error
*/
library SafeMath {

/**
* @dev Multiplies two numbers, throws on overflow.
*/
function mul(uint256 a, uint256 b) internal pure returns (uint256) {
if (a == 0) {
return 0;
}
uint256 c = a * b;
assert(c / a == b);
return c;
}

/**
* @dev Integer division of two numbers, truncating the quotient.
*/
function div(uint256 a, uint256 b) internal pure returns (uint256) {
// assert(b > 0); // Solidity automatically throws when dividing by 0
uint256 c = a / b;
// assert(a == b * c + a % b); // There is no case in which this doesn't hold
return c;
}

/**
* @dev Substracts two numbers, throws on overflow (i.e. if subtrahend is greater than minuend).
*/
function sub(uint256 a, uint256 b) internal pure returns (uint256) {
assert(b <= a);
return a - b;
}

/**
* @dev Adds two numbers, throws on overflow.
*/
function add(uint256 a, uint256 b) internal pure returns (uint256) {
uint256 c = a + b;
assert(c >= a);
return c;
}
}
Binary file added Lesson-3/assignment/addEmployee.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/addFund.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/calculateRunway.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/getPaid.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/hasEnoughFund.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/removeEmployee.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/updateEmployee.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading