Build, deploy, and sell dynamic NFTs

Time:2021-11-27

Build, deploy, and sell dynamic NFTs
Original address

NFT is a tool that exists only in the field of blockchain and has a wide range of applications and opportunities. Erc721 token standard can build collections, independent tokens, bills, games and other applications. For developers who want to participate in the construction, a dynamic and random NFT is a good start. But what can we do with it now? Isn’t it great to show your newly cast randomized or dynamic characters?

We think so, too. In this tutorial, we will guide you through all the steps to deploy your own dynamic or randomized NFT to the opensea market. The following is an example of what we want to achieve.

Build, deploy, and sell dynamic NFTs

Screenshot from opensea

Let’s learn how to make something similar to the above!

NFT quick review

Erc721 (also known as NFTs) defines a framework for making tokens. These tokens are unique and different from each other (so it is called non homogenization), while the popular erc20 standard defines “homogenized” tokens, which means that tokens are interchangeable and each token has the same value. We will explore in more depth how to build these and how communities represent them across platforms. You can also read more in the opensea NFT Bible.

If you haven’t read the last article on getting random numbers in NFT, be sure to review it! There are various tutorials, guides and strategies for smart contract and blockchain engineering teaching in the developer page.

What is metadata?

In the last blog, we learned how to build a random NFT. Now we will use another important part of the erc721 standard:metadata, take it to a new level.

All NFTs have so-called metadata. You can read this in the original ERC / EIP 721 proposal. The community found that storing images on Ethereum was really laborious and expensive. If you want to store an 8 × The cost of storing so much data is acceptable, but if you want a picture with good resolution, it will cost a lot of money.

The cost of data storage is (approximately) 640000 gas per KB of data. If the current gas is about 50gwei or 0.000000050eth, and 1eth is equivalent to the current $600, you will spend $20.
Build, deploy, and sell dynamic NFTs
1KB image

Spending $20 to add such a graphic to the blockchain did not really excite the creators of NFT.

We realize that Ethereum 2.0 will solve many of these troublesome expansion problems (and congratulate the successful launch of eth2.0), but at present, the community needs a standard to help solve this problem. Metadata is the answer to this question.

Metadata provides descriptive information for tokenid stored outside the chain. These are simple APIs. The out of chain UI calls these APIs to collect all information about tokens. Each tokenid has a specific tokenid, which defines the API call and returns a JSON object. It looks like this:

{
    "name": "You NFT token name",
    "description": "Something Cool here",
    "image": "https://ipfs.io/ipfs/QmTgqnhFBMkfT9s8PHKcdXBn1f5bG3Q5hmBaR4U6hoTvb1?filename=Chainlink_Elf.png",
    "attributes": [. . .]
}

Metadata has four different keys:

  • name, defines the readable name of tokenids
  • description, which provides some background information about token
  • imageIs another URI for the image
  • attributesThis is the statistical data of token

If your NFT interacts with other NFTs, it is very important to ensure that the attributes on the token URI match the attributes of your NFT smart contract. Otherwise, you may be confused when the battle or interaction does not meet your expectations!

bytokenURIAfter allocating tokenids, the NFT market can display your tokens and let you show your creativity. You can see an NFT created by us using the updated random NFT code warehouse of dragon and dungeon on the opensea market of rinkeby testnet. There are many such markets, such as mintable, rarible and opensea.

On chain and off chain metadata

You can always store all metadata on the chain (in fact, this is the only way to interact with tokens), but many NFT markets don’t know how to read metadata on the chain_ Now_ So at present, it is ideal to use off chain metadata to visualize your tokens and have all on chain metadata at the same time, so that your tokens can interact with each other.

Names, descriptions and attributes are easy to store on the chain, but images are the difficulty. In addition, where do we store the API of the token URI? Many people choose to run the server to host information, which is good, but this is a centralized place for visualizing tokens. If we can store the pictures on the chain, it will not be down or attacked by hackers. It is a better choice. In the above example, their pictures use a URL to IPFs, which is a common way to store pictures.

IPFs is the abbreviation of interplanetary file system. It is a point-to-point hypermedia protocol designed to make the network faster, safer and more open. It allows anyone to upload a file, and the file is hashed, so if the file changes, its hash value will also change. This is ideal for storing images, because it means that the hash / token URI on the chain will change every time the image is updated. At the same time, it means that we can have the history of metadata. Adding images to IPFs is also very simple, and there is no need to run the server.

Now that we know what to do, let’s build and deploy! After you deploy your NFT token and market, a token will look like this:

Build, deploy, and sell dynamic NFTs

Chainlink knight on opensea

levelsPart is the place of random statistics of tokens!

Give you an extra surprise

Now, friends reading here, we have prepared an extra surprise for you. We will introduce how to deploy these NFTs in one second, but before that, the four tokens we created in this game will compete in the first NFT treasure hunt!

Here is a link to their main market. Good luck.

How to deploy your dynamic NFT Market

Again, we use the latest version of the Dungeons & Dragons code warehouse, which is also described in readme.

Here’s what we need to do:

  1. Build a verifiable random D & D role using chainlink VRF
  2. Add a token URI using IPFs
  3. Add random NFT to opensea marketplace

Remember, you can change the warehouse to make it suitable for dynamic NFTs. You can easily replace VRF with chainlink price feeds or chainlink API.

This warehouse is only applicable to rinkeby at present, so please be sure to switch to rinkeby network! We’ll explain it from the beginning. If you haven’t read the last article, don’t worry.

