2020-04-20 19:43:24 +00:00
***** *****
Cheatsheet
***** *****
2023-04-01 06:51:14 +00:00
.. index :: operator;precedence
2020-04-20 19:43:24 +00:00
Order of Precedence of Operators
================================
2023-04-01 06:51:14 +00:00
2022-06-14 12:41:41 +00:00
.. include :: types/operator-precedence-table.rst
2020-04-20 19:43:24 +00:00
2023-04-01 06:51:14 +00:00
.. index :: abi;decode, abi;encode, abi;encodePacked, abi;encodeWithSelector, abi;encodeCall, abi;encodeWithSignature
2020-04-20 19:43:24 +00:00
2023-04-01 06:51:14 +00:00
ABI Encoding and Decoding Functions
===================================
2020-04-20 19:43:24 +00:00
- `` abi.decode(bytes memory encodedData, (...)) returns (...) `` : :ref: `ABI <ABI>` -decodes
the provided data. 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) `` : :ref: `ABI <ABI>` -encodes the given arguments
- `` abi.encodePacked(...) returns (bytes memory) `` : Performs :ref: `packed encoding <abi_packed_mode>` of
the given arguments. Note that this encoding can be ambiguous!
- `` abi.encodeWithSelector(bytes4 selector, ...) returns (bytes memory) `` : :ref: `ABI <ABI>` -encodes
the given arguments starting from the second and prepends the given four-byte selector
2021-11-11 15:21:23 +00:00
- `` abi.encodeCall(function functionPointer, (...)) returns (bytes memory) `` : ABI-encodes a call to `` functionPointer `` with the arguments found in the
tuple. Performs a full type-check, ensuring the types match the function signature. Result equals `` abi.encodeWithSelector(functionPointer.selector, (...)) ``
2020-04-20 19:43:24 +00:00
- `` abi.encodeWithSignature(string memory signature, ...) returns (bytes memory) `` : Equivalent
2022-12-07 05:18:04 +00:00
to `` abi.encodeWithSelector(bytes4(keccak256(bytes(signature))), ...) ``
2023-04-01 06:51:14 +00:00
.. index :: bytes;concat, string;concat
Members of `` bytes `` and `` string ``
====================================
2021-03-17 12:46:26 +00:00
- `` bytes.concat(...) returns (bytes memory) `` : :ref:`Concatenates variable number of
arguments to one byte array<bytes-concat>`
2023-04-01 06:51:14 +00:00
2022-02-04 13:56:03 +00:00
- `` string.concat(...) returns (string memory) `` : :ref:`Concatenates variable number of
arguments to one string array<string-concat>`
2023-04-01 06:51:14 +00:00
.. index :: address;balance, address;codehash, address;send, address;code, address;transfer
Members of `` address ``
======================
- `` <address>.balance `` (`` uint256 `` ): balance of the :ref: `address` in Wei
- `` <address>.code `` (`` bytes memory `` ): code at the :ref: `address` (can be empty)
- `` <address>.codehash `` (`` bytes32 `` ): the codehash of the :ref: `address`
- `` <address payable>.send(uint256 amount) returns (bool) `` : send given amount of Wei to :ref: `address` ,
returns `` false `` on failure
- `` <address payable>.transfer(uint256 amount) `` : send given amount of Wei to :ref: `address` , throws on failure
.. index :: blockhash, block, block;basefree, block;chainid, block;coinbase, block;difficulty, block;gaslimit, block;number, block;prevrandao, block;timestamp
.. index :: gasleft, msg;data, msg;sender, msg;sig, msg;value, tx;gasprice, tx;origin
Block and Transaction Properties
================================
- `` blockhash(uint blockNumber) returns (bytes32) `` : hash of the given block - only works for 256 most recent blocks
2021-07-12 14:50:37 +00:00
- `` block.basefee `` (`` uint `` ): current block's base fee (`EIP-3198 <https://eips.ethereum.org/EIPS/eip-3198> `_ and `EIP-1559 <https://eips.ethereum.org/EIPS/eip-1559> `_ )
2020-09-22 17:12:01 +00:00
- `` block.chainid `` (`` uint `` ): current chain id
2020-04-20 19:43:24 +00:00
- `` block.coinbase `` (`` address payable `` ): current block miner's address
2022-11-23 10:51:34 +00:00
- `` block.difficulty `` (`` uint `` ): current block difficulty (`` EVM < Paris `` ). For other EVM versions it behaves as a deprecated alias for `` block.prevrandao `` that will be removed in the next breaking release
2020-04-20 19:43:24 +00:00
- `` block.gaslimit `` (`` uint `` ): current block gaslimit
- `` block.number `` (`` uint `` ): current block number
2022-11-23 10:51:34 +00:00
- `` block.prevrandao `` (`` uint `` ): random number provided by the beacon chain (`` EVM >= Paris `` ) (see `EIP-4399 <https://eips.ethereum.org/EIPS/eip-4399> `_ )
2022-02-06 17:16:04 +00:00
- `` block.timestamp `` (`` uint `` ): current block timestamp in seconds since Unix epoch
2020-04-20 19:43:24 +00:00
- `` gasleft() returns (uint256) `` : remaining gas
- `` msg.data `` (`` bytes `` ): complete calldata
2020-12-03 21:33:58 +00:00
- `` msg.sender `` (`` address `` ): sender of the message (current call)
2022-01-22 19:56:15 +00:00
- `` msg.sig `` (`` bytes4 `` ): first four bytes of the calldata (i.e. function identifier)
2020-04-20 19:43:24 +00:00
- `` msg.value `` (`` uint `` ): number of wei sent with the message
- `` tx.gasprice `` (`` uint `` ): gas price of the transaction
2020-12-03 21:33:58 +00:00
- `` tx.origin `` (`` address `` ): sender of the transaction (full call chain)
2023-04-01 06:51:14 +00:00
.. index :: assert, require, revert
Validations and Assertions
==========================
2020-04-20 19:43:24 +00:00
- `` assert(bool condition) `` : abort execution and revert state changes if condition is `` false `` (use for internal error)
- `` require(bool condition) `` : abort execution and revert state changes if condition is `` false `` (use
for malformed input or error in external component)
- `` require(bool condition, string memory message) `` : abort execution and revert state changes if
condition is `` false `` (use for malformed input or error in external component). Also provide error message.
- `` revert() `` : abort execution and revert state changes
- `` revert(string memory message) `` : abort execution and revert state changes providing an explanatory string
2023-04-01 06:51:14 +00:00
.. index :: cryptography, keccak256, sha256, ripemd160, ecrecover, addmod, mulmod
Mathematical and Cryptographic Functions
========================================
2020-04-20 19:43:24 +00:00
- `` keccak256(bytes memory) returns (bytes32) `` : compute the Keccak-256 hash of the input
- `` sha256(bytes memory) returns (bytes32) `` : compute the SHA-256 hash of the input
- `` ripemd160(bytes memory) returns (bytes20) `` : compute the RIPEMD-160 hash of the input
- `` ecrecover(bytes32 hash, uint8 v, bytes32 r, bytes32 s) returns (address) `` : recover address associated with
the public key from elliptic curve signature, return zero on error
- `` 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 `` . Assert that `` k != 0 `` starting from version 0.5.0.
- `` 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 `` . Assert that `` k != 0 `` starting from version 0.5.0.
2023-04-01 06:51:14 +00:00
.. index :: this, super, selfdestruct
Contract-related
================
2020-04-20 19:43:24 +00:00
- `` this `` (current contract's type): the current contract, explicitly convertible to `` address `` or `` address payable ``
2023-04-01 06:51:14 +00:00
- `` super `` : a contract one level higher in the inheritance hierarchy
2020-04-20 19:43:24 +00:00
- `` selfdestruct(address payable recipient) `` : destroy the current contract, sending its funds to the given address
2023-04-01 06:51:14 +00:00
.. index :: type;name, type;creationCode, type;runtimeCode, type;interfaceId, type;min, type;max
Type Information
================
2020-04-20 19:43:24 +00:00
- `` type(C).name `` (`` string `` ): the name of the contract
- `` type(C).creationCode `` (`` bytes memory `` ): creation bytecode of the given contract, see :ref: `Type Information<meta-type>` .
- `` type(C).runtimeCode `` (`` bytes memory `` ): runtime bytecode of the given contract, see :ref: `Type Information<meta-type>` .
2020-04-24 13:06:15 +00:00
- `` type(I).interfaceId `` (`` bytes4 `` ): value containing the EIP-165 interface identifier of the given interface, see :ref: `Type Information<meta-type>` .
2020-05-11 13:35:41 +00:00
- `` type(T).min `` (`` T `` ): the minimum value representable by the integer type `` T `` , see :ref: `Type Information<meta-type>` .
- `` type(T).max `` (`` T `` ): the maximum value representable by the integer type `` T `` , see :ref: `Type Information<meta-type>` .
2020-04-20 19:43:24 +00:00
.. index :: visibility, public, private, external, internal
Function Visibility Specifiers
==============================
2021-07-14 16:52:39 +00:00
.. code-block :: solidity
:force:
2020-04-20 19:43:24 +00:00
function myFunction() <visibility specifier> returns (bool) {
return true;
}
- `` public `` : visible externally and internally (creates a :ref: `getter function<getter-functions>` for storage/state variables)
- `` private `` : only visible in the current contract
- `` external `` : only visible externally (only for functions) - i.e. can only be message-called (via `` this.func `` )
- `` internal `` : only visible internally
.. index :: modifiers, pure, view, payable, constant, anonymous, indexed
Modifiers
=========
- `` pure `` for functions: Disallows modification or access of state.
- `` view `` for functions: Disallows modification of state.
- `` payable `` for functions: Allows them to receive Ether together with a call.
- `` constant `` for state variables: Disallows assignment (except initialisation), does not occupy storage slot.
- `` immutable `` for state variables: Allows exactly one assignment at construction time and is constant afterwards. Is stored in code.
- `` anonymous `` for events: Does not store event signature as topic.
- `` indexed `` for event parameters: Stores the parameter as topic.
- `` virtual `` for functions and modifiers: Allows the function's or modifier's
2023-05-30 19:49:25 +00:00
behavior to be changed in derived contracts.
2020-04-20 19:43:24 +00:00
- `` override `` : States that this function, modifier or public state variable changes
2023-05-30 19:49:25 +00:00
the behavior of a function or modifier in a base contract.
2020-04-20 19:43:24 +00:00