135.001 智能合约设计-——单员工薪酬系统

@(135- Block Chain| 区块链)

Introduction

  • 参考阅读:老董-以太坊智能合约全栈开发
    课程导读

1. 最终产品Demo

员工系统——人员管理+工资发放
大公司如何发工资?雇主跑路,讨薪难。
雇佣员工时,先把半年工资打到员工​​​
去中心化信任
雇主,雇员
传统的员工系统
人力资源公司的成本太高
小公司——信任问题,拖欠工资,讨薪难​

  • 目标
    高效低成本
    防止黑心老板违约拖欠工资​

    2.项目特点

    1)前端交互很简单,后端运行在智能合约​​​​
    2)智能合约——不存在所谓登陆过程​,依靠的是私钥

    3.项目内容介绍

    1)智能合约设计初阶——单员工薪酬系统
    2)​智能合约设计进阶——多员工薪酬系统
    3)智能合约后端优化和产品化
    4)使用Truffle架构进行前后端交互
    5)分布式应用前端产品化
    6)智能合约的主网部署

一、Solidity 语法

​关键词

程序版本
contract声明
状态变量声明
function 声明​​​​
关键词—constant payable this revert​​

1.1 静态类型语言

需要声明变量类型
不存在float类型

1.2 地址Address

eg.account , 智能合约
成员变量

address.balance// balance用来查询账户余额
address.transfer(value) //transfer()用来发送以太币(以wei为单位)
address.send(send)
address.call
address.callcode
address.callcode

1.3ETHER 单位

wei = 1 (integer)
szabo = 10^12 wei
finney = ​10^15wei
ether = 10^18wei​

1.4Block 块

类比:Singleton 单例
只有一份,谁都能access
有点像静态变量​

1.5变量作用域

与JavaScript相同,不同于c++
函数中局部变量,整个函数内有效

function getPaid{
uint nextDay = lastPayday + payDuration'
​​ if(nextPayDay > now){
uint dayPast = now - nextPayday;
​}​
daysPast = 3; //​与上一定义的daysPast相同
​}​

设计-如何发放薪水

  • 定时器?×
    solidify被动调用
    函数不会等待,每一个函数一次完成
    不具备定时功能​
  • 智能合约作为可信中介
    在合约上存钱,Frank在每30天领取薪酬

    addFund、calculateRunaway、hasEnoughFund、getPaid​

二、Q&A

2.1交易中出现异常导致交易中断会不会出现账户的钱扣了但收款人没收到钱的情况?

根据Solidity官方文档,区块链是具有事务性质的。执行过程中抛出异常,比如更新了lastpayday之后,抛出了异常,那么所有之前操作都会回滚。

区块链是一个全局共享的,事务性的数据库。这意味着参与这个网络的每一个人都可以读取其中的记录。如果你想修改这个数据库中的东西,就必须创建一个事务,并得到其他所有人的确认。事务这个词意味着你要做的修改(假如你想同时修改两个值)只能被完完全全的实施或者一点都没有进行。

此外,当你的事务被应用到这个数据库的时候,其他事务不能修改该数据库。

举个例子,想象一张表,里面列出了某个电子货币所有账号的余额。当从一个账户到另外一个账户的转账请求发生时,这个数据库的事务特性确保从一个账户中减掉的金额会被加到另一个账户上。如果因为某种原因,往目标账户上增加金额无法进行,那么源账户的金额也不会发生任何变化。

更多内容可点击链接:https://github.com/twq0076262/solidity-zh/blob/master/introduction-smart-contracts.md#交易事务

这满足了事务ACID四个特性中的原子性(Atomicity)

Atomicity requires that each transaction be 'all or nothing': if one part of the transaction fails, then the entire transaction fails, and the database state is left unchanged. An atomic system must guarantee atomicity in each and every situation, including power failures, errors and crashes. To the outside world, a committed transaction appears (by its effects on the database) to be indivisible ('atomic'), and an aborted transaction does not happen.
更多内容点击链接:
https://en.wikipedia.org/wiki/ACID

2.2 关于gas的补充

第一课中提到了gas的概念,以太坊上的每笔交易都需要消耗gas。
以太坊上的每笔交易都会被收取一定数量的gas,gas的目的是限制执行交易所需的工作量,同时为执行支付费用。当EVM执行交易时,gas将按照特定规则被逐渐消耗。
gas price是由交易创建者设置的,发送账户需要预付的交易费用 = gas price * gas amount。 如果执行结束还有gas剩余,这些gas将被返还给发送账户。
一旦gas被耗尽,将会触发一个out-of-gas异常,当前调用帧所做的所有状态修改都将被回滚。
更多内容点击链接:
https://github.com/twq0076262/solidity-zh/blob/master/introduction-smart-contracts.md#gas

2.3gas与ether的联系与区别

Gas is supposed to be the constant cost of network resources/utilisation. You want the real cost of sending a transaction to always be the same, so you can’t really expect Gas to be issued, currencies in general are volatile.
So instead, we issue ether whose value is supposed to vary, but also implement a Gas Price in terms of Ether. If the price of ether goes up, the Gas Price in terms of ether should go down to keep the real cost of Gas the same.
更多内容点击链接:
https://my.oschina.net/swingcoder/blog/759271

2.4补充阅读:

对于以太坊中gas的理解https://zhuanlan.zhihu.com/p/26875541
如何理解区块链上的经济模型http://www.8btc.com/feesgas-qtum-1013
————————————————————————
整理来自学员 Shunda Lin

Code in Remix (Solidity IDE)

pragma solidity ^0.4.0;

contract Payroll{
    uint salary = 1 ether;
    address frank = 0xca35b7d915458ef540ade6068dfe2f44e8fa733c;
   // uint constant payDuration = 30 days;
   uint constant payDuration = 10 seconds;//for test:10 seconds
    uint lastPayday = now;
    
    function addFund() payable returns(uint){
        return this.balance;//address.balance
    }
    function calculateRunway()returns(uint){
        return this.balance / salary;
    }
    function hasEnoughFund() returns(bool){
        // return this.balance >=salary;
        //return this.calculateRunway() > 0; //this方法 使用的gas 较多,不推荐
        return calculateRunway() > 0; //vm jump 操作,使用gas较少,推荐
    }
    function getPaid (){
        if(msg.sender != frank)//了解调用者信息
        {
            revert();
        }
        
        uint nextPayday = lastPayday + payDuration;
         //每一次运算都是真金白银~
         //原则:不重复运算!——省gas
        if( nextPayday > now){
            revert();
              //throw or revert
            //throw: 所有的gas 均会被消耗殆尽
            //revert:回滚,return没有消耗的gas
           
        }
        
            lastPayday = nextPayday;//原则:先修改内部变量,再给钱——》之后会讲,安全问题
            frank.transfer(salary);

    }
}

猜你喜欢

转载自www.cnblogs.com/Neo007/p/9166482.html