Creating a Solidity Source File

A Solidity source file has a .sol extension and is composed of a header and the source code of one or several smart contracts.

Solidity headers

The header of a Solidity source file contains a directive to the compiler, known as a pragma, and standard comments (that are understood by the compiler) describing some metadata.

In Solidity, single-line comments begin with //, and multiple-line comments are written between /** and */.

SPDX license identifier

The first thing we specify is the license under which the source file will be published, using the standard comment // SPDX-License-Identifier:.

Any license type from the Linux Foundation's SPDX License List can be specified.

Pragma directive

A pragma statement specifies which version of the Solidity compiler should be used with our source file. We can restrict compilation to a specific version, for instance, pragma solidity 0.8.15, which is the latest compiler version (as of the time of this writing), but for the sake of backward compatibility, it is also possible to specify a range of compiler versions.

For instance, pragma solidity ^0.8.15 means any version later than 0.8.15, or pragma solidity >=0.5.0 <0.9.0, any version later than 0.5.0 and earlier than 0.9.0.

Other metadata

We can also optionally identify the author of the source code, and give it a human-readable title, with the standard comments /// @author and /// @title, respectively.

These are known as NatSpec comments (for Natural Language Specification) and are promoted by the Ethereum foundation as a standard to provide rich documentation. The following six tags can be used in the NatSpect format:

NatSpec Tags

Tag

Description

Context

@title

Human-readable title for the contract

Contracts

@author

Author's name

Contracts, functions

@notice

Explanation of functionality

Contracts, functions

@dev

Additional details on functionality

Contracts, functions

@param

Followed by the parameter name and its meaning

Functions

@return

Return value of a function

Functions

Solidity smart contract declaration

Smart contracts in Solidity define persistent state variables, as well as functions to modify these variables and interact with the blockchain at large. The execution of any function in a smart contract has to be initiated by an Externally Owned Account, through a transaction.

To declare a smart contract, we use the contract keyword, followed by its name, and source code inside curly brackets.

Example of a Solidity source file

Below is an example of the structure of a Solidity source file under an MIT license and to be compiled by any compiler later than version 0.5.0 and earlier than version 0.9.0.

Press + to interact
// SPDX-License-Identifier: MIT
pragma solidity >=0.5.0 <=0.9.0;
/**
* @title Solidity source file example
* @author ndehouche
* @notice Shows the structure of a Solidity source file
*/
contract exampleContract {
// State variables, functions, modifiers, and events are declared here
}
  • Line 1: We specify the license type, an MIT license, for instance.

  • Line 2: We write the compiler version for our contracts. Here, we allow any version from 0.5.0 to 0.9.0.

  • Lines 3–7: We specify the authorship information and a notice about what the contract does.

  • Line 8: We declare a contract named exampleContract, the body of which will be written between the curly brackets that follow.

  • Other contracts can be declared in the same source file.