📚
Open Polkadot Bootcamp
  • 📚 About the Bootcamp
    • 📖 Additional Resources
    • 👐 Ask For Support
  • 📖 Curriculum
  • 📕Rust Programming Language
    • Basic Rust
      • Introduction to Rust
        • 🧑‍💻 Excercises
      • Common Programming Concepts
        • 🧑‍💻 Excercises
      • Program Life Cycle
        • 🧑‍💻 Excercises
      • Ownership & Borrow Checker
      • Common Data Structures
    • Advanced Rust
      • Generic types, Trait extension and Advanced types
      • Lifetime Notation
      • Smart pointers & Macros
      • Common design patterns in Rust
      • Package management & How to structure your Rust project
      • Overview of the Rust ecosystem
  • 📘Building a blockchain with Polkadot SDK
    • Polkadot
      • Additional Reads
        • Why do you want to build a blockchain on Polkadot?
        • Understanding the sharded network design of Polkadot
      • Development on Polkadot
    • Polkadot SDK
      • Substrate
        • Create a new blockchain
          • 🧑‍💻 Exercise: Clone the minimal template
          • Understanding the architecture
          • Break down the node architecture
          • Introducing to Pop CLI tool
        • Adding a custom logic to runtime
          • 🧑‍💻 Exercise: Rust State Machine
          • Components of a Pallet
          • Hooks
          • Weights & Benchmarking
          • Extensions
            • Signed Extensions
            • Transaction Extensions
        • Common runtime modules
          • 📕Example: System Pallet
          • 📕Example: Contracts Pallet
          • 📕Example: Assets Pallet
          • 📕Example: Utility Pallet
        • Runtime API and RPC
        • Runtime upgrade
        • Bump Polkadot SDK versions
      • Cumulus
        • Introduction to Cumulus
          • Parachain from scratch
          • 🧑‍💻 Exercise: Build a parachain from scratch
        • Running a local relaychain network
          • Register & reserve a parachain
          • Launch the network & run a collator node
          • Launch the network with Pop CLI
        • Agile Coretime
    • Polkadot Hub
  • 📒Smart Contract Development
    • Introduction
      • Introduction to PolkaVM
      • Getting started with Solidity development
      • Solidity File Structure
      • Contract Structure
    • Basic Solidity
      • Value types
      • Reference Types
      • Mapping Types
      • Simple Storage
    • Advanced Solidity
      • Units
      • Global Variables
      • Expression and Control Structures
      • Advanced Storage
      • Contract Tests
      • Contracts
Powered by GitBook
On this page
  • Structure of a Solidity file
  • SPDX License Identifier
  • Pragmas
  • Import file from other sources
  • Comment
  1. Smart Contract Development
  2. Introduction

Solidity File Structure

PreviousGetting started with Solidity developmentNextContract Structure

Last updated 5 months ago

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:

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 "FileName"

Import all and assign alias

import * as symbolName from "FileName";

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.

import {ContractOne as alias, ContractTwo} from "FileName";

Comment

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

// comment for 1 line.

/*
Comment for
multiple lines
*/
// SPDX-License-Identifier: GPL-3.0
pragma solidity >=0.4.16 <0.9.0;

/// @author The Project Team
/// @title A simple storage example
contract SimpleStorage {
    uint storedData;

    /// Store `x`.
    /// @param x the new value to store
    /// @dev stores the number in the state variable `storedData`
    function set(uint x) public {
        storedData = x;
    }

    /// Return the stored value.
    /// @dev retrieves the value of the state variable `storedData`
    /// @return the stored value
    function get() public view returns (uint) {
        return storedData;
    }
}

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

📒
https://spdx.org/licenses/
NatSpec