Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
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
64 changes: 64 additions & 0 deletions Lesson-1/assignment/yours.sol
Original file line number Diff line number Diff line change
@@ -1 +1,65 @@
/*作业请提交在这个目录下*/
pragma solidity ^0.4.14;

contract Payroll {

uint constant payDuration = 10 seconds;
address owner;
uint salary;
address employee;
uint lastPayday;

function setAddress(address _address){
employee=_address;
}
function setSalary(uint _salary){
salary = _salary;
}

function getAddress() constant returns(address){
return employee;
}

function getSalary() constant returns(uint){
return salary;
}

function Payroll() {
owner = msg.sender;
}

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

if (employee != 0x0) {
uint payment = salary * (now - lastPayday) / payDuration;
employee.transfer(payment);
}

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);
}
}
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
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函数的优化

109 changes: 109 additions & 0 deletions Lesson-2/assignment/yours.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
/*作业请提交在这个目录下*/
现在我的代码还有点问题,正在修改当中
/*
transact to Payroll.addEmployee errored: VM error: invalid opcode.
invalid opcode
The execution might have thrown.
Debug the transaction to get more information.
*/
gas消费应该是增加的,因为每一次都会多执行for循环
代码现在正在修改中ing

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 getEmployee()
{
for(uint i=0;i<employees.length;i++ ){
//return (employees[i],i);
}
}

//构造函数
function Payroll(){
owner = msg.sender;
}
//应付的ETH
function _partialPaid(Employee employee) private {
uint payment = employee.salary * (now-employee.lastPayday)/payDuration;
employee.id.transfer(payment);
}

//查找用户是否存在
function _findEmployee(address employee) private returns(Employee storage,uint)
{
for(uint i= 0; i< employees.length; i++){
if(employees[i].id == employee){
return (employees[i],i);
}

}
}
//添加用户
function addEmployee(address employeeId,uint salary)
{
require(msg.sender == owner);
var (employee,index) = _findEmployee(employeeId);
assert(employeeId == 0x0);
assert(index > 10);
employees.push(Employee(employeeId,salary,now));
}

//删除用户
function removeEmployee(address employeeId)
{
require(msg.sender == owner);
var (employee,index) = _findEmployee(employeeId);
assert(employeeId != 0x0);
_partialPaid(employees[index]);
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(employees[index]);
employees[index].salary = salary;
employees[index].lastPayday = now;
}

//能够支付多少薪水
function calculateRunway() returns(uint){
uint totalSalary = 0;
for(uint i= 0; i< employees.length; i++){
totalSalary+=employees[i].salary;
}
return this.balance/totalSalary;
}

//支付薪水
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);
}
//合约中的ETH
function addFund() payable returns (uint) {
return this.balance;
}

}
3 changes: 3 additions & 0 deletions Lesson-2/orgin/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
## 硅谷live以太坊智能合约 第二课《智能合约设计进阶-多员工薪酬系统》

这里是每一课的初始代码,有需要的同学可以参考
50 changes: 50 additions & 0 deletions Lesson-2/orgin/payroll.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
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 {
}

function _findEmployee(address employeeId) private returns (Employee, uint) {
}

function addEmployee(address employeeId, uint salary) {
}

function removeEmployee(address employeeId) {
}

function updateEmployee(address employeeId, uint salary) {
}

function addFund() payable returns (uint) {
}

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) {
}

function getPaid() {
}
}