How to use OpenZeppelin to implement ERC standards in Solidity

In this guide, we are going to learn how to use OpenZeppelin to implement ERC standards and create smart contracts based on these standards.

In this guide, we are going to learn how to use OpenZeppelin to implement ERC standards in Solidity and create smart contracts based on these standards.

Using OpenZeppelin, you don't need to rewrite all the code to implement an ERC standard.

You can just use their implementation and derive your smart contract from it.

The most used ERC standards are the ERC-20 and ERC-721 that are used respectively for tokens and NFTs.

But these standards are not the only ones that OpenZeppelin implements, you also have ERC-777 and ERC-1155. You can find more information about it in the documentation.

The good thing is that no matter what standard you choose, the process to integrate it to your project is the same!

All you have to do is install OpenZeppelin in your project.

Using OpenZeppelin with Hardhat and NPM

If you're using Hardhat or you just want to use NPM (or yarn), run this command to install OpenZeppelin:

npm install @openzeppelin/contracts

Using OpenZeppelin with Brownie

If you're using Brownie, you can install OpenZeppelin with this command:

brownie pm install OpenZeppelin/openzeppelin-contracts@4.0.0

Note that you can replace @4.0.0 with the version that you want to use. You can see the version you should use by going to OpenZeppelin's documentation and see the latest version available for your standard.

Using OpenZeppelin smart contracts

First, you need to import the smart contract(s) you want to inherit from.

Let's say you want to create an ERC-721 smart contract. You would import it and create your contract that way, if you used npm:

// contracts/MyNFT.sol
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;

import "@openzeppelin/contracts/token/ERC721/ERC721.sol";

contract MyNFT is ERC721 {
    constructor() ERC721("MyNFT", "MNFT") {
    }
}

And if you're using Brownie:

// contracts/MyNFT.sol
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;

import "OpenZeppelin/openzeppelin-contracts@4.0.0/contracts/token/ERC721/ERC721.sol";

contract MyNFT is ERC721 {
    constructor() ERC721("MyNFT", "MNFT") {
    }
}

If you're using the ERC-20 standard, the import path would become:

// with Hardhat and NPM
@openzeppelin/contracts/token/ERC20/ERC20.sol

// with Brownie:
OpenZeppelin/openzeppelin-contracts@4.0.0/contracts/token/ERC20/ERC20.sol

To find the path to the contract you want to use, you can go to OpenZeppelin's Github repository right here:

GitHub - OpenZeppelin/openzeppelin-contracts: OpenZeppelin Contracts is a library for secure smart contract development.
OpenZeppelin Contracts is a library for secure smart contract development. - GitHub - OpenZeppelin/openzeppelin-contracts: OpenZeppelin Contracts is a library for secure smart contract development.

The path to the smart contract you want is going to be the same as the path where you'll find your smart contract in the repository.

For instance, the ERC-721 code is in contracts/token/ERC721/ERC721.sol so the import path is going to be:

  • for NPM: @openzeppelin/ followed by the path to the ERC-721 smart contract above
  • for Brownie: OpenZeppelin/openzeppelin-contracts@4.0.0/ followed by the path to ERC-721 above

Next you can simply inherit from that smart contract using the is keyword:

contract MyNFT is ERC721 {
    // ...
}

And use the constructor of that smart contract in your constructor:

contract MyNFT is ERC721 {
    constructor() ERC721("MyNFT", "MNFT") {
    }
}

It works the same for ERC20 and all other smart contracts.

And that's it 🎉

Thank you for reading this article, if you want to go further and get better at blockchain development, leave your email below and you'll get:

  • access to the private Discord of a community of Web3 builders
  • access to free guides that will teach you a subject from scratch like the Web3 JS Cheat Sheet