2015-12-07 20:16:25 +00:00
***** ***** ***** ***** ***** ***** ***** ***
Units and Globally Available Variables
***** ***** ***** ***** ***** ***** ***** ***
.. index :: wei, finney, szabo, ether
Ether Units
===========
2018-09-20 12:27:02 +00:00
A literal number can take a suffix of `` wei `` , `` finney `` , `` szabo `` or `` ether `` to specify a subdenomination of Ether, where Ether numbers without a postfix are assumed to be Wei.
::
assert(1 wei == 1);
assert(1 szabo == 1e12);
assert(1 finney == 1e15);
assert(1 ether == 1e18);
The only effect of the subdenomination suffix is a multiplication by a power of ten.
2015-12-07 20:16:25 +00:00
.. index :: time, seconds, minutes, hours, days, weeks, years
Time Units
==========
2018-09-20 12:27:02 +00:00
Suffixes like `` seconds `` , `` minutes `` , `` hours `` , `` days `` and `` weeks ``
after literal numbers can be used to specify 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 ``
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.
2018-05-02 14:24:19 +00:00
.. note ::
2018-09-20 12:27:02 +00:00
The suffix `` years `` has been removed in version 0.5.0 due to the reasons above.
2018-05-02 14:24:19 +00:00
2018-10-24 11:23:15 +00:00
These suffixes cannot be applied to variables. For example, if you want to
interpret a function parameter in days, you can in the following way::
2015-12-07 20:16:25 +00:00
2017-12-12 18:47:30 +00:00
function f(uint start, uint daysAfter) public {
2017-07-26 13:44:15 +00:00
if (now >= start + daysAfter * 1 days) {
// ...
}
2015-12-07 20:16:25 +00:00
}
2019-04-23 06:40:43 +00:00
.. _special-variables-functions:
2015-12-07 20:16:25 +00:00
Special Variables and Functions
===============================
There are special variables and functions which always exist in the global
2018-04-13 16:36:33 +00:00
namespace and are mainly used to provide information about the blockchain
or are general-use utility functions.
2015-12-07 20:16:25 +00:00
2018-04-13 16:36:33 +00:00
.. index :: abi, block, coinbase, difficulty, encode, number, block;number, timestamp, block;timestamp, msg, data, gas, sender, value, now, gas price, origin
2015-12-07 20:16:25 +00:00
Block and Transaction Properties
2016-05-25 14:14:57 +00:00
--------------------------------
2015-12-07 20:16:25 +00:00
2018-09-20 12:27:02 +00:00
- `` blockhash(uint blockNumber) returns (bytes32) `` : hash of the given block - only works for 256 most recent, excluding current, blocks
2018-09-05 15:59:55 +00:00
- `` block.coinbase `` (`` address payable `` ): current block miner's address
2016-05-25 14:32:53 +00:00
- `` 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
2018-03-02 16:58:27 +00:00
- `` gasleft() returns (uint256) `` : remaining gas
2018-09-20 12:27:02 +00:00
- `` msg.data `` (`` bytes calldata `` ): complete calldata
2018-09-05 15:59:55 +00:00
- `` msg.sender `` (`` address payable `` ): sender of the message (current call)
2016-05-25 14:32:53 +00:00
- `` 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
2018-09-05 15:59:55 +00:00
- `` tx.origin `` (`` address payable `` ): 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.
2017-08-29 22:34:53 +00:00
.. note ::
2018-03-05 18:24:51 +00:00
Do not rely on `` block.timestamp `` , `` now `` and `` blockhash `` as a source of randomness,
2017-08-29 22:34:53 +00:00
unless you know what you are doing.
Both the timestamp and the block hash can be influenced by miners to some degree.
Bad actors in the mining community can for example run a casino payout function on a chosen hash
and just retry a different hash if they did not receive any money.
The current block timestamp must be strictly larger than the timestamp of the last block,
but the only guarantee is that it will be somewhere between the timestamps of two
consecutive blocks in the canonical chain.
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.
2018-09-20 12:27:02 +00:00
.. note ::
2019-01-03 06:35:06 +00:00
The function `` blockhash `` was previously known as `` block.blockhash `` , which was deprecated in
2018-09-20 12:27:02 +00:00
version 0.4.22 and removed in version 0.5.0.
.. note ::
2019-01-03 06:35:06 +00:00
The function `` gasleft `` was previously known as `` msg.gas `` , which was deprecated in
2018-09-20 12:27:02 +00:00
version 0.4.21 and removed in version 0.5.0.
2018-04-13 16:36:33 +00:00
.. index :: abi, encoding, packed
2018-08-08 15:24:45 +00:00
ABI Encoding and Decoding Functions
-----------------------------------
2018-04-13 16:36:33 +00:00
2018-09-20 12:27:02 +00:00
- `` abi.decode(bytes memory encodedData, (...)) returns (...) `` : ABI-decodes the given data, while the types are given in parentheses as second argument. Example: `` (uint a, uint[2] memory b, bytes memory c) = abi.decode(data, (uint, uint[2], bytes)) ``
- `` abi.encode(...) returns (bytes memory) `` : ABI-encodes the given arguments
2019-01-24 15:07:59 +00:00
- `` abi.encodePacked(...) returns (bytes memory) `` : Performs :ref: `packed encoding <abi_packed_mode>` of the given arguments. Note that packed encoding can be ambiguous!
2018-09-20 12:27:02 +00:00
- `` abi.encodeWithSelector(bytes4 selector, ...) returns (bytes memory) `` : ABI-encodes the given arguments starting from the second and prepends the given four-byte selector
- `` abi.encodeWithSignature(string memory signature, ...) returns (bytes memory) `` : Equivalent to `` abi.encodeWithSelector(bytes4(keccak256(bytes(signature))), ...) ` ``
2018-04-13 16:36:33 +00:00
.. note ::
2018-09-20 12:27:02 +00:00
These encoding functions can be used to craft data for external function calls without actually
calling an external function. Furthermore, `` keccak256(abi.encodePacked(a, b)) `` is a way
2018-06-13 15:49:41 +00:00
to compute the hash of structured data (although be aware that it is possible to
2018-10-24 11:23:15 +00:00
craft a "hash collision" using different function parameter types).
2018-04-13 16:36:33 +00:00
See the documentation about the :ref: `ABI <ABI>` and the
:ref: `tightly packed encoding <abi_packed_mode>` for details about the encoding.
2017-06-15 16:36:16 +00:00
.. index :: assert, revert, require
Error Handling
--------------
2018-09-20 12:27:02 +00:00
See the dedicated section on :ref: `assert and require<assert-and-require>` for
more details on error handling and when to use which function.
2020-04-20 16:24:31 +00:00
`` assert(bool condition) ``
2018-09-20 12:27:02 +00:00
causes an invalid opcode and thus state change reversion if the condition is not met - to be used for internal errors.
2020-04-20 16:24:31 +00:00
`` require(bool condition) ``
2018-05-07 09:44:37 +00:00
reverts if the condition is not met - to be used for errors in inputs or external components.
2020-04-20 16:24:31 +00:00
`` require(bool condition, string memory message) ``
2018-05-07 09:44:37 +00:00
reverts if the condition is not met - to be used for errors in inputs or external components. Also provides an error message.
2020-04-20 16:24:31 +00:00
`` revert() ``
2017-06-15 16:36:16 +00:00
abort execution and revert state changes
2020-04-20 16:24:31 +00:00
`` revert(string memory reason) ``
2017-12-30 12:46:02 +00:00
abort execution and revert state changes, providing an explanatory string
2017-06-15 16:36:16 +00:00
.. index :: keccak256, ripemd160, sha256, ecrecover, addmod, mulmod, cryptography,
2015-12-07 20:16:25 +00:00
Mathematical and Cryptographic Functions
----------------------------------------
2020-04-20 16:24:31 +00:00
`` addmod(uint x, uint y, uint k) returns (uint) ``
2018-02-15 12:04:40 +00:00
compute `` (x + y) % k `` where the addition is performed with arbitrary precision and does not wrap around at `` 2**256 `` . Assert that `` k != 0 `` starting from version 0.5.0.
2019-01-21 09:23:55 +00:00
2020-04-20 16:24:31 +00:00
`` mulmod(uint x, uint y, uint k) returns (uint) ``
2018-02-15 12:04:40 +00:00
compute `` (x * y) % k `` where the multiplication is performed with arbitrary precision and does not wrap around at `` 2**256 `` . Assert that `` k != 0 `` starting from version 0.5.0.
2019-01-21 09:23:55 +00:00
2020-04-20 16:24:31 +00:00
`` keccak256(bytes memory) returns (bytes32) ``
2018-09-20 12:27:02 +00:00
compute the Keccak-256 hash of the input
2019-01-21 09:23:55 +00:00
2019-03-06 10:57:39 +00:00
.. note ::
There used to be an alias for `` keccak256 `` called `` sha3 `` , which was removed in version 0.5.0.
2020-04-20 16:24:31 +00:00
`` sha256(bytes memory) returns (bytes32) ``
2018-06-13 15:49:41 +00:00
compute the SHA-256 hash of the input
2019-01-21 09:23:55 +00:00
2020-04-20 16:24:31 +00:00
`` ripemd160(bytes memory) returns (bytes20) ``
2018-06-13 15:49:41 +00:00
compute RIPEMD-160 hash of the input
2019-01-21 09:23:55 +00:00
2020-04-20 16:24:31 +00:00
`` ecrecover(bytes32 hash, uint8 v, bytes32 r, bytes32 s) returns (address) ``
2019-01-21 09:23:55 +00:00
recover the address associated with the public key from elliptic curve signature or return zero on error.
The function parameters correspond to ECDSA values of the signature:
2020-04-20 16:24:31 +00:00
* `` r `` = first 32 bytes of signature
* `` s `` = second 32 bytes of signature
* `` v `` = final 1 byte of signature
2019-01-21 09:23:55 +00:00
`` ecrecover `` returns an `` address `` , and not an `` address payable `` . See :ref: `address payable<address>` for
conversion, in case you need to transfer funds to the recovered address.
For further details, read `example usage <https://ethereum.stackexchange.com/q/1777/222> `_ .
2015-12-07 20:16:25 +00:00
2019-06-19 09:03:10 +00:00
.. warning ::
If you use `` ecrecover `` , be aware that a valid signature can be turned into a different valid signature without
requiring knowledge of the corresponding private key. In the Homestead hard fork, this issue was fixed
for _transaction_ signatures (see `EIP-2 <http://eips.ethereum.org/EIPS/eip-2#specification> `_ ), but
the ecrecover function remained unchanged.
This is usually not a problem unless you require signatures to be unique or
use them to identify items. OpenZeppelin have a `ECDSA helper library <https://docs.openzeppelin.org/v2.3.0/api/cryptography#ecdsa> `_ that you can use as a wrapper for `` ecrecover `` without this issue.
2018-09-20 12:27:02 +00:00
.. note ::
2019-03-06 10:57:39 +00:00
When running `` sha256 `` , `` ripemd160 `` or `` ecrecover `` on a *private blockchain* , you might encounter Out-of-Gas. This is because these functions are implemented as "precompiled contracts" and only really exist after they receive the first message (although their contract code is hardcoded). Messages to non-existing contracts are more expensive and thus the execution might run into an Out-of-Gas error. A workaround for this problem is to first send Wei (1 for example) to each of the contracts before you use them in your actual contracts. This is not an issue on the main or test net.
2018-09-20 12:27:02 +00:00
2018-08-15 12:40:20 +00:00
.. index :: balance, send, transfer, call, callcode, delegatecall, staticcall
2019-01-21 09:23:55 +00:00
2017-06-16 13:52:56 +00:00
.. _address_related:
2016-05-25 14:44:35 +00:00
2018-09-20 12:27:02 +00:00
Members of Address Types
------------------------
2016-05-25 14:44:35 +00:00
2020-04-20 16:24:31 +00:00
`` <address>.balance `` (`` uint256 `` )
2016-06-02 13:13:27 +00:00
balance of the :ref: `address` in Wei
2020-04-20 16:24:31 +00:00
`` <address payable>.transfer(uint256 amount) ``
2018-09-20 12:27:02 +00:00
send given amount of Wei to :ref: `address` , reverts on failure, forwards 2300 gas stipend, not adjustable
2020-04-20 16:24:31 +00:00
`` <address payable>.send(uint256 amount) returns (bool) ``
2018-02-19 19:06:42 +00:00
send given amount of Wei to :ref: `address` , returns `` false `` on failure, forwards 2300 gas stipend, not adjustable
2020-04-20 16:24:31 +00:00
`` <address>.call(bytes memory) returns (bool, bytes memory) ``
2018-09-20 12:27:02 +00:00
issue low-level `` CALL `` with the given payload, returns success condition and return data, forwards all available gas, adjustable
2020-04-20 16:24:31 +00:00
`` <address>.delegatecall(bytes memory) returns (bool, bytes memory) ``
2018-09-20 12:27:02 +00:00
issue low-level `` DELEGATECALL `` with the given payload, returns success condition and return data, forwards all available gas, adjustable
2020-04-20 16:24:31 +00:00
`` <address>.staticcall(bytes memory) returns (bool, bytes memory) ``
2018-09-20 12:27:02 +00:00
issue low-level `` STATICCALL `` with the given payload, returns success condition and return data, forwards all available gas, adjustable
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` .
2019-01-16 15:11:23 +00:00
.. warning ::
2019-01-17 08:19:16 +00:00
You should avoid using `` .call() `` whenever possible when executing another contract function as it bypasses type checking,
2019-01-16 15:11:23 +00:00
function existence check, and argument packing.
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
2018-07-05 12:32:32 +00:00
.. note ::
Prior to version 0.5.0, Solidity allowed address members to be accessed by a contract instance, for example `` this.balance `` .
This is now forbidden and an explicit conversion to address must be done: `` address(this).balance `` .
2018-02-27 10:48:16 +00:00
.. note ::
2018-09-20 12:27:02 +00:00
If state variables are accessed via a low-level delegatecall, the storage layout of the two contracts
2018-02-27 10:48:16 +00:00
must align in order for the called contract to correctly access the storage variables of the calling contract by name.
This is of course not the case if storage pointers are passed as function arguments as in the case for
the high-level libraries.
2018-09-03 13:01:15 +00:00
2018-09-20 12:27:02 +00:00
.. note ::
Prior to version 0.5.0, `` .call `` , `` .delegatecall `` and `` .staticcall `` only returned the
success condition and not the return data.
2018-09-03 13:01:15 +00:00
2017-07-26 11:20:38 +00:00
.. note ::
2018-09-20 12:27:02 +00:00
Prior to version 0.5.0, there was a member called `` callcode `` with similar but slightly different
semantics than `` delegatecall `` .
2017-07-26 11:20:38 +00:00
2015-12-07 20:16:25 +00:00
.. index :: this, selfdestruct
Contract Related
----------------
2020-04-20 16:24:31 +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
2020-04-20 16:24:31 +00:00
`` selfdestruct(address payable recipient) ``
2019-11-18 12:10:34 +00:00
Destroy the current contract, sending its funds to the given :ref: `address`
and end execution.
Note that `` selfdestruct `` has some peculiarities inherited from the EVM:
- the receiving contract's receive function is not executed.
- the contract is only really destroyed at the end of the transaction and `` revert `` s might "undo" the destruction.
2015-12-07 20:16:25 +00:00
Furthermore, all functions of the current contract are callable directly including the current function.
2018-09-20 12:27:02 +00:00
.. note ::
Prior to version 0.5.0, there was a function called `` suicide `` with the same
semantics as `` selfdestruct `` .
2019-01-16 09:55:18 +00:00
.. index :: type, creationCode, runtimeCode
.. _meta-type:
Type Information
----------------
2020-05-11 10:49:55 +00:00
The expression `` type(X) `` can be used to retrieve information about the type
`` X `` . Currently, there is limited support for this feature (`` X `` can be either
a contract or an integer type) but it might be expanded in the future.
The following properties are available for a contract type `` C `` :
2019-01-16 09:55:18 +00:00
2020-04-20 16:24:31 +00:00
`` type(C).name ``
2019-03-04 11:59:14 +00:00
The name of the contract.
2020-04-20 16:24:31 +00:00
`` type(C).creationCode ``
2019-01-16 09:55:18 +00:00
Memory byte array that contains the creation bytecode of the contract.
This can be used in inline assembly to build custom creation routines,
especially by using the `` create2 `` opcode.
This property can **not** be accessed in the contract itself or any
derived contract. It causes the bytecode to be included in the bytecode
of the call site and thus circular references like that are not possible.
2020-04-20 16:24:31 +00:00
`` type(C).runtimeCode ``
2019-01-16 09:55:18 +00:00
Memory byte array that contains the runtime bytecode of the contract.
This is the code that is usually deployed by the constructor of `` C `` .
If `` C `` has a constructor that uses inline assembly, this might be
different from the actually deployed bytecode. Also note that libraries
modify their runtime bytecode at time of deployment to guard against
regular calls.
The same restrictions as with `` .creationCode `` also apply for this
property.
2020-04-08 22:08:49 +00:00
In addition to the properties above, the following properties are available
for an interface type `` I `` :
`` type(I).interfaceId `` :
A `` bytes4 `` value containing the `EIP-165 <https://eips.ethereum.org/EIPS/eip-165> `_
interface identifier of the given interface `` I `` . This identifier is defined as the `` XOR `` of all
function selectors defined within the interface itself - excluding all inherited functions.
2020-05-11 10:49:55 +00:00
The following properties are available for an integer type `` I `` :
`` type(I).min ``
The smallest value representable by type `` I `` .
`` type(I).max ``
The largest value representable by type `` I `` .