Solidity Signed & Unsigned Integers (Syntax + Code Examples)

Integers are whole numbers that include zero, with signed integers (int) allowing negatives and unsigned integers (uint) only allowing positives. Integers can be declared in a range of sizes from 8-bit to 256-bit, with 256-bit being the default size. Integers are a simple data type used extensively in Solidity to represent whole numbers. They are … Read more

Solidity Lessons

These Solidity Lessons are free to read, use, and share. If you found these helpful, my only ask is that you view more pages for the algorithm and keep learning! 1 Introduction to Solidity Imagine you have a digital notepad that nobody can erase or change, not even you once you’ve written something on it. … Read more

Solidity Inheritance

The Solidity programming language provides the use of inheritance, which means smart contracts can be built using other contract’s code. Here is a simple example of a contract inheriting two other contracts: To inherit another contract in Solidity, when defining the current contract, you simply add the “is” keyword with a comma-separated list of the … Read more

Data Locations (Storage, Memory, Calldata)

In Solidity, there are 3 locations for data to be stored. Storage is reserved for data that is stored on the blockchain permanently, Memory is used for temporary computations, and Calldata is reserved for function arguments that don’t require any manipulation. Here is an example of Storage, Memory, and Calldata in Solidity: Storage is used … Read more

Solidity Private Variables

State variables in Solidity can be defined as public or private by using those respective keywords. A private variable isn’t a hidden or encrypted variable, but it can only be modified or “seen” by the contract that creates it. Here is an example of a public and private variable in Solidity: First off, private variables … Read more

Solidity While Loop (Code Examples)

While loops are control structures that repeatedly run a block of code while a boolean condition remains true, or a break statement is encountered. Here’s an example of a while loop in Solidity: While loops create a “stripped down” looping structure that can use non-traditional loop conditions. This makes them useful for algorithms, such as those … Read more

Solidity Function Type (View, Pure, Payable)

In Solidity, there are 3 main function types which consist of View, Pure, and Payable. These determine if the functions can read or write to the blockchain, and if they can receive ETH upon being called. Here is an example of each of these functions in a smart contract: In the example above, we create … Read more

Function Access Modifiers (Public, Private, Internal, External)

In solidity, the permission to call a function can be restricted to 4 different permission levels. These are called Access Modifiers and include Public, Private, Internal, and External. The main benefit of controlling access to a function is security. Here is an example with each modifier: A “Public” function can be called by anyone, including … Read more

Solidity Assert

Assert is a function in Solidity that is used to verify a really important conditional statement that should never return false. If the condition is false, the state reverts to it’s previous state, and future gas is refunded. Here’s an example of using an Assert error in Solidity: Assert is very similar to Require and … Read more

Solidity Strings

Strings are a reference-type variable in Solidity that hold characters and numbers in an array format. They are useful for holding text or longer messages. Here is an example of a contract that uses a string in Solidity: Strings are actually dynamically-sized arrays that are encoded with the UTF-8 protocol. These can contain any ASCII … Read more