2015-12-07 20:16:25 +00:00
***** ***** ***** ***** ***** ***** ***** ***
Units and Globally Available Variables
***** ***** ***** ***** ***** ***** ***** ***
.. index :: wei, finney, szabo, ether
Ether Units
===========
2016-08-12 13:54:25 +00:00
A literal number can take a suffix of `` wei `` , `` finney `` , `` szabo `` or `` ether `` to convert between the subdenominations of Ether, where Ether currency numbers without a postfix are assumed to be Wei, e.g. `` 2 ether == 2000 finney `` evaluates to `` true `` .
2015-12-07 20:16:25 +00:00
.. index :: time, seconds, minutes, hours, days, weeks, years
Time Units
==========
2016-08-12 13:33:22 +00:00
Suffixes like `` seconds `` , `` minutes `` , `` hours `` , `` days `` , `` weeks `` and
2016-06-20 14:16:10 +00:00
`` years `` after literal numbers can be used to convert between units of time where seconds are the base
2015-12-07 20:16:25 +00:00
unit and units are considered naively in the following way:
2016-08-29 19:47:31 +00:00
* `` 1 == 1 seconds ``
2016-05-24 17:57:36 +00:00
* `` 1 minutes == 60 seconds ``
* `` 1 hours == 60 minutes ``
* `` 1 days == 24 hours ``
2017-05-01 18:05:25 +00:00
* `` 1 weeks == 7 days ``
* `` 1 years == 365 days ``
2015-12-07 20:16:25 +00:00
Take care if you perform calendar calculations using these units, because
not every year equals 365 days and not even every day has 24 hours
because of `leap seconds <https://en.wikipedia.org/wiki/Leap_second> `_ .
Due to the fact that leap seconds cannot be predicted, an exact calendar
library has to be updated by an external oracle.
These suffixes cannot be applied to variables. If you want to
interpret some input variable in e.g. days, you can do it in the following way::
function f(uint start, uint daysAfter) {
2016-05-18 15:05:28 +00:00
if (now >= start + daysAfter * 1 days) { ... }
2015-12-07 20:16:25 +00:00
}
Special Variables and Functions
===============================
There are special variables and functions which always exist in the global
namespace and are mainly used to provide information about the blockchain.
.. index :: block, coinbase, difficulty, number, block;number, timestamp, block;timestamp, msg, data, gas, sender, value, now, gas price, origin
Block and Transaction Properties
2016-05-25 14:14:57 +00:00
--------------------------------
2015-12-07 20:16:25 +00:00
2016-11-01 23:03:20 +00:00
- `` block.blockhash(uint blockNumber) returns (bytes32) `` : hash of the given block - only works for 256 most recent blocks excluding current
2016-05-25 14:32:53 +00:00
- `` block.coinbase `` (`` address `` ): current block miner's address
- `` block.difficulty `` (`` uint `` ): current block difficulty
- `` block.gaslimit `` (`` uint `` ): current block gaslimit
- `` block.number `` (`` uint `` ): current block number
2017-05-13 05:13:45 +00:00
- `` block.timestamp `` (`` uint `` ): current block timestamp as seconds since unix epoch
2016-05-25 14:32:53 +00:00
- `` msg.data `` (`` bytes `` ): complete calldata
- `` msg.gas `` (`` uint `` ): remaining gas
- `` msg.sender `` (`` address `` ): sender of the message (current call)
- `` msg.sig `` (`` bytes4 `` ): first four bytes of the calldata (i.e. function identifier)
- `` msg.value `` (`` uint `` ): number of wei sent with the message
- `` now `` (`` uint `` ): current block timestamp (alias for `` block.timestamp `` )
- `` tx.gasprice `` (`` uint `` ): gas price of the transaction
- `` tx.origin `` (`` address `` ): sender of the transaction (full call chain)
2015-12-07 20:16:25 +00:00
.. note ::
2016-05-24 17:57:36 +00:00
The values of all members of `` msg `` , including `` msg.sender `` and
`` msg.value `` can change for every **external** function call.
2015-12-07 20:16:25 +00:00
This includes calls to library functions.
If you want to implement access restrictions in library functions using
2016-05-24 17:57:36 +00:00
`` msg.sender `` , you have to manually supply the value of
`` msg.sender `` as an argument.
2015-12-07 20:16:25 +00:00
.. note ::
The block hashes are not available for all blocks for scalability reasons.
You can only access the hashes of the most recent 256 blocks, all other
values will be zero.
2017-02-09 16:32:48 +00:00
.. index :: assert, revert, keccak256, ripemd160, sha256, ecrecover, addmod, mulmod, cryptography, this, super, selfdestruct, balance, send
2015-12-07 20:16:25 +00:00
Mathematical and Cryptographic Functions
----------------------------------------
2017-02-09 15:31:03 +00:00
`` assert(bool condition) `` :
throws if the condition is not met.
2016-05-24 17:57:36 +00:00
`` addmod(uint x, uint y, uint k) returns (uint) `` :
compute `` (x + y) % k `` where the addition is performed with arbitrary precision and does not wrap around at `` 2**256 `` .
`` mulmod(uint x, uint y, uint k) returns (uint) `` :
compute `` (x * y) % k `` where the multiplication is performed with arbitrary precision and does not wrap around at `` 2**256 `` .
2016-10-06 12:24:36 +00:00
`` keccak256(...) returns (bytes32) `` :
compute the Ethereum-SHA-3 (Keccak-256) hash of the (tightly packed) arguments
2016-05-24 17:57:36 +00:00
`` sha3(...) returns (bytes32) `` :
2017-03-15 23:41:02 +00:00
alias to `` keccak256() ``
2016-05-24 17:57:36 +00:00
`` sha256(...) returns (bytes32) `` :
2015-12-07 20:16:25 +00:00
compute the SHA-256 hash of the (tightly packed) arguments
2016-05-24 17:57:36 +00:00
`` ripemd160(...) returns (bytes20) `` :
2015-12-07 20:16:25 +00:00
compute RIPEMD-160 hash of the (tightly packed) arguments
2016-05-25 14:52:11 +00:00
`` ecrecover(bytes32 hash, uint8 v, bytes32 r, bytes32 s) returns (address) `` :
2016-08-12 13:54:17 +00:00
recover the address associated with the public key from elliptic curve signature or return zero on error
2017-04-21 09:42:31 +00:00
(`example usage <https://ethereum.stackexchange.com/q/1777/222> `_ )
2017-02-09 16:32:48 +00:00
`` revert() `` :
abort execution and revert state changes
2015-12-07 20:16:25 +00:00
In the above, "tightly packed" means that the arguments are concatenated without padding.
This means that the following are all identical::
2016-10-06 12:24:36 +00:00
keccak256("ab", "c")
keccak256("abc")
keccak256(0x616263)
keccak256(6382179)
keccak256(97, 98, 99)
2015-12-07 20:16:25 +00:00
2016-10-06 12:24:36 +00:00
If padding is needed, explicit type conversions can be used: `` keccak256("\x00\x12") `` is the
same as `` keccak256(uint16(0x12)) `` .
2015-12-07 20:16:25 +00:00
2016-08-29 19:36:50 +00:00
Note that constants will be packed using the minimum number of bytes required to store them.
2016-10-06 12:24:36 +00:00
This means that, for example, `` keccak256(0) == keccak256(uint8(0)) `` and
`` keccak256(0x12345678) == keccak256(uint32(0x12345678)) `` .
2016-08-29 19:12:35 +00:00
2016-05-24 17:57:36 +00:00
It might be that you run into Out-of-Gas for `` sha256 `` , `` ripemd160 `` or `` ecrecover `` on a *private blockchain* . The reason for this is that those are implemented as so-called precompiled contracts and these contracts only really exist after they received the first message (although their contract code is hardcoded). Messages to non-existing contracts are more expensive and thus the execution runs into an Out-of-Gas error. A workaround for this problem is to first send e.g. 1 Wei to each of the contracts before you use them in your actual contracts. This is not an issue on the official or test net.
2015-12-07 20:16:25 +00:00
2016-06-02 13:29:53 +00:00
.. _address_related:
2016-05-25 14:44:35 +00:00
Address Related
---------------
`` <address>.balance `` (`` uint256 `` ):
2016-06-02 13:13:27 +00:00
balance of the :ref: `address` in Wei
2017-02-08 23:37:23 +00:00
`` <address>.transfer(uint256 amount) `` :
send given amount of Wei to :ref: `address` , throws on failure
2017-03-15 23:33:26 +00:00
`` <address>.send(uint256 amount) returns (bool) `` :
send given amount of Wei to :ref: `address` , returns `` false `` on failure
`` <address>.call(...) returns (bool) `` :
issue low-level `` CALL `` , returns `` false `` on failure
`` <address>.callcode(...) returns (bool) `` :
issue low-level `` CALLCODE `` , returns `` false `` on failure
`` <address>.delegatecall(...) returns (bool) `` :
issue low-level `` DELEGATECALL `` , returns `` false `` on failure
2016-06-01 17:18:43 +00:00
2016-06-03 13:20:18 +00:00
For more information, see the section on :ref: `address` .
2016-05-25 14:44:35 +00:00
.. warning ::
2016-06-02 13:14:53 +00:00
There are some dangers in using `` send `` : The transfer fails if the call stack depth is at 1024
2016-06-01 17:59:16 +00:00
(this can always be forced by the caller) and it also fails if the recipient runs out of gas. So in order
2017-03-15 23:33:26 +00:00
to make safe Ether transfers, always check the return value of `` send `` , use `` transfer `` or even better:
2016-06-01 17:59:16 +00:00
Use a pattern where the recipient withdraws the money.
2016-05-25 14:44:35 +00:00
2015-12-07 20:16:25 +00:00
.. index :: this, selfdestruct
Contract Related
----------------
2016-05-24 17:57:36 +00:00
`` this `` (current contract's type):
2016-05-09 16:15:58 +00:00
the current contract, explicitly convertible to :ref: `address`
2016-05-18 15:35:00 +00:00
2016-05-25 14:52:11 +00:00
`` selfdestruct(address recipient) `` :
2016-05-09 16:15:58 +00:00
destroy the current contract, sending its funds to the given :ref: `address`
2015-12-07 20:16:25 +00:00
Furthermore, all functions of the current contract are callable directly including the current function.