Solidity Cheatsheet
Solidity Cheatsheet
[ Work in progress. Will be updating as I learn new stuff ]
Basics
Basic Structure solidity
// SPDX-License-Identifier: GPL-3.0pragma solidity >=0.4.16 <0.9.0;contract MyContract {uint256 storedData;address contractOwner;constructor(address _owner) public {contractOwner = _owner;}function set(uint256 x) public {storedData = x;}function get() public view returns (uint256) {return storedData;}}
Data Types solidity
Payable Functions solidity
Events solidity
Address
- code : bytes code at the Address (can be empty)
- balance : uint256 balance of the Address in Wei
- codehash : bytes32 the codehash of the Address
- send : send given amount of Wei to Address, returns false on failure
- transfer : send given amount of Wei to Address, throws on failure
Block Variables
- block.blockhash : uint hash of the given block - only works for the 256 most recent blocks excluding current
- block.coinbase : address current block miner’s address
- block.difficulty : uint current block difficulty
- block.gaslimit : uint current block gaslimit
- block.number : uint current block gaslimit
- block.chainid : uint current chain id
- block.basefee : uint current block’s base fee
- block.timestamp : uint current block timestamp as seconds since unix epoch
- now : uint current block timestamp (alias for block.timestamp)
Function Visibility
- Public : Visible internal and externally.
- Private : Accessible only inside the current contract.
- External : Can be only called from outside.
- Internal : Accessible only current contract and anything that inherits it.
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 behaviour to be changed in derived contracts.
- override : States that this function, modifier or public state variable changes the behaviour of a function or modifier in a base contract.
Transaction Variables
- msg.gas : uintremaining gas
- msg.sender : address sender of the message (current call)
- msg.value : uint number of wei sent with the message
- msg.sig : bytes4 first four bytes of the calldata (i.e. function identifier)
- msg.data : bytes complete calldata
- tx.gasprice : uint gas price of the transaction
- tx.origin : address sender of the transaction (full call chain)
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
Credits
The descripts are taken from these resources. I just made it more easy to visualize