Ethereum Smart Contracts Development

Solidity Fundamentals: Types

Value Types: Part One

Ferdi Kurt
Coinmonks
Published in
4 min readNov 26, 2020

--

Let’s briefly look at what this section will include. We will be examining value types that are passed by value, further, if value store its own data not a pointer to a specific memory location it’s called value types — also known as primitive data types. Following are the value types that we will be discussing;

  • Booleans
  • Integers
  • Address
  • Address Literals
  • Fixed-sized byte arrays
  • Dynamically-sized byte arrays (bytes, string)
  • Enums

Solidity is a statically typed language, which means every variable must be specified at compile time. In this manner, it is similar to Java, C, C++, Scala, etc. Besides, types can interact with each other in expressions containing operators. We will have a section on operators in Solidity next so that things will be getting clear section by section.

Unlike Javascript, C# or Perl, null or undefined values don’t exist in Solidity, however default value is dependent on the type. For instance, if we declare a boolean type and did not specify any value, the default value will be false for that variable.

Booleans

Possible values are true and false.

I know there are new keywords in below contract but read comments carefully and just concentrate on boolean value types. Later we will explain these new keywords in their own sections.

Integers

Solidity can manage integers signed/unsigned of various sizes (up to 256 bit) and also fixed point numbers for development (in the form of unfixed/fixed) which are not fully supported by the language yet and can be declared but can not be assigned to or from.

Watch out folks, we are going to talk about a very scary scenario which can cause huge harm.

When uint (unsigned integer) reaches its byte size, the next value added will return the first variable element.

note: 1 byte = 8 bits

Let’s say we have a uint8, which is 8 bits. This means that the largest number we can store is 1111111 that is equal to 255. Look at below example. If we execute increment function what will happen? This is called Overflow — it means that our new num_one is 0.

Underflow is similar, if we subtract 1 from 0, the new result will be 255. We will discuss how to prevent such scenarios in later sections.

Address

Every account and smart contract has an address that is stored as 20 bytes. Address is used to send and receive ethers across accounts. This can be considered as public identity or account number on Blockchain.

The Address types come in two flavors, address and address payable— an address payable can receive Ether, while address can’t.

msg.sender is a built-in function in Solidity which indicates the address currently connecting with the contract. More its type is address payable.

balance returns the address balance in wei.

note: 1 Ether = 1^18 Wei = 1,000,000,000,000,000,000 Wei

transfer transfers the amount of ether in wei to a payable address. If the balance. This function reverts on failure, stops the current contract and throw an exception on any error.

note: Behind the scenes, the transfer function queries the balance of an address, by applying the property balance, before to send the ether.

send is the low-level counterpart of transfer. If the execution fails, the current contract will not stop with an exception, but send will return false.

There are also call, delegatecall and staticcall which are members of address type. We will talk on these 3 important function later in great detail.

Implicit conversions from address payable to address are allowed, whereas conversions from address to address payable must be explicit via payable(<address>).

Address Literals

Address literals are the hexadecimal representation of an Ethereum address that contain 40 characters (20 bytes) and prefixed with 0x.

Hexadecimal literals must pass the address checksum test, otherwise checksum test produce an error.

note: The mixed-case address checksum format is defined in EIP-55

Let’s digest all information above with some examples.

Next, we will continue and finish value types. Thanks for reading.

Feel free to ask any question.

Stay safe, do good work, and keep in touch!

Ferdi Kurt

--

--