Network
Launch Date
Consensus
Note
Sepolia
Oct 2021
PoW
Like-for-like representation of Ethereum
Görli
Jan 2019
PoA
Proof-of-Authority
Kiln
Mar 2022
PoS
Post-Merge (for ETH2), shadow fork of the mainnet
Kintsugi
Dec 2021
PoS
DEPRECATED, use Kiln; post-Merge (for ETH2)
Ropsten
Nov 2016
PoW
DEPRECATED, use Sepolia; the Merge to happen on Jun 8, 2022
Rinkeby
Apr 2017
PoA
DEPRECATED, use Görli and Görli Faucet
Kovan
Mar 2017
PoA
DEPRECATED, use Sepolia or Görli
List of active and deprecated Ethereum testnets, including Kintsugi.
Features
Optimistic rollup 
ZK-rollup 
Proof
Uses fraud proofs to prove transaction validity. 
Uses validity (zero-knowledge) proofs to prove transaction validity. 
Capital efficiency
Requires waiting through a 1-week delay (dispute period) before withdrawing funds. 
Users can withdraw funds immediately because validity proofs provide incontrovertible evidence of the authenticity of off-chain transactions. 
Data compression
Publishes full transaction data as calldata to Ethereum Mainnet, which increases rollup costs. 
Doesn't need to publish transaction data on Ethereum because ZK-SNARKs and ZK-STARKs already guarantee the accuracy of the rollup state. 
EVM compatibility
Uses a simulation of the Ethereum Virtual Machine (EVM), which allows it to run arbitrary logic and support smart contracts. 
Doesn't widely support EVM computation, although a few EVM-compatible ZK-rollups have appeared. 
Rollup costs
Reduces costs since it publishes minimal data on Ethereum and doesn't have to post proofs for transactions, except in special circumstances. 
Faces higher overhead from costs involved in generating and verifying proofs for every transaction block. ZK proofs require specialized, expensive hardware to create and have high on-chain verification costs. 
Trust assumptions
Doesn't require a trusted setup. 
Requires a trusted setup to work. 
Liveness requirements
Verifiers are needed to keep tabs on the actual rollup state and the one referenced in the state root to detect fraud. 
Users don't need someone to watch the L2 chain to detect fraud. 
Security properties 
Relies on cryptoeconomic incentives to assure users of rollup security. 
Relies on cryptographic guarantees for security. 
Start building
on Alchemy.
Sign up for free
Start building on Optimism.
Sign up for free
Start building on Arbitrum.
Sign up for free
Start building on Ethereum.
Sign up for free
Start building on Polygon.
Sign up for free
Start building on Starknet.
Sign up for free
Start building on Flow.
Sign up for free
kiln faucet
Get free Kiln ETH.
Start building today
Goerli faucet
Get free Goerli ETH.
Start building today
SEPOLIA FAUCET
Get free Sepolia ETH.
Start Building Today
mumbai faucet
Get free Mumbai Matic.
Start building today
rinkeby faucet
Get free Rinkeby
ETH.
Start building today
Start building on Ethereum.
Get started for free
Start building on Ethereum.
Get started for free
Start building on Flow.
Get started for free
Start building on Polygon.
Get started for free
Start building on Starknet.
Get started for free
Start building on Optimism.
Get started for free
Start building on Solana.
Get started for free
Start building on Solana.
Sign up for beta access
Start building on Solana.
Join the waitlist
Arbitrum logo
Start building on Arbitrum.
Get started for free
Build with Alchemy's
Gas Manager & Bundler APIs
Learn
Solidity at
Alchemy
University
Get started today
Build with Alchemy's
Gas Manager & Bundler APIs
curl 
https://release.solana.com/v1.10.32/solana-install-init-x86_64-pc-windows-msvc.exe 
--output 
C:\solana-install-tmp\solana-install-init.exe 
--create-dirs
Learn Solidity
SOLIDITY SMART CONTRACT OVERVIEW

Solidity Smart Contracts 101

Gain A High-Level Understanding of Ethereum Smart Contracts
Last Updated:
October 4, 2022
Table of Contents
Table of Contents
Table of Contents

{{learn-solidity}}

Solidity smart contracts have various uses ranging from multi signature wallets to decentralized exchanges. Solidity is one of the major web3 programming languages built specially for creating smart contracts and has therefore seen a steady increase in popularity. 

