美文网首页
solidity-1.合约结构

solidity-1.合约结构

作者: jection | 来源:发表于2018-11-20 16:12 被阅读0次

    solidity 英文文档阅读笔记

    官方英文文档:
    https://solidity.readthedocs.io/en/v0.4.23/structure-of-a-contract.html

    solidity中文翻译文档(翻译的不错):
    http://www.tryblockchain.org/Solidity-%E8%AF%AD%E8%A8%80%E4%BB%8B%E7%BB%8D.html

    当前版本:0.4.23

    Structure of a Contract 合约结构

    合约的状态变量就不能被别的合约访问,因为:
    调用另外一个合约实例的函数,会执行一个EVM函数调用,这个操作会引起上下文切换。

    创建合约

    创建合约,会执行构造函数
    构造函数只有一个,可选,不支持重载

    1. 可以在一个合约里创建另外一个合约
    2. 可以用发起交易的方式,使用web3.js创建合约

    合约调用

    账户执行合约会发起交易,而合约之间的调用发起的是消息调用。
    案例:
    外部账户A发起交易,执行合约B的函数,该函数又调用了合约C的函数,这个时候:
    msg.sender:合约B里msg.sender是账户A的地址,在合约C的函数里获取到的msg.sender是合约B的地址。
    GAS:哪个账户发起的交易,谁为gas买单,即上面的过程消费的gas由外部账户A来付。

    State Variables 状态变量

    状态变量永久存储在合约存储空间
    pragma solidity ^0.4.0;

    contract SimpleStorage {
        uint storedData; // State variable
        // ...
    }
    

    Functions 函数

    函数是合约里的可执行单元

    pragma solidity ^0.4.0;
    
    contract SimpleAuction {
        function bid() public payable { // Function
            // ...
        }
    }
    

    Function Modifiers 函数修饰符

    可以用来限制函数的访问权限

    pragma solidity ^0.4.22;
    
    contract Purchase {
        address public seller;
    
        modifier onlySeller() { // Modifier
            require(
                msg.sender == seller,
                "Only seller can call this."
            );
            _;
        }
    
        function abort() public onlySeller { // Modifier usage
            // ...
        }
    }
    

    Events 事件

    是EVM日志的接口

    pragma solidity ^0.4.21;
    
    contract SimpleAuction {
        event HighestBidIncreased(address bidder, uint amount); // Event
    
        function bid() public payable {
            // ...
            emit HighestBidIncreased(msg.sender, msg.value); // Triggering event
        }
    }
    

    Struct Types 结构类型

    struct type可以用来自定义结构类型,把多个变量组合在一起,成为复杂一点的数据类型

    pragma solidity ^0.4.0;
    
    contract Ballot {
        struct Voter { // Struct
            uint weight;
            bool voted;
            address delegate;
            uint vote;
        }
    }
    

    Enum Types 枚举类型

    枚举类型用来定义有限集值的数据类型

    pragma solidity ^0.4.16;
    
    contract test {
        enum ActionChoices { GoLeft, GoRight, GoStraight, SitStill }
        ActionChoices choice;
        ActionChoices constant defaultChoice = ActionChoices.GoStraight;
    
        function setGoStraight() public {
            choice = ActionChoices.GoStraight;
        }
    
        // Since enum types are not part of the ABI, the signature of "getChoice"
        // will automatically be changed to "getChoice() returns (uint8)"
        // for all matters external to Solidity. The integer type used is just
        // large enough to hold all enum values, i.e. if you have more values,
        // `uint16` will be used and so on.
        function getChoice() public view returns (ActionChoices) {
            return choice;
        }
    
        function getDefaultChoice() public pure returns (uint) {
            return uint(defaultChoice);
        }
    }
    

    相关文章

      网友评论

          本文标题:solidity-1.合约结构

          本文链接:https://www.haomeiwen.com/subject/dhjxqqtx.html