INTRODUCTION TO SOLIDILITY

Tseke Daniel
4 min readSep 30, 2023

--

Solidity is an object-oriented, high-level programming language for implementing Smart contracts created by Ethereum which is the second-largest market of cryptocurrency by capitalization. It is designed to target the Ethereum Virtual Machine. It is statically typed, supports inheritance, libraries and complex user-defined types among other features.

Ethereum Virtual Machine (EVM)

The Ethereum Virtual Machine or EVM is a piece of software that executes smart contracts and computes the state of the Ethereum network after each new block is added to the chain. EVM is like the brain of Ethereum that handles the blockchain’s data and allows smart contracts to work. It allows us to perform credible transactions without any interference from the third party, these transactions are trackable and irreversible.

Smart Contracts

Smart contracts are high-level program codes compiled into EVM before being posted to the Ethereum blockchain for execution. It enables you to conduct trustworthy transactions without the involvement of a third party; these transactions are traceable and irreversible.

How to Get Started with Solidity Programming?

There are 3 types of variables in Solidity

  1. Local variable: Declared inside a function and are not stored on blockchain. This type of variable is usually used to store temporary values.
  2. State variable: Declared outside a function to maintain the state of the smart contract. The values of these variables are permanently stored in the contract storage. Variables are stored on the blockchain.
  3. Global: Provide information about the blockchain. They are injected by the Ethereum Virtual Machine during runtime. Includes things like transaction sender, block timestamp, block hash, etc.
   //SPDX-License-Identifier: MIT

pragma solidity ^0.8.19;

// Create a contract named Test
contract Test {

//Declaring State variables
uint public myUint = 10;
int public myInt = -10;
string public myName = "Daniel";
address public myAddress = 0x5B38Da6a701c568545dCfcB03FcB875f56beddC4;
bool public myBool = true;

function localVariable() {
// Declaring Local Variables

uint ui = 20;

// Declaring Global Variables
address sender = msg.sender; //address of the caller
uint timestamp = block.timestamp; // Timestamp of the current block

}
}



Explanation:

1. License Identifier:

 //SPDX-License-Identifier: MIT

This is a comment that specifies the license for the smart contract. In this case, it signifies that the smart contract can be used under the terms of the MIT License, which is a permissive open-source license.

2. Version Pragma:

pragma solidity ^0.8.19;

pragma solidity” indicates that you’re providing instructions to the Solidity compiler. ‘‘^0.8.19” specifies a version range which in this case means that the contract is intended to be compiled using a Solidity compiler version greater than or equal to 0.8.19 but less than the next major version (e.g., 0.9.0).

3. The contract keyword:

contract Test{

}

The contract keyword declares a contract under which is the code wrapped inside.

4. State variables:

      
//uint stands for unsigned integer, meaning non negative integers
uint public myUint = 10;
//Negative numbers are allowed for int types.
int public myInt = -10;
string public myName = "Daniel";
//address stands for an ethereum address
address public myAddress = 0x5B38Da6a701c568545dCfcB03FcB875f56beddC4;
//bool stands for boolean
bool public myBool = true;



// Default values
// Unassigned variables have a default value in Solidity
bool public defaultBoo2; // false
uint public defaultUint; // 0
int public defaultInt; // 0
address public defaultAddr; // 0x0000000000000000000000000000000000000000

uint indicates that a variable is an integer. Adding a number after it (such as uint256 or uint18) specifies the maximum size it should take, but uint assumes uint256 by default.

public” means that the variable can be accessed internally by the contract and can also be read by the external world.

5. A function declaration:

function set(uint x, uint y) public
function get() public view returns (uint)

This is a function named “set” of access modifier type public which takes a variable x and variable y of data type unit as a parameter. It will retrieve and print the value of the state variable sum using the “get” function.

How to Execute the Code:

There are practically two ways to execute a solidity smart contract:

1. Offline Mode: Running a solidity smart contract in Offline mode requires three prerequisites and 4 major steps to be followed to get the smart contract running:

Prerequisites:

  • Download and install node.js.
  • Install Truffle globally.
  • Install ganache-cli.

Objectives:

  • Create a truffle project and configure a development network
  • Create and deploy smart contracts
  • Interact with the smart contract from Truffle console
  • Write tests for testing the main features offered by Solidity.

2. Online Mode: Remix IDE is generally used to compile and run Solidity smart contracts in the Online Mode.

--

--