A Guide to Solidity
Solidity is an object-oriented programming language currently used to write smart contracts on different blockchain platforms, including Ethereum. First proposed by Gavin Wood in 2014, it was later developed by Christian Reitwiessner and some of the core Ethereum contributors.
That means if you can use C++, Python, Perl, Java, and other modern programming languages, it will be relatively easy for you to learn Solidity because it contains programming elements found in other languages. These similarities include variables, arithmetic operations, functions, classes, string manipulations, and more.
For example, if the C++ language allows you to create some code for “main function”, Solidity creates a “contract” in a similar process.
Since Solidity is a programming language designed specifically for writing smart contracts, it leaves an official record of transactions that cannot be repudiated. Programmers who are using it to write applications should keep the rules of the EVM in mind.
Solidity is the most popular of the programming languages used on the EVM (the others are LLL, Viper, Serpent, and Mutan). Some of its significant advantages compared to the other Ethereum programming languages are:
– It supports complex member variables in smart contracts. Member variables are used in object-oriented programming and are associated with a specific object. Examples of these complex member variables are hierarchical structures and mappings.
– It enables contracts to support inheritance. It is a mechanism in object-oriented programming that bases an object on another object or a class on another class. Inheritance in programming is similar to genetic inheritance: Just as a child inherits the traits of his parents, through inheritance, the programmer reuses the methods and fields of the existing class.
– It has an Application Binary Interface (ABI), an interface between two program modules. One of these program modules is at the level of machine code. In Ethereum, Solidity supports an ABI that facilitates multiple type-safe functions within a single contract. Type safety refers to the process in which the Solidity command line compiler validates and deletes datatypes. If the programmer assigns a wrong datatype to a variable, the compiler will delete the error.
Solidity and Smart Contracts
A contract is a function – collection of code and data – in the Ethereum blockchain, written in Solidity and compiled into JSON or binary. After the compilation, the contract is sent to a specific address in the blockchain. Whoever wants to use the deployed contract just submits the correct data along with the Ether needed to pay for it.
Smart contract functions can be used to define conditions that all the parties using the contract agree upon. Smart contracts enable parties to deal and communicate with each other directly, eliminating the need for a middleman.
This feature gives developers vast opportunities to create things, such as new markets or registries of debts. All of these are possible as long as there is a consensus in the network that will validate all transactions.
Anyone can send a smart contract into the decentralized network, provided they pay a fee, which is proportional to the storage size of the code.
Ethereum then stores the items in one of three places.
– Storage – the place where all the contract’s state variables can be found. A state variable is a set of variable that describes the mathematical state of a dynamic system. Each contract includes a storage which persists between function calls.
– Memory – where temporary values are placed and erased between function calls. It is, therefore, advisable to use memory to store everything that is only temporary.
– Stack – it works similarly to memory and is almost free. However, the stack has a minimum of 1024 items but only the top 16 items are easily accessible.
Solidity has four value types – types that are always passed by a value. This means that these types are always copied when utilized in assignments or for arguments. Furthermore, these types can interact with each other in expressions with operators.
Operators are also divided into four categories – arithmetic, incremental, bitwise, and logical.
As the name suggests, these are math operations, like those used in most programming languages. Solidity also has an exponential operator in this category.
– Addition: x + y
– Subtraction: x – y
– Division: x / y
– Multiplication: x * y
– Modulo/remainder: x%y
In Solidity, incremental operators are:
– Bitwise AND – &
– Bitwise inclusive OR – |
– Bitwise XOR (exclusive OR) ^
– Bitwise NOT – ~
– Bitwise right shift – >>
– Bitwise left shift – <<
– Logical negation !
– Logical AND &&
– Logical OR ||
– Equality = =
– Not equal ! =
Solidity Value Types
This value type uses the keyword “bool,” and its two possible values are constants: “true” and “false.” This value type uses logical operators, with the logical OR (| |) and the logical AND (& &) applying the common short-circuiting rules.
For example, in the expression m(x) | | n(y), if m(x) evaluates to true, n(y) will not be assessed whether it has a positive or negative side effect.
This value type uses the keywords “int” and “uint” – signed and unsigned integers, respectively. An unsigned integer can have a large positive value while a signed integer identifies whether the number is positive or negative using the left-most-bit. Signed integers can also hold numbers with both positive and negative values.
These integers can have different values; thus, if you see keywords like uint8 to uint256 in steps of 8, that means they are unsigned integers of 8 up to 256 bits. Integers use bitwise and arithmetic operators as well as comparisons and shift operators.
This value type uses the keyword “address” and has two very similar formats:
Address – this has a 20-byte value size, which is the size of an Ethereum address.
Address payable – the same as the “address” format but uses the “transfer” and “send” functions.
These values are written using either single or double quotes: ‘foo’ or “bar.” They do not imply trailing zeroes like the C++ language. Strings literals use the backslash symbol to escape special characters like the ones below:
– Backslash \\
– Backspace \b
– Escapes an actual newline \<newline>
– Carriage return \r
– Double quote \”
– Form feed \f
– Hex escape, see below \xNN
– Newline \n
– Single quote \’
– Tab \t
– Vertical tab \v
– Unicode escape, \uNNN