Solidity Tutorial

How to Import External Files in Solidity with Examples

Share on twitter
Share on linkedin
Share on whatsapp
Share on facebook
Share on telegram

Contents

Import in Solidity

In Solidity the import command lets you import external solidity files into your current solidity code. This helps to keep your entire project in a modular codebase.

Solidity lets you import the entire file and its symbols into your current code, although it may not be always advisable to load external files completely. Instead, Solidity also allows you to import only the required symbols explicitly which is a more recommended way.

How to Import Files in Solidity

Let us assume that there is a solidity file Demo.sol that contains the two contracts Demo1 and Demo2 as shown below. In the subsequent examples, we will see how to import files in Solidity in different ways.

// SPDX-License-Identifier: MIT
pragma solidity ^0.8.10;

contract Demo1
{
    string public line = "This line is from Demo1.sol";
}

contract Demo2 
{
    string public line = "This line is from Demo2.sol";
}

 

Import Entire File

Example 1

In the below example the entire Demo.sol file is imported which is placed in the current directory.  To verify that it has been imported properly the Demo1 contract is initialized and its line variable is called inside getLine function.

Although it is not shown here, you can also access Demo2 of Demo.sol since the entire file is imported.

// SPDX-License-Identifier: MIT
pragma solidity ^0.8.10;

// Import Demo.sol from current directory
import "./Demo.sol";

contract Import 
{
   // Initialize Demo2 contract
    Demo1 public demo = new Demo1();

    // Getting line variable of Demo1 contract
    function getLine() public view returns (string memory) 
    {
        return demo.line();
    }

}

 

Example 2

In this approach, a new global symbol DemoFile is created while importing Demo.sol due to which all global symbols of Demo.sol become members of DemoFile.

You may notice that to initialize the Demo1 contract we are now using DemoFile.Demo1 in the below example.

// SPDX-License-Identifier: MIT
pragma solidity ^0.8.10;

// Import Demo.sol as global symbol DemoFile from current directory
import * as DemoFile from "./Demo.sol";

contract Import 
{
   // Initialize Demo2 contract
    DemoFile.Demo1 public demo = new DemoFile.Demo1();

    // Getting line variable of Demo1 contract
    function getLine() public view returns (string memory) 
    {
        return demo.line();
    }

}

 

Example 3

This is another variation of importing the Demo.sol file with a new symbol DemoFile.

// SPDX-License-Identifier: MIT
pragma solidity ^0.8.10;

// Import Demo.sol as global symbol DemoFile from current directory
import "./Demo.sol" as DemoFile;

contract Import 
{
   // Initialize Demo2 contract
    DemoFile.Demo1 public demo = new DemoFile.Demo1();

    // Getting line variable of Demo1 contract
    function getLine() public view returns (string memory) 
    {
        return demo.line();
    }

}

 

 

Import Specific Symbols from File

Example 1

In the below example we have imported only one specific contract Demo2 from the Demo.sol file

// SPDX-License-Identifier: MIT
pragma solidity ^0.8.10;

// Importing only Demo2 contract
import {Demo2} from "./Demo.sol";


contract Import 
{
   // Initialize Demo2 contract
    Demo2 public demo = new Demo2();

    // Getting line variable of Demo2 contract
    function getLine() public view returns (string memory) 
    {
        return demo.line();
    }

}

 

Example 2

It is also possible to import only a specific symbol from the file and give it a new alias. In the below example Demo2 contract of Demo.sol file is imported as an alias DemoContract.  Subsequently, DemoContract is used to initialize the Demo2 contract. If this alias is not used then it will give a compilation error.

// SPDX-License-Identifier: MIT
pragma solidity ^0.8.10;

// Importing only Demo2 contract as alias DempContract
import {Demo2 as DemoContract} from "./Demo.sol";


contract Import 
{
   // Initialize Demo2 contract
    DemoContract public demo = new DemoContract();

    // Getting line variable of Demo.sol
    function getLine() public view returns (string memory) 
    {
        return demo.line();
    }

}