How to create an ERC721 token with OpenZeppelin (2024)

This is lesson 4 of the Smart Contracts 101 free course by Rohas Nagpal.

There are 2 ways to build a Smart Contract:

  • Option 1: Start from scratch and code it yourself.

  • Option 2: Use the OpenZeppelin collection of secure & tested smart contracts as building blocks.

I strongly recommend option 2 - it minimizes risk by using "battle-tested libraries" of smart contracts for Ethereum and other blockchains.

This post will walk you through the process of creating an ERC721 token (commonly called a non-fungible token) with the OpenZeppelin Contracts Wizard.

1. Let’s get started

Go to the OpenZeppelin Contracts Wizard and click on ERC721.

Here’s what your screen will look like:

How to create an ERC721 token with OpenZeppelin (1)

Enter the name & symbol e.g. Rohas Nagpal & ROHAS.

Let’s add https://www.rohasnagpal.com as the base URI. This will be concatenated with token IDs to generate the token URIs.

Here’s what your code will look like:

How to create an ERC721 token with OpenZeppelin (2)

Here’s what it means:

// SPDX-License-Identifier: MIT

This is a comment indicating that the smart contract is licensed under the MIT License, a permissive open-source license.

pragma solidity ^0.8.9;

This line specifies the version of the Solidity compiler required to compile the smart contract. In this case, it requires a version compatible with 0.8.9.

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

This line imports the ERC721 contract from the OpenZeppelin library.

contract RohasNagpal is ERC721

This line defines a new smart contract called "RohasNagpal" that inherits from the ERC721 contract imported from OpenZeppelin. By inheriting the ERC721 contract, RohasNagpal will have all the standard functionality of an ERC721 token.

constructor() ERC721("Rohas Nagpal", "ROHAS")

This is the constructor of the RohasNagpal contract. It will be called only once when the contract is deployed. The constructor calls the constructor of the ERC721 contract with two parameters: the name of the token ("Rohas Nagpal") and the symbol of the token ("ROHAS").

When deployed, this smart contract will create a new ERC721 token named "Rohas Nagpal" with the symbol "ROHAS", utilizing the well-tested and secure OpenZeppelin ERC721 contract as its foundation.

