How to create your own ERC721 NFT collection and publish it on Opensea

In this tutorial we’re going to create our own ERC721 NFT collection and publish it on the Opensea marketplace.

Our NFT collection will consist of 3 items of dog images. You can find the collection here: https://drive.google.com/drive/folders/1eV9RCOhXCkBmWvMHURyRI21sWfmW-W0S?usp=sharing. We are going to publish this collection on the Opensea rinkeby testnet.

Here is the result collection: https://testnets.opensea.io/collection/my-nft-dogs-v2

The full source code: https://github.com/ryzhak/my-nft 

How it works basically:

  1. 1. Create NFT images and upload them to IPFS (or whenever you want)
  2. 2. Create metadata files describing those NFT images and upload them to IPFS(or whenever you want).
  3. 3. Create an NFT smart contract.
  4. 4. Deploy NFT smart contract.
  5. 5. Publish NFT collection on Opensea.

Initial setup

Create a new folder my-nft somewhere on your hard drive. Inside the my-nft folder run npm init -y to initialize an empty npm project. Then run npm install truffle -g to install the truffle framework globally. 

Initialize a new truffle project via truffle init. Then run npm install @openzeppelin/contracts –save to install Openzeppelin contacts. Openzeppelin maintains a set of community trusted smart contracts where you can also find ERC721 contracts for NFT collections.

Creating NFT images

Luckily for us we already have our images designed. You can find them at https://drive.google.com/drive/folders/1eV9RCOhXCkBmWvMHURyRI21sWfmW-W0S?usp=sharing. Create a new folder images in the project root and copy all 3 dog images there.

We are going to use the https://nft.storage/ service to upload our images to IPFS. To upload any files to https://nft.storage we need to convert those files to the CAR format

Run npm i ipfs-car –save to install the converter. Now convert the images folder to the images.car format via ./node_modules/.bin/ipfs-car –pack images –output images.car.

Then go to https://nft.storage/files/ and upload images.car file.

Creating NFT metadata

Each image must have a corresponding metadata according to Opensea docs. Create a new folder metadata in the project root. 

Create a file “1” (without any extension) with the following JSON content:

Create a file “2” (without extensions) for the token with id 2:

Create a file “3” (without extensions) for the token with id 3:

Now convert the metadata folder to metadata.car format via ./node_modules/.bin/ipfs-car –pack metadata –output metadata.car. Then go to https://nft.storage/files/ and upload the metadata.car file there. The URL from https://nft.storage/files/ which contains the metadata folder is going to be our base token URI in the ERC721 NFT smart contract.

Creating ERC721 NFT contract

Run truffle create contract MyNFT to create a new contract. Edit the contracts/MyNFT.sol file:

Above we created a smart contract with the following features:

  • – NFT token description: “My NFT dogs”
  • – NFT token short name: “NFTDGS”
  • – Max supply is restricted to 10k items
  • – Only owner can mint new items
  • – Only owner can set base token URI

Run truffle compile to create contract build files.

Notice about token URIs.

Each NFT token should have a unique URI which is basically a URL address. Example of token URI with id 1: https://example.com/tokens/1. When dealing with Opensea this URI should return a JSON file with metadata in a special format. This format also contains the image field which is basically our NFT image. Our smart contract has a method setBaseTokenURI which must set the base URL for token URI (https://example.com/tokens/) on contract deploy.

You can check how token URI is created here https://github.com/OpenZeppelin/openzeppelin-contracts/blob/master/contracts/token/ERC721/ERC721.sol#L97. If base token URI exists then concatenate base token url with token number. If the base token URI does not exist then return an empty string.

So you can see that anything can be converted to NFT.

Deploying NFT smart contract

Install metamask and create a new account. Save your mnemonic seed phrase as we are going to use it later. 

In metamask connect to the rinkeby network. Use any rinkeby faucet to send some ETH to the 1st account in your metamask wallet. 

Register at https://www.alchemy.com/ and create a new app connected to the rinkeby network. In the alchemy dashboard you should see the API_URL which we are going to use later.

Install library for working with accounts via npm install @truffle/hdwallet-provider –save.

Run npm install dotenv –save to install dotenv library (which helps to store secrets in a pretty secure way). 

Create a new .env in the project root with the following content:

Here API_URL is the Alchemy API URL (which you can find in the Alchemy dashboard) and MNEMONIC is your seed phrase which you used when creating a new account in metamask wallet.

Update your truffle-config.js file:

Here we added a rinkeby network configuration.

Now create a new file migrations/2_deploy_contracts.js with the following content:

Here we:

  1. 1. Deploy our NFT contract
  2. 2. Set base token URI. NOTICE: you should update base token URI in the script to your own address. Base token URL is the metadata folder url which you can find at https://nft.storage/files/
  3. 3. Mint 3 NFT tokens to our own address.

Now run truffle migrate –network rinkeby to deploy the smart contract to the rinkeby testnet. You should see the following console output:

You can see that our NFT contract is deployed at address 0xD0cF0b4D240AaA7b4FE67723FC995779c01b4fE3.

Publishing NFT collection on Opensea

Now open Opensea get listed page, click on the “Live on a testnet” button, enter your NFT contract address and hit “Submit”. You should see your NFT collection live https://testnets.opensea.io/collection/my-nft-dogs-v2

The full source code can be found here: https://github.com/ryzhak/my-nft 

Summary

In this tutorial we learned how to deploy images and metadata to IPFS, created our own NFT smart contract, deployed it on the rinkeby testnet and published our NFT collection on Opensea. Now you should have a basic understanding of how NFTs work.

Leave a Reply

Your email address will not be published. Required fields are marked *