\documentclass[10pt,a4paper]{article} % Packages \usepackage{fancyhdr} % For header and footer \usepackage{multicol} % Allows multicols in tables \usepackage{tabularx} % Intelligent column widths \usepackage{tabulary} % Used in header and footer \usepackage{hhline} % Border under tables \usepackage{graphicx} % For images \usepackage{xcolor} % For hex colours %\usepackage[utf8x]{inputenc} % For unicode character support \usepackage[T1]{fontenc} % Without this we get weird character replacements \usepackage{colortbl} % For coloured tables \usepackage{setspace} % For line height \usepackage{lastpage} % Needed for total page number \usepackage{seqsplit} % Splits long words. %\usepackage{opensans} % Can't make this work so far. Shame. Would be lovely. \usepackage[normalem]{ulem} % For underlining links % Most of the following are not required for the majority % of cheat sheets but are needed for some symbol support. \usepackage{amsmath} % Symbols \usepackage{MnSymbol} % Symbols \usepackage{wasysym} % Symbols %\usepackage[english,german,french,spanish,italian]{babel} % Languages % Document Info \author{kabann} \pdfinfo{ /Title (solidity-based-ethereum.pdf) /Creator (Cheatography) /Author (kabann) /Subject (Solidity based Ethereum Cheat Sheet) } % Lengths and widths \addtolength{\textwidth}{6cm} \addtolength{\textheight}{-1cm} \addtolength{\hoffset}{-3cm} \addtolength{\voffset}{-2cm} \setlength{\tabcolsep}{0.2cm} % Space between columns \setlength{\headsep}{-12pt} % Reduce space between header and content \setlength{\headheight}{85pt} % If less, LaTeX automatically increases it \renewcommand{\footrulewidth}{0pt} % Remove footer line \renewcommand{\headrulewidth}{0pt} % Remove header line \renewcommand{\seqinsert}{\ifmmode\allowbreak\else\-\fi} % Hyphens in seqsplit % This two commands together give roughly % the right line height in the tables \renewcommand{\arraystretch}{1.3} \onehalfspacing % Commands \newcommand{\SetRowColor}[1]{\noalign{\gdef\RowColorName{#1}}\rowcolor{\RowColorName}} % Shortcut for row colour \newcommand{\mymulticolumn}[3]{\multicolumn{#1}{>{\columncolor{\RowColorName}}#2}{#3}} % For coloured multi-cols \newcolumntype{x}[1]{>{\raggedright}p{#1}} % New column types for ragged-right paragraph columns \newcommand{\tn}{\tabularnewline} % Required as custom column type in use % Font and Colours \definecolor{HeadBackground}{HTML}{333333} \definecolor{FootBackground}{HTML}{666666} \definecolor{TextColor}{HTML}{333333} \definecolor{DarkBackground}{HTML}{A3A3A3} \definecolor{LightBackground}{HTML}{F3F3F3} \renewcommand{\familydefault}{\sfdefault} \color{TextColor} % Header and Footer \pagestyle{fancy} \fancyhead{} % Set header to blank \fancyfoot{} % Set footer to blank \fancyhead[L]{ \noindent \begin{multicols}{3} \begin{tabulary}{5.8cm}{C} \SetRowColor{DarkBackground} \vspace{-7pt} {\parbox{\dimexpr\textwidth-2\fboxsep\relax}{\noindent \hspace*{-6pt}\includegraphics[width=5.8cm]{/web/www.cheatography.com/public/images/cheatography_logo.pdf}} } \end{tabulary} \columnbreak \begin{tabulary}{11cm}{L} \vspace{-2pt}\large{\bf{\textcolor{DarkBackground}{\textrm{Solidity based Ethereum Cheat Sheet}}}} \\ \normalsize{by \textcolor{DarkBackground}{kabann} via \textcolor{DarkBackground}{\uline{cheatography.com/203346/cs/44304/}}} \end{tabulary} \end{multicols}} \fancyfoot[L]{ \footnotesize \noindent \begin{multicols}{3} \begin{tabulary}{5.8cm}{LL} \SetRowColor{FootBackground} \mymulticolumn{2}{p{5.377cm}}{\bf\textcolor{white}{Cheatographer}} \\ \vspace{-2pt}kabann \\ \uline{cheatography.com/kabann} \\ \end{tabulary} \vfill \columnbreak \begin{tabulary}{5.8cm}{L} \SetRowColor{FootBackground} \mymulticolumn{1}{p{5.377cm}}{\bf\textcolor{white}{Cheat Sheet}} \\ \vspace{-2pt}Published 4th September, 2024.\\ Updated 4th September, 2024.\\ Page {\thepage} of \pageref{LastPage}. \end{tabulary} \vfill \columnbreak \begin{tabulary}{5.8cm}{L} \SetRowColor{FootBackground} \mymulticolumn{1}{p{5.377cm}}{\bf\textcolor{white}{Sponsor}} \\ \SetRowColor{white} \vspace{-5pt} %\includegraphics[width=48px,height=48px]{dave.jpeg} Measure your website readability!\\ www.readability-score.com \end{tabulary} \end{multicols}} \begin{document} \raggedright \raggedcolumns % Set font size to small. Switch to any value % from this page to resize cheat sheet text: % www.emerson.emory.edu/services/latex/latex_169.html \footnotesize % Small font. \begin{tabularx}{17.67cm}{X} \SetRowColor{DarkBackground} \mymulticolumn{1}{x{17.67cm}}{\bf\textcolor{white}{Solidity v.0.5.2-0.6.0.}} \tn \SetRowColor{white} \mymulticolumn{1}{x{17.67cm}}{{\bf{\#Import files}} \newline % Row Count 1 (+ 1) import "filename"; \newline % Row Count 2 (+ 1) import * as symbolName from "filename"; or import "filename" as symbolName; \newline % Row Count 4 (+ 2) import \{symbol1 as alias, symbol2\} from "filename"; \newline % Row Count 6 (+ 2) {\bf{\#Types}} \newline % Row Count 7 (+ 1) Boolean \newline % Row Count 8 (+ 1) bool : true or false \newline % Row Count 9 (+ 1) {\bf{\#Operators:}} \newline % Row Count 10 (+ 1) Logical : ! (logical negation), \&\& (AND), || (OR) \newline % Row Count 12 (+ 2) Comparisons : == (equality), != (inequality) \newline % Row Count 13 (+ 1) Integer \newline % Row Count 14 (+ 1) Unsigned : uint8 | uint16 | uint32 | uint64 | uint128 | uint256(uint) \newline % Row Count 16 (+ 2) Signed : int8 | int16 | int32 | int64 | int128 | int256(int) \newline % Row Count 18 (+ 2) \#Operators: \newline % Row Count 19 (+ 1) Comparisons: \textless{}=, \textless{}, ==, !=, \textgreater{}= and \textgreater{} \newline % Row Count 20 (+ 1) Bit operators: \&, |, \textasciicircum{} (bitwise exclusive or) and \textasciitilde{} (bitwise negation) \newline % Row Count 22 (+ 2) Arithmetic operators: +, -, unary -, unary +, {\emph{, /, \%, }}* (exponentiation), \textless{}\textless{} (left shift) and \textgreater{}\textgreater{} (right shift) \newline % Row Count 25 (+ 3) \#Address \newline % Row Count 26 (+ 1) address: Holds an Ethereum address (20 byte value). address payable : Same as address, but includes additional methods transfer and send \newline % Row Count 29 (+ 3) \#Operators: \newline % Row Count 30 (+ 1) } \tn \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{17.67cm}{X} \SetRowColor{DarkBackground} \mymulticolumn{1}{x{17.67cm}}{\bf\textcolor{white}{Solidity v.0.5.2-0.6.0. (cont)}} \tn \SetRowColor{white} \mymulticolumn{1}{x{17.67cm}}{ Comparisons: \textless{}=, \textless{}, ==, !=, \textgreater{}= and \textgreater{} \newline % Row Count 1 (+ 1) {\bf{\#Methods:}} \newline % Row Count 2 (+ 1) {\bf{balance}} \newline % Row Count 3 (+ 1) \textless{}address\textgreater{}.balance (uint256): balance of the Address in Wei \newline % Row Count 5 (+ 2) {\bf{transfer and send}} \newline % Row Count 6 (+ 1) \textless{}address\textgreater{}.transfer(uint256 amount): send given amount of Wei to Address, throws on failure \newline % Row Count 8 (+ 2) \textless{}address\textgreater{}.send(uint256 amount) returns (bool): send given amount of Wei to Address, returns false on failure \newline % Row Count 11 (+ 3) {\bf{call}} \newline % Row Count 12 (+ 1) \textless{}address\textgreater{}.call(...) returns (bool): issue low-level CALL, returns false on failure \newline % Row Count 14 (+ 2) {\bf{delegatecall}} \newline % Row Count 15 (+ 1) \textless{}address\textgreater{}.delegatecall(...) returns (bool): issue low-level DELEGATECALL, returns false on failure \newline % Row Count 18 (+ 3) Delegatecall uses the code of the target address, taking all other aspects (storage, balance, ...) from the calling contract. The purpose of delegatecall is to use library code which is stored in another contract. The user has to ensure that the layout of storage in both contracts is suitable for delegatecall to be used. \newline % Row Count 25 (+ 7) contract A \{ \newline % Row Count 26 (+ 1) uint value; \newline % Row Count 27 (+ 1) address public sender; \newline % Row Count 28 (+ 1) address a = address(0); // address of contract B \newline % Row Count 30 (+ 2) } \tn \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{17.67cm}{X} \SetRowColor{DarkBackground} \mymulticolumn{1}{x{17.67cm}}{\bf\textcolor{white}{Solidity v.0.5.2-0.6.0. (cont)}} \tn \SetRowColor{white} \mymulticolumn{1}{x{17.67cm}}{ function makeDelegateCall(uint \_value) public \{ \newline % Row Count 1 (+ 1) a.delegatecall( \newline % Row Count 2 (+ 1) \seqsplit{abi.encodePacked(bytes4(keccak256("setValue(uint)"))}, \_value) \newline % Row Count 4 (+ 2) ); // Value of A is modified \newline % Row Count 5 (+ 1) \} \newline % Row Count 6 (+ 1) \} \newline % Row Count 7 (+ 1) contract B \{ \newline % Row Count 8 (+ 1) uint value; \newline % Row Count 9 (+ 1) address public sender; \newline % Row Count 10 (+ 1) function setValue(uint \_value) public \{ \newline % Row Count 11 (+ 1) value = \_value; \newline % Row Count 12 (+ 1) sender = msg.sender; // msg.sender is preserved in delegatecall. It was not available in callcode. \newline % Row Count 15 (+ 3) \} \newline % Row Count 16 (+ 1) \} \newline % Row Count 17 (+ 1) gas() option is available for call, callcode and delegatecall. value() option is not supported for delegatecall. \newline % Row Count 20 (+ 3) {\bf{callcode}} \newline % Row Count 21 (+ 1) \textless{}address\textgreater{}.callcode(...) returns (bool): issue low-level CALLCODE, returns false on failure \newline % Row Count 23 (+ 2) Prior to homestead, only a limited variant called callcode was available that did not provide access to the original msg.sender and msg.value values. \newline % Row Count 27 (+ 4) {\bf{\#Array}} \newline % Row Count 28 (+ 1) Arrays can be dynamic or have a fixed size. \newline % Row Count 29 (+ 1) uint{[}{]} dynamicSizeArray; \newline % Row Count 30 (+ 1) } \tn \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{17.67cm}{X} \SetRowColor{DarkBackground} \mymulticolumn{1}{x{17.67cm}}{\bf\textcolor{white}{Solidity v.0.5.2-0.6.0. (cont)}} \tn \SetRowColor{white} \mymulticolumn{1}{x{17.67cm}}{uint{[}7{]} fixedSizeArray; \newline % Row Count 1 (+ 1) {\bf{Fixed byte arrays}} \newline % Row Count 2 (+ 1) bytes1(byte), bytes2, bytes3, ..., bytes32. \newline % Row Count 3 (+ 1) {\bf{\#Operators:}} \newline % Row Count 4 (+ 1) Comparisons: \textless{}=, \textless{}, ==, !=, \textgreater{}=, \textgreater{} (evaluate to bool) Bit operators: \&, |, \textasciicircum{} (bitwise exclusive or), \textasciitilde{} (bitwise negation), \textless{}\textless{} (left shift), \textgreater{}\textgreater{} (right shift) Index access: If x is of type bytesI, then x{[}k{]} for 0 \textless{}= k \textless{} I returns the k th byte (read-only). \newline % Row Count 10 (+ 6) {\bf{\#Members}} \newline % Row Count 11 (+ 1) .length : read-only \newline % Row Count 12 (+ 1) {\bf{\#Dynamic byte arrays}} \newline % Row Count 13 (+ 1) bytes: Dynamically-sized byte array. It is similar to byte{[}{]}, but it is packed tightly in calldata. Not a value-type! \newline % Row Count 16 (+ 3) string: Dynamically-sized UTF-8-encoded string. It is equal to bytes but does not allow length or index access. Not a value-type! \newline % Row Count 19 (+ 3) Enum \newline % Row Count 20 (+ 1) {\bf{Enum}} works just like in every other language. \newline % Row Count 21 (+ 1) enum ActionChoices \{ \newline % Row Count 22 (+ 1) GoLeft, \newline % Row Count 23 (+ 1) GoRight, \newline % Row Count 24 (+ 1) GoStraight, \newline % Row Count 25 (+ 1) SitStill \newline % Row Count 26 (+ 1) \} \newline % Row Count 27 (+ 1) ActionChoices choice = \seqsplit{ActionChoices.GoStraight;} \newline % Row Count 28 (+ 1) {\bf{\#Struct}} \newline % Row Count 29 (+ 1) New types can be declared using struct. \newline % Row Count 30 (+ 1) } \tn \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{17.67cm}{X} \SetRowColor{DarkBackground} \mymulticolumn{1}{x{17.67cm}}{\bf\textcolor{white}{Solidity v.0.5.2-0.6.0. (cont)}} \tn \SetRowColor{white} \mymulticolumn{1}{x{17.67cm}}{struct Funder \{ \newline % Row Count 1 (+ 1) address addr; \newline % Row Count 2 (+ 1) uint amount; \newline % Row Count 3 (+ 1) \} \newline % Row Count 4 (+ 1) Funder funders; \newline % Row Count 5 (+ 1) {\bf{\#Mapping}} \newline % Row Count 6 (+ 1) Declared as mapping(\_KeyType =\textgreater{} \_ValueType) \newline % Row Count 7 (+ 1) Mappings can be seen as hash tables which are virtually initialized such that every possible key exists and is mapped to a value. \newline % Row Count 10 (+ 3) key can be almost any type except for a mapping, a dynamically sized array, a contract, an enum, or a struct. value can actually be any type, including mappings. \newline % Row Count 14 (+ 4) Control Structures \newline % Row Count 15 (+ 1) {\bf{Most of the control structures from JavaScript are available in Solidity except for switch and goto.}} \newline % Row Count 18 (+ 3) if else \newline % Row Count 19 (+ 1) while \newline % Row Count 20 (+ 1) do \newline % Row Count 21 (+ 1) for \newline % Row Count 22 (+ 1) break \newline % Row Count 23 (+ 1) continue \newline % Row Count 24 (+ 1) return \newline % Row Count 25 (+ 1) ? : \newline % Row Count 26 (+ 1) {\bf{\#Functions}} \newline % Row Count 27 (+ 1) {\bf{Structure}} \newline % Row Count 28 (+ 1) function (\textless{}parameter types\textgreater{}) \{internal|external|public|private\} {[}pure|constant|view|payable{]} {[}returns (\textless{}return types\textgreater{}){]} \newline % Row Count 31 (+ 3) } \tn \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{17.67cm}{X} \SetRowColor{DarkBackground} \mymulticolumn{1}{x{17.67cm}}{\bf\textcolor{white}{Solidity v.0.5.2-0.6.0. (cont)}} \tn \SetRowColor{white} \mymulticolumn{1}{x{17.67cm}}{{\bf{Access modifiers}} \newline % Row Count 1 (+ 1) public - Accessible from this contract, inherited contracts and externally \newline % Row Count 3 (+ 2) private - Accessible only from this contract \newline % Row Count 4 (+ 1) internal - Accessible only from this contract and contracts inheriting from it \newline % Row Count 6 (+ 2) external - Cannot be accessed internally, only externally. Recommended to reduce gas. Access internally with this.f. \newline % Row Count 9 (+ 3) {\bf{\#Parameters}} \newline % Row Count 10 (+ 1) {\bf{Input parameters}} \newline % Row Count 11 (+ 1) Parameters are declared just like variables and are memory variables. \newline % Row Count 13 (+ 2) function f(uint \_a, uint \_b) \{\} \newline % Row Count 14 (+ 1) {\bf{Output parameters}} \newline % Row Count 15 (+ 1) Output parameters are declared after the returns keyword \newline % Row Count 17 (+ 2) function f(uint \_a, uint \_b) returns (uint \_sum) \{ \newline % Row Count 19 (+ 2) \_sum = \_a + \_b; \newline % Row Count 20 (+ 1) \} \newline % Row Count 21 (+ 1) Output can also be specified using return statement. In that case, we can omit parameter name returns (uint). \newline % Row Count 24 (+ 3) Multiple return types are possible with return (v0, v1, ..., vn). \newline % Row Count 26 (+ 2) {\bf{Constructor}} \newline % Row Count 27 (+ 1) Function that is executed during contract deployment. Defined using the constructor keyword. \newline % Row Count 29 (+ 2) contract C \{ \newline % Row Count 30 (+ 1) } \tn \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{17.67cm}{X} \SetRowColor{DarkBackground} \mymulticolumn{1}{x{17.67cm}}{\bf\textcolor{white}{Solidity v.0.5.2-0.6.0. (cont)}} \tn \SetRowColor{white} \mymulticolumn{1}{x{17.67cm}}{ address owner; \newline % Row Count 1 (+ 1) uint status; \newline % Row Count 2 (+ 1) constructor(uint \_status) \{ \newline % Row Count 3 (+ 1) owner = msg.sender; \newline % Row Count 4 (+ 1) status = \_status; \newline % Row Count 5 (+ 1) \} \newline % Row Count 6 (+ 1) \} \newline % Row Count 7 (+ 1) {\bf{\#Function Calls}} \newline % Row Count 8 (+ 1) {\bf{Internal Function Calls}} \newline % Row Count 9 (+ 1) Functions of the current contract can be called directly (internally - via jumps) and also recursively \newline % Row Count 12 (+ 3) contract C \{ \newline % Row Count 13 (+ 1) function funA() returns (uint) \{ \newline % Row Count 14 (+ 1) return 5; \newline % Row Count 15 (+ 1) \} \newline % Row Count 16 (+ 1) function FunB(uint \_a) returns (uint ret) \{ \newline % Row Count 17 (+ 1) return funA() + \_a; \newline % Row Count 18 (+ 1) \} \newline % Row Count 19 (+ 1) \} \newline % Row Count 20 (+ 1) {\bf{\#External Function Calls}} \newline % Row Count 21 (+ 1) this.g(8); and c.g(2); (where c is a contract instance) are also valid function calls, but, the function will be called "externally", via a message call. \newline % Row Count 25 (+ 4) .gas() and .value() can also be used with external function calls. \newline % Row Count 27 (+ 2) {\bf{\#Named Calls}} \newline % Row Count 28 (+ 1) Function call arguments can also be given by name in any order as below. \newline % Row Count 30 (+ 2) } \tn \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{17.67cm}{X} \SetRowColor{DarkBackground} \mymulticolumn{1}{x{17.67cm}}{\bf\textcolor{white}{Solidity v.0.5.2-0.6.0. (cont)}} \tn \SetRowColor{white} \mymulticolumn{1}{x{17.67cm}}{function f(uint a, uint b) \{ \} \newline % Row Count 1 (+ 1) function g() \{ \newline % Row Count 2 (+ 1) f(\{b: 1, a: 2\}); \newline % Row Count 3 (+ 1) \} \newline % Row Count 4 (+ 1) {\bf{Unnamed function parameters}} \newline % Row Count 5 (+ 1) Parameters will be present on the stack, but are not accessible. \newline % Row Count 7 (+ 2) function f(uint a, uint) returns (uint) \{ \newline % Row Count 8 (+ 1) return a; \newline % Row Count 9 (+ 1) \} \newline % Row Count 10 (+ 1) {\bf{\#Function type}} \newline % Row Count 11 (+ 1) Pass function as a parameter to another function. Similar to callbacks and delegates \newline % Row Count 13 (+ 2) pragma solidity \textasciicircum{}0.4.18; \newline % Row Count 14 (+ 1) contract Oracle \{ \newline % Row Count 15 (+ 1) struct Request \{ \newline % Row Count 16 (+ 1) bytes data; \newline % Row Count 17 (+ 1) function(bytes memory) external callback; \newline % Row Count 18 (+ 1) \} \newline % Row Count 19 (+ 1) Request{[}{]} requests; \newline % Row Count 20 (+ 1) event NewRequest(uint); \newline % Row Count 21 (+ 1) function query(bytes data, function(bytes memory) external callback) \{ \newline % Row Count 23 (+ 2) \seqsplit{requests.push(Request(data}, callback)); \newline % Row Count 24 (+ 1) \seqsplit{NewRequest(requests.length} - 1); \newline % Row Count 25 (+ 1) \} \newline % Row Count 26 (+ 1) function reply(uint requestID, bytes response) \{ \newline % Row Count 28 (+ 2) // Here goes the check that the reply comes from a trusted source \newline % Row Count 30 (+ 2) } \tn \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{17.67cm}{X} \SetRowColor{DarkBackground} \mymulticolumn{1}{x{17.67cm}}{\bf\textcolor{white}{Solidity v.0.5.2-0.6.0. (cont)}} \tn \SetRowColor{white} \mymulticolumn{1}{x{17.67cm}}{ requests{[}requestID{]}.callback(response); \newline % Row Count 1 (+ 1) \} \newline % Row Count 2 (+ 1) \} \newline % Row Count 3 (+ 1) contract OracleUser \{ \newline % Row Count 4 (+ 1) Oracle constant oracle = Oracle(0x1234567); // known contract \newline % Row Count 6 (+ 2) function buySomething() \{ \newline % Row Count 7 (+ 1) oracle.query("USD", this.oracleResponse); \newline % Row Count 8 (+ 1) \} \newline % Row Count 9 (+ 1) function oracleResponse(bytes response) \{ \newline % Row Count 10 (+ 1) require(msg.sender == address(oracle)); \newline % Row Count 11 (+ 1) \} \newline % Row Count 12 (+ 1) \} \newline % Row Count 13 (+ 1) {\bf{\#Function Modifier}} \newline % Row Count 14 (+ 1) Modifiers can automatically check a condition prior to executing the function. \newline % Row Count 16 (+ 2) modifier onlyOwner \{ \newline % Row Count 17 (+ 1) require(msg.sender == owner); \newline % Row Count 18 (+ 1) \_; \newline % Row Count 19 (+ 1) \} \newline % Row Count 20 (+ 1) function close() onlyOwner \{ \newline % Row Count 21 (+ 1) selfdestruct(owner); \newline % Row Count 22 (+ 1) \} \newline % Row Count 23 (+ 1) {\bf{\#View or Constant Functions}} \newline % Row Count 24 (+ 1) Functions can be declared view or constant in which case they promise not to modify the state, but can read from them. \newline % Row Count 27 (+ 3) function f(uint a) view returns (uint) \{ \newline % Row Count 28 (+ 1) return a * b; // where b is a storage variable \newline % Row Count 30 (+ 2) } \tn \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{17.67cm}{X} \SetRowColor{DarkBackground} \mymulticolumn{1}{x{17.67cm}}{\bf\textcolor{white}{Solidity v.0.5.2-0.6.0. (cont)}} \tn \SetRowColor{white} \mymulticolumn{1}{x{17.67cm}}{\} \newline % Row Count 1 (+ 1) {\bf{The compiler does not enforce yet that a view method is not modifying state.}} \newline % Row Count 3 (+ 2) {\bf{\#Pure Functions}} \newline % Row Count 4 (+ 1) Functions can be declared pure in which case they promise not to read from or modify the state. \newline % Row Count 6 (+ 2) function f(uint a) pure returns (uint) \{ \newline % Row Count 7 (+ 1) return a * 42; \newline % Row Count 8 (+ 1) \} \newline % Row Count 9 (+ 1) {\bf{\#Payable Functions}} \newline % Row Count 10 (+ 1) Functions that receive Ether are marked as payable function. \newline % Row Count 12 (+ 2) {\bf{Fallback Function}} \newline % Row Count 13 (+ 1) A contract can have exactly one unnamed function. This function cannot have arguments and cannot return anything. It is executed on a call to the contract if none of the other functions match the given function identifier (or if no data was supplied at all). \newline % Row Count 19 (+ 6) function() \{ \newline % Row Count 20 (+ 1) // Do something \newline % Row Count 21 (+ 1) \} \newline % Row Count 22 (+ 1) {\bf{\#Contracts}} \newline % Row Count 23 (+ 1) Creating contracts using new \newline % Row Count 24 (+ 1) Contracts can be created from another contract using new keyword. The source of the contract has to be known in advance. \newline % Row Count 27 (+ 3) contract A \{ \newline % Row Count 28 (+ 1) function add(uint \_a, uint \_b) returns (uint) \{ \newline % Row Count 30 (+ 2) } \tn \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{17.67cm}{X} \SetRowColor{DarkBackground} \mymulticolumn{1}{x{17.67cm}}{\bf\textcolor{white}{Solidity v.0.5.2-0.6.0. (cont)}} \tn \SetRowColor{white} \mymulticolumn{1}{x{17.67cm}}{ return \_a + \_b; \newline % Row Count 1 (+ 1) \} \newline % Row Count 2 (+ 1) \} \newline % Row Count 3 (+ 1) contract C \{ \newline % Row Count 4 (+ 1) address a; \newline % Row Count 5 (+ 1) function f(uint \_a) \{ \newline % Row Count 6 (+ 1) a = new A(); \newline % Row Count 7 (+ 1) \} \newline % Row Count 8 (+ 1) \} \newline % Row Count 9 (+ 1) {\bf{\#Contract Inheritance}} \newline % Row Count 10 (+ 1) Solidity supports multiple inheritance and polymorphism. \newline % Row Count 12 (+ 2) contract owned \{ \newline % Row Count 13 (+ 1) function owned() \{ owner = msg.sender; \} \newline % Row Count 14 (+ 1) address owner; \newline % Row Count 15 (+ 1) \} \newline % Row Count 16 (+ 1) contract mortal is owned \{ \newline % Row Count 17 (+ 1) function kill() \{ \newline % Row Count 18 (+ 1) if (msg.sender == owner) selfdestruct(owner); \newline % Row Count 20 (+ 2) \} \newline % Row Count 21 (+ 1) \} \newline % Row Count 22 (+ 1) contract final is mortal \{ \newline % Row Count 23 (+ 1) function kill() \{ \newline % Row Count 24 (+ 1) super.kill(); // Calls kill() of mortal. \newline % Row Count 25 (+ 1) \} \newline % Row Count 26 (+ 1) \} \newline % Row Count 27 (+ 1) {\bf{Multiple inheritance}} \newline % Row Count 28 (+ 1) contract A \{\} \newline % Row Count 29 (+ 1) contract B \{\} \newline % Row Count 30 (+ 1) } \tn \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{17.67cm}{X} \SetRowColor{DarkBackground} \mymulticolumn{1}{x{17.67cm}}{\bf\textcolor{white}{Solidity v.0.5.2-0.6.0. (cont)}} \tn \SetRowColor{white} \mymulticolumn{1}{x{17.67cm}}{contract C is A, B \{\} \newline % Row Count 1 (+ 1) {\bf{\#Constructor of base class}} \newline % Row Count 2 (+ 1) contract A \{ \newline % Row Count 3 (+ 1) uint a; \newline % Row Count 4 (+ 1) constructor(uint \_a) \{ a = \_a; \} \newline % Row Count 5 (+ 1) \} \newline % Row Count 6 (+ 1) contract B is A(1) \{ \newline % Row Count 7 (+ 1) constructor(uint \_b) A(\_b) \{ \newline % Row Count 8 (+ 1) \} \newline % Row Count 9 (+ 1) \} \newline % Row Count 10 (+ 1) {\bf{\#Abstract Contracts}} \newline % Row Count 11 (+ 1) Contracts that contain implemented and non-implemented functions. Such contracts cannot be compiled, but they can be used as base contracts. \newline % Row Count 14 (+ 3) pragma solidity \textasciicircum{}0.4.0; \newline % Row Count 15 (+ 1) contract A \{ \newline % Row Count 16 (+ 1) function C() returns (bytes32); \newline % Row Count 17 (+ 1) \} \newline % Row Count 18 (+ 1) contract B is A \{ \newline % Row Count 19 (+ 1) function C() returns (bytes32) \{ return "c"; \} \newline % Row Count 21 (+ 2) \} \newline % Row Count 22 (+ 1) {\bf{\#Interface}} \newline % Row Count 23 (+ 1) Interfaces are similar to abstract contracts, but they have restrictions: \newline % Row Count 25 (+ 2) Cannot have any functions implemented. \newline % Row Count 26 (+ 1) Cannot inherit other contracts or interfaces. \newline % Row Count 27 (+ 1) Cannot define constructor. \newline % Row Count 28 (+ 1) Cannot define variables. \newline % Row Count 29 (+ 1) Cannot define structs. \newline % Row Count 30 (+ 1) } \tn \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{17.67cm}{X} \SetRowColor{DarkBackground} \mymulticolumn{1}{x{17.67cm}}{\bf\textcolor{white}{Solidity v.0.5.2-0.6.0. (cont)}} \tn \SetRowColor{white} \mymulticolumn{1}{x{17.67cm}}{ Cannot define enums. \newline % Row Count 1 (+ 1) pragma solidity \textasciicircum{}0.4.11; \newline % Row Count 2 (+ 1) interface Token \{ \newline % Row Count 3 (+ 1) function transfer(address recipient, uint amount); \newline % Row Count 5 (+ 2) \} \newline % Row Count 6 (+ 1) {\bf{\#Events}} \newline % Row Count 7 (+ 1) Events allow the convenient usage of the EVM logging facilities, which in turn can be used to "call" JavaScript callbacks in the user interface of a dapp, which listen for these events. \newline % Row Count 11 (+ 4) Up to three parameters can receive the attribute indexed, which will cause the respective arguments to be searched for. \newline % Row Count 14 (+ 3) All non-indexed arguments will be stored in the data part of the log. \newline % Row Count 16 (+ 2) pragma solidity \textasciicircum{}0.4.0; \newline % Row Count 17 (+ 1) contract ClientReceipt \{ \newline % Row Count 18 (+ 1) event Deposit( \newline % Row Count 19 (+ 1) address indexed \_from, \newline % Row Count 20 (+ 1) bytes32 indexed \_id, \newline % Row Count 21 (+ 1) uint \_value \newline % Row Count 22 (+ 1) ); \newline % Row Count 23 (+ 1) function deposit(bytes32 \_id) payable \{ \newline % Row Count 24 (+ 1) emit Deposit(msg.sender, \_id, msg.value); \newline % Row Count 25 (+ 1) \} \newline % Row Count 26 (+ 1) \} \newline % Row Count 27 (+ 1) {\bf{\#Library}} \newline % Row Count 28 (+ 1) Libraries are similar to contracts, but they are deployed only once at a specific address, and their code is used with delegatecall (callcode). \newline % Row Count 31 (+ 3) } \tn \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{17.67cm}{X} \SetRowColor{DarkBackground} \mymulticolumn{1}{x{17.67cm}}{\bf\textcolor{white}{Solidity v.0.5.2-0.6.0. (cont)}} \tn \SetRowColor{white} \mymulticolumn{1}{x{17.67cm}}{library arithmatic \{ \newline % Row Count 1 (+ 1) function add(uint \_a, uint \_b) returns (uint) \{ \newline % Row Count 3 (+ 2) return \_a + \_b; \newline % Row Count 4 (+ 1) \} \newline % Row Count 5 (+ 1) \} \newline % Row Count 6 (+ 1) contract C \{ \newline % Row Count 7 (+ 1) uint sum; \newline % Row Count 8 (+ 1) function f() \{ \newline % Row Count 9 (+ 1) sum = arithmatic.add(2, 3); \newline % Row Count 10 (+ 1) \} \newline % Row Count 11 (+ 1) \} \newline % Row Count 12 (+ 1) {\bf{\#Using - For}} \newline % Row Count 13 (+ 1) using A for B; can be used to attach library functions to any type. \newline % Row Count 15 (+ 2) library arithmatic \{ \newline % Row Count 16 (+ 1) function add(uint \_a, uint \_b) returns (uint) \{ \newline % Row Count 18 (+ 2) return \_a + \_b; \newline % Row Count 19 (+ 1) \} \newline % Row Count 20 (+ 1) \} \newline % Row Count 21 (+ 1) contract C \{ \newline % Row Count 22 (+ 1) using arithmatic for uint; \newline % Row Count 23 (+ 1) uint sum; \newline % Row Count 24 (+ 1) function f(uint \_a) \{ \newline % Row Count 25 (+ 1) sum = \_a.add(3); \newline % Row Count 26 (+ 1) \} \newline % Row Count 27 (+ 1) \} \newline % Row Count 28 (+ 1) {\bf{\#Error Handling}} \newline % Row Count 29 (+ 1) assert(bool condition): throws if the condition is not met - to be used for internal errors. \newline % Row Count 31 (+ 2) } \tn \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{17.67cm}{X} \SetRowColor{DarkBackground} \mymulticolumn{1}{x{17.67cm}}{\bf\textcolor{white}{Solidity v.0.5.2-0.6.0. (cont)}} \tn \SetRowColor{white} \mymulticolumn{1}{x{17.67cm}}{ require(bool condition): throws if the condition is not met - to be used for errors in inputs or external components. \newline % Row Count 3 (+ 3) revert(): abort execution and revert state changes \newline % Row Count 5 (+ 2) function sendHalf(address addr) payable returns (uint balance) \{ \newline % Row Count 7 (+ 2) require(msg.value \% 2 == 0); // Only allow even numbers \newline % Row Count 9 (+ 2) uint balanceBeforeTransfer = this.balance; \newline % Row Count 10 (+ 1) addr.transfer(msg.value / 2); \newline % Row Count 11 (+ 1) assert(this.balance == balanceBeforeTransfer - msg.value / 2); \newline % Row Count 13 (+ 2) return this.balance; \newline % Row Count 14 (+ 1) \} \newline % Row Count 15 (+ 1) {\bf{Catching exceptions is not yet possible.}} \newline % Row Count 16 (+ 1) {\bf{\#Global variables}} \newline % Row Count 17 (+ 1) {\bf{Block variables}} \newline % Row Count 18 (+ 1) block.blockhash(uint blockNumber) returns (bytes32): hash of the given block - only works for the 256 most recent blocks excluding current \newline % Row Count 21 (+ 3) block.coinbase (address): current block miner's address \newline % Row Count 23 (+ 2) block.difficulty (uint): current block difficulty \newline % Row Count 25 (+ 2) block.gaslimit (uint): current block gaslimit \newline % Row Count 26 (+ 1) block.number (uint): current block number \newline % Row Count 27 (+ 1) block.timestamp (uint): current block timestamp as seconds since unix epoch \newline % Row Count 29 (+ 2) now (uint): current block timestamp (alias for block.timestamp) \newline % Row Count 31 (+ 2) } \tn \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{17.67cm}{X} \SetRowColor{DarkBackground} \mymulticolumn{1}{x{17.67cm}}{\bf\textcolor{white}{Solidity v.0.5.2-0.6.0. (cont)}} \tn \SetRowColor{white} \mymulticolumn{1}{x{17.67cm}}{{\bf{\#Transaction variables}} \newline % Row Count 1 (+ 1) msg.data (bytes): complete calldata \newline % Row Count 2 (+ 1) msg.gas (uint): remaining gas \newline % Row Count 3 (+ 1) msg.sender (address): sender of the message (current call) \newline % Row Count 5 (+ 2) msg.sig (bytes4): first four bytes of the calldata (i.e. function identifier) \newline % Row Count 7 (+ 2) msg.value (uint): number of wei sent with the message \newline % Row Count 9 (+ 2) tx.gasprice (uint): gas price of the transaction \newline % Row Count 11 (+ 2) tx.origin (address): sender of the transaction (full call chain) \newline % Row Count 13 (+ 2) {\bf{\#Mathematical and Cryptographic Functions}} \newline % Row Count 14 (+ 1) addmod(uint x, uint y, uint k) returns (uint): compute (x + y) \% k where the addition is performed with arbitrary precision and does not wrap around at 2**256. \newline % Row Count 18 (+ 4) mulmod(uint x, uint y, uint k) returns (uint): compute (x {\emph{ y) \% k where the multiplication is performed with arbitrary precision and does not wrap around at 2}}*256. \newline % Row Count 22 (+ 4) keccak256(...) returns (bytes32): compute the Ethereum-SHA-3 (Keccak-256) hash of the (tightly packed) arguments \newline % Row Count 25 (+ 3) sha256(...) returns (bytes32): compute the SHA-256 hash of the (tightly packed) arguments \newline % Row Count 27 (+ 2) sha3(...) returns (bytes32): alias to keccak256 \newline % Row Count 29 (+ 2) ripemd160(...) returns (bytes20): compute RIPEMD-160 hash of the (tightly packed) arguments \newline % Row Count 31 (+ 2) } \tn \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{17.67cm}{X} \SetRowColor{DarkBackground} \mymulticolumn{1}{x{17.67cm}}{\bf\textcolor{white}{Solidity v.0.5.2-0.6.0. (cont)}} \tn \SetRowColor{white} \mymulticolumn{1}{x{17.67cm}}{ ecrecover(bytes32 hash, uint8 v, bytes32 r, bytes32 s) returns (address): recover the address associated with the public key from elliptic curve signature or return zero on error (example usage) \newline % Row Count 4 (+ 4) {\bf{\#Contract Related}} \newline % Row Count 5 (+ 1) this (current contract's type): the current contract, explicitly convertible to Address \newline % Row Count 7 (+ 2) selfdestruct(address recipient): destroy the current contract, sending its funds to the given Address \newline % Row Count 10 (+ 3) suicide(address recipient): alias to selfdestruct. Soon to be deprecated.% Row Count 12 (+ 2) } \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \SetRowColor{LightBackground} \mymulticolumn{1}{x{17.67cm}}{Content reference: \seqsplit{https://github.com/manojpramesh/solidity-cheatsheet}} \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \end{tabularx} \par\addvspace{1.3em} \end{document}