Ether Units
一個整數的後面可以跟一個單位,ether
,finney
,szabo
或者wei
。
他們的單位換算如下:
1 ether = 1000 finney
1 ether = 1000000 szabo 1
ether = 10 ** 18 wei
Time Units
時間的單位有seconds
, minutes
, hours
, days
, weeks
和 years
。換算如下:
1 == 1 seconds
1 minutes == 60 seconds
1 hours == 60 minutes
1 days == 24 hours
1 weeks == 7 days
1 years == 365 days
特殊的變量和函數和函數
有一些特殊的變量和函數存在於全局的命名空間以提供區塊相關信息。
區塊和交易屬性
block.blockhash(uint blockNumber) returns (bytes32):
某個區塊的區塊鏈hash值block.coinbase (address):
當前區塊的挖礦地址block.difficulty (uint):
當前區塊的難度block.gaslimit (uint):
當前區塊的gaslimitblock.number (uint):
當前區塊編號block.timestamp (uint):
當前區塊時間戳msg.data (bytes):
參數msg.gas (uint):
剩餘的gasmsg.sender (address):
當前發送消息的地址msg.sig (bytes4):
方法IDmsg.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);