📜  Solidity-特殊变量

📅  最后修改于: 2020-11-04 04:25:05             🧑  作者: Mango


 

特殊变量是全局可用变量,并提供有关区块链的信息。以下是特殊变量的列表-

Sr.No. Special Variable & Description
1 blockhash(uint blockNumber) returns (bytes32)

Hash of the given block – only works for 256 most recent, excluding current, blocks.

2 block.coinbase (address payable)

Current block miner’s address.

3 block.difficulty (uint)

current block difficulty.

4 block.gaslimit (uint)

Current block gaslimit.

5 block.number (uint)

Current block number.

6 block.timestamp

Current block timestamp as seconds since unix epoch.

7 gasleft() returns (uint256)

Remaining gas.

8 msg.data (bytes calldata)

Complete calldata.

9 msg.sender (address payable)

Sender of the message (current call).

10 msg.sig (bytes4)

First four bytes of the calldata (i.e. function identifier)

11 msg.value (uint)

Number of wei sent with the message.

12 now (uint)

Current block timestamp (alias for block.timestamp).

13 tx.gasprice (uint)

Gas price of the transaction.

14 tx.origin (address payable)

Sender of the transaction (full call chain).

尝试以下代码来查看msg的用法,msg是在Solidity中获取发件人地址的特殊变量。

pragma solidity ^0.5.0;

contract LedgerBalance {
   mapping(address => uint) public balances;

   function updateBalance(uint newBalance) public {
      balances[msg.sender] = newBalance;
   }
}
contract Updater {
   function updateBalance() public returns (uint) {
      LedgerBalance ledgerBalance = new LedgerBalance();
      ledgerBalance.updateBalance(10);
      return ledgerBalance.balances(address(this));
   }
}

使用Solidity First Application一章中提供的步骤运行上述程序。

首先单击updateBalance按钮将值设置为10,然后查看日志,解码后的输出将显示为-

输出

{
   "0": "uint256: 10"
}