Contracts in Ethereum using Solidity

Reading Time: 2 minutes

The Contracts in the sense of Solidity is a collection of code and data. That resides at a specific address on the Ethereum blockchain. Contracts in Solidity are similar to classes in object-oriented languages. They contain persistent data in state variables, and functions that can modify these variables.

Creating Contracts

Contracts can be create from outside via Ethereum transactions or from within Solidity contracts. Lets us see a basic contract.

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

contract SimpleContract {
    uint data;

    function set(uint ut) public {
        data = ut;
    }

    function get() public view returns (uint) {
        return data;
    }
}

The above contract sets the value of a variable and exposes it for other contract to access. When a contract is created, its constructor is executed once. A constructor is optional. One constructor is allow which means no overloading.

Visibility and Getters

Solidity knows two kinds of function calls: internal ones that do not create an actual EVM call and external ones that do. There are following types of visibility to functions :-

  • External
  • Public
  • Internal
  • Private

External

External functions are part of the contract interface. Which means they can be call from other contracts and via transactions.

Example :

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

contract SolidityExample {
    function fun(uint one) external add_func returns(uint) { return one + 1; }
}

Public

Public functions are part of the contract interface. We can call them internally or via messages.

Example :

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

contract SolidityExample {
    function setData(uint one) public { data = one; }
}

Internal

Internal functions and state variables can only be accessed internally. The Internal is default visibility level for state variables.

Example :

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

contract SolidityExample {
    function setData(uint one) internal { data = one; }
}

Private

Private functions and state variables are only visible for the contract. They are defined in and not in derived contracts.

Example :

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

contract SolidityExample {
    function fun(uint one) private copy_func returns(uint two) { return one + 1; }
}

Getter Functions

The compiler automatically creates getter functions for all public state variables. State variables can be initialized when they are declared.

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

contract SolidityExample {
           uint public data = 42;
}

contract Caller {
    SolidityExample se = new SolidityExample();
    function func_example() public view returns (uint) {
        return se.data();
    }
}

The getter functions have external visibility. Contracts in Solidity are similar to classes in object-oriented languages.

Thanks for Reading !!

If you want to read more content like this?  Subscribe to Rust Times Newsletter and receive insights and latest updates, bi-weekly, straight into your inbox. Subscribe to Rust Times Newsletter: https://bit.ly/2Vdlld7.


Knoldus-blog-footer-image

Written by 

I am Software Consultant at Knoldus and I am curious about learning new technologies.