Begin with Solidity

Begin with Solidity

Sneha Maurya's photo
Sneha Maurya
·Jan 16, 2022·

5 min read

gm! there. To understand what is Web3 is all about I have been learning and developing some small Dapps and trying to see through the lens of these decentralized applications. You must be wondering from where I'm getting this hands-on experience with DApp. I got you covered. buildspace (tada!!!), they amazing DApps projects to practice and understands more about Web3 and the tools to implement it. Without any further due let's learn to write some solidity code for your first smart contract.

"Solidity is an object-oriented, high-level language for implementing smart contracts".

Before we jump to Solidity and language understanding let's understand what is this Smart Contract is. Smart Contracts are programs stored on the blockchain and intended to execute automatically when predetermined conditions or agreements between two parties are met without any third-party interference.

Build Smart contracts with.gif

If you want to deploy any smart contract on the Ethereum Blockchain network, you need to program that contract using solidity as a programming language.

Solidity is a statically-typed curly-braces language that runs on Ethereum Virtual Machine. It is highly influenced by c++, python, and javascript. It is partially designed after ECMAScript therefore you may have heard it is more similar to javascript. It follows the concept of inheritance as well.

Solidity at a glance

Contract Structure

// SPDX-License-Identifier: MIT
pragma solidity ^0.8.7;

contract GMWorld{
    string private _msg;  // State variable

    function setMessage(string message) public {

    function getMessage() public view returns(string){
        return _msg;
  • the first line in the code below tells you the source code is licensed under MIT.
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.7;
  • the second line is important because it tells you what version of the solidity compiler is valid to compile this file. (source code) The caret symbol (^) before the version number tells Solidity that it can use the latest build in a major version range. but it is better practice to drop the caret and tell solidity exactly what compiler version you expect. note: this version always matches with your solidity compiler, you'll get errors if the version in your code mismatches with the version from the actual (build-in) solidity compiler.

We define our smart contract by adding the contract keyword before defining the name of your contract, similar to the object-oriented programming language we define class by the class keyword. In the above code, my contract name is GMWorld.

Each contract can contain declarations of State Variables, Functions, Function Modifiers, Events, Errors, Struct Types, and Enum Types.

State Variable:

A variable whose value is stored in its contract storage. we can understand the state variable as a Global variable that stores the variable value in the contract storage.

As we know Solidity is a statically-typed programming language which means we need to define the type of a variable(state or local).

Data Type of state variable: Boolean Integer Address String Literals Enums

Function Types: In solidity, we also declare a function with types that are external or internal.

external typed functions are called only outside the contract, they can't be called from inside a contract where it is declared. (i.e f() doesn't work in contract) internal typed functions are called only inside its contract (i.e. from within the current contract or contracts deriving from it) but not from outside the contract. public typed functions or state variables can be called publicly (from outside as well as from inside the contract). private typed functions or state variables are only visible for the contract they are defined in and not in derived contracts.


Arrays can have a compile-time fixed size, or they can have a dynamic size.

pragma solidity ^0.8.9;
contract MyContract {

      uint [] firstArr;  // Arrays in solidity with dynamic size
      uint [] secondArr =new unit[](4) // Arrays in solidity with fixed size

      function foo() external {   // CRUD

         // create

          // read
          console.log( firstArr[1]);

        //   update

          delete firstArr[1];  


Solidity provides a way to define new types in the form of structs.

 struct User{  //user defined data type
        address _address;
       string firstName;
       uint score;
User[] users;

    function foo(string calldata _name) external{ //CRUD
        User memory user1=User(msg.sender,_name,0);  //create a new struct

         //read user field

         // update
         user1.score =20;

         delete user1;



Mapping Types

Mapping type uses mapping(_KeyType => _ValueType) _VariableName the _keyType and _ValueType can be of any type built in types or user defined.

let's take look at the code

// SPDX-License-Identifier: GPL-3.0
pragma solidity >=0.4.0 <0.9.0;

contract MappingExample {
    mapping(address => uint) public balances; //key is address type and value is integer type

    function update(uint newBalance) public {
        balances[msg.sender] = newBalance;  //(msg.sender is address who is executing  contract)

contract MappingUser {
    function f() public returns (uint) {
        MappingExample m = new MappingExample();
        return m.balances(address(this));


Address type comes in two flavors, address and address payable, where it holds 20 bytes of address(address of Ethereum contract) address payable comes with additional member transfer and send if the address is payable where one can send ether to whereas only address type does not give such (ether transaction) facility.

There we go! we have completed Types in Solidity and are ready to dive in. For understanding more in-depth of each type and type conversion please go through the solidity document.

Share this