メインコンテンツまでスキップ

Issue NFT (ERC721)

What is Non-Fungible Token?

NFT is a Non Fungible Token, which is a digital token with a serial number assigned to each one. For example, it is convenient to use for digital tickets and trading cards whose limit is limited to 100.

ERC721 is a standard for creating NFT smart contracts. Click here for NFT and ERC721 standards.

It is necessary to associate 「metadata」 with NFT, and it is in the format to specify a general h external URL.

caution

There are many types of NFTs, and there is virtually no limit to what you can write to Metadata. Image-type NFTs are popular in the world, but there are parts that are misunderstood as if the NFT itself is valuable, and the value of an NFT is different from the legal value of that NFT. Only if someone guarantees it in the terms and conditions of. If the company that guarantees it disappears, the value of the NFT will be lost, or you will have to prove yourself that you have the right to do so.

In this tutorial, we will deploy an ERC721 smart contract on a G.U.Sandbox Chain with an image-type NFT and Mint the work there.

Prerequisites

  • ERC721Basic SDK
  • NFT image

ERC721 SDK usage

Install ERC721 SDK

Prepare empty folder

mkdir test-erc721
cd test-erc721

Quick initialize npm project

npm init -y

Setting npm server for G.U.SDK

echo > .npmrc
echo @gusdk:registry=https://gusdk.gu.net/ >> .npmrc

At this point, the contents of the directory are as follows.

DIRECTORY ROOT
├── .npmrc
└── package.json

Install ERC20 SDK and ethers

npm install @gusdk/erc721-basic ethers@v5

Deploy new token with ERC721 SDK

Create src folder and create deploy.js file

mkdir src && cd src && echo > deploy.js

Modify content deploy.js following:

const { ERC721Basic__factory } = require("@gusdk/erc721-basic")
const { ethers } = require("ethers")
async function deploy() {
const provider = new ethers.providers.JsonRpcProvider({ url: "https://sandbox1.japanopenchain.org:8545/" })
const signer = new ethers.Wallet(YOUR_PRIVATE_KEY, provider);

const factory = new ERC721Basic__factory(signer)
const erc721 = await factory.deploy(
YOUR_TOKEN_NAME, // Eg. "TEST"
YOUR_TOKEN_SYMBOL, // Eg. "TEST"
);

return erc721.address
}

deploy().then(address => console.log(address));

Execute it following command

node deploy.js

NOTE:: Don't forget write down your token address above

Upload NFT metadata

Detailed information about the work is generally not recorded in the blockchain. Described off-chain, that is, outside the blockchain. You can store this data on a centralized storage server, but if you delete the server, the images will disappear. Let's upload to distributed storage according to the manners of web3.0. Uploading to distributed storage is easy with @gusdk/gu-uploader-basic.

Install

npm install @gusdk/uploader-basic tslib

Sign up NTF.Storage

We will use IPFS as distributed storage. IPFS (InterPlanetary File System) is a hypermedia protocol and its implementation that runs on P2P networks under development by Protocol Labs. As a service that can easily use IPFS, we will use NFT.Storage.

caution

Even if you store metadata such as images in IPFS distributed storage, that data will disappear from the world unless someone has pinned your content on the IPFS server. When using it in a product, you need to use an IPFS server service such as NFT.Storage, or you need to set up an IPFS server yourself and pin it with at least one IPFS server.

After signing up, get your API key

Upload image

Create upload.js file under src folder

echo > upload.js

Modify upload.js following code below:

  const { GUUploaderBasic } = require('@gusdk/uploader-basic')

async function upload() {
const sdk = new GUUploaderBasic({ nftStorageToken: "YOUR_API_KEY" })
const cid = await sdk.storeMetadata({
name: 'enter your work name',
description: "enter your work detail",
imagePath: "/path/to/your/work.jpg"
})

return cid
}

upload().then(cid => console.log(cid))

Run following command

node upload.js

You will receive response like this:

Token {
ipnft: 'bafyreihy7g6qap2gphinbdwy47w367yqsfhphgtlfgrqsujrztlxilq3em',
url: 'ipfs://bafyreihy7g6qap2gphinbdwy47w367yqsfhphgtlfgrqsujrztlxilq3em/metadata.json'
}

NOTE:

  • The url will be your uri when mint the nft
  • Please write down your cid, which link to your metadata. You can view it via on of those IPFS gateway. Eg. https://[YOUR_CID].ipfs.nftstorage.link/metadata.json

Mint

Create mint.js under src folder

echo > mint.js

Modify mint.js following code below:

const { ERC721Basic__factory} = require("@gusdk/erc721-basic")
const { ethers } = require("ethers")

async function mint(uri) {
const provider = new ethers.providers.JsonRpcProvider({ url: "https://sandbox1.japanopenchain.org:8545/" })
const signer = new ethers.Wallet(YOUR_PRIVATE_KEY, provider);

const erc721basic = ERC721Basic__factory.connect(YOUR_TOKEN_ADDRESS, signer);
const tx = await erc721basic.safeMint(signer.address, uri)
await tx.wait()
}

const uri = "ipfs://[YOUR_CID]/metadata.json"
mint(uri)

Run following command:

node mint.js

Check your token balance

Create balance.js under src folder

echo > balance.js

Modify balance.js following code below:

const { ERC721Basic__factory} = require("@gusdk/erc721-basic")
const { ethers } = require("ethers")

async function balance(walletAddress) {
const provider = new ethers.providers.JsonRpcProvider({ url: "https://sandbox1.japanopenchain.org:8545/" })

const erc721basic = ERC721Basic__factory.connect(YOUR_TOKEN_ADDRESS, provider);
return await erc721basic.balanceOf(walletAddress)
}

const walletAddress = YOUR_WALLET_ADDRESS
balance(walletAddress).then(balance => console.log(balance))

Run following command:

node balance.js

Please see the reference for other features.