You need rinkeby testnet Eth and rinkeby testnet link in your wallet to continue.

Configure environment variables

to configureMNEMONICAnd a rinkebyRINKEBY_RPC_URLEnvironment variables. YoursMNEMONICIs the seed phrase of your wallet. You can find one from the node provider serviceRINKEBY_RPC_URLLike infra.

Then, set them tobash_profileFile, or export them to your terminal like this:

export MNEMONIC='cat dog frog....'

export RINKEBY_RPC_URL='www.infura.io/asdfadsfafdadf'

Next you need to do this:

Clone warehouse and deploy

git clone https://github.com/PatrickAlphaC/dungeons-and-dragons-nft

cd dungeons-and-dragons-nft

git checkout opensea-update

npm install

truffle migrate --reset --network rinkeby

The above command deploys your D & D NFT!

Create role

You can try this with the following command:

truffle exec scripts/fund-contract.js --network rinkeby

truffle exec scripts/generate-character.js --network rinkeby

truffle exec scripts/get-character.js --network rinkeby

The above command can create a new role through random data!

Depending on how often you deploy, you can changeget-character.jsMediumdnd.getCharacterOverView(1)Command to select which role0Replace it with the tokenid of any character you like.

The above commands provide an overview of NFT. Because the call returns big numbers, you will seeBN, you can turn them into integers to see what they are. Or you can go further

View on Etherscan

You can get an Etherscan API key for free and interact with the NFT on the chain. ThenETHERSCAN_API_KEYSet as environment variable.

npm install truffle-plugin-verify

truffle run verify DungeonsAndDragonsCharacter --network rinkeby --license MIT

In this way, you can verify and publish your contract, which can be sent to your EtherscanRead ContractPartial view.

Alternatively, you can use oneclickdapp, just add the contract address and abi. Can be inbuild/contractsABI found in folder. Note that ABI is not the entire file, it just saysABIPart of.

Deploy to opensea

After creating NFTs, we need to give them atokenURI。 Tokenuri is a standard for presenting NFTs data to the world. It can make it easier for us to store pictures and other contents without wasting our efforts to store them on the chain.

TokenURIRepresents a URL or other unique identifier. It is a URL with some parameters.jsonFile.

{

    "name": "Name for it ",

    "description": "Anything you want",

    "image": "https://ipfs.io/ipfs/HASH_HERE?file.png",

    "attributes": [...]

}

Download IPFs and IPFs companion

Now we will store these images and metadata in IPFs. You need

  1. IPFS
  2. IPFS companion
  3. Pinata

IPFs companion allows us to view IPFs data natively in browsers such as brake or chrome. Pinata allows us to keep IPFs files running normally when nodes are down (don’t worry about this problem for the time being). If you click this link in the browser, you will know that IPFs partner is working: https://ipfs.io/ipfs/QmTgqnhF… 。

The following image will appear:
Build, deploy, and sell dynamic NFTs

Chainlink Wizard

Add image to IPFs

Once the IPFs node is established, you can start adding files to it. First, upload our NFT image. Go to the “files” section of the IPFs installation.
Build, deploy, and sell dynamic NFTs

IPFs file

What does this D & D role look like? Add it to your IPFs node and “pin” it. Now, you can pin a blank picture, or anything.

Add metadata file to IPFs

Then you need to add the metadata JSON object to IPFs. You need to get the name and attributes from the deployed token. We arecreate-metadata.jsSome work has been done for in the script. Just run

truffle exec scripts/create-metadata.js --network rinkeby

The metadata is displayed in themetadataFolder. It now only needs the URL of the picture! Metadata is random numbers and statistics created by chainlink VRF. Now we get the CID of the image we created and add it to the metadata JSON file, then add the file to IPFs and pin it! It will look like this:

Build, deploy, and sell dynamic NFTs

Chainlink wizard JSON

Pinata

If the IPFs node goes down or the computer is shut down, we will not be able to pull metadata, so we need a way to keep their pin status and let other nodes host data. This is the role of < U > Pinata < / u >. Don’t worry, it’s free! The purpose is to help us maintain data when our IPFs node is down. Copy the CID of the image and JSON metadata file and add it to the Pinata account. This takes a few seconds to register.
Build, deploy, and sell dynamic NFTs

Copy CID

The metadata JSON file istokenURI, so we use the tokenid given to the picture NFT to modify itset-token-uri.js, and add the IPFs token URI.

Then we use

truffle exec scripts/set-token-uri.js --network rinkeby

Now you can get the address of NFT, and then go to the opensea testnet market to see if we are doing it correctly. If done correctly, it will look like this). We need to register an account with opensea first.

Here is the link to add your testnet NFT contract (see it on opensea). Then you can start selling your NFT.

next step

We should all be ready! It covers a lot of information, so if you have any questions, be sure to contact our discipline. Smart contract and chainlink engineers have a huge community. Many excellent people are gathering together to push NFT and smart contract into the spotlight, so discord is also a good place to meet other friends. You also want to see the chainlink builders program, where you can win some cool prizes by using chainlink builders!

As usual, be sure to access the development documents. You can also subscribe to chainlink newsletter to learn about the latest developments of chainlink.

If you learn something new here, want to show what you have developed, or develop a front end for some demo warehouses, you are welcome to share it on twitter, discipline or reddit, and add #chainlink to your warehouse.