Show Menu
Cheatography

Solidity Cheat Sheet (DRAFT) by

Solidity Language cheat sheet

This is a draft cheat sheet. It is a work in progress and is not finished yet.

Function Visibility

public
acces­sible to all
private
acces­sible only to contract
internal
acces­sible to contract and subcon­tracts
external
acces­sible only outside contract
Defining function visibility is mandat­ory.

Function Types

pure
does not access the blockc­hain
view
does not modify the blockc­hain
payable
can receive Ether
pure and view functions do not cost any gas.

Data Location

storage
stored on the blockc­hain
memory
stored in memory
Data location must be explic­itely defined for all variables.
Storage is very expensive and must be used with caution.

Parameter Types

int / uint {8/256}
string
bool
address / address payable

Structures

struct StructureName {
   <parameter type> var1;
   <parameter type> var2;
   ...
}
Similar types should be grouped together in structures to lower gas cost.
 

Array and Mappings

<parameter type>[] arrayName;
mapping ( <parameter type> => <parameter type>) mappingName;
Arrays created in storage can have a variable size, arrays created in memory must have a fixed size at instan­tia­tion.
Array can be read through by indexes, mappings cannot.

Contract

contract contractName [is inheritedContract,...] {...}

Constr­uctor

constructor(<parameter types>) {public|private|internal|external} {...}
Constr­uctors are optional, they are executed at contract creation.

Functions

function functionName(<parameter types>) {public|private|internal|external} [pure|view|payable] [modifiers] [returns (<return types>)] {...}

Interface

function functionName(<parameter types>) {public|private|internal|external} [pure|view|payable] [modifiers] [returns (<return types>)];
Definition must be identical to source function.

Modifiers

modifier modifierName(<parameter types>) {...}
Use _; to continue with the function after running modifier code.
 

Events

event eventName(<parameter types>);
emit eventName(<parameters>);
Events are defined at contract root and emitted inside functions.

Useful links

Security

Use Ownable contract to define owner of a contract and restrict usage of some functions using onlyO­wner modifier.
Mind Overfl­ow/­Und­erflow when using integers. Use OpenZe­ppelin SafeMath library to prevent problems.

Help Us Go Positive!

We offset our carbon usage with Ecologi. Click the link below to help us!

We offset our carbon footprint via Ecologi