Solidity File Structure

Structure of a Solidity file

SPDX License Identifier

All solidity contracts should have License declaration in the first line.

// SPDX-License-Identifier: MIT

List of licenses from SPDX repository: https://spdx.org/licenses/

Pragmas

pragma is the keyword used to declare the compiler version of Solidity. pragma only applies to the current local file so you must add pragma to all files in the project directory.

// SPDX-License-Identifier: MIT
pragma solidity 0.8.20;

You can use the ^ sign or the comparison operators <, <=, >, >= in conjunction with the compiler declaration.

// SPDX-License-Identifier: MIT
pragma solidity ^0.8.20; // use compiler version 0.8.20 and above
// SPDX-License-Identifier: MIT
pragma solidity >=0.4.22 <0.9.0; // use compiler version bigger or equal to 0.4.22 and less than 0.9.0

Import file from other sources

Import the whole file

Import all and assign alias

Name import

Name import means you will specify the name of the import object from another file. The reason you should use this option is that it makes your code clearer.

Comment

To comment, you can use // and /* */

There is also NatSpec comment with /// or /** **/

Last updated