Learn Solidity: Layout of a Solidity Source File & Structure of Smart Contracts

In this post, we will understand the default standard structure of the Solidity-Based Smart Contract file which generally has the .sol extension.

Advertisements
World is looking for a Certified Blockchain Expert Become one

Notice: This is one of the multi-post series of Learn Solidity - Build Decentralized Application in Ethereum. This is an attempt to teach you all about Solidity - A Programming Language for Ethereum Based Smart Contracts. If you want to take this as a video course please signup using below button.
Default Structure of Solidity Files
//versiona pragma
pragma solidity 0.4.8; 

//import section
import "filename";

//begin the contract
/// @title This is the layout of the solidity code
contract ContractName {
    /*
    * @title A Simple Layout Example
    * @author Toshendra Sharma
    * @notice Example for the Solidity Course
    * @dev This line is for developers only
    * 
    */

    // This is a single-line comment.

    /*
    This is a
    multi-line comment.
    */

    // State Variables
    address public stateVariable1;
    uint public stateVariable2;
    uint private stateVariable3;
    string public constant HOST_ID = 0x1234;

    // Events
    event LogEvent1(address param1, uint param2);
    event LogEvent2(address param1);
    event LogEvent3();

    // Function Modifiers
    modifier onlyIfOwnerModifier() { 
        if (msg.sender != owner) throw;
        _;
    }

    modifier onlyIfMortalModifier() { 
        if (msg.sender != mortal) throw;
        _;
    }

    // Struct, arrays or Enum if any here
    enum enum1 { val1, val2, val3 }
    struct struct1 { 
        uint weight;
        uint height;
        address location;
    }
    mapping (address => uint) balances;


    // Define consutruct here
    function ContractName(uint initialCoins) {
       // Initialize state variables here
    }

    /// @dev Comment for developers
    /// @param parameters details
    /// @return return variable details
    function function1(address param1, uint param2) {
       //body of function here
       //
       //
    }

    /// @dev Comment for developers
    /// @param parameters details
    /// @return return variable details
    function function2(address param1, uint param2) {
       //body of function here
       //
       //
    }

    //default function
    function(){
        throw;
    }

}

As you can see in the above code, It always starts with version pragma followed by optional import section through which you can import any existing.sol files into the current .sol file. Then we begin the smart contract, write the inline comments for the auto-generation of the documentation for developers & define the state variables, events, & modifiers without any particular order.

We then write the constructor of the contract which gets called at the time of the creation of the contract. All the parameters of the constructor must be passed during the creation of the contract.

Then we continue writing the functions and at last (which is optional) we define the default function which will be called if the called function is not defined in the contract.

Let’s move the to the next post and understand the General Value Types in Solidity.

Previous: Basics of Solidity By Example? Next: General Data Types in Solidity