function _baseURI() internal pure override returns (string memory) {return "https://www.rohasnagpal.com";

This is a function that overrides the _baseURI() function of the ERC721 contract. It returns the base URI for the tokens, which is the URL "https://www.rohasnagpal.com".

2. Let’s add some features

Now you can tick/check the various options under the FEATURES section. As you check an option, you will notice that some code gets added.

Here’s what each option means:

Mintable

Mintable: Privileged accounts will be able to create more supply.

How to create an ERC721 token with OpenZeppelin (3)
function _baseURI() internal pure override returns (string memory) {return "https://www.rohasnagpal.com";

This function allows the contract owner to mint new tokens. It calls the _safeMint() function of the ERC721 contract, which mints a new token and assigns it to the specified address.

Burnable

Burnable: Token holders will be able to destroy their tokens.

contract RohasNagpal is ERC721, ERC721Burnable, Ownable {

ERC721Burnable is another OpenZeppelin contract that extends ERC721 and adds the functionality to burn (or destroy) tokens. This can be useful if a token needs to be removed from circulation or if a mistake was made when creating a token.

Pausable

Pausable: Privileged accounts will be able to pause the functionality marked as whenNotPaused. Useful for emergency response.

How to create an ERC721 token with OpenZeppelin (4)

This defines two public functions:

These can only be called by the contract owner to pause & unpause the token contract, respectively.

Additionally, it overrides the internal _beforeTokenTransfer function to ensure transfers are only allowed when the contract is not paused.

Votes

Votes: Keeps track of historical balances for voting in on-chain governance, with a way to delegate one's voting power to a trusted account.

Enumerable

Allows on-chain enumeration of all tokens or those owned by an account. Increases gas cost of transfers.

URI Storage

Allows updating token URIs for individual token IDs.

3. Let’s set the access control

Now you can tick/check the various options under the ACCESS CONTROL section. These options restrict who can access the functions of a contract or when they can do it.

Here’s what each option means:

  • Ownable: Simple mechanism with a single account authorized for all privileged actions.

  • Roles: Flexible mechanism with a separate role for each privileged action. A role can have many authorized accounts.

4. Let’s add upgradeability

Now you can tick/check the various options under the UPGRADEABILITY section. Smart contracts are immutable by default unless deployed behind an upgradeable proxy.

Here’s what each option means:

  • Transparent: Uses a more complex proxy with higher overhead, and requires fewer changes in your contract. Can also be used with beacons.

  • UUPS: Uses simpler proxy with less overhead, and requires including extra code in your contract. Allows flexibility for authorizing upgrades.

5. Let’s add a security contact

Enter the email address where people can contact you to report security issues.

6. Download your smart contract code

You can download the code by clicking on the Download button in the top right corner of the wizard.

7. Compile the smart contract

Now that the code for the smart contract has been written, it needs to be compiled into bytecode that can be executed by the Ethereum Virtual Machine (EVM).

This is typically done using an Integrated Development Environment (IDE) like Remix. An IDE is a software application that provides tools for writing, testing, and debugging code.

Now click on the Open in Remix button on the top right corner of the wizard.

Note: Make sure to use the same version of a compiler that is mentioned in your code.

How to create an ERC721 token with OpenZeppelin (5)

Click on Compile contract.

After a few seconds, you will see a green tick in the right column:

How to create an ERC721 token with OpenZeppelin (6)

8. Deploy the smart contract

Once the smart contract has been written & compiled, it can be deployed to the blockchain (e.g. Ethereum). This involves sending a transaction to the blockchain network that contains the bytecode for the smart contract.

Click on the Ethereum logo below the green tick.

A screen like this will show up.

How to create an ERC721 token with OpenZeppelin (7)

Select the environment / test network.

How to create an ERC721 token with OpenZeppelin (8)

Your Metamask wallet will open up. Select your account address, and then click on Deploy.

How to create an ERC721 token with OpenZeppelin (2024)

FAQs

How to create an ERC721 token with OpenZeppelin? ›

The ERC-721 token smart contract demonstrates how to create and transfer non-fungible tokens. Non-fungible tokens represent ownership over digital or physical assets. Example assets are artworks, houses, tickets, etc. Non-fungible tokens are distinguishable and we can track the ownership of each one separately.

How to create ERC721 token? ›

4 Steps to Create an ERC721 Token on Kaleido
  1. Step 1: Create a Kaleido Account.
  2. Step 2: Create a Blockchain Network.
  3. Step 3: Create a Token Pool.
  4. Step 4: Create Your Token.
  5. Step 5: Transfer Your NFT to Metamask.
Feb 13, 2024

What is an example of ERC721 token? ›

The ERC-721 token smart contract demonstrates how to create and transfer non-fungible tokens. Non-fungible tokens represent ownership over digital or physical assets. Example assets are artworks, houses, tickets, etc. Non-fungible tokens are distinguishable and we can track the ownership of each one separately.

What is ERC721 token code? ›

ERC721 is a standard for representing ownership of non-fungible tokens, that is, where each token is unique. ERC721 is a more complex standard than ERC20, with multiple optional extensions, and is split across a number of contracts.

What is the difference between ERC721URIStorage and ERC-721? ›

The ERC721URIStorage contract is an implementation of ERC721 that includes the metadata standard extensions ( IERC721Metadata ) as well as a mechanism for per-token metadata. That's where the _setTokenURI method comes from: we use it to store an item's metadata.

How to create ERC721 token OpenZeppelin? ›

How to create an ERC721 token with OpenZeppelin
  1. Let's get started. Go to the OpenZeppelin Contracts Wizard and click on ERC721. ...
  2. Let's add some features. ...
  3. Let's set the access control. ...
  4. Let's add upgradeability. ...
  5. Let's add a security contact. ...
  6. Download your smart contract code. ...
  7. Compile the smart contract. ...
  8. Deploy the smart contract.
Mar 24, 2023

Can I create my own token? ›

Create your own cryptocurrency token through coding

As a blockchain developer, you can code your token as you like, without any restrictions. Creating your own blockchain for your own token is the best option, especially if you are a token creator with big innovative plans.

What is the first ERC721 token? ›

ERC-721
ERC-721: Non-Fungible Token Standard
ERC-721 illustrated with checkmarks and puzzle pieces representing standardized properties, compatibility and non-fungibility
First published2018
SeriesEthereum Improvement Proposals (EIP)
AuthorsWilliam Entriken, Dieter Shirley, Jacob Evans, Nastassia Sachs
5 more rows

What is the standard token ERC-721? ›

The ERC-721 token standard has been used in the context of digital art, where artists can create unique pieces of art as NFTs, providing them with a new avenue for their work. The ERC-721 token standard is used in the gaming industry, where game developers can create unique in-game items as NFTs.

What is OpenZeppelin? ›

OpenZeppelin provides a great platform for creating smart contracts in a secure manner. It offers reusable and thoroughly tested code that has been audited. Similar to using someone else's code, you can modify it to suit your requirements.

Which is better ERC-721 or ERC1155? ›

This means that ERC-1155 tokens can be used for fractional ownership or as a form of currency, while ERC-721 tokens are typically used for digital collectibles and other unique digital assets. Another difference is that ERC-721 tokens can only be traded one at a time, while ERC-1155 tokens can be traded in bulk.

What is the difference between ERC20 and ERC-721 tokens? ›

The key difference between ERC20 and ERC721 tokens lies in their fungibility. ERC20 tokens are fungible and represent a uniform asset, while ERC721 tokens are non-fungible and symbolize a set of unique assets. Additionally, ERC721 tokens cannot be divided into smaller units.

How do I know if my contract is ERC-721 or ERC1155? ›

According to EIPs, ERC721 and ERC1155 will implements EIP165. Therefore, we can use the supportsInterface of EIP165 to check whether the contract is ERC721 or ERC1155. The interface id for ERC1155 is 0xd9b67a26 , while the interface of ERC721 is 0x80ac58cd .

Can I create my own NFT token? ›

Yes, you can generate an NFT for free. For example, NFT-inator allows you to generate NFTs for free. However, minting typically costs a small fee called “gas.” In some marketplaces, you can defer gas fees to the eventual buyer, in a process known as “lazy minting.”

How do I create an ERC20 token? ›

How to Create an ERC-20 Token
  1. Set Up your Developer Environment. First, create an Alchemy account, and set up Metamask, HardHat, and Solidity for this project. ...
  2. Write ERC-20 Token Smart Contract. ...
  3. Write a Deployment Script for your ERC-20 Token. ...
  4. Deploy your ERC-20 Token to Goerli. ...
  5. Step 4: Send Some Tokens!

How much does it cost to create an ERC token? ›

The cost of writing a smart contract for a simple token can range from $1,000 to $5,000. However, complex smart contracts can include additional functionality. These functionalities include governance mechanisms, stake features, and integration with other decentralized applications (dApps).

How to mint ERC721 token? ›

Create an ERC721 Token on Kaleido in 4 Simple Steps
  1. Step 1: Create a Kaleido Account. If you haven't already, sign up for a Kaleido account here. ...
  2. Step 2: Create a Blockchain Network. To get started, create a blockchain network. ...
  3. Step 3: Create a Token Pool. ...
  4. Step 4: Mint Your NFT. ...
  5. Step 5: Transfer Your NFT to Metamask.
May 29, 2024

Top Articles
Best ways to take money to Nicaragua
Nearly two-thirds of middle-class Americans say they are struggling financially: ‘Gasping for air’
San Angelo, Texas: eine Oase für Kunstliebhaber
Fat Hog Prices Today
Naturalization Ceremonies Can I Pick Up Citizenship Certificate Before Ceremony
Azeroth Pilot Reloaded - Addons - World of Warcraft
Best Pawn Shops Near Me
Hmr Properties
Sams Early Hours
Local Collector Buying Old Motorcycles Z1 KZ900 KZ 900 KZ1000 Kawasaki - wanted - by dealer - sale - craigslist
2021 Lexus IS for sale - Richardson, TX - craigslist
Kvta Ventura News
Amazing deals for DKoldies on Goodshop!
Why Should We Hire You? - Professional Answers for 2024
Panic! At The Disco - Spotify Top Songs
Bernie Platt, former Cherry Hill mayor and funeral home magnate, has died at 90
Selfservice Bright Lending
Diakimeko Leaks
Busted Mcpherson Newspaper
Cookie Clicker Advanced Method Unblocked
Fleet Farm Brainerd Mn Hours
Turbo Tenant Renter Login
Violent Night Showtimes Near Johnstown Movieplex
'Insidious: The Red Door': Release Date, Cast, Trailer, and What to Expect
Anesthesia Simstat Answers
Himekishi Ga Classmate Raw
The Creator Showtimes Near Baxter Avenue Theatres
Page 2383 – Christianity Today
Davita Salary
First Light Tomorrow Morning
2015 Chevrolet Silverado 1500 for sale - Houston, TX - craigslist
Newcardapply Com 21961
#scandalous stars | astrognossienne
Tgh Imaging Powered By Tower Wesley Chapel Photos
Compress PDF - quick, online, free
2008 Chevrolet Corvette for sale - Houston, TX - craigslist
3496 W Little League Dr San Bernardino Ca 92407
Keir Starmer looks to Italy on how to stop migrant boats
Cpmc Mission Bernal Campus & Orthopedic Institute Photos
Craigslist Odessa Midland Texas
Pekin Soccer Tournament
Santa Clara County prepares for possible ‘tripledemic,’ with mask mandates for health care settings next month
Gt500 Forums
The Average Amount of Calories in a Poke Bowl | Grubby's Poke
bot .com Project by super soph
Muni Metro Schedule
Smoke From Street Outlaws Net Worth
About us | DELTA Fiber
Goosetown Communications Guilford Ct
Nfsd Web Portal
Immobiliare di Felice| Appartamento | Appartamento in vendita Porto San
Latest Posts
Article information

Author: Lidia Grady

Last Updated:

Views: 5650

Rating: 4.4 / 5 (65 voted)

Reviews: 88% of readers found this page helpful

Author information

Name: Lidia Grady

Birthday: 1992-01-22

Address: Suite 493 356 Dale Fall, New Wanda, RI 52485

Phone: +29914464387516

Job: Customer Engineer

Hobby: Cryptography, Writing, Dowsing, Stand-up comedy, Calligraphy, Web surfing, Ghost hunting

Introduction: My name is Lidia Grady, I am a thankful, fine, glamorous, lucky, lively, pleasant, shiny person who loves writing and wants to share my knowledge and understanding with you.