1. 程式人生 > >135.001 智能合約設計-——單員工薪酬系統

135.001 智能合約設計-——單員工薪酬系統

作用 智能 分布 目標 ons left 項目 clu 原子性

@(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);

    }
}

135.001 智能合約設計-——單員工薪酬系統