3

How can they be used with solc?

3

You may want to take a look at the docs on Layout of a Solidity Source File.

The solidity compiler can only reference files that exist locally on your computer. So importing directly from GitHub (as an example) is not possible.

Now, let's imagine you want to use OpenZeppelin's amazing library of smart contracts, @openzeppelin/contracts-ethereum-package:

pragma solidity 0.5.11;

import "@openzeppelin/contracts-ethereum-package/contracts/math/SafeMath.sol";

contract MyContract {
    using SafeMath for uint256;
    ...
}

If you're using truffle, this is easy-peasy. You just run truffle compile and the imports are magically sorted out for you.

If you want to use solc, things get a bit more complicated. You have to specify the following:

  • "prefix" = the path that's used in your smart contract, i.e. "@openzeppelin/contracts-ethereum-package"
  • "target" = the absolute path of OpenZeppelin's contracts downloaded on your computer

The format looks like this:

solc prefix=target ./MyContract.sol

Let's plug & play the paths:

solc --bin @openzeppelin/contracts-ethereum-package=/Your/Absolute/Path/To/@openzeppelin/contracts-ethereum-package ./MyContract.sol

(I specified --bin so that the compiler will print the bytecode in your terminal)

If you downloaded the OpenZeppelin library via npm, it should be located in the "node_modules" folder at the root of your repo.

Caveats:

  • If you don't have the solc compiler, check out the docs to ensure you get it right for your operating system.
  • Do NOT use npm/node to install the compiler, since it has less features and remappings don't work.
  • Different tools may treat remappings differently. At the time of writing this answer, sol-compiler@3.1.13 was generating a different data area at the end of the contract bytecode than truffle@5.0.33.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.