欢迎光临
黎跃春区块链技术博客

【Solidity智能合约开发第22篇】3分钟了解单位(Units) 和 全局变量(Globally Available Variables)

Ether Units

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

他们的单位换算如下:

  • 1 ether = 1000 finney
  • 1 ether = 1000000 szabo
  • 1 ether = 10 ** 18 wei
// SPDX-License-Identifier: MIT
pragma solidity >=0.5.0 <0.7.0;

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; //bool: true
    }

    function isTrueAEquleToC() view public returns (bool) {

        return a == c; //bool: true
    }

    function isTrueAEquleToD() view public returns (bool) {

        return a == d; //bool: true
    }

}

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 -----> deprecated
// SPDX-License-Identifier: MIT
pragma solidity >=0.5.0 <0.7.0;

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 //Using "years" as a unit denomination is deprecated

    function test1() pure public returns (bool) {

        return 1 == 1 seconds; //bool: true
    }

    function test2() pure public returns (bool) {

        return 1 minutes == 60 seconds; //bool: true
    }

    function test3() pure public returns (bool) {

        return 1 hours == 60 minutes; //bool: true 
    }

    function test4() pure public returns (bool) {

        return 1 days == 24 hours; //bool: true
    }

    function test5() pure public returns (bool) {

        return 1 weeks == 7 days; //bool: true
    }

    // function test6() pure public returns (bool) {

    //     return 1 years == 365 days;
    // }
    /*
    browser/C.sol:40:16: TypeError: Using "years" as a unit denomination is deprecated.
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来处理异常。如下所示:

// SPDX-License-Identifier: MIT
pragma solidity >=0.5.0 <0.7.0;

contract HasAnOwner {

    address owner;

    function useSuperPowers() public { 
        if (msg.sender != owner) { 
            throw; 
        }
    }
}
browser/C.sol:10:13: SyntaxError: "throw" is deprecated in favour of "revert()", "require()" and "assert()".
throw;
^---^

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

  • if(msg.sender != owner) { revert(); }
  • assert(msg.sender == owner);
  • require(msg.sender == owner);
赞(0) 打赏
未经允许不得转载:黎跃春区块链技术博客 » 【Solidity智能合约开发第22篇】3分钟了解单位(Units) 和 全局变量(Globally Available Variables)

评论 抢沙发

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址

区块链在线课程、区块链职业技术水平认证考试一网打尽

区块链技术在线课程区块链技术职业认证

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

微信扫一扫打赏