An Overview of Ethereum and Solidity
By: Burakcan Ekici
Tags: Programming, Ethereum, Blockchain, Smart Contracts, Solidity

Solidity

Solidity is an object-oriented, high-level language for implementing smart contracts, and it is designed to target the Ethereum Virtual Machine (EVM), managing the behavior of accounts within the Ethereum state.

It is a contract-oriented language, which means that smart contracts are responsible for storing all of the programming logic that transacts with the blockchain.

With Solidity, any contract can be developed for uses such as voting, crowdfunding, blind auctions, and multi-signature wallets.

Among several methods (i.e., solc-js, a Node.js framework, or up-to-date Docker builds) to compile the Solidity code and compile it, Remix IDE is a powerful, open source tool that helps you write Solidity contracts straight from the browser without installing anything. Written in JavaScript, Remix supports both usage in the browser and locally. Remix also supports testing, debugging, and deploying smart contracts, and much more.

The following elements that of a smart contract are good to understand the general structure of a smart contract.

Declaring a contract

The Solidity version and the contract name must be declared by the following.

pragma solidity ^0.4.24; contract MyContract {

// do something

}

Importing other source files

Ethereum Solidity supports import statements that are very similar to those available in JavaScript, although Solidity does not know the concept of a “default export.”

import "filename"; ----- OR ----- import * as symbolName from "filename";

Comments

Just like any other language, single-line and multi-line comments are possible in Solidity.

// This is a single-line comment. ----- OR ----- /*

This is a

multi-line comment

*/

Variables

Solidity supports the following variables:

state variables — permanently stored in contract storage

— permanently stored in contract storage local variables — presented till the function is executing

— presented till the function is executing global variables — special variables that exist in the global namespace and are used to get information about the blockchain

pragma solidity … contract HelloWorld{

uint storedData; // state variable



function getResult() public view returns(uint){

uint a = 1; // local variable

}

}

Variable scope

Solidity supports the following types of visibilities that are common for both functions and variables

public — public functions or variables that can be called internally or through messages

— public functions or variables that can be called internally or through messages internal — functions and variables only available to the current contract and derived contracts

— functions and variables only available to the current contract and derived contracts private — only available to the current contract and not derived contracts

— only available to the current contract and not derived contracts external — can just be used to functions to be called from other contracts and transactions

Data types

The following types are also called value types because variables of these types will always be passed by value.

boolean — bool ; the possible values are constants, i.e., true or false

— ; the possible values are constants, i.e., true or false integer — int/uint ; signed and unsigned integers of various sizes

— ; signed and unsigned integers of various sizes string — string ; string literals are written with either double or single-quotes “foo” or ‘bar’

— ; string literals are written with either double or single-quotes “foo” or ‘bar’ address — address/address payable holds a 20-byte value, the size of an Ethereum address, etc.

Address types

Address types also have members and serve as a base for all contracts. There are two types of address: address holds a 20-byte value, and address payable is the same as address above but just has transfer and send members. The distinction between address / address payable is that address payable is an address you can send ether to, while a plain address cannot be sent Ether.

Implicit conversions from address payable to address are allowed, whereas conversions from address to address payable are not allowed. Address type has several members:

balance returns the balance of given address (in units of wei)

returns the balance of given address (in units of wei) transfer sends a given amount to a given address (in units of wei)

sends a given amount to a given address (in units of wei) send sends a given amount of wei to a given address (not a safe alternative for transfer)

Data structures

The following types are specialized in Solidity:

Struct — struct Solidity provides a way to define new types in the form of structs. Structs are custom-defined types that can group several variables.

— Solidity provides a way to define new types in the form of structs. Structs are custom-defined types that can group several variables. Array — [] Arrays can have a compile-time fixed size, or they can have a dynamic size.

— Arrays can have a compile-time fixed size, or they can have a dynamic size. Mapping — mapping(k => v) Mappings can be seen as hash tables which are virtually initialized such that every possible key exists and is mapped to a value whose byte-representation is all zeros: a type’s default value, etc.

Data locations

All reference types contain information on where they are stored, and there are three areas to store them:

Storage — State variable, local variables of struct, array, and mapping: Each account has its own storage, which is persistent between function calls and transactions.

— State variable, local variables of struct, array, and mapping: Each account has its own storage, which is persistent between function calls and transactions. Memory — Function arguments, which a contract obtains a freshly cleared instance for each message call. It holds temporary values and gets erased between (external) function calls and is cheaper to use.

— Function arguments, which a contract obtains a freshly cleared instance for each message call. It holds temporary values and gets erased between (external) function calls and is cheaper to use. Stack — Small local variables. The EVM is not a register machine but a stack machine, so all computations are performed on a data area called the stack. It holds small local variables and is almost free to use, but can only hold a limited amount of values.

Functions

In a function, <parameter types> can be empty, but <return types> must be filled.

Internal functions can only be called inside the current contract.

can only be called inside the current contract. External functions consist of an address and a function signature, and they can be passed and returned from an external function.

function (<parameter types>) {internal|external} [pure|view|payable] [returns (<return types>)]

Type of functions

pure ensures that they do not change the state (it can be converted to view).

view does not read from state or modify the state.

payable makes it so ether can be sent in the function.

fallback does not have any arguments and does not return anything, but it is especially used for sending Ether, writing to storage, and creating a contract.

Functions can be set as view and pure to restrict reading and modifying of the state.

Method overloading

Function overloading occurs when several functions in a contract have the same name but differing arguments.

function sayHi(string n1) public pure returns (string out) {

out = "Hi " + n1 + " !" ;

} function sayHi(string n1, string n2) public pure returns (string out) {

out = "Hi " + n1 + " and " + n2 + " !" ;

}

Modifiers

Modifiers are defined to change the way functions work in Solidity. By using modifiers, the condition, which is checked before the function execution, can be set.