2

I have the following import statement in a Solidity contract ( this works).

import "@openzeppelin/contracts/token/ERC20/IERC20.sol"

The interface I'm importing is at the following repo: https://github.com/OpenZeppelin/openzeppelin-contracts/blob/master/contracts/token/ERC20/IERC20.sol

My question is, what is the syntax or rules I should follow when importing from a github repo to Solidity? what does the @ sign in the import statement mean ?

maskara
  • 329
  • 1
  • 2
  • 12

1 Answers1

3

Your snippet shows a direct import that searches for the file in your local directories based on the compiler config.

One of the default sources is the node_modules directory where NPM packages are installed.

The @ symbol is just a prefix of NPM scoped packages, allowing to group more packages into the same namespace (in this case @openzeppelin/<package_name>).


To import a contract from GitHub, you can just pass its full URL:

import "https://github.com/OpenZeppelin/openzeppelin-contracts/blob/master/contracts/token/ERC20/IERC20.sol";

Again because of the default config, the compiler downloads the remote file from GitHub to a local temp directory, and imports its contents before compiling.

Petr Hejda
  • 40,554
  • 8
  • 72
  • 100
  • It should be noted that without input remappings this github url import won't work locally – ihor.eth Apr 15 '22 at 18:21
  • 1
    @ihorbond Correct, thanks for the clarification. Same goes for NPM import. And both GitHub and NPM remappings are usually already set up in the compiler default config, so in most cases devs don't need to care about configuring the remappings. But in some cases such as Etherscan verification, that might not be configured and throw an error. – Petr Hejda Apr 15 '22 at 18:48