Solidity data types
A guide on the data types in Solidity
Table of Contents for Solidity data types
Value types
addresses
An address in Solidity is a 20 byte (160 bits) representation of an address.
There are two types - address
and address payable
.
You can convert an address
to address payable
easily - payable(yourAddress)
.
Enums
You can use enums like this in solidity:
contract YourContract {
enum Size{SMALL, MEDIUM, LARGE}
Size tshirtSize;
function setSize() public {
tshirtSize = Size.MEDIUM;
}
}
Numbers
- signed ints
int
which is equal toint256
- you can also use anything such as
int8
,int32
etc
- unsigned ints
uint
which is equal touint256
booleans
bool
which acceptstrue
orfalse
as values.- The default value for a bool is always
false
bytes
A byte is a 8 bit signed int. You can use the data types bytes1
up to bytes32
Reference types
Arrays
You can create arrays of multiple elements (of the same type) easily. They can also hold arrays of arrays, arrays of mappings etc.
Fixed size arrays
If you know how many items are in the array, you can set the fixed size. Here is an example of 7 strings.
string[7] snowWhitesDwarfs;
Dynamic sized arrays
You can also add arrays of a dynamic size
string[] peopleInGameOfThrones
Strings
Strings are very similar to arrays.
string greeting = "Hello world";
Structs
A struct is your own defined custom data type.
struct Person {
uint age;
string name;
}
Mapping
You can use a mapping like this:
mapping (uint => address) balances;
This in typescript would be Record<number, string>
.
Converting between types in Solidity
There are a few ways to convert between types in Solidity - read my guide here for more!.
Spotted a typo or have a suggestion to make this crypto dev article better? Please let me know!
Next post
π Solidity Auditing online quiz
Learn how to audit smart contracts by looking at some example code and trying to find the bugs
β½ Solidity Gas Optimizations Guide
How to optimize and reduce gas usage in your smart contracts in Solidity
π§ͺ Guide to testing with Foundry
Guide to adding testing for your Solidity contracts, using the Foundry and Forge tools
π Guide to UTXO
UTXO and the UTXO set (used by blockchains such as Bitcoin) explained
π Solidity Assembly Guide
Introduction guide to using assembly in your Solidity smart contracts
π¦ Ethereum EOF format explained
Information explaining what the upcoming Ethereum EOF format is all about