This article will introduce you to Solidity smart contracts, how they work and their properties. We will also run through Solidity smart contract syntax and data types to get you started on interacting with Solidity smart contracts for your next project. To expand your knowledge of Solidity, complete Alchemy University's free Solidity syntax course.

What is a Solidity smart contract?

A Solidity smart contract is a program written in Solidity, deployed to the Ethereum blockchain, and executed in the Ethereum Virtual Machine (EVM). Ethereum stores the smart contract code and data (its state) at a designated address.

When predetermined conditions are met, the program, or smart contract, stored on the blockchain is executed without the need of an intermediary. Smart contracts are, in other words, programs which govern the behavior of accounts within the Ethereum state. 

You can write smart contracts in Solidity or any other EVM-compatible programming language. They must be compiled into bytecode first to be EVM compatible. 

Solidity is an object-oriented, high-level language for writing smart contracts. Smart contracts written in Solidity can be used for various purposes like voting, crowdfunding, blind auctions, and multi-signature wallets.

How do smart contracts work? 

Smart contracts are programs stored on the blockchain. A smart contract fortifies agreements between various parties in code and enforces rules automatically when pre-conditions are met. This means that all parties involved in the smart contract are immediately certain of the outcome, without time lost or involvement of a third party. Smart contracts can also automate workflows to trigger the next event upon execution.

Smart contracts that follow the ERC20 standard are considered ERC20 tokens. ERC20 tokens allow for the transfer of tokens between holders. 

What are the properties of Solidity smart contracts?

Smart contracts are by nature, immutable and deterministic. This means that once a smart contract is deployed or built on Ethereum, it will never cease to exist unless conditions which trigger a self-destruct have been programmed within it. The code will always execute on the conditions it has been programmed to. 

Additionally, smart contracts are deterministic since each network node is able to produce the same result when the same input is given for a method. Should different nodes arrive at different outputs on execution of the smart contract, the consensus protocol is violated and the smart contract is rendered unusable.

Also, smart contracts are permissionless, which means anyone in possession of ETH and has access to the internet is able to deploy a smart contract on Ethereum. 

Finally, smart contracts are composable. This means that you can use smart contracts from other projects as building blocks for your project. Smart contracts can be thought of as open APIs where users do not need to write their own smart contract to become a dApp developer, users just need to know how to interact with them. 

Solidity Smart Contract Syntax

While there are many resources available to help you get started learning Solidity, this section will outline the syntax used to write Solidity smart contracts. 

1. Contracts

Contracts in Solidity are similar to classes in object-oriented languages. Each contract can contain declarations of State Variables, Functions, Function Modifiers, Events, Errors, Struct Types and Enum Types. In this article we will discuss Constructors, State Variables and Integer Variables in further detail. Furthermore, contracts can inherit from other contracts.

There are also special contracts called libraries and interfaces. 

2. Semantic Versioning

Solidity, like most softwares, uses semantic versioning. This means there are no big changes unless you are updating a major version. You can refer to the main breaking changes introduced to each Solidity version on the docs. The most updated version of Solidity is v0. 8.16, as of August 2022.

3. Constructor

A constructor is called only once during deployment, or contract creation. A constructor is an optional function declared with the constructor keyword and allows you to run contract initialization code. 

Before the constructor code is executed, state variables are initialized to their specified value if you initialize them inline, or their default value if you do not.

After the constructor has run, the final code of the contract is deployed to the blockchain.

If there is no constructor, the contract will assume the default constructor. 

4. State Variables

State variables are variables whose values are stored in contract storage. Each function has its own scope, and state variables should always be defined outside of that scope.

State variables adhere to visibility. You can make state variables public with the public visibility keyword, which provides a getter function which other smart contracts can access.

5. Modification

In Solidity, a modifier amends the semantics of a function in a declarative way. In other words, modifiers change the behavior of the function to which they are attached. Modifiers are useful in eliminating code redundancy and can be reused to check for the same condition in multiple functions within a smart contract. 

6. Integer variables

There are two types of integer variables in Solidity: unsigned integers (uint), and signed integers (int). A signed integer is a value data type that can store negative and positive values. An unsigned integer, on the other hand, has no sign and therefore is a value data type that must be non-negative.

Solidity Data Types

Like other programming languages, Solidity has various classifications of data types. However, Solidity is unique in that various elementary data types can be combined to form more complex data types. This section outlines the major data types in Solidity.

