-
Ether units:
1 wei == 1
,1 gwei == 1e9
,1 ether == 1e18
, finney and szabo have been removed since0.7.0
-
Time units:
1 == 1 seconds
,1 minutes == 60 seconds
,1 hour == 60 minutes
,1 days == 24 hours
,1 weeks == 7days
years
was removed in 0.5.0Note: Due to the fact that leap seconds cannot be predicted, an exact calendar library has to be updated by an oracle
-
Special variables and functions. Block and Transaction properties
-
blockhash(uint blocknumner) returns (bytes32)
- hash of the given block when blocknumber is one of 256 most recent blocks, otherwise returns zero -
block.basefee
- uint -
block.chainid
- uint -
block.coinbase
- address payable current block miner's address -
block.difficulty
- uint (EVM < Paris) -
block.gaslimit
- uint -
block.number
- uint -
block.prevrandao
- uint (EVM >= Paris) -
block.timestamp
- uint -
gasleft()
- returns (uint256) -
msg.data
- (bytes calldata) -
msg.sender
- address -
msg.sig
- bytes4, first four bytes of calldata i.e function identifier -
msg.value
- uint wei sent with message -
tx.gasprice
- uint gas price of the transaction -
tx.origin
- addressNote: don't rely on
block.*
andtx.*
for contracts evaluated off-chain, the valuse depend on the EVM executing te contractsNote: All members of
mgs.*
, can change for every external functioncall, this includes calls to library functionsNote: Don't rely on
block.timestamp
orblockhash
for randomness, both can be influenced by miner's to some degree. Current block timestamp must be strickly larger than of the last block, but the only guarantee is that it will be somewhere between the timestamps of two xonsecutive bocks in the cannonical chainNote: blockhashes are not available for all blocks for scalability reasons, only have access to the most recent 256 blocks, all other values will be zero
Note: The function
blockhash
was previouslyblock.blockhash
which deprecated in 0.4.22 and removed in 0.5.0Note: Function
gasleft()
was previouslymsg.gas
, which was depricated in 0.4.21 and removed in 0.5.0Note: In version 0.7.0, the alias
now
forblock.timestamp
was removed
-
-
ABI Encoding and Decoding functions
-
abi.decode(bytes memory encodedData, (...)) returns (..)
decodes given data, while the types are give 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 give arguments
-
abi.encodePacked(...) returns (bytes memory)
performs packed encoding of given arguments, note packed encoding can be ambiguous
-
abi.encodeWithSelector(bytes4 selector, ...) returns (bytes memory)
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))))
-
-
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 equalsabi.encodeWithSelector(functionPointer.selector, (...))
Note: These encodings can be used to craft data for external functions calls, without actually calling an external function. Furthermore,
keccak256(abi.encodePacked(a,b))
is a way to compute the hash of a structured data (although be aware that it is possible to craft a "Hashing collision" using different function parameter types).See documentation about abi and tightly packed encoding
-
-
-
Members of bytes
-
bytes.concat(...) returns (bytes memory)
concatenates variable number of bytes and bytes1, ..., bytes32 arguments to one byte array
-
-
Members is strings
-
string.concat(...) returns (string memory)
concatenates variabe number of string arguments to on string array
-
-
Error Handling
See the dedicated section on assert and require
-
assert(bool condition)
Causes a panic error and thus state change reversion if the condition is not met - to be used for internal errors
-
require(bool condition, string memory message)
orrequire(bool condition)
reverts if the condition is not met - used for inputs or external components
-
revert()
orrevert(string memory message)
abort execution and revert state changes
-
-
Mathematical and Cryptographic functions
-
addmod(uint x, uint y, uint k) returns (uint)
compute
(x + y) % k
, condition is performed with arbitrary precision and does not wrap around at$2^{256}$ . Assert thatk != 0
starting version 0.5.0 -
mulmod(uint x, uinty, uint k) returns (uint)
compute
(x * y) % k
-
keccak256(bytes memory) returns (bytes32)
compute the keccak256 hash of the input
Note: There used to be an alias
keccak256
calledsha3
, which was removed in version 0.5.0 -
sha256(bytes memory) returns (bytes32)
-
ripemd160(bytes memory) returns (bytes20)
compute thr RIPEMD-160 hash of the input
-
ecrecover(bytes32 hash, uint8 v, bytes32 r, bytes32 s) returns (address)
recover the address associated with the public key from elliptic curve signature or return on error. Teh function parameters correspind to ECDSA values of the signature
-
r
- first 32 bytes of signature -
s
- second 32 bytes of signature -
v
- final 1 byte of signatureecrecover
returns anaddress
, and not anaddress payable
. Seeaddress payable
for conversion, incase you need to transfer funds to the recovered address. Further, read example usage
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-2Not usually a problem unless you require signatures to be unique or use them to identify items. OpenZeppelin has an ECDSA helper library that you can use as a wrapper for ecrecover, without this issue
Note: When running
sha256
,ripemd160
orecrecover
on a private blockchain, you might encoutre Out-of-Gas. This is because these function 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 this 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. -
-
-
Members of Address types
-
address.balance
- uint26, -
address.code
- bytes memory, code at the Address can be empty -
address.codehash
- bytes32 -
address-payable.transfer(uint26 amount)
reverts on failure, forwards 2300 gas stipend - not adjustable
-
address-payable.send(uint256 amount) returns (bool)
returns
false
on failure, forwards 2300 gas - not adjustable -
address.call(bytes memory) returns (bool, bytes memory)
issue low-level
CALL
with the given payload, returns success condition and return data, forwards all available gas, adjustable -
address.delegatecall(bytes memory) returns (bool, bytes memory)
issue lol-level
DELEGATECALL
with the given payload, returns success condition and return data, forwards all available gas, adjustable -
address.staticcall(bytes memory) returns (bool, bytes memory)
issue low-level
STATICCALL
with the given payload, returns success condition, and return data, forwards all available gas, adjustable
Warning: You should avoid using
.call()
whenever possible when executing another contract function as it bypasses type checking, function existense check, and argument packingWarning: the are some dangers with
send
. The transfer fails if the call stack depth is 1024 (this can always be forced byt the caller) and it also fails if the recipient runs out of gas. So in order to make safe Ether transfers, always check the return value ofsend
, usetransfer
of even better: use a pattern where the recipent withdraws the ether.Warning: Due to the fact that the EVM considers a call to a non-existing contract to always succeed, Solidity includes an extra check using the
extcodesize
opcode when performing external calls. This ensures that the contract exists of an exception is raised. The low level calls.call()
,.delegatecall()
,.staticcall()
,.send()
and.transfer()
do not include this check, ehich makes them cheaper in terms of gas but also less safeNote: prior to 0.5.0 this.balance now forbidden use address(this).balance
Note: if state variables are accessed via a low level delegatecall, the storage layout of the two contracts 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 pointer are passed as function arguments as in the case for the high-level libraries
Note: prior to 0.5.0
.call
,.delegatecall
and.staticcall
on returned the success condition and not the return data -
-
Contract related
-
this
the current contract, explicitly convertible to Address
-
super
A contract one level higher in the inheritance hierarchy
-
selfdestruct(address payable receipient)
Destroy the current contract, sending its funds to the given addrss, and end execution.
selfdestruct
has many peculiarities inherited from the EVM: a.) the receiving contract's receive function is not executed b.) the contract is only really destroyed at the end of the transaction andreverts
might undo the destruction
Note: All functions of the current contract are callable directly including the current function.
Warning: From 0.8.18 and up, the use of
selfdestruct
in both solidity and Yul will trigger a deprecation warning, sinceSELFDESTRUCT
opcode will eventually undergo breaking changes in behavior as stated in EIP-6049 -
-
Type Information
The expression
type(x)
can be used to retrieve information about the type ofx
. Currently, there is limited support for this feature (X
can be either a contract or an integert type) but it might be expanded in the future-
type(Contract).name
the name of contract
-
type(Contract).creationCode
memory bytes array that contains the creation bytecode of the contract. This can be used in inline assembly to build custom creation routines, especially by using
create2
opcode. This property CANNOT be accesed 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. -
type(Contract).runtimeCode
memory bytes array that contains the runtime bytecode of the contract. This is the code that is usually deployed by the constructor of contract
C
. ifC
has a constructor that uses inline assumbly, 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 call. The same restrictions as with.creationCode
also apply for this property -
type(Interface).interfaceId
A
bytes4
value containing theEIP-165
interface identifier of the given interfaceI
. The identifier is defined as theXOR
of all function selectors defined within the interface itself - excluding all the inherited funcitions -
type(T).min
smallest value represented but type
T
-
type(T).max
largest value represented but type
T
-
-
Reserved Keywords
-
after
,alias
,apply
,auto
,byte
,case
,copyof
,default
,define
,final
,implements
,in
,inline
,let
,macro
,match
,mutable
,null
,of
,partial
,promise
,reference
,relocatable
,sealed
,sizeof
,static
,supports
,switch
,typedef
,typeof
,var
-