Solidity Cheat Sheet



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.

PrecedenceDescriptionOperator
1Postfix increment and decrement++, --
New expressionnew<typename>
Array subscripting<array>[<index>]
Member access<object>.<member>
Function-like call<func>(<args...>)
Parentheses(<statement>)
2Prefix increment and decrement++, --
Unary minus-
Unary operationsdelete
Logical NOT!
Bitwise NOT~
3Exponentiation**
4Multiplication, division and modulo*, /, %
5Addition and subtraction+, -
6Bitwise shift operators<<, >>
7Bitwise AND&
8Bitwise XOR^
9Bitwise OR|
10Inequality operators<, >, <=, >=
11Equality operators, !=
12Logical AND&&
13Logical OR||
14Ternary operator<conditional>?<if-true>:<if-false>
Assignment operators=, |=, ^=, &=, <<=,>>=, +=, -=, *=, /=,%=
15Comma operator,
Pdf

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 arguments
  • abi.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 selector
  • abi.encodeWithSignature(stringmemorysignature,...)returns(bytesmemory): Equivalentto abi.encodeWithSelector(bytes4(keccak256(bytes(signature)),...)`
  • block.chainid (uint): current chain id
  • block.coinbase (addresspayable): current block miner’s address
  • block.difficulty (uint): current block difficulty
  • block.gaslimit (uint): current block gaslimit
  • block.number (uint): current block number
  • block.timestamp (uint): current block timestamp
  • gasleft()returns(uint256): remaining gas
  • msg.data (bytes): complete calldata
  • msg.sender (address): sender of the message (current call)
  • msg.value (uint): number of wei sent with the message
  • tx.gasprice (uint): gas price of the transaction
  • tx.origin (address): sender of the transaction (full call chain)
  • assert(boolcondition): abort execution and revert state changes if condition is false (use for internal error)
  • require(boolcondition): abort execution and revert state changes if condition is false (usefor malformed input or error in external component)
  • require(boolcondition,stringmemorymessage): abort execution and revert state changes ifcondition is false (use for malformed input or error in external component). Also provide error message.
  • revert(): abort execution and revert state changes
  • revert(stringmemorymessage): abort execution and revert state changes providing an explanatory string
  • blockhash(uintblockNumber)returns(bytes32): hash of the given block - only works for 256 most recent blocks
  • keccak256(bytesmemory)returns(bytes32): compute the Keccak-256 hash of the input
  • sha256(bytesmemory)returns(bytes32): compute the SHA-256 hash of the input
  • ripemd160(bytesmemory)returns(bytes20): compute the RIPEMD-160 hash of the input
  • ecrecover(bytes32hash,uint8v,bytes32r,bytes32s)returns(address): recover address associated withthe public key from elliptic curve signature, return zero on error
  • addmod(uintx,uinty,uintk)returns(uint): compute (x+y)%k where the addition is performed witharbitrary precision and does not wrap around at 2**256. Assert that k!=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 at 2**256. Assert that k!=0 starting from version 0.5.0.
  • this (current contract’s type): the current contract, explicitly convertible to address or addresspayable
  • super: the contract one level higher in the inheritance hierarchy
  • selfdestruct(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,returns false on failure
  • <addresspayable>.transfer(uint256amount): send given amount of Wei to Address, throws on failure
  • type(C).name (string): the name of the contract
  • type(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 type T, see Type Information.
  • type(T).max (T): the maximum value representable by the integer type T, 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

Solidity cheat sheet free

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 contract
  • external: only visible externally (only for functions) - i.e. can only be message-called (via this.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.