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.

Please check out the amazing & professional courses on Blockchain, Ethereum, Hyperledger etc. on Tosh Academy.
//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;
    }

}
Default Structure of Solidity Files

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