1. Booleans

The possible values of booleans, denoted by bool in Solidity, are constants true and false.

2. Strings

String literals are written with either double or single-quotes. Strings can also be split into multiple consecutive parts, which can be helpful when dealing with long strings. 

3. Numbers

Fixed and ufixed refer respectively to signed and unsigned fixed point numbers of various sizes.

4. Bytes

In Solidity, byte refers to 8-bit signed integers. Bits are stored in the memory with binary values of 0 or 1. In Solidity, the data type byte represents a sequence of bytes.

There are two types of byte types in Solidity, fixed-sized byte arrays and dynamically-sized byte arrays. The keyword bytesX is used to define variables where X denotes the sequence of bytes from 1 up to 32. 

Bytes in Solidity represents a dynamic array of bytes. They are not, however, a value type. 

5. Address

The address holds a 20-byte value, which is the size of an Ethereum address. Hexadecimal literals that pass the address checksum test are of address type. Hexadecimal literals that are between 39 and 41 digits long and do not pass the checksum test produce an error. You can prepend (for integer types) or append (for bytesNN types) zeros to remove the error.

6. Payable Address

The payable address, denoted by address payable, is similar to the address but with additional members transfer and send. The distinction is necessary because a plain address may be a smart contract that is not built to accept Ether (ETH). An address payable is an address you can send Ether to.

7. Enums

Enums, also known as enumeration values in Solidity, are one way to create a user-defined type, which allows creating constant values, such as the names of integral constants. Enums make a smart contract easier to read and maintain as opposed to elementary value types.

Enums are explicitly convertible to and from all integer types, but implicit conversion is not allowed. The explicit conversion from integer checks at runtime that the value lies inside the range of the enum and causes a panic error otherwise. Enums therefore reduce the incidence of bugs in your code.

Enums require at least one member, and its default value when declared is the first member. Enums cannot have more than 256 members.

8. Arrays

Arrays are a group of variables of the same data type, with each variable having a unique index. Arrays can have a compile-time fixed size, which makes them fixed arrays, or they can have a dynamic size, which makes them dynamic arrays.

Fixed-size arrays have a predefined size upon declaration. The new keyword cannot be used to introduce additional array members. Instead, the data variables must be initialized inline.

Dynamic-sized arrays do not have a predefined size when they are declared. Instead, their size is determined at run time.

Array elements can be of any type, including a mapping or a struct, a user-defined data structure.

9. Mappings

In Solidity, mappings work like hash tables or dictionaries in other programming languages. Mappings work as a reference type and store data in the form of key-value pairs, where the key can be any of the inbuilt data types except for reference types, and the value can be any type.

If state variables of a mapping type are public, Solidity creates a getter function for you. 

10. Structs (user-defined types)

A struct, or a structure, allows you to define new data types. Struct types can be used inside mappings and arrays and they can themselves contain nested mappings and arrays. However, It is not possible for a struct to contain a member of its own type since the size of the struct must be finite. 

What is smart contract context?

A smart contract context provides information about the environment a transaction is running in. When a smart contract function is called via a transaction, the called function receives some extra information passed to it. Within a smart contract function you’ll have access to these context variables.

You have access to the message sender, gas prices, and the tests conducted at the transaction. Therefore, you have access to a bunch of context variables that you can use in your code’s logic interchangeably.

Sign Up for an Ethereum Developer Bootcamp to Learn More

In this article, we have introduced the basics of getting started with Solidity smart contracts and outlined the key data types and syntax to increase your understanding. Sign up for Alchemy's free online Ethereum Developer Bootcamp to learn more about creating smart contracts with Solidity. If developers are new to development in general, Alchemy University's 3-week JavaScript for Ethereum crash course is a great prerequisite.

ALCHEMY SUPERNODE - ETHEREUM NODE API

Scale to any size, without any errors

Alchemy Supernode finally makes it possible to scale blockchain applications without all the headaches. Plus, our legendary support will guide you every step of the way.

Get started for free
Supernode footer
Learn Solidity
SOLIDITY SMART CONTRACT OVERVIEW

Introduction to Solidity Smart Contracts

Gain A High-Level Understanding of Ethereum Smart Contracts
Last Updated:
October 4, 2022
Last Updated:
March 14, 2023
Don't miss an update
Sign up for our newsletter to get alpha, key insights, and killer resources.
Thank you! Your submission has been received!
Oops! Something went wrong while submitting the form.
Table of Contents

