- Solidity 教程
- Solidity - 首頁
- Solidity - 概述
- Solidity - 環境設定
- Solidity - 基本語法
- Solidity - 第一個應用程式
- Solidity - 註釋
- Solidity - 資料型別
- Solidity - 變數
- Solidity - 變數作用域
- Solidity - 運算子
- Solidity - 迴圈
- Solidity - 條件語句
- Solidity - 字串
- Solidity - 陣列
- Solidity - 列舉
- Solidity - 結構體
- Solidity - 對映
- Solidity - 型別轉換
- Solidity - 以太幣單位
- Solidity - 特殊變數
- Solidity - 樣式指南
- Solidity 函式
- Solidity - 函式
- Solidity - 函式修飾符
- Solidity - view 函式
- Solidity - pure 函式
- Solidity - 回退函式
- 函式過載
- 數學函式
- 加密函式
- Solidity 常用模式
- Solidity - 提款模式
- Solidity - 受限訪問
- Solidity 高階
- Solidity - 合約
- Solidity - 繼承
- Solidity - 建構函式
- Solidity - 抽象合約
- Solidity - 介面
- Solidity - 庫
- Solidity - 彙編
- Solidity - 事件
- Solidity - 錯誤處理
- Solidity 有用資源
- Solidity - 快速指南
- Solidity - 有用資源
- Solidity - 討論
Solidity - 特殊變數
特殊變數是全域性可用的變數,提供有關區塊鏈的資訊。以下是特殊變數的列表:
| 序號 | 特殊變數及描述 |
|---|---|
| 1 | blockhash(uint blockNumber) returns (bytes32) 給定區塊的雜湊值 - 僅適用於最近的 256 個區塊(不包括當前區塊)。 |
| 2 | block.coinbase (address payable) 當前區塊礦工的地址。 |
| 3 | block.difficulty (uint) 當前區塊難度。 |
| 4 | block.gaslimit (uint) 當前區塊 gas 限制。 |
| 5 | block.number (uint) 當前區塊編號。 |
| 6 | block.timestamp 自 Unix 元年以來的當前區塊時間戳(秒)。 |
| 7 | gasleft() returns (uint256) 剩餘 gas。 |
| 8 | msg.data (bytes calldata) 完整的 calldata。 |
| 9 | msg.sender (address payable) 訊息傳送者(當前呼叫)。 |
| 10 | msg.sig (bytes4) calldata 的前四個位元組(即函式識別符號)。 |
| 11 | msg.value (uint) 隨訊息傳送的 wei 數量。 |
| 12 | now (uint) 當前區塊時間戳(block.timestamp 的別名)。 |
| 13 | tx.gasprice (uint) 交易的 gas 價格。 |
| 14 | tx.origin (address payable) 交易傳送者(完整的呼叫鏈)。 |
示例
嘗試以下程式碼,檢視如何使用 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 第一個應用程式章節中提供的步驟執行上述程式。
首先點選**updateBalance**按鈕將值設定為 10,然後檢視日誌,其中將顯示解碼後的輸出為:
輸出
{
"0": "uint256: 10"
}
廣告