lfx区块链博客


lfx 微信:chen1048258,全球区块链职业教育布道师,通信和信息技术培养工程区块链高级授课专家。


微信
技术交流群

『0021』- 单位(Units) 和 全局变量(Globally Available Variables)

孔壹学院:国内区块链职业教育领先品牌

作者:黎跃春,区块链、高可用架构工程师 微信:liyc1215 QQ群:348924182 博客:http://liyuechun.org

Ether Units

一个整数的后面可以跟一个单位,ether,finney,szabo或者wei。

他们的单位换算如下:

  • 1 ether = 1000 finney
  • 1 ether = 1000000 szabo
  • 1 ether = 10 ** 18 wei
pragma solidity ^0.4.4;

contract C {
    
    uint a = 1 ether;
    uint b = 10 ** 18 wei;
    uint c = 1000 finney;
    uint d = 1000000 szabo;
    
    function isTrueAEquleToB() view public returns (bool) {
        
        return a == b;
    }
    
    function isTrueAEquleToC() view public returns (bool) {
        
        return a == c;
    }
    
    function isTrueAEquleToD() view public returns (bool) {
        
        return a == d;
    }

}

Time Units

时间的单位有seconds, minutes, hours, days, weeksyears。换算如下:

  • 1 == 1 seconds
  • 1 minutes == 60 seconds
  • 1 hours == 60 minutes
  • 1 days == 24 hours
  • 1 weeks == 7 days
  • 1 years == 365 days
pragma solidity ^0.4.4;

contract C {
    
    // 1 == 1 seconds
    // 1 minutes == 60 seconds
    // 1 hours == 60 minutes
    // 1 days == 24 hours
    // 1 weeks == 7 days
    // 1 years == 365 days
    
    function test1() pure public returns (bool) {
        
        return 1 == 1 seconds;
    }
    
    function test2() pure public returns (bool) {
        
        return 1 minutes == 60 seconds;
    }
    
    function test3() pure public returns (bool) {
        
        return 1 hours == 60 minutes;
    }
    
    function test4() pure public returns (bool) {
        
        return 1 days == 24 hours;
    }
    
    function test5() pure public returns (bool) {
        
        return 1 weeks == 7 days;
    }
    
    function test6() pure public returns (bool) {
        
        return 1 years == 365 days;
    }
}

特殊的变量和函数和函数

有一些特殊的变量和函数存在于全局的命名空间以提供区块相关信息。

区块和交易属性

  • block.blockhash(uint blockNumber) returns (bytes32): 某个区块的区块链hash值
  • block.coinbase (address): 当前区块的挖矿地址
  • block.difficulty (uint): 当前区块的难度
  • block.gaslimit (uint): 当前区块的gaslimit
  • block.number (uint): 当前区块编号
  • block.timestamp (uint): 当前区块时间戳
  • msg.data (bytes): 参数
  • msg.gas (uint): 剩余的gas
  • msg.sender (address): 当前发送消息的地址
  • msg.sig (bytes4): 方法ID
  • msg.value (uint): 伴随消息附带的以太币数量
  • now (uint): 时间戳,等价于block.timestamp (uint)
  • tx.gasprice (uint): 交易的gas单价
  • tx.origin (address):交易发送地址

错误处理

  • assert(bool condition):不满足条件,将抛出异常

  • require(bool condition):不满足条件,将抛出异常

  • revert() 抛出异常

Solidity 0.4.10版本之前,使用throw来处理异常。如下所示:

contract HasAnOwner {

    address owner;
    
    function useSuperPowers(){ 
        if (msg.sender != owner) { 
            throw; 
        }
    }
}

Solidity 0.4.10版本之后,我们通常如下使用:

  • if(msg.sender != owner) { revert(); }
  • assert(msg.sender == owner);
  • require(msg.sender == owner);

技术交流

  • 区块链技术交流QQ群:348924182

  • 「区块链部落」官方公众号

版权声明:博客中的文章版权归博主所有,未经授权,禁止转载,转载请注明出处,合作请联系:chen1048258(微信)

打赏一个呗

取消

感谢您的支持,我会继续努力的!

扫码支持
扫码支持
扫码打赏,你说多少就多少

打开支付宝扫一扫,即可进行扫码打赏哦