Talk to an Expert

Learn how Alchemy's blockchain developer tools can help your business succeed in web3!
Valid number
Thank you! An Alchemy expert will be in touch with you shortly!
Oops! Something went wrong while submitting the form.

{{learn-solidity}}

Table of Contents

Solidity smart contracts have various uses ranging from multi signature wallets to decentralized exchanges. Solidity is one of the major web3 programming languages built specially for creating smart contracts and has therefore seen a steady increase in popularity. 

This article will introduce you to Solidity smart contracts, how they work and their properties. We will also run through Solidity smart contract syntax and data types to get you started on interacting with Solidity smart contracts for your next project. To expand your knowledge of Solidity, complete Alchemy University's free Solidity syntax course.

What is a Solidity smart contract?

A Solidity smart contract is a program written in Solidity, deployed to the Ethereum blockchain, and executed in the Ethereum Virtual Machine (EVM). Ethereum stores the smart contract code and data (its state) at a designated address.

When predetermined conditions are met, the program, or smart contract, stored on the blockchain is executed without the need of an intermediary. Smart contracts are, in other words, programs which govern the behavior of accounts within the Ethereum state. 

You can write smart contracts in Solidity or any other EVM-compatible programming language. They must be compiled into bytecode first to be EVM compatible. 

Solidity is an object-oriented, high-level language for writing smart contracts. Smart contracts written in Solidity can be used for various purposes like voting, crowdfunding, blind auctions, and multi-signature wallets.

How do smart contracts work? 

Smart contracts are programs stored on the blockchain. A smart contract fortifies agreements between various parties in code and enforces rules automatically when pre-conditions are met. This means that all parties involved in the smart contract are immediately certain of the outcome, without time lost or involvement of a third party. Smart contracts can also automate workflows to trigger the next event upon execution.

Smart contracts that follow the ERC20 standard are considered ERC20 tokens. ERC20 tokens allow for the transfer of tokens between holders. 

What are the properties of Solidity smart contracts?

Smart contracts are by nature, immutable and deterministic. This means that once a smart contract is deployed or built on Ethereum, it will never cease to exist unless conditions which trigger a self-destruct have been programmed within it. The code will always execute on the conditions it has been programmed to. 

Additionally, smart contracts are deterministic since each network node is able to produce the same result when the same input is given for a method. Should different nodes arrive at different outputs on execution of the smart contract, the consensus protocol is violated and the smart contract is rendered unusable.

Also, smart contracts are permissionless, which means anyone in possession of ETH and has access to the internet is able to deploy a smart contract on Ethereum. 

Finally, smart contracts are composable. This means that you can use smart contracts from other projects as building blocks for your project. Smart contracts can be thought of as open APIs where users do not need to write their own smart contract to become a dApp developer, users just need to know how to interact with them. 

Solidity Smart Contract Syntax

While there are many resources available to help you get started learning Solidity, this section will outline the syntax used to write Solidity smart contracts. 

1. Contracts

Contracts in Solidity are similar to classes in object-oriented languages. Each contract can contain declarations of State Variables, Functions, Function Modifiers, Events, Errors, Struct Types and Enum Types. In this article we will discuss Constructors, State Variables and Integer Variables in further detail. Furthermore, contracts can inherit from other contracts.

There are also special contracts called libraries and interfaces. 

2. Semantic Versioning

Solidity, like most softwares, uses semantic versioning. This means there are no big changes unless you are updating a major version. You can refer to the main breaking changes introduced to each Solidity version on the docs. The most updated version of Solidity is v0. 8.16, as of August 2022.

3. Constructor

A constructor is called only once during deployment, or contract creation. A constructor is an optional function declared with the constructor keyword and allows you to run contract initialization code. 

Before the constructor code is executed, state variables are initialized to their specified value if you initialize them inline, or their default value if you do not.

After the constructor has run, the final code of the contract is deployed to the blockchain.

If there is no constructor, the contract will assume the default constructor. 

4. State Variables

State variables are variables whose values are stored in contract storage. Each function has its own scope, and state variables should always be defined outside of that scope.

State variables adhere to visibility. You can make state variables public with the public visibility keyword, which provides a getter function which other smart contracts can access.

5. Modification

In Solidity, a modifier amends the semantics of a function in a declarative way. In other words, modifiers change the behavior of the function to which they are attached. Modifiers are useful in eliminating code redundancy and can be reused to check for the same condition in multiple functions within a smart contract. 

6. Integer variables

There are two types of integer variables in Solidity: unsigned integers (uint), and signed integers (int). A signed integer is a value data type that can store negative and positive values. An unsigned integer, on the other hand, has no sign and therefore is a value data type that must be non-negative.

Solidity Data Types

Like other programming languages, Solidity has various classifications of data types. However, Solidity is unique in that various elementary data types can be combined to form more complex data types. This section outlines the major data types in Solidity.

1. Booleans

The possible values of booleans, denoted by bool in Solidity, are constants true and false.

2. Strings

String literals are written with either double or single-quotes. Strings can also be split into multiple consecutive parts, which can be helpful when dealing with long strings. 

3. Numbers

Fixed and ufixed refer respectively to signed and unsigned fixed point numbers of various sizes.

4. Bytes

In Solidity, byte refers to 8-bit signed integers. Bits are stored in the memory with binary values of 0 or 1. In Solidity, the data type byte represents a sequence of bytes.

There are two types of byte types in Solidity, fixed-sized byte arrays and dynamically-sized byte arrays. The keyword bytesX is used to define variables where X denotes the sequence of bytes from 1 up to 32. 

Bytes in Solidity represents a dynamic array of bytes. They are not, however, a value type. 

5. Address

The address holds a 20-byte value, which is the size of an Ethereum address. Hexadecimal literals that pass the address checksum test are of address type. Hexadecimal literals that are between 39 and 41 digits long and do not pass the checksum test produce an error. You can prepend (for integer types) or append (for bytesNN types) zeros to remove the error.

6. Payable Address

The payable address, denoted by address payable, is similar to the address but with additional members transfer and send. The distinction is necessary because a plain address may be a smart contract that is not built to accept Ether (ETH). An address payable is an address you can send Ether to.

7. Enums

Enums, also known as enumeration values in Solidity, are one way to create a user-defined type, which allows creating constant values, such as the names of integral constants. Enums make a smart contract easier to read and maintain as opposed to elementary value types.

Enums are explicitly convertible to and from all integer types, but implicit conversion is not allowed. The explicit conversion from integer checks at runtime that the value lies inside the range of the enum and causes a panic error otherwise. Enums therefore reduce the incidence of bugs in your code.

Enums require at least one member, and its default value when declared is the first member. Enums cannot have more than 256 members.

8. Arrays

Arrays are a group of variables of the same data type, with each variable having a unique index. Arrays can have a compile-time fixed size, which makes them fixed arrays, or they can have a dynamic size, which makes them dynamic arrays.

Fixed-size arrays have a predefined size upon declaration. The new keyword cannot be used to introduce additional array members. Instead, the data variables must be initialized inline.

Dynamic-sized arrays do not have a predefined size when they are declared. Instead, their size is determined at run time.

Array elements can be of any type, including a mapping or a struct, a user-defined data structure.

9. Mappings

In Solidity, mappings work like hash tables or dictionaries in other programming languages. Mappings work as a reference type and store data in the form of key-value pairs, where the key can be any of the inbuilt data types except for reference types, and the value can be any type.

If state variables of a mapping type are public, Solidity creates a getter function for you. 

10. Structs (user-defined types)

A struct, or a structure, allows you to define new data types. Struct types can be used inside mappings and arrays and they can themselves contain nested mappings and arrays. However, It is not possible for a struct to contain a member of its own type since the size of the struct must be finite. 

What is smart contract context?

A smart contract context provides information about the environment a transaction is running in. When a smart contract function is called via a transaction, the called function receives some extra information passed to it. Within a smart contract function you’ll have access to these context variables.

You have access to the message sender, gas prices, and the tests conducted at the transaction. Therefore, you have access to a bunch of context variables that you can use in your code’s logic interchangeably.

Sign Up for an Ethereum Developer Bootcamp to Learn More

In this article, we have introduced the basics of getting started with Solidity smart contracts and outlined the key data types and syntax to increase your understanding. Sign up for Alchemy's free online Ethereum Developer Bootcamp to learn more about creating smart contracts with Solidity. If developers are new to development in general, Alchemy University's 3-week JavaScript for Ethereum crash course is a great prerequisite.

Solidity smart contracts have various uses ranging from multi signature wallets to decentralized exchanges. Solidity is one of the major web3 programming languages built specially for creating smart contracts and has therefore seen a steady increase in popularity. 

This article will introduce you to Solidity smart contracts, how they work and their properties. We will also run through Solidity smart contract syntax and data types to get you started on interacting with Solidity smart contracts for your next project. To expand your knowledge of Solidity, complete Alchemy University's free Solidity syntax course.

What is a Solidity smart contract?

A Solidity smart contract is a program written in Solidity, deployed to the Ethereum blockchain, and executed in the Ethereum Virtual Machine (EVM). Ethereum stores the smart contract code and data (its state) at a designated address.

When predetermined conditions are met, the program, or smart contract, stored on the blockchain is executed without the need of an intermediary. Smart contracts are, in other words, programs which govern the behavior of accounts within the Ethereum state. 

You can write smart contracts in Solidity or any other EVM-compatible programming language. They must be compiled into bytecode first to be EVM compatible. 

Solidity is an object-oriented, high-level language for writing smart contracts. Smart contracts written in Solidity can be used for various purposes like voting, crowdfunding, blind auctions, and multi-signature wallets.

How do smart contracts work? 

Smart contracts are programs stored on the blockchain. A smart contract fortifies agreements between various parties in code and enforces rules automatically when pre-conditions are met. This means that all parties involved in the smart contract are immediately certain of the outcome, without time lost or involvement of a third party. Smart contracts can also automate workflows to trigger the next event upon execution.

Smart contracts that follow the ERC20 standard are considered ERC20 tokens. ERC20 tokens allow for the transfer of tokens between holders. 

What are the properties of Solidity smart contracts?

Smart contracts are by nature, immutable and deterministic. This means that once a smart contract is deployed or built on Ethereum, it will never cease to exist unless conditions which trigger a self-destruct have been programmed within it. The code will always execute on the conditions it has been programmed to. 

Additionally, smart contracts are deterministic since each network node is able to produce the same result when the same input is given for a method. Should different nodes arrive at different outputs on execution of the smart contract, the consensus protocol is violated and the smart contract is rendered unusable.

Also, smart contracts are permissionless, which means anyone in possession of ETH and has access to the internet is able to deploy a smart contract on Ethereum. 

Finally, smart contracts are composable. This means that you can use smart contracts from other projects as building blocks for your project. Smart contracts can be thought of as open APIs where users do not need to write their own smart contract to become a dApp developer, users just need to know how to interact with them. 

Solidity Smart Contract Syntax

While there are many resources available to help you get started learning Solidity, this section will outline the syntax used to write Solidity smart contracts. 

1. Contracts

Contracts in Solidity are similar to classes in object-oriented languages. Each contract can contain declarations of State Variables, Functions, Function Modifiers, Events, Errors, Struct Types and Enum Types. In this article we will discuss Constructors, State Variables and Integer Variables in further detail. Furthermore, contracts can inherit from other contracts.

There are also special contracts called libraries and interfaces. 

2. Semantic Versioning

Solidity, like most softwares, uses semantic versioning. This means there are no big changes unless you are updating a major version. You can refer to the main breaking changes introduced to each Solidity version on the docs. The most updated version of Solidity is v0. 8.16, as of August 2022.

3. Constructor

A constructor is called only once during deployment, or contract creation. A constructor is an optional function declared with the constructor keyword and allows you to run contract initialization code. 

Before the constructor code is executed, state variables are initialized to their specified value if you initialize them inline, or their default value if you do not.

After the constructor has run, the final code of the contract is deployed to the blockchain.

If there is no constructor, the contract will assume the default constructor. 

4. State Variables

State variables are variables whose values are stored in contract storage. Each function has its own scope, and state variables should always be defined outside of that scope.

State variables adhere to visibility. You can make state variables public with the public visibility keyword, which provides a getter function which other smart contracts can access.

5. Modification

In Solidity, a modifier amends the semantics of a function in a declarative way. In other words, modifiers change the behavior of the function to which they are attached. Modifiers are useful in eliminating code redundancy and can be reused to check for the same condition in multiple functions within a smart contract. 

6. Integer variables

There are two types of integer variables in Solidity: unsigned integers (uint), and signed integers (int). A signed integer is a value data type that can store negative and positive values. An unsigned integer, on the other hand, has no sign and therefore is a value data type that must be non-negative.

Solidity Data Types

Like other programming languages, Solidity has various classifications of data types. However, Solidity is unique in that various elementary data types can be combined to form more complex data types. This section outlines the major data types in Solidity.

1. Booleans

The possible values of booleans, denoted by bool in Solidity, are constants true and false.

2. Strings

String literals are written with either double or single-quotes. Strings can also be split into multiple consecutive parts, which can be helpful when dealing with long strings. 

3. Numbers

Fixed and ufixed refer respectively to signed and unsigned fixed point numbers of various sizes.

4. Bytes

In Solidity, byte refers to 8-bit signed integers. Bits are stored in the memory with binary values of 0 or 1. In Solidity, the data type byte represents a sequence of bytes.

There are two types of byte types in Solidity, fixed-sized byte arrays and dynamically-sized byte arrays. The keyword bytesX is used to define variables where X denotes the sequence of bytes from 1 up to 32. 

Bytes in Solidity represents a dynamic array of bytes. They are not, however, a value type. 

5. Address

The address holds a 20-byte value, which is the size of an Ethereum address. Hexadecimal literals that pass the address checksum test are of address type. Hexadecimal literals that are between 39 and 41 digits long and do not pass the checksum test produce an error. You can prepend (for integer types) or append (for bytesNN types) zeros to remove the error.

6. Payable Address

The payable address, denoted by address payable, is similar to the address but with additional members transfer and send. The distinction is necessary because a plain address may be a smart contract that is not built to accept Ether (ETH). An address payable is an address you can send Ether to.

7. Enums

Enums, also known as enumeration values in Solidity, are one way to create a user-defined type, which allows creating constant values, such as the names of integral constants. Enums make a smart contract easier to read and maintain as opposed to elementary value types.

Enums are explicitly convertible to and from all integer types, but implicit conversion is not allowed. The explicit conversion from integer checks at runtime that the value lies inside the range of the enum and causes a panic error otherwise. Enums therefore reduce the incidence of bugs in your code.

Enums require at least one member, and its default value when declared is the first member. Enums cannot have more than 256 members.

8. Arrays

Arrays are a group of variables of the same data type, with each variable having a unique index. Arrays can have a compile-time fixed size, which makes them fixed arrays, or they can have a dynamic size, which makes them dynamic arrays.

Fixed-size arrays have a predefined size upon declaration. The new keyword cannot be used to introduce additional array members. Instead, the data variables must be initialized inline.

Dynamic-sized arrays do not have a predefined size when they are declared. Instead, their size is determined at run time.

Array elements can be of any type, including a mapping or a struct, a user-defined data structure.

9. Mappings

In Solidity, mappings work like hash tables or dictionaries in other programming languages. Mappings work as a reference type and store data in the form of key-value pairs, where the key can be any of the inbuilt data types except for reference types, and the value can be any type.

If state variables of a mapping type are public, Solidity creates a getter function for you. 

10. Structs (user-defined types)

A struct, or a structure, allows you to define new data types. Struct types can be used inside mappings and arrays and they can themselves contain nested mappings and arrays. However, It is not possible for a struct to contain a member of its own type since the size of the struct must be finite. 

What is smart contract context?

A smart contract context provides information about the environment a transaction is running in. When a smart contract function is called via a transaction, the called function receives some extra information passed to it. Within a smart contract function you’ll have access to these context variables.

You have access to the message sender, gas prices, and the tests conducted at the transaction. Therefore, you have access to a bunch of context variables that you can use in your code’s logic interchangeably.

Sign Up for an Ethereum Developer Bootcamp to Learn More

In this article, we have introduced the basics of getting started with Solidity smart contracts and outlined the key data types and syntax to increase your understanding. Sign up for Alchemy's free online Ethereum Developer Bootcamp to learn more about creating smart contracts with Solidity. If developers are new to development in general, Alchemy University's 3-week JavaScript for Ethereum crash course is a great prerequisite.

{{learn-solidity}}

Contact Us

Talk to an expert at Alchemy to answer all of your product questions.
Valid number
Thank you! Your submission has been received!
Oops! Something went wrong while submitting the form.

Build blockchain magic with Alchemy

Alchemy combines the most powerful web3 developer products and tools with resources, community and legendary support.

Get started for free