OpenZeppelin – How to Use SafeERC20.sol for Beginners

openzeppelin

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

Best Answer

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

yarn add @openzeppelin/contracts

or

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.

Related Topic