Structure of a Contract in Solidity

Reading Time: 3 minutes

Smart Contracts for Ethereum are primarily written using Solidity. They are the basic unit of deployment and also used in execution for EVMs. Contracts in Solidity are similar to classes in object-oriented languages. Here we will learn about structure of contract.

Each contract can contain declarations of State Variables, Functions, Function Modifiers, Events, Errors, Struct Types and Enum Types. Furthermore, contracts can inherit from other contracts.

solidity
solidity

Libraries and interfaces are special type of contracts in solidity.

Let’s take a look at very basic smart contract structure which has written in Solidity.

pragma solidity ^0.4.2;
 

 contract Customer {
    string id;

    function setId(string serial) public {
          id = serial;
    }

    function getId() public constant returns (string) {
          return id;
    }
 }

A contract consists of the following multiple constructs:

  • State variables
  • Functionsdefinition
  • Function Modifier
  • Errors
  • Struct Types
  • Function definitions

State Variables

The variables whose values are permanently stored in contract storage are known as State variables.

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

contract SimpleStorage {
    uint storedData; // State variable
    // ...
}

Functions

We define Functions inside a contract . Functions are executable units of code.

It is also possible to define functions outside of the contract.

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

contract SimpleAuction {
    function bid() public payable { // Function
        // ...
    }
}

// Helper function defined outside of a contract
function helper(uint variable1) pure returns (uint) {
    return variable1 * 2;
}

Function Modifier

Function modifiers are like a function that checks the validation rules before executing the main function.

Overloading, that is, having the same modifier name with different parameters, is not possible.

Just as functions are overridden , modifiers can also be overridden.

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

contract Purchase {
    address public seller;

    modifier onlySeller() { // Modifier
        require(
            msg.sender == seller,
            "Only seller can call this."
        );
        _;
    }

    function abort() public view onlySeller { // Modifier usage
        // ...
    }
}

Events

Now lets talk about the Events, Events are functions that are bubbled up after compilation of function execution. If event code written there ,it provide  EVM logging facilities.

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

contract SimpleAuction {
    event HighestBidIncreased(address bidder, uint amount); // Event

    function bid() public payable {
        // ...
        emit HighestBidIncreased(msg.sender, msg.value); // Triggering event
    }
}

Errors

For failure situations, Errors allow you to define descriptive names and data.

In revert statements we use Errors.

In comparison to string descriptions, errors are much cheaper and allow you to encode additional data.

// SPDX-License-Identifier: GPL-3.0
pragma solidity ^0.8.4;

/// Not enough funds for transfer. Requested `requested`,
/// but only `available` available.
error NotEnoughFunds(uint requested, uint available);

contract Token {
    mapping(address => uint) balances;
    function transfer(address to, uint amount) public {
        uint balance = balances[msg.sender];
        if (balance < amount)
            revert NotEnoughFunds(amount, balance);
        balances[msg.sender] -= amount;
        balances[to] += amount;
        // ...
    }
}

Struct Types

A struct is refer as a custom defined data type. And Struct consists of several variables declared in it

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

contract Ballot {
    struct Voter { // Struct
        uint weight;
        bool voted;
        address delegate;
        uint vote;
    }
}

Enum Types

It is used to create custom types. And it has fix the set of values 

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

contract Purchase {
    enum State { Created, Locked, Inactive } // Enum
}

So this was all about smart contract structure in solidity. Stay connected to explore such topics related to Solidity.


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.

solidity

Knoldus-blog-footer-image

Written by 

Ayushi is a Software Developer having more than 1.5 year of experience in RUST. Her practice area is Rust and Go. She loves to solve daily coding challenges.