Order of Precedence of Operators¶
The following is the order of precedence for operators, listed in order of evaluation.
Solidity cheat-sheet. Write smart contracts for Ethereum. Posted by 3 years ago. Solidity cheat-sheet. Write smart contracts for Ethereum. This document is a cheatsheet for Solidity that you can use to write Smart Contracts for Ethereum based blockchain. This guide is not intended to teach you Solidity from the ground up, but to help developers with basic knowledge who may struggle to get familiar with Smart Contracts and Blockchain because of the Solidity concepts used.
Precedence | Description | Operator |
---|---|---|
1 | Postfix increment and decrement | ++ , -- |
New expression | new<typename> | |
Array subscripting | <array>[<index>] | |
Member access | <object>.<member> | |
Function-like call | <func>(<args...>) | |
Parentheses | (<statement>) | |
2 | Prefix increment and decrement | ++ , -- |
Unary minus | - | |
Unary operations | delete | |
Logical NOT | ! | |
Bitwise NOT | ~ | |
3 | Exponentiation | ** |
4 | Multiplication, division and modulo | * , / , % |
5 | Addition and subtraction | + , - |
6 | Bitwise shift operators | << , >> |
7 | Bitwise AND | & |
8 | Bitwise XOR | ^ |
9 | Bitwise OR | | |
10 | Inequality operators | < , > , <= , >= |
11 | Equality operators | , != |
12 | Logical AND | && |
13 | Logical OR | || |
14 | Ternary operator | <conditional>?<if-true>:<if-false> |
Assignment operators | = , |= , ^= , &= , <<= ,>>= , += , -= , *= , /= ,%= | |
15 | Comma operator | , |
Global Variables¶
abi.decode(bytesmemoryencodedData,(...))returns(...)
: ABI-decodesthe provided data. The types are given in parentheses as second argument.Example:(uinta,uint[2]memoryb,bytesmemoryc)=abi.decode(data,(uint,uint[2],bytes))
abi.encode(...)returns(bytesmemory)
: ABI-encodes the given argumentsabi.encodePacked(...)returns(bytesmemory)
: Performs packed encoding ofthe given arguments. Note that this encoding can be ambiguous!abi.encodeWithSelector(bytes4selector,...)returns(bytesmemory)
: ABI-encodesthe given arguments starting from the second and prepends the given four-byte selectorabi.encodeWithSignature(stringmemorysignature,...)returns(bytesmemory)
: Equivalenttoabi.encodeWithSelector(bytes4(keccak256(bytes(signature)),...)`
block.chainid
(uint
): current chain idblock.coinbase
(addresspayable
): current block miner’s addressblock.difficulty
(uint
): current block difficultyblock.gaslimit
(uint
): current block gaslimitblock.number
(uint
): current block numberblock.timestamp
(uint
): current block timestampgasleft()returns(uint256)
: remaining gasmsg.data
(bytes
): complete calldatamsg.sender
(address
): sender of the message (current call)msg.value
(uint
): number of wei sent with the messagetx.gasprice
(uint
): gas price of the transactiontx.origin
(address
): sender of the transaction (full call chain)assert(boolcondition)
: abort execution and revert state changes if condition isfalse
(use for internal error)require(boolcondition)
: abort execution and revert state changes if condition isfalse
(usefor malformed input or error in external component)require(boolcondition,stringmemorymessage)
: abort execution and revert state changes ifcondition isfalse
(use for malformed input or error in external component). Also provide error message.revert()
: abort execution and revert state changesrevert(stringmemorymessage)
: abort execution and revert state changes providing an explanatory stringblockhash(uintblockNumber)returns(bytes32)
: hash of the given block - only works for 256 most recent blockskeccak256(bytesmemory)returns(bytes32)
: compute the Keccak-256 hash of the inputsha256(bytesmemory)returns(bytes32)
: compute the SHA-256 hash of the inputripemd160(bytesmemory)returns(bytes20)
: compute the RIPEMD-160 hash of the inputecrecover(bytes32hash,uint8v,bytes32r,bytes32s)returns(address)
: recover address associated withthe public key from elliptic curve signature, return zero on erroraddmod(uintx,uinty,uintk)returns(uint)
: compute(x+y)%k
where the addition is performed witharbitrary precision and does not wrap around at2**256
. Assert thatk!=0
starting from version 0.5.0.mulmod(uintx,uinty,uintk)returns(uint)
: compute(x*y)%k
where the multiplication is performedwith arbitrary precision and does not wrap around at2**256
. Assert thatk!=0
starting from version 0.5.0.this
(current contract’s type): the current contract, explicitly convertible toaddress
oraddresspayable
super
: the contract one level higher in the inheritance hierarchyselfdestruct(addresspayablerecipient)
: destroy the current contract, sending its funds to the given address<address>.balance
(uint256
): balance of the Address in Wei<address>.code
(bytesmemory
): code at the Address (can be empty)<address>.codehash
(bytes32
): the codehash of the Address<addresspayable>.send(uint256amount)returns(bool)
: send given amount of Wei to Address,returnsfalse
on failure<addresspayable>.transfer(uint256amount)
: send given amount of Wei to Address, throws on failuretype(C).name
(string
): the name of the contracttype(C).creationCode
(bytesmemory
): creation bytecode of the given contract, see Type Information.type(C).runtimeCode
(bytesmemory
): runtime bytecode of the given contract, see Type Information.type(I).interfaceId
(bytes4
): value containing the EIP-165 interface identifier of the given interface, see Type Information.type(T).min
(T
): the minimum value representable by the integer typeT
, see Type Information.type(T).max
(T
): the maximum value representable by the integer typeT
, see Type Information.
Note
Do not rely on block.timestamp
or blockhash
as a source of randomness,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 hashand 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 twoconsecutive blocks in the canonical chain.
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 othervalues will be zero.
Note
In version 0.5.0, the following aliases were removed: suicide
as alias for selfdestruct
,msg.gas
as alias for gasleft
, block.blockhash
as alias for blockhash
andsha3
as alias for keccak256
.
Note
In version 0.7.0, the alias now
(for block.timestamp
) was removed.
Function Visibility Specifiers¶
public
: visible externally and internally (creates a getter function for storage/state variables)private
: only visible in the current contractexternal
: only visible externally (only for functions) - i.e. can only be message-called (viathis.func
)internal
: only visible internally
Modifiers¶
Solidity Cheat Sheets
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’sbehaviour to be changed in derived contracts.override
: States that this function, modifier or public state variable changesthe behaviour of a function or modifier in a base contract.
Reserved Keywords¶
Solidity Cheat Sheet Free
These keywords are reserved in Solidity. They might become part of the syntax in the future:
Solidity Cheat Sheet Excel
after
, alias
, apply
, auto
, case
, copyof
, default
,define
, final
, immutable
, implements
, in
, inline
, let
, macro
, match
,mutable
, null
, of
, partial
, promise
, reference
, relocatable
,sealed
, sizeof
, static
, supports
, switch
, typedef
, typeof
,unchecked
.