BTC
ETH
HTX
SOL
BNB
View Market
简中
繁中
English
日本語
한국어
ภาษาไทย
Tiếng Việt

Tutorial: Learn how to use Solidity to create your own NFT in three minutes

星球君的朋友们
Odaily资深作者
2021-11-05 07:49
This article is about 2908 words, reading the full article takes about 5 minutes
If 2020 is the year of DeFi, then at least a large part of 2021 will belong to NFT.
AI Summary
Expand
If 2020 is the year of DeFi, then at least a large part of 2021 will belong to NFT.

Reference article: How To Create NFTs With Solidity

By Patrick Collins

Compilation and arrangement: Chen Zou

NFT(Non-Fungible Tokens,Non-homogeneous tokens) is one of the brightest stars in the field of smart contracts, and is a token standard similar to ERC20. If 2020 is the year of DeFi, then at least a large part of 2021 will belong to NFT.

Non-fungible means that it is unique, which is very different from ERC20 tokens, which are all fungible tokens. As an example of homogenization, your dollar bill will be worth $1 no matter what dollar bill you use. The serial numbers on the dollar bills may be different, but the bills are interchangeable because they are worth $1 anyway.

first level title

The development progress of NFT

The first thing to be clear is that NFT has unlimited potential. In February of this year, Axie Infinity sold 9 plots for 888.25 ETH. (currently worth about $3.91 million)

first level title

standard

As mentioned above, NFT starts with the ERC721 token standard, which has a syntax similar to ERC20 with some adjustments.

In order for your contract to be recognized by the system as an NFT, all it has to do is follow this standard. We import this standard into our contracts so we don't have to reinvent the wheel every time we want to do a new contract.

NFTs have a tokenURI variable, which we'll discuss in a minute, they have a mapping of tokenIds to their owners, each token has its own"owner". This is different from ERC20, which only has a mapping of addresses to balances. ERC721 still allows people to transfer tokens, set permissions on tokens, etc. By convention, this is a lightweight standard, so we can build anything we want with them.

Here's where things start to get really different - Metadata. When you enter OpenSea (a top NFT marketplace), you can see a bunch of pictures. So since NFTs are on the blockchain, all their data should be on the chain, right? Well, not quite.

Storing data on-chain can be very expensive, and the file size of artwork is often not too small. Ethereum and smart contract developers realized that uploading even a 1 MB image would quickly put them out of business, so they had to create a way to display their work without having to store large amounts of data.

As a workaround, most NFTs have a so-called tokenURI. This is a globally unique identifier used in the field of NFT's visibility. This makes it easier to add visual effects to NFTs. A URI is a Uniform Resource Identifier, which could be an HTTPS API call, something over IPFS, or some other kind of unique identifier. This metadata looks like this.

Their JSON file structure is as follows:

  • name

  • description

  • image

  • attributes

These files are usually stored on API or IPFS.

Now, you may suddenly have a doubt,"first level title"

On-chain metadata VS off-chain metadata

If you want your NFT to have all the interesting things you can imagine, its on-chain properties are a must. To name a few examples, if you want to give them battle stats like Pokemon, some type of rarity stat, or have them have on-chain encrypted scarcity. These properties must be on-chain. (Because these need to be traceable, falsifiable, and open to the public)

Currently NFT platforms don't have a good way to visualize properties on-chain, so you just need to create a tokenURI with your minted NFT.

For tokenURI, one of the most popular methods is to use IPFS to store your data. You'd upload your data there and then use a pinning service to make sure the data stays there forever. I am looking forward to more interaction between the smart contract platform and the storage side.

Tip: The following is my time to talk to myself. Smart contract platforms at the front, Chainlink in the middle, dStorage at the back...but that's another topic.

To be honest, I thought NFTs were stupid at first. Other than that, I used to think that art was something idiots do... but considering I love music, movies, and games with good visuals, I would be hypocritical to say that. NFTs solve long-standing pain points in the art world around royalties and authenticity. We now have a decentralized auditing service. If you want to use someone's artwork, you can see everything about that artwork on-chain.

The original creator of Nyan cat sold its NFT version for 300 ETH. This proves that digital scarcity has value.

If you create something amazing, then it has its unique value. That's why I think owning a Chainlink VRF (Chainlink's random number generation service) NFT is a very important thing, because you can create very rare collectibles. I'm looking forward to someone making a Pokemon type game on these platforms. These NFTs will last forever.

first level title

A simple NFT contract

This is the easiest way to play with NFT contracts. It includes grammars for Truffle, Hardhat, and Brownie. I like using OpenZepplin contracts because it's very well structured and has all the tools we usually need to start working straight away. Make sure to install them ahead of time:

  • npm install @openzeppelin/contracts

first level title

Advanced version of NFT contract

In this NFT contract, we use Chainlink VRF to provide random state to our NFT. In this case, we're using randomness to give our dog a random breed. When we call createCollectible this time, we're actually sending a request to the Chainlink Oracle to return a cryptographically proven random number. In this way, there will be no "black-box operations" that may damage the fairness of the game. This request is asynchronous and we have to wait for the Chainlink oracle to do the second transaction with the nonce.

Summarize:

Summarize:

This article is from Bitpush.News, reproduced with authorization.

This article is from Bitpush.News, reproduced with authorization.

NFT
Welcome to Join Odaily Official Community