solidity智能合约[24]-global

solidity中的全局属性

block.blockhash(uint blockNumber) returns (bytes32):返回给定区块号的哈
希值,只支持最近256个区块,且不包含当前区块。在版本0.4.22中弃用并被替换为。blockhash(uint blockNumber)
block.coinbase (address): 当前块矿工的地址。
block.difficulty (uint):当前块的难度。
block.gaslimit (uint):当前块的gaslimit。
block.number (uint):当前区块的块号。
block.timestamp (uint): 当前块的Unix时间戳(从1970/1/1 00:00:00 UTC开始所经过的秒数)
msg.data (bytes): 完整的调用数据(calldata)。
msg.sender (address): 当前调用发起人的地址。
msg.sig (bytes4):调用数据(calldata)的前四个字节(例如为:函数标识符)。
msg.value (uint): 这个消息所附带的以太币,单位为wei。
now (uint): 当前块的时间戳(block.timestamp的别名)
tx.gasprice (uint) : 交易的gas价格。
tx.origin (address): 交易的发送者(全调用链)

案例

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
pragma solidity ^0.4.23;

contract global{
   function  getGlobal1() public view returns(address){
       return msg.sender ;
   }

      function  getGlobal2() public view returns(uint){
       return block.difficulty;
   }
    function  getGlobal3() public view returns(uint){
       return block.number;
   }

    function  getGlobal4() public view returns(address){
       return block.coinbase;
   }

    function  getGlobal5() public pure returns(bytes){
       return msg.data;
   }

    function  getGlobal6() public payable returns(uint ){
       return msg.value ;
   }

     function  getGlobal7() public view  returns(address ){
       return tx.origin;
   }

    function  getGlobal8() public view returns(uint ){
       return now;
   }

     function  getGlobal9() public view returns(bytes32 ){
       return blockhash(block.number-1);
   }
}

参考资料:
https://solidity.readthedocs.io/en/develop/units-and-global-variables.html

image.png

猜你喜欢

转载自blog.51cto.com/13784902/2321572