I want to use SafeERC20.sol in my project, purely because of safeTransfer(). The way I do it is I just put the SafeERC20.sol file in my project folder, and then use the line import "./SafeERC20.sol"; at the head of the file, plus using SafeERC20 for IERC20 in the contract. The problem is that as I see it, SafeERC20.sol also uses Address.sol from OpenZeppelin, so I would have to put that file also in my project folder.

Is this the correct approach to import files? Or should I just download all the libraries/folders from OpenZeppelin and put them in my project folder? If I do this and I only use 2 of them, will the compiler only compile those 2 OZ files and not all of them? Thanks

2 Answers 2


You need to install the OpenZeppelin npm package into your project directory via yarn or npm like:

yarn add @openzeppelin/contracts


npm install --save @openzeppelin/contracts

Then import the SafeERC20.sol from those installed packages like:

import "@openzeppelin/contracts/<route to file>/SafeERC20.sol";

Then, you'll need to use it for IERC20 like:

using SafeERC20 for IERC20;

You don't need to import OpenZeppelin's Address.sol. If it's a dependency for IERC20, it will already be imported implicitly when you import SafeERC20.sol.


There are two basic approaches:

  1. Clone or fork the entire OpenZeppelin repository. Then import the files you need. This way you can easily import more files if/when needed, since all the files are there already.

  2. Download only the files you need. The benefit is that your repository stays compact and clean. The downside is that if you need something more, you have to go and get it from OpenZeppelin.

No matter which option you choose, the compiler only includes files needed by your contracts.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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