Deploy NFTs

This work is inspired by this blog

In this tutorial, we will create a non-fungible token (NFT) and deploy it to a public testnet.

ERC721 is a standard for representing ownership of non-fungible tokens, that is, where each token is unique such as in real estate or collectibles.

We will use Presets contracts in OpenZeppelin Contracts to create an ERC721 and deploy using Truffle.

Setting up the Environment

We begin by creating a new project.

$ mkdir mynft && cd mynft
$ npm init -y

Then we install OpenZeppelin Contracts which has an implementation of ERC721.

$ npm i --save-dev @openzeppelin/contracts

Next, we install a development tool for deployment, for this tutorial we will use Truffle but we could use any other tools such as Builder, Remix, or OpenZeppelin CLI.

$ npm i truffle

Getting the Contract Artifacts

We will set up our Solidity project using truffle init to create a contracts directory and configuration to connect to a network.

$ npx truffle init
Starting init...
================

> Copying project files to

Init successful, sweet!

We are going to use Preset ERC721PresetMinterPauserAutoId which is an ERC721 that is preset so it can be minted (with auto token ID and metadata URI), paused, and burned.

The Preset contracts have already been compiled, so we only need to copy the artifacts to the build/contracts directory.

Using your favorite editor create 2_deploy.js in the migrations directory with the following contents:

Deploy the Contract to a Local Blockchain

We will use truffle develop to open a Truffle console with a development blockchain

We can deploy our new NFT to our development blockchain using migrate.

We can then use our deployed contract.

Interact With Your Token

The accounts that we can use were displayed when we started truffle develop

Token Metadata

We can call the contract to read token metadata such as name, symbol and baseURI

Mint

We can send a transaction to mint tokens to a given account, from an account with the minter role. In our case we are minting from the account which deployed the token, which is given the minter role.

We will mint 1 NFT with token ID 0.

We can check the owner of the token and the token URI for the metadata

Metadata

EIP-721 2 includes an optional metadata extension with a name, symbol and for each tokenID a tokenURI with can point to a JSON file with name, description and image for the given token ID.

How you create and host this metadata is up to you. I would suggest using a domain that you control to point to where you host the data so that you can move it as required.

For this tutorial, we will use My JSON Server where we can store a single JSON file in a GitHub repository that we can access via a fake JSON server.

Warning For production we need to store our metadata in a permanent location that can exist for the life of the token.

A sample JSON for tokenID 1 is: http://my-json-server.typicode.com/huangsuyu/nft/tokens/1

Deploy to a Public Testnet

Next we will deploy to Agora testnet .

To deploy, we will use the instructions for Connecting to Public Test Networks with Truffle

You will need the following:

  • RPC URL of TestNet

  • @truffle/hdwallet-provider installed

  • Configure truffle-config.js for TestNet

  • A funded testnet account and mnemonic

  • A secrets.json or another secret-management solution. Make sure you don’t commit this to GitHub!

My truffle-config.js has the following

Deploy to Agora Testnet

Mint

We can send a transaction to mint tokens to a given account, from an account with the minter role.

In our case we are minting from the account which deployed the token, which is given the minter role.

To see configured accounts run the command accounts.

We will mint 1 NFT with token ID 1. Specify the address that you want to be the token holder (0xc7e4bBc4269fdC62F879834E363173aeE7895F45 is one of my test accounts)

Last updated