DEV Community

Shlok Kumar
Shlok Kumar

Posted on

Layout of a Solidity Source File

Solidity Source File Layout-

The solidity source code can include import directives, pragma directives, struct, enum and function declarations as well as error and constant variable definitions.

SPDX License Identifier

The source code of smart contracts may help build trust. The Solidity compiler advocates the use of machine-readable SPDX license IDs since making source code public inevitably raises copyright issues. Every source file should begin with the following license:

//SPDX-License: MIT

The compiler does not check the license, but it does include it in the bytecode information. If you don't want to indicate a license or the code isn't open source, use UNLICENSED.

Adding this remark does not relieve you of other licensing duties, such as mentioning a particular license header in each source file or the original copyright holder.

The compiler recognizes the comment anywhere in the file, although it is suggested towards the top. The SPDX website has further information on using SPDX license identification.

Pragmas

The first line of code in any Solidity file is Pragma. Pragma indicates the compiler version for the current Solidity file.

Solidity is a new programming language that is always being improved. It releases a new version whenever a new feature or upgrade is added. The current version is 0.8.13 as of the time this document is written. Using the pragma directive, you may choose the compiler version and target your code as follows:

pragma solidity ^0.8.13;

Although it is not mandatory, it is good practice to declare the pragma directive as the first statement in a Solidity file. The syntax for the pragma directive is as follows:

pragma solidity <version number>;

The version number has two parts: a major build and a minor build.

The previous example's major build number is 8 and the minor build number is 13. Minor versions have fewer or no major changes, whereas major versions may have considerable changes. Choose the option that best meets your needs.

The caret ’^’ character is optional in version numbers, although it is important in determining the version number based on the following rules:

The ^ character denotes the most recent major version. So, ^0.8.0 relates to the current build number 8, which is 0.8.13. The character will only target the primary build given. The Solidity file will only compile with a major build of 8. No other major build will compile it.

It is best to build Solidity programs using a specific compiler version rather than utilizing ‘^’. Newer versions may deprecate your code if you use pragma. For example, the toss statement was deprecated in favor of newer constructs like assert, need, and reverse. You don't want to be startled when your code begins acting strange.

For more content, follow me on - https://linktr.ee/shlokkumar2303

Top comments (0)