Solidity Tutorial: All About Imports | by Jean Cvllr | Mar, 2022

Picture by Tim Schmidbauer on Unsplash
Modules and Code Modularity.Solidity Imports.Forms of Solidity Import Syntax.
* World Import
* Particular Imports
Import Aliases.
* World Alias
* Alias Particular
Which import syntax to make use of?What are you able to import from a Solidity file?Solidity Import Syntax Cheatsheet
  • Creating reusable items that different information can import.
  • Making it simpler to grasp and digest your entire Solidity codebase of your challenge.
  • Making it simpler to work with the “Solidity modules” by specializing in smaller information (helpful when debugging).
import “./MySolidityFile.sol”;

World Imports

import “./MySolidityFile.sol”;
  1. All world symbols (what I name “Solidity Objects”) outlined in “./MySolidityFile.sol” get imported into the present world scope.
  2. All world symbols imported inside “./MySolidityFile.sol” get additionally imported into the present world scope.

“This type isn’t really helpful to be used as a result of it unpredictably pollutes the namespace.”

When you add new top-level objects inside “filename”, they mechanically seem in all information that import like this from “filename”.

Particular Imports

Import  One thing  from “./MySolidityFile.sol”;

“solely import what you want”

  • Aliases for world imports: can then be used to discuss with particular objects outlined within the file, utilizing the dot “.” syntax (like object properties).
  • Aliases for particular imports: allow to rename objects or symbols imported from a Solidity file, as an illustration, to keep away from naming collision (or to present higher identify for higher code readability if wanted).

World Aliases

// longer syntax
import * as Endpoints from “./Endpoints.sol”.
// shorter syntax
import “./Endpoints.sol” as Endpoints;

Particular Aliases

import  Level as Coordinate, GPS  from “./Endpoints.sol”;
  • Keep away from naming collisions: in instances the place what you need to import is already imported by different information. When you encounter naming collisions whereas importing, use aliases to resolve this.
  • Stop sudden behaviour (if the compiler didn’t flag it, and a variable you might be utilizing references the incorrect variable imported from elsewhere).
  • Rename what you might be importing: if you wish to use extra significant names in your context and in your code readability.

“On the precept that clearer code is healthier code, you must import the belongings you need to use in a module.”

“Wildcard imports (from <module> import *) needs to be prevented, as they make it unclear which names are current within the namespace, complicated each readers and lots of automated instruments.”


  • Runs the danger of complicated the maintainers and customers of Solidity contract or library.
  • Reduces code readability. Builders utilizing your Solidity contracts or library can have issue figuring out the place the imported names come from.

Bugs associated to namespace

  • Importing every part clutters the native “namespace”, making debugging tougher.
  • Imported names can change if you replace your dependencies. Due to this fact, a wildcard import that works as we speak may be damaged tomorrow.
  • contract
  • library
  • interface
  • enum (outlined on the file degree)
  • struct (outlined on the file degree)
  • error definitions (outlined on the file degree)
  • performs (outlined on the file degree, since Solidity 0.7.1)
  • User-defined Value Types (since their introduction in Solidity 0.8.8)
import "./modules/MySolidityFile.sol";
import * as mySolidityModule from "./modules/MySolidityFile.sol";
import "./modules/MySolidityFile.sol" as mySolidityModule;
import  MyContract  from "./modules/MySolidityFile.sol";
from "./modules/MySolidityFile.sol";
reallyReallyLongSolidityModuleExportName as solidityModule
from "./modules/MySolidityFile.sol";
reallyReallyLongSolidityModuleExportName as solidityModule,
anotherLongSolidityStructName as MyStruct
from "/modules/my-module.js";

More Posts