二、學習基礎智能合約

第一講 介紹

1、 智能合約是什麼?

智能合約並非現實中常見的合同,而是存在區塊鏈上,可以被觸發執行的一段程序代碼,這些代碼實現了某種預定的規則,是存在於區塊鏈執行環境中的“自治代理”。智能合約需要被觸發,代碼纔會執行,不被觸發他的狀態將會始終保持一個狀態,並且部署後的智能合約將不可被修改。智能合約語言的語法和js腳本語言很像,因此有過js開發經驗的小夥伴們學習起來會很快。

2、 編程工具的介紹。

我們都知道“預先善其事、必先利其器”的道理,現實中織布是需要織布機才能完成織布,同樣的我們的智能合約學習也是要有編程工具的使用的。我們本套課程都將以 remix 爲編程工具進行講解課程。remix 就是我們手裏的織布機,能織出什麼布就看我們這些使用 remix 的程序員了。
地址爲(http://remix.ethereum.org/)可以直接在瀏覽器中開發,很方便,只要有一個 google chrome 谷歌瀏覽器就可以開發了。

remix 有幾個主要的常用面板,分別是文件面板、編譯器、運行器、以及佔用最大一部分的文本編輯器組成。

文件面板:我們可以在這個面板進行創建文件、選擇文件等管理文件的操作。
編譯器:我們可以將sol文件進行編譯,編譯不通過將不能執行合約,並且可以得到code.json以及abi.json。我們可以將他們在支持sol語言的公鏈上運行。
運行器:可以將sol智能合約部署在eth鏈上,並且能對合約的方法進行調用等操作。
文本編輯器:我們的代碼就是寫在這個位置的,後面基本大部分時間你將面對的是這個頁面。

3、 我的第一個智能合約程序

下面的智能合約是我們第一個合約程序,因此我們命名爲 “FirstContract.sol” 文件名

pragma solidity ^0.6.0;

// first contract
contract FirstContract {
    // first method
    function firstMethod() public pure returns (string memory) {
        return 'I am first method !!!';
    }
}

上面代碼很多小夥伴應該不是很懂什麼意思,不懂沒關係,我來給大家一行一行講解。
pragma solidity ^0.6.0;
這一行是說明使用solidity 0.6.0版本寫的,可以運行在0.6.0到0.7.0之間的版本上。
contract FirstContract {
這一句是定義一個叫 FirstContract 名稱的合約。
function firstMethod() public pure returns (string memory){
這一行是定義一個方法叫做 firstMethod, 該方法有返回值,類型是 string 類型的。
return ‘I am first method !!!’;
這一行是這個方法將會返回 “I am first method !!!”。

看起來可能還是會有小夥伴們有不明白的地方,但是我們先只教這麼多,關於什麼是string,string 就是字符串的意思,字符串你就可以當作是任意的abcde這些字母等還有標點符號寫在了單引號或者雙引號中。這就是字符串最通俗易懂的解釋了,小夥伴們,大家可以動手試試自定義一些字符串讓他返回。

第二講 智能合約結構

在solidity中,合約有點類似面嚮對象語言中的類,每個合約中包含狀態變量、函數、函數變量、函數修飾器、事件、結構、和枚舉類的聲明,合約也可以繼承其他的合約。大家可能對類和類中的結構的概念沒有什麼瞭解,我簡單給大家舉個例子。一個類可以比作是汽車,汽車裏面的油就是變量,然後油門、剎車等就是函數,我們踩油門相當於調用類中的函數,汽車動起來,油減少,相當於變量值改變了。

我們來根據上面的描述寫一個汽車的合約。先使用remix 創建一個CarContract1.sol文件,然後設定一個CarContract1名字的合約。汽車有了,還要有一個油箱,設定一個變量_gasoline,作爲油箱。然後我們再給汽車加一個油門,寫一個startUp函數作爲油門。現在有了油箱但是不知道有多少油,再加gatGasoline函數作爲一個儀表盤。咱們只有油箱沒有油汽車也跑不了,在加一個加油的接口,給汽車加油,使用addGasoline函數進行加油。下面就是我們完整的小汽車的代碼。

CarContract1.sol

pragma solidity ^0.6.0;

contract CarContract1 {
    uint256 _gasoline;
    
    function startUp() public {
        require(_gasoline >= 1, "gasoline is not haved");
        _gasoline = _gasoline - 1;
    }
    
    function getGasoline() public view returns(uint256 gasoline) {
        return _gasoline;
    }
    
    function addGasoline(uint256 gasoline) public {
        _gasoline = _gasoline + gasoline;
    }
    
}

1、 狀態變量

小汽車合約中的_gasoline就是我們定義的狀態變量,類型是 uint256 類型。 該變量是存儲在鏈上的,也就是說他的數據是被保存起來的,每次改動都會記錄下來。因此我們在進行調用 addGasoline 函數時,會給這個小汽車加油成功,_gasoline 的值會變化,同樣的我們調用 startUp 函數時,_gasoline 的值也會變化。

2、 函數

在CarContract1小汽車中,startUp()、getGasoline()、addGasoline(uint256 gasoline) 都是函數。這些函數有的是沒有參數的,又叫無參函數,比如:startUp()、getGasoline()。有的是有參數的,就叫有參函數,比如:addGasoline(uint256 gasoline)。這些函數,有的有返回值,有的沒有返回值,根據具體場景來定,一般call操作都是有返回值的,call操作不會改變合約狀態。只有send操作,纔會進行改變合約的狀態。

3、 函數變量
我們都知道加不同的型號汽油會有不一樣的效果,我們來給汽車換下不同的型號汽油,在汽車上我們放置一個桶名字是_bucket,用來裝另一個型號的汽油。如果我們自己的兩個容器裏面有一個是空的,我們可以直接進行轉換汽油。但是我們自己的兩個容器中都有油的時候,兩個容器很明顯不能進行交換汽油,這個時候我們需要向隔壁的老李借一個桶 __tempBucket,這樣三個容器就能進行轉換油箱裏面的汽油和桶裏面的汽油進行對換了,換完以後把桶還回去。

我們進行在進行造一個新的小汽車名字是CarContract2,增加一個桶,設定變量爲_bucket,作爲桶。還需要記錄當前汽車的油的型號。設定變量 _gasolineType 爲當前油類型,默認是 1類型。設定一個函數 changeGasolineType,進行交換汽油類型,在設定一個函數進行查看當前汽車的類型 getGasolineType 。至此我們小汽車升級成功。

CarContract2.sol

pragma solidity ^0.6.0;

contract CarContract2 {
    uint256 _gasoline;
    uint256 _bucket;
    int _gasolineType = 1;
    
    function startUp() public {
        require(_gasoline >= 1, "gasoline is not haved");
        _gasoline = _gasoline - 1;
    }
    
    function getGasoline() public view returns(uint256 gasoline) {
        return _gasoline;
    }
    
    function addGasoline(uint256 gasoline) public {
        _gasoline += gasoline;
    }
    
    function changeGasolineType() public {
        require(_gasoline != 0 || _bucket != 0, "can not change");
        
        if (_gasoline == 0) {
            _gasoline = _bucket;
            _bucket = 0;
        } else if (_bucket == 0) {
            _bucket = _gasoline;
            _gasoline = 0;
        } else {
            uint256 __tempBucket = _gasoline;
            _gasoline = _bucket;
            _bucket = __tempBucket;
        }
        
        _gasolineType = -1 * _gasolineType;
    }
    
    function getGasolineType() public view returns(int gasolineType) {
        return _gasolineType;
    }
    
}

上面的小汽車2代正式出爐,我來給大家講下做了那些升級,首先我們的changeGasolineType內部定義了 __tempBucket 變量,該變量就是函數變量,是臨時創建的並且不會被記錄在鏈上的變量,也就是我們用完就還給隔壁老李了,還回去的時候桶是空的。

4、 函數修飾器

我們的小汽車還是很簡單,我們在給他加一點東西,規定小汽車要想啓動必須關閉車門。

下面我們再一次修改我們的小汽車,加一個_doorStatus狀態變量作爲我們的車門狀態。再加連個函數getDoorStatus()、changeDoorStatus(),用來控制開門/關門並且查看門的狀態。並且加入一個whenDoorClose()作爲我們的判斷器。

pragma solidity ^0.6.0;

contract CarContract3 {
    uint256 _gasoline;
    uint256 _bucket;
    int _gasolineType = 1;
    
    bool _doorStatus;
    
    modifier whenDoorClose() {
        require(!_doorStatus, "door is not close");
        _;
        
    }
    
    function startUp() public whenDoorClose {
        require(_gasoline >= 1, "gasoline is not haved");
        _gasoline = _gasoline - 1;
    }
    
    function getGasoline() public view returns(uint256 gasoline) {
        return _gasoline;
    }
    
    function addGasoline(uint256 gasoline) public {
        _gasoline += gasoline;
    }
    
    function changeGasoline() public {
        require(_gasoline != 0 || _bucket != 0, "can not change");
        
        if (_gasoline == 0) {
            _gasoline = _bucket;
            _bucket = 0;
        } else if (_bucket == 0) {
            _bucket = _gasoline;
            _gasoline = 0;
        } else {
            uint256 __tempBucket = _gasoline;
            _gasoline = _bucket;
            _bucket = __tempBucket;
        }
        
        _gasolineType = -1 * _gasolineType;
    }
    
    function getGasolineType() public view returns(int gasolineType) {
        return _gasolineType;
    }
    
    
    function getDoorStatus() public view returns(bool doorStatus) {
        return _doorStatus;
    }
    
    function changeDoorStatus() public {
        _doorStatus = ! _doorStatus;
    }
}

上面我們的3代小汽車已經完成了,whenDoorClose() 就是我們定義的函數修飾器 使用modifier 來定義的。

5、 事件

每次都到沒有油了纔去加油,我們加一個功能,當行駛時油量低於5的時候我們要進行預警。

我們加入一個 gasolineAlarm 事件,該事件有一個參數,當前的油量。這樣我們在啓動的函數中加入這個事件的調用,判斷本次使用後的油量是否小於等於5,是的話進行調用該事件

pragma solidity ^0.6.0;

contract CarContract4 {
    uint256 _gasoline;
    uint256 _bucket;
    int _gasolineType = 1;
    
    bool _doorStatus;
    
    modifier whenDoorClose() {
        require(!_doorStatus, "door is not close");
        _;
        
    }
    
    event gasolineAlarm(uint256 gasoline);
    
    function startUp() public whenDoorClose {
        require(_gasoline >= 1, "gasoline is not haved");
        _gasoline = _gasoline - 1;
        if (_gasoline <= 5) {
            emit gasolineAlarm(_gasoline);
        }
    }
    
    function getGasoline() public view returns(uint256 gasoline) {
        return _gasoline;
    }
    
    function addGasoline(uint256 gasoline) public {
        _gasoline += gasoline;
    }
    
    function changeGasoline() public {
        require(_gasoline != 0 || _bucket != 0, "can not change");
        
        if (_gasoline == 0) {
            _gasoline = _bucket;
            _bucket = 0;
        } else if (_bucket == 0) {
            _bucket = _gasoline;
            _gasoline = 0;
        } else {
            uint256 __tempBucket = _gasoline;
            _gasoline = _bucket;
            _bucket = __tempBucket;
        }
        
        _gasolineType = -1 * _gasolineType;
    }
    
    function getGasolineType() public view returns(int gasolineType) {
        return _gasolineType;
    }
    
    function getDoorStatus() public view returns(bool doorStatus) {
        return _doorStatus;
    }
    
    function changeDoorStatus() public {
        _doorStatus = ! _doorStatus;
    }
}

我們已經更新到第四代小汽車了,四代小汽車的gasolineAlarm 就是我們定義的事件,事件是會在虛擬機上記錄一條日誌的,我麼可以通過查詢日誌的方式得到事件內容。

6、 結構

我們的汽車感覺成熟了,這個時候我們要給我們的汽車打上一些特性,比如顏色,比如車輪數,比如車門數等等。

我們在小汽車裏面加入CarInfo結構體,裏面可以定義color顏色,wheelNum 車輪數等等,然後我們加入設置和獲取的函數:setCarInfo()、getCarInfo(), 這樣我們的小汽車就有了一些參數了。

pragma solidity ^0.6.0;

contract CarContract5 {
    uint256 _gasoline;
    uint256 _bucket;
    int _gasolineType = 1;
    bool _doorStatus;
    
    struct CarInfo {
        string color;
        uint8 wheelNum;
    }
    
    CarInfo _carInfo;
    
    modifier whenDoorClose() {
        require(!_doorStatus, "door is not close");
        _;
        
    }
    
    event gasolineAlarm(uint256 gasoline);
    
    function startUp() public whenDoorClose {
        require(_gasoline >= 1, "gasoline is not haved");
        _gasoline = _gasoline - 1;
        if (_gasoline <= 5) {
            emit gasolineAlarm(_gasoline);
        }
    }
    
    function getGasoline() public view returns(uint256 gasoline) {
        return _gasoline;
    }
    
    function addGasoline(uint256 gasoline) public {
        _gasoline += gasoline;
    }
    
    function changeGasoline() public {
        require(_gasoline != 0 || _bucket != 0, "can not change");
        
        if (_gasoline == 0) {
            _gasoline = _bucket;
            _bucket = 0;
        } else if (_bucket == 0) {
            _bucket = _gasoline;
            _gasoline = 0;
        } else {
            uint256 __tempBucket = _gasoline;
            _gasoline = _bucket;
            _bucket = __tempBucket;
        }
        
        _gasolineType = -1 * _gasolineType;
    }
    
    function getGasolineType() public view returns(int gasolineType) {
        return _gasolineType;
    }
    
    
    function getDoorStatus() public view returns(bool doorStatus) {
        return _doorStatus;
    }
    
    function changeDoorStatus() public {
        _doorStatus = ! _doorStatus;
    }
    
    function setCarInfo(string memory color, uint8 wheelNum) public {
        _carInfo.color = color;
        _carInfo.wheelNum = wheelNum;
        
        //_carInfo = CarInfo(color, wheelNum);

    }
    
    function getCarInfo() public view returns(string memory color, int wheelNum) {
        color = _carInfo.color;
        wheelNum = _carInfo.wheelNum;
    }
}

我們的5代小汽車加入了CarInfo就是結構體,結構體中不能進行設置初值,我們能把一類的屬性等進行分類的放在結構體中,可以充當我們的數據模型。

7、 枚舉類

我們的小汽車想要開門,需要打開車鎖,車鎖是一種狀態,開/關。

我們加入枚舉類DoorSwitch,定義兩個狀態open,close 。在定義whenDoorSwitch函數修飾器,進行判斷。

pragma solidity ^0.6.0;

contract CarContract6 {
    uint256 _gasoline;
    uint256 _bucket;
    int _gasolineType = 1;
    bool _doorStatus;
    
    enum DoorSwitch{ open, close }
    
    DoorSwitch _doorSwitch;
    
    struct CarInfo {
        string color;
        uint8 wheelNum;
    }
    
    CarInfo _carInfo;
    
    modifier whenDoorClose() {
        require(!_doorStatus, "door is not close");
        _;
        
    }
    
    modifier whenDoorSwitch() {
        if (!_doorStatus) {
            require(_doorSwitch == DoorSwitch.open, "door switch is close");
        }
        _;
    }
    
    event gasolineAlarm(uint256 gasoline);
    
    function startUp() public whenDoorClose {
        require(_gasoline >= 1, "gasoline is not haved");
        _gasoline = _gasoline - 1;
        if (_gasoline <= 5) {
            emit gasolineAlarm(_gasoline);
        }
    }
    
    function getGasoline() public view returns(uint256 gasoline) {
        return _gasoline;
    }
    
    function addGasoline(uint256 gasoline) public {
        _gasoline += gasoline;
    }
    
    function changeGasoline() public {
        require(_gasoline != 0 || _bucket != 0, "can not change");
        
        if (_gasoline == 0) {
            _gasoline = _bucket;
            _bucket = 0;
        } else if (_bucket == 0) {
            _bucket = _gasoline;
            _gasoline = 0;
        } else {
            uint256 __tempBucket = _gasoline;
            _gasoline = _bucket;
            _bucket = __tempBucket;
        }
        
        _gasolineType = -1 * _gasolineType;
    }
    
    function getGasolineType() public view returns(int gasolineType) {
        return _gasolineType;
    }
    
    
    function getDoorStatus() public view returns(bool doorStatus) {
        return _doorStatus;
    }
    
    function changeDoorStatus() public {
        _doorStatus = ! _doorStatus;
    }
    
    function setCarInfo(string memory color, uint8 wheelNum) public {
        _carInfo.color = color;
        _carInfo.wheelNum = wheelNum;
        
        //_carInfo = CarInfo(color, wheelNum);
    }
    
    function getCarInfo() public view returns(string memory color, int wheelNum) {
        color = _carInfo.color;
        wheelNum = _carInfo.wheelNum;
    }
    
    function setDoorSwitch(DoorSwitch doorSwitch) public {
        _doorSwitch = doorSwitch; 
    }
}

我們已經更新到6代小汽車了,在6代小汽車中我們加入了DoorSwitch車門的開關,使用的就是枚舉定義的,在實際項目中枚舉定義的話,一般使用在狀態和類型的定義上,方便進行管理。

到此我們的小汽車已經完成了,經歷了6代的更新,相信大家對於本節課程有空了一定的瞭解了。智能合約包含的狀態變量、函數、函數變量、函數修飾器、事件、結構、枚舉類都已經在製作和升級小汽車中使用了。

第三講 數據類型

在solidity中有專門的數據類型,什麼是數據類型呢,我們可以認爲數字是類型,是否是類型,地址是類型等。在solidity中存在以下常用的類型,uint/int、bool、 address、 mapping、bytes、string、fixed/ufixed 常用的類型。

pragma solidity ^0.6.0;


contract Test {
    uint v1 = 1;
    int v2 = -2;
    bool v3 = true;
    address v4 = 0x8a5fa31F2bf83812ECd8E5Ef1878dD12bBaDb40C;
    mapping(uint => uint) v5;
    bytes v6 = "0x123"; 
    string v7 = "asd";
    fixed v8 = 1.3;
    ufixed v9 = 1.2;
}

1、 uint/int類型

uint/int 類型都是整型,也就是都是整數。1、2、3、4類似的數沒有小數點。區別是uint是沒有符號的。看代碼中的 v1 就是無符號整數,v2就是有符號整數。默認是0

2、bool

bool 中文是布爾類型,表示是/否的類型。v3就是bool類型,只有兩個值 true/false,默認是false

3、address

address是地址類型,存儲地址的,賬戶地址,合約地址等,都可以。

4、mapping

映射類型示例中的代碼不是很全,我來解釋下,實際上他是一個key-value模型,也就是一個key對應一個value。比如我們說小明,我們就是到小明這個人。就是這個道理。

5、bytes

定長數組,就是說他的長度是固定的,不能改變,設定是多少就是多少。

6、string

字符串類型,可以放字符串,長度是255位二級制數。

7、fixed/ufixed

浮點類型,就是帶有小數點的,1.2、1.4、1.9這類數值。也是分爲有無符號項。

第四講 控制語句

在solidity中可以使用以下控制結構,有 if,else,while,for,break,continue,return,? : 我們來以此介紹這些結構。

1、if else語句

大家肯定好奇什麼是if else語句。他就是我們說的如果 否者。也就是說如果我怎麼樣否者我怎麼樣。注意else只能與if一同使用,大家一起來看下面代碼:

pragma solidity ^0.6.0;


contract Test {
    uint256 temp;
    
    function test1(bool doorSwitch) public {
        if (doorSwitch) {
            temp = 1;
        }
    }
    
    function test2(bool doorSwitch) public {
        if (doorSwitch) {
            temp = 1;
        }
        temp = 2;
    }
    
    function test3(bool doorSwitch) public {
        if (doorSwitch) {
            temp = 1;
        } else {
            temp = 2;
        }
    }
    
    function getTemp() public view returns(uint256){
        return temp;
    }
}

上面代碼中我們定義了三個測試方法,以及一個獲取temp值的方法。第一個測試方法表示如果我們傳進去的doorSwitch是true,門是開的,那麼temp就等於1,否者doorSwitch等於false的話temp值不變。第二個測試方法表示如果我們傳進去的doorSwitch是true,門是開的,那麼temp就先等於1,然後等於2,否者doorSwitch等於false的話temp直接等於2。第三個測試方法表示如果我們傳進去的doorSwitch是true,門是開的,那麼temp就等於1,否者doorSwitch等於false的話temp等於2。

2、while語句

while 語句是循環語句,表示滿足條件就一直循環,一般我麼會和break來使用,當達到某種情況進行跳出循環,可以讓循環體自動結束循環。

pragma solidity ^0.6.0;


contract Test {
    function test1() public {
        int a = 1;
        while(true) {
            if (a == 5) {
                break;
            }
            a++;
        }
    }
    
    function test2() public {
        int a = 1;
        while(a != 5) {
            
            a++;
        }
    }
}

上面兩個函數內部都有一個循環,實際上兩個循環的效果是一樣的,有一些細微的差別,需要根據具體業務場景分析了,不建議使用while 一不小心死循環就不好玩了。

3、for循環

for循環概念直接上代碼大家好理解些。

pragma solidity ^0.6.0;


contract Test {
    function test1() public {
        for(uint8 i = 0; i < 10; i ++) {
            ...
        }
    }
}

上面就是一個常用的for循環使用方式,循環10次 “…” 的內容。

4、continue 語句

continue語句是跳過本次循環進入下一次循環,來看代碼:

pragma solidity ^0.6.0;


contract Test {
    function test1() public {
        for(uint8 i = 0; i < 10; i ++) {
            if (i == 3) {
                continue;
            }
            
            ...
        }
    }
}

上面的代碼是如果i = 3後跳過這次循環。

5、return 語句

return 是返回的命令當遇到return時就是表示結束了。看代碼:

pragma solidity ^0.6.0;


contract Test {
    function test1() public {
        for(uint8 i = 0; i < 10; i ++) {
            if (i == 3) {
                return;
            }
            
            ...
        }
    }
}

上面的代碼可以看出,運行到i = 3時,程序直接結束。

6、 結束

到此我們的控制語句基本學習結束。這些概念每個語言都差不多,所以很重要。多練小夥伴!

結束

學習完上面的課程大家對於智能合約基本已經學習好了,但是還是缺乏使用,只是知道有啥東西,什麼時候用不知道,這個時候需要大家進行鍛鍊了,多寫一些小東西練習。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章