以太坊智能合約solidity

https://ethereum.github.io/browser-solidity/#version=soljson-v0.4.13+commit.fb4cb1a.js (solidity 在線IDE)

solidity 語言類型及示例solidity

  1. int/uint 數組類型的定義:
    • uint[] array;
    • int[] array;
    • 數組類型的成員有兩個: push 和 length
      • push 給數組類型增加一個元素, 同時該數組長度加1;
      • length 返回當前的數組長度。
contract Demo{
    uint[] public array
    function testDemo(uint a) returns (uint lenth){
        array.push(a);
        return array.length;    
    }
}



2. 數組的增刪改查:

contract ArrayOperation{
    uint[] public array;
     //數組添加元素
    function add(uint a) returns (uint result){
        array.push(a);
    }
    //獲取數組下標元素值
    function valueOf(uint index) returns (uint value){
        if(index >= array.length)   throw;
        return array[index];
    }
    //更新元素
    function updata(uint index, uint value){
        if(index >= array.length)   throw;
        array[index] = value;
    }
    //刪除元素
    function delete(uint index){
        uint len = array.length
        if(index >= len )   throw;
        for(uint i = index; i < len - 1; i++){
            array[index] = array[index + 1]
        }
        delete (array[len - 1]);
        array.length --;
    }
}



3. 簡單模擬代幣合約, 包含(轉賬, 挖礦, 構造):

pragma solidity ^0.4.0;
contract Token{
    uint[] public balanceOf;
    //初始化兩個模擬賬戶
    function Token(){
        balanceOf.push(4500);
        balanceOf.push(1000);
    }
    //兩個賬戶進行轉賬
    function transfer(uint _from, uint _to, uint amount){
        if(_from >= balanceOf.length || _to >= balanceOf.length || _from == _to )   return;
        if(balanceOf[_from] < amount)   return;
        balanceOf[_from] -= amount;
        balanceOf[_to] += amount;
    }
    //挖礦,所有的收入都放入第一個賬戶中
    function mint(uint amount){
        balanceOf[0] += amount;
    }
}



4. address, mapping 類型的使用:
* 1 mapping的使用, 一般鍵爲地址, 值爲餘額 mapping(address => uint);
* 2 address : 以太坊賬戶地址;
* 3 address: Holds a 20 byte value (size of an Ethereum address). Address types also have members and serve as base for all contracts.
* 4 address 含有兩個成員, balance 和 send;




5. 合約賬戶中 msg 的定義
* 1 msg 指誰調用該智能合約發來的信息。 注意:智能合約也是一個賬戶
* 2 msg 包含的成員:
* msg.data(bytes): 獲取調用者傳入的數據信息
* msg.gas(uint): 剩餘的以太幣
* msg.sender(address): 當前調用者的以太坊賬戶地址
* msg.sig(bytes4): first four bytes of cakkData
* msg.value(uint): number of wei sent with the message;注意: 只有當有以太坊幣的轉義時, 該變量纔有值(除調用合約所花費以太幣這種情況)

pragma solidity ^0.4.0;

contract  Token{
    mapping (address => uint) public balanceOf;
    address public owner;
    event Sent(address sender, address receiver, uint amount); //定義一個時間

    //獲取部署合約的賬戶地址, 並初始化該地址的餘額
    function Token(){
        owner = msg.sender;
        balanceOf[owner] = 10000;
    }
    //賬戶地址交易
    function transfer(address _to, uint amount){
        if(balanceOf[msg.sender] < amount)     throw;  //進行判斷,防止發送賬戶的餘額不足
        if(balanceOf[_to] + amount < balanceOf[_to])   throw;  //防止自己給自己轉賬,或遞歸調用(因爲每次調用都有額外的花費) 
        balanceOf[_to] += amount;
        balanceOf[msg.sender] -= amount;
        Sent(msg.sender,  _to, amount);   ///調用事件
    }

    //挖礦
    function mint(uint amount){
         balanceOf[owner] += amount;
    }


}



6. enum 類型

contract Demo{

    enum ActionCode{ Left, Right, down}

    ActionCode public enumType;

    function Demo(){
        enumType = ActionCode.Left;
    }

    function Left(){
        enumType = ActionCode.Left;
    }

    function Right(){
        enumType = ActionCode.Right;
    }

    function Down(){
        enumType = ActionCode.down;
    }

    function f() returns(string str){
        if(enumType == ActionCode.Left)     return "Left";
        if(enumType == ActionCode.Right)    return "Right";
        if(enumType == ActionCode.down)     return "down";
    }
}



7. struct 類型

contract DemoStruct{
    struct Person{
        uint age;
        string name;
        uint sexy;
        string phone;
    }

    Person[] public PersonLists;

    function DemoStruct(){
        uint id = PersonLists.length++;
        Person p = PersonLists[id];
        p.age = 20;
        p.sexy = 0;
        p.name = "wukong";
        p.phone = "1235432328";
    }


    function addPerson(uint age, uint sexy, string name, string phone){
        uint id =  PersonLists.length++;
        Person p = PersonLists[id];
        p.age = age;
        p.sexy = sexy;
        p.name = name;
        p.phone = phone;
    }
    function addPersontwo(uint _age, uint _sexy, string _name, string _phone){
        uint id =  PersonLists.length++;
        PersonLists[id] = Person({age: _age, name: _name, sexy: _sexy, phone: _phone});
    }
}



8. 以太坊中的數據單位
* ether
* finney
* szabo
* wei
* 1 ether = 1 * 10^18 wei;
* 1 ether = 1 * 10^6 szabo;
* 1 ehter = 1* 10^3 finney;



9. 時間類型
* seconds
* minutes
* hours
* days
* weeks
* years
* now



10. mapping 類型

參考博客

  1. 以太中文網
發佈了76 篇原創文章 · 獲贊 23 · 訪問量 15萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章