• Articles
  • Tutorials
  • Interview Questions

Solidity Cheat Sheet

Solidity User Handbook

Are you a programmer looking for a powerful tool to work on Ethereum Virtual Machines? If yes, then you must take Solidity into consideration. This Solidity cheat sheet is designed for the one who has already started learning about Ethereum Blockchain and using Solidity as a tool, then this sheet will be a handy reference.

Don’t worry if you are a beginner and have no idea about how Solidity works, this cheat sheet will give you a quick reference of the keywords, variables, syntax, and basics that you must know to get started.
Download the printable PDF of this Solidity cheat sheet
Solidity Cheat Sheet

What is Solidity:

Solidity is a contract-oriented high-level language for implementing smart contracts. It was influenced by C++, JavaScript, and Python and is designed to target the EVM.

Contracts:

Contracts in solidity are similar to classes in an object-oriented language.
A contract can be created using the “new” keyword

contract L {
    function add(uint _p, uint _q) returns (uint) {
        return _p + _q;
    }
}
contract M {
    address p;
    function f(uint _p) {
        p = new L();
    }
}

Check this video on Blockchain by intellipaat:

Video Thumbnail

Smart contracts:

The smart contract is a computer protocol that is used to streamline the process of contracts by digitally enforcing, verifying, and managing them.

Remix:

The remix is a browser-based IDE with an integrated compiler and a solidity run-time environment without server-side components

Get 100% Hike!

Master Most in Demand Skills Now!

Solium:

Solium is a linter to identify and fix style and security issues in solidity

Doxity:

Doxity is a tool that is used in solidity as a documentation generator

Solograph:

Solograph is used to visualize solidity control flow and highlight potential security vulnerabilities

Solidity Assembly:

Solidity defines an assembly language that can be used without solidity and can be used as inline assembly inside solidity source code

Gas:

Gas is a measurement roughly equivalent to the computational steps

Block Gas limit:

It is used to control the amount of gas consumed during the transactions
Solidity Compiler

ABI:

A data encoding scheme called “Application Binary Interface” (ABI) is used in for working with smart contracts

Global Variables:

Solidity provides special variables that exist inside the global namespace are known as Global Variables

  • blockhash(uint numberOfBlock) returns (bytes32) – hash function of the given block which works for the 256 most recent blocks excluding the current block
  • block.coinbase (address): shows the miner’s address of the current block
  • block.number (unit): number of current block
  • block.gaslimit (unit) : gaslimit of the current block
  • block.timestamp (uint): timestamp of current block
  • msg.gas (uint): the gas that remains
  • msg.value (uint): the amount of WEI sent along with the message
  • msg.sender (address): address of the message sender (current call)
  • msg.sig (bytes4): first four bytes of the call data
  • now (uint): timestamp for the current block
  • tx.origin (address): the sender of the transaction (whole call chain)

WEI:

the smallest denomination or base unit of Ether.

Pragma:

Pragmas in solidity are used to specify certain conditions under which the source files can or cannot run
Example:

pragma solidity ^0.2.32
This code compiles with a compiler function >=0.2.32
A pseudocode example for pragma syntax
'pragma' Identifier ([^;]+) ';'

Import files:

Syntax to import the files

import "filename";
import * as symbolName from "filename"; or import "filename" as symbolName;
import {symbol1 as alias, symbol2} from "filename";

Data Types:

Data type is a particular kind of data defined by the values it can take

Boolean

Boolean syntax:
bool: true or false
Operators:
Logical:
! (Logical negation)
&& (AND)
|| (OR)
Comparisons:
== (equality)
!= (inequality)

Integer

Unsigned Integer:
Eg: uint8 | uint16 | uint32 | unit64 | uint128 | uint256 (uint)
Signed integer: int8 | int16 | int32 | nit64 | int128 | int256 (uint)
Operators:
Comparisons:
<= (Less than or equal to)
< (Less than)
== (equal to)
!= (Not equal to)
>= (Greater than or equal to)
(Greater than)

Bitwise operators:

& (AND)
| (OR)
^ (Bitwise Exclusive OR)
~ (Bitwise negation)

Arithmetic Operators:

+ (Addition)
 - (Subtraction)
Unary –
Unary +
*(Multiplication)
% (Division)
** (Exponential)
<< (Left shift)
>> (Right shift)

Address:

Holds an Ethereum address (20-byte value).

Operators: Comparisons: <=, <, ==, !=, >= and >
Methods:
Balance:
<address>.balance (unit256): balance of address in WEI
Transfer and send:
<address>.transfer(uint256 amount): send given amount of Wei to Address, throws on failure
<address>.send(uint256 amount) returns (bool): send given amount of Wei to Address, returns false on failure
Call:
<address>.call(...) returns (bool): issue low-level CALL, returns false on failure

Array:

An array can be dynamic or fixed size array

uint[] dynamicSizeArray;
uint[9] fixedSizeArray;

Struct:

New types can be declared using Struct

Example:

struct Funder {
address addr;
uint amount:
}
Funder funders;

Mapping:

Mapping can be seen as hash tables, declared as mapping(_KeyType => _ValueType)

Function:

Syntax of a function:

function (<parameter types>) {internal|external|public|private} [pure|constant|view|payable] [returns (<return types>]
Pure functions:
function f(uint a) pure returns (uint) {
return a *24
}

Global functions:  

Solidity provides special functions that exist inside the global namespace are known as Global Functions

  • keccak256(…) returns (bytes32)- computes the ethereum SHA-3 hash associated with the arguments
  • sha256(…) returns (bytes32) – Computes the SHA-256 argument hash
  • mulmod( uint a, uint b, uint c) returns (unit) : It computes (a*b)%c, if the multiplication gets executed using arbitrary precision, thus not wrapping around 2**256
  • addmod(uint p, uint q, uint m) returns (uint): Computes (p+q)%m
  • ecrecover(bytes32 _hash, uint8 _v, bytes32 _r, bytes32 _s) returns (address): recovers the address linked with the public key and if an error occurs, zero is returned
  • ripemd160(…) returns (bytes20): computes the RIPEMD-160 (tightly packed) argument hash
  • <address>.balance(uint256) : Returns the balance of the specified address in WEI
  • <address>.send(uint256 amount) returns (bool): It sends the specified number of WEI to the address given, on failure it returns false as an output
  • <address>.transfer(uint256 amount): Sends specified number of WEI to the particular address, on failure throws

Blockchain masters program

Access Modifiers:

Access Modifiers are keywords that set the accessibility of classes, methods, and other members and are used to facilitate the encapsulation of the components

  • public – Accessible from the current contract, inherited contracts, and externally
  • private – Accessible only from the current contract
  • internal – Accessible only from current contract and contracts inheriting from it
  • external – Can be accessed externally only

Interface:

Interface in solidity is defined as contracts, but the function bodies are omitted for the functions

Example:

pragma solidity ^0.22;
interface Token {
    function transfer(address recipient, uint amount);
}

Important Terms

  • Payable function: Functions that receive Ether are marked as payable functions
  • Events: Events writes the information from the contract to Block chain logs
  • Voting: When the contract is quiet complex, it uses voting contract, it shows how delegated voting can be done so that vote counting is automatic and completely transparent at the same time
  • Delegate call: It is a reusable library code that can be applied to a contract’s storage in solidity
  • Logs: A feature called logs is used in solidity to implement events
  • NPM: It is a convenient and portable way to install the Solidity compiler
  • Truffle: It is a test bed that will allow to easily test and compile the smart contract
  • Inheritance: In solidity, inheritance is more syntactic. In the final compilation, the compiler copies the parent class members to create the bytecode of the derived contract with the copied members
  • This: This is a keyword that refers to the instance of the contract where the call is made
  • Sender: This function refers to the address where the contract is being called from
  • Pure: It is a modifier that assures not to be read from or modified
  • View: In solidity, the view is a function that promises to not modify the state of the contract
  • Suicide: Suicide is an alias for self destruct function, that is used to destroy the current contract
  • Delete: Delete is used to delete all elements in an array
  • coinbase(address): It refers to the miner’s address of the current block
  • Address(contractVar).send(amount): If a built-in send function needs to be used, it can be accessed using this function
  • Super: It is used to refer to the contract that is higher by one level in the inheritance hierarchy

TRANSACTION EXAMPLE:

A transaction of data (0x614) from 0x712 to 0x145
TRANSACTION EXAMPLE

Download a Printable PDF of this Solidity Cheat Sheet

This would be all for the Solidity cheat sheet. In case you are looking to learn Solidity in depth then you should definitely check out the Blockchain Course provided by Intellipaat. In this online training, you will get to learn all about blockchains including bitcoin mining, Hyperledger, Block Explorer, Building a Private Blockchain, etc. You will work on real-life projects and assignments and prepare yourself for Certified Solidity Professional in this Solidity training. On top of that, you will have 24*7 technical support from our experts here at Intellipaat.

 

Course Schedule

Name Date Details
Blockchain Course 14 Dec 2024(Sat-Sun) Weekend Batch View Details
21 Dec 2024(Sat-Sun) Weekend Batch
28 Dec 2024(Sat-Sun) Weekend Batch

About the Author

Technical Lead - Blockchain and Cyber Security Professional

Arpit is a Technical Lead in blockchain and cyber security. He has 5+ years of experience helping companies secure their applications via ethical hacking practices and has helped many fintech companies set up their blockchain implementations. In his free time, he provides training on cybersecurity and related domains.