What is Solidity?

Solidity is the programming language that is used to write, create, and implement smart contracts. If you want to learn smart contracts, you cannot do so without understanding and learning Solidity.

It was initially proposed back in 2014 by Gavin Wood, but was later developed and launched by Christian Reitwiessner, Alex Beregszaszi, Yoichi Hirai, and several former Ethereum core contributors, Solidity allows for the creation and writing of smart contracts on blockchain platforms like Ethereum, Ethereum Classic, Tron, among others.

It serves as the primary language on Ethereum’s Virtual Machine (EVM).

Similar to Javascript

In its most basic form, it is a “loosely-typed language”. The language functions very similar to C and Javascript but integrates additional support for state variables, data types, and programming functions.

When Wood first designed this, he said it was to make it familiar and easy for existing web developers. In contrast to Javascript, Solidity has static typing and variadic return types.

With Solidity, blockchain developers are able to write applications that implement self-enforcing (self-governing) business logic embodied in smart contracts. In plain, this is what allows the contracts to be self-executing, not requiring human involvement, once the code is written.

For those who are skilled programmers, this language is fairly easy. To the layman, however, it takes time and patience.

Understanding Syntax and General Data Types

Solidity supports generic value types, specifically booleans, integers, addresses, string literals, and modifiers. Let’s explore each.

Booleans

In computer science, Boolean data type is a data type that has two possible values--true or false. These values are intended to represent the two truth values of logic and Boolean algebra. For more information on this, please click here.

The logical operators returning Boolean data types:

  • ! Logical negation
  • && logical conjunction, “and”
  • || logical disjunction, “or”
  • == equality
  • != inequality

Integers

Solidity supports int/unit for both signed and unsigned integers respectively. These storage allocations can be of various sizes. Keywords such as uint8 and uint256 can be used to allocate a storage size of 8 bits to 256 bits respectively. By default, the allocation is 256 bits. That is, uint and int can be used in place of uint256 and int256.

The operators compatible with integer data types are:

  • Comparisons: <=, <, ==, !=, >=, >. These are used to evaluate to bool.
  • Bit operators: &, |, ^ bitwise exclusive ‘or’, ~ bitwise negation, “not”.
  • Arithmetic operators: +, -, unary -, unary +, *, /, % remainder, ** exponentiation, << left shift, >> right shift.

The EVM returns a Runtime Exception when the modulus operator is applied to the zero of a “divide by zero” operation.

Address

An address can hold a 20-byte value that is equivalent to the size of an Ethereum address. These address types are backed up with members that serve as the contract base.

For more information on “byte-values”, please click here.

String Literals

A “string literal” or anonymous string, is a type of literal in programming for the representation of a “string” value within the source code of a computer program. For more information on this, please click here.

Unlike C language, string literals in Solidity imply trailing value zeroes. These can be represented by using either single or double quotes--- “foo” or ‘bar.’

For example, “bar” will represent a 3-byte element, rather than four (4).

Similarly, with integer literals, the literals are convertible inherently, using the corresponding fit--byte or string.

Modifier

In smart contracts, modifiers help to ensure the coherence of the conditions defined, before executing the code. In other words, it makes sure the conditions are properly defined, so as to risk inaccurate calculation and thus execution.

Mappings

Solidity provides basic arrays, enums, operators, and hash values to create a data structure, known as “mappings”. These are used to return values associated with a given storage location.

An “array” is a contiguous memory allocation of a size defined by the programmer where if the size is initialized as “K”, and the type of element is instantiated as “T”, the array can be written as T [k].

Dynamic Arrays

Arrays can also be dynamically instantiated using the notation uint [ ] [6]. This means that a dynamic array with six contiguous memory allocations will be initiated, while a two-dimensional array can be initialized as arr [2] [4], where the two indices show the dimensions of the matrix.

Sample Solidity Code

ragma solidity >=0.5.0 <0.7.0;

contract Coin {
// The keyword "public" makes variables
// accessible from other contracts
address public minter;
mapping (address => uint) public balances;
// Events allow clients to react to specific
// contract changes you declare
event Sent (address from, address to, uint amount);
// Constructor code is only run when the contract
// is created
constructor() public {
minter = msg.sender;
}
// Sends an amount of newly created coins to an address
// Can only be called by the contract creator
function mint(address receiver, uint amount) public {
require(msg.sender == minter);
require(amount < 1e60);
balances[receiver] += amount;
}
// Sends an amount of existing coins
// from any caller to an address
function send(address receiver, uint amount) public {
require(amount <= balances[msg.sender], "Insufficient balance.");
balances[msg.sender] -= amount;
balances[receiver] += amount;
emit Sent (msg.sender, receiver, amount);
}
}
Start buying and trading cryptocurrency
Sign up and start opportunities with the future of currency
Sign Up Now