Sunday, August 20, 2017

Data Types in Ethereum Solidity Smart Contracts


Solidity is a statically typed language which employs a type system that allows compiler to check for correct usage of variables. Solidity compiler forces that every variable be defined at compile time. Solidity language provides several basic types. These basic types can be combined to form complex types.

Like other static type languages such as C#, Solidity has Value types and Reference types. Solidity Value Types and Reference Types behave similar to C# as outlined below:

Value Types: Value Type variables are always passed by value. These variables are copied when they are used as function arguments or in assignment. These are basic data types provided by Solidity

Reference Types: These are the complex data types that cannot be copied. Their memory or data location is passed around.

Below are the popular data types used in Solidity. For more information refer to the Solidity documentation http://solidity.readthedocs.io/en/latest/types.html
  • Value Types
    • Boolean
    • Integer
    • Address
    • Enum
    • Function
  • Reference Types
    • String
    • Array
    • Struct
    • Mapping
Here are the brief details of the common data types in Solidity

Boolean

Boolean data type is used to store true or false value. This is similar to C# and other programming languages.

Boolean supports all the typical comparison operators such as ! , ||, &&, ==, !=. The operators || and && apply the common short-circuiting rules. This means that in the expression f(x) || g(y), if f(x) evaluates to true, g(y) will not be evaluated.

Integer

Solidity support signed and unsigned integers. Signed integers are declared with keyword int and unsigned integers with keyword uint. In addition to better optimize memory Solidity allows specifing the size of the integer data types, such as uint8, uint16, …, uint256 and int8, int16, …, int256. The size of the data types varies by a step of 8 bits. Solidity provided a short hand alias of uint for uint256 and int for int256. This is similar to int alias in C#.

uint budget; // amount budgeted for the service
uint amountPaid; // amount paid to the contractor

Like other languages (C#), Solidity provides the following operators
  • Comparison operators: <=, <, ==, !=, >=, >
  • Bit operators: &, |, ^, ~
  • Arithmetic Operators: +, -, *, /, %
  • Unary operators: ++, --
  • Assignment operators: -=, *=, /=, %=, |=, &=, ^=

String

Strings are the dynamic sized array (see below for Arrays). This type is used to store an arbitrary length of string. As Strings are arrays they are reference type.

Solidity also supports string literals which are fixed sized byte arrays. String literals are written with either double or single quotes (“Client”, ‘Contractor’). Similar to integer data types, the string literals can have the data types of bytes1, ..., bytes32 (depending on the string size)

Address

Address data type is used to store an Ethereum address. It can contain the address of a user account or an address of a contract. Ethereum address is a 20 byte value as shown below:

// 0x account address
address platformAddress = 0x1e2f51e97f2772dafe59d345bbe48c32a7d2b46b;

The Address data type has below two frequently used members:
  • balance() returns(uint): This method returns the ether balance of the address in units of wei
  • transfer(uint): This method is used to send ethers from the current contract address to another address. The ethers are defined in the units of wei.

Enum

The enum keyword is used to declare an enumeration, a distinct type that consists of a set of named constants called the enumerator list. Hence Enums are strongly typed constants which makes the code more readable and less prone to errors.

// enum to define states of the service contract
enum State { Bid, Award, Work, Paid }
State contractState; // to store the current state of the service contract

Enums need at least one member. Enum types have to be explicitly converted from and to integer types. Solidity sometimes implicitly convert the enum types when used in function return parameters.

Function Types

Function Types are the types of functions. Similar to Java Script, Solidity allows functions to be passed as function parameters and return types. Solidity functions can be assigned to a variable of Function Type. These variables can be passed as inputs to the functions and can be returned as function return values. Function Types comes in two flavours – internal and external functions.

Internal functions contain the actual code of the function. They can only be called inside the current contract and can only be executed inside the current contract context. Calling an internal function is similar to calling any other function that is defined in the current contract. The advantage of using Internal function type is that they can be passed around and invoked based on the application logic.

External functions contains the address and the function signature. These types can be used, as the name indicates, to execute the external functions.

Arrays

Arrays can be fixed size or dynamic at the compile time. The fixed size arrays are declared as T[k] where k is the fixed size of element T. The dynamic arrays are declared as T[]. The Array data type is similar to arrays in C# and other programming language.

Solidity support multi-dimensional array and this multi-dimensional array is declared as T[][]. We can even declare an array of dynamic arrays. For example to declare an array of 5 dynamic arrays, T[][5].

Array data type supports the following two members:
  • length: The length member holds and returns the number of elements in the array.
  • push: For dynamic storage arrays and bytes, push can be used to append an element at the end of the array. The function returns the new length of the array

Structs

Structs let us define complex data types or user defined data types in solidity. These structs are similar to other programming languages like C#. In our contract we will define a bidder type which holds the information of the contractor and his bid amount.

// struct type to hold bidder information
struct Bidder {
    address contractor;
    uint bidAmount;
}

Mappings

Mapping data types are the dynamic array of key value pair. Mappings are similar to hash table or dictionary in C# or other programming languages. Solidity uses SHA-3 hash (keccak256) on the key and stores the value with that hash. When needed it will use this hash to look up and retrieve the value.

mapping(address => Bidder) bids; // storing all bids received for the contract

Mappings are only allowed for state variables. Hence mappings typically used to data for quick retrieval.

No comments:

Post a Comment