How to Create and Deploy ERC-721 NFTs
0x01 What is ERC-721？
ERC is an abbreviation of Ethereum Request for Comment, and 721 is the proposal identifier number.ERCs are application-level standards in the Ethereum ecosystem, they can be a smart contract standard for tokens such as ERC-20, the author of an ERC is responsible for building consensus with the Ethereum community and once the proposal is reviewed and approved by the community it becomes a standard. ERC-721 was created to propose the functionality to track and transfer NFTs within smart contracts.
ERC-721 is an open standard that describes how to build Non-Fungible tokens on EVM (Ethereum Virtual Machine) compatible blockchains; it is a standard interface for Non-Fungible tokens; it has a set of rules which make it easy to work with NFTs. NFTs are not only of ERC-721 type; they can also be ERC-1155tokens.
The following are the set of functions and events defined in the ERC-721 standard:
ERC-721 defines some functions with compliance with ERC-20. This makes it easier for existing wallets to display simple token information.
- name: Used to define the token’s name, which other contracts and applications can identify.
- symbol: Used to define token’s shorthand name or symbol.
- totalSupply: This function is used to define the total number of tokens on the blockchain; the supply doesn’t have to be constant.
- balanceOf: Returns number of NFTs owned by an address.
- ownerOf: This function returns the address of the owner of a token. As each ERC-721 token is unique and non-fungible, they are represented on the blockchain by an ID. Other users, contracts, and apps can use this ID to determine the owner of the token.
- approve: This function grants or approves another entity the permission to transfer tokens on the owner’s behalf.
- takeOwnership: This is an optional function that acts like a withdraw function since an outside party can call it to take tokens out of another user’s account. Therefore, takeOwnership can be used when a user has been approved to own a certain amount of tokens and wishes to withdraw said tokens from another user’s balance.
- transfer: This is another type of transfer function; it allows the owner to transfer the token to another user, just like other digital tokens/coins.
- tokenOfOwnerByIndex: This is an optional but recommended function. Each owner can own more than one NFT at the same time. Its unique ID identifies every NFT, and eventually, it can become difficult to keep track of IDs. So the contract stores these IDs in an array and the tokenOfOwnerByIndex function lets us retrieve this information from the array.
- tokenMetadata: This optional feature is an interface that lets us discover a token’s metadata or a link to its data.
- Transfer: This event is fired when the ownership of the token changes from one individual to another. It emits the information on which account transferred the token, which account received the token, and which token (by ID) was transferred.
- Approve: This event is fired when a user approves another user to take ownership of the token, i.e., it is fired whenever approve function is executed. It emits the information on which account currently owns the token, which account is approved to take ownership of the token in the future, and which token (by ID) is approved to have its ownership transferred.
0x02 scenario of NFTs
- Digital art (or physical art): Art pieces are the most popular use cases of NFTs. Digital art auctions were the first application of NFTs and continue to grow.
- Gaming: Providing in-game purchases and collectibles of games.
- Real estate: Tokenizing properties and smart contracts and carry buying and selling.
- Finance: Financial instruments like loans, futures, and other responsibilities.
- Software titles: Software licenses to ensure anti-piracy and privacy.
- Concert tickets/Sports match tickets: To ensure that no fraud happens in ticket selling and fans can have a single place to view past experiences.
- KYC compliance: Creating a token for a specific user’s KYC.
0x03 Getting test ETH
We’ll deploy our contract on the Ropsten testnet. To get started, you will need the Metamask browser extension to create an ETH wallet and some test ETH, which you can get by going to the Ropsten faucet. You’ll need to select Ropsten Test Network on your Metamask wallet and copy-paste the wallet address into the text field in the faucet, then click Send me test Ether.
Ropsten Ethereum (also known as “Ethereum Testnet”) is an Ethereum test network that allows for blockchain development testing before deployment on Mainnet, the main Ethereum network. Ropsten is the successor of Morden, the first Ethereum Testnet.
0x04 Adding Files to IPFS
Before writing our NFT contract, we need to host our art for NFT and create a metadata file; for this, we’ll use IPFS — a peer-to-peer file storing and sharing distributed system.
Step 1: Creating IPFS repo.
Start the IPFS repo by typing the following in a terminal/cmd window.
Repo is the storage repository of IPFS nodes, and it is the subsystem where IPFS nodes actually store data. All IPFS objects are stored in a repository (similar to git). There are multiple repo implementations, depending on the storage medium used. Most commonly, IPFS nodes use fs-repo (stored in the os filesystem), but also mem-repo, and s3-repo.
Step 2: Starting the IPFS daemon.
Start IPFS daemon, open a separate terminal/cmd window and type the following.
The ipfs daemon command is used to start a network-connected IPFS node
Step 3: Adding an image to IPFS
Go to the first terminal window and add the image to IPFS (art.png here).
Copy the hash starting from Qm and add the “https://ipfs.io/ipfs/” prefix to it; it must look something like this
note: Each file stored in the IPFS network has a unique hash address (that is, the content address, also called CID), and the address is the hash value formed by the algorithm. These hash values are unique, and users can locate files and access data as long as they access the corresponding hashes. Whenever the file changes, the hash address (value) changes. Then you will find that the hash of most IPFS files starts with “Qm”. In order to represent the hash of the approximate object of the file, IPFS uses the Multihash format and Base58 encoding.
The format of Multihash is actually a string, which consists of three parts: Hash algorithm encoding, the length of the HasH value (number of bytes), and the Hash value. Multihash starts with a byte (0x12), which will indicate which hash algorithm it is. This is followed by another byte of length (0x20). Currently, IPFS uses the sha2–256 hash function algorithm by default, and the binary length of the hash digest is 32 bytes. After adding the hash value, the length of the string is very, very long. At this time, the Base58 encoding technology is used, so that the length of the string can be compressed, which is convenient for storage and transmission. Because the current Multihash value of IPFS starts with 1220, and according to the Base58 encoding operation, the calculated results all start with “Qm”.
Step 4: Adding JSON file to IPFS
Create a JSON file nft.json and save it in the same directory as the image.
JSON file format:
Now add the JSON file.
Copy the hash starting from Qm and add the “https://ipfs.io/ipfs/” prefix to it; it must look something like this https://ipfs.io/ipfs/QmUFbUjAifv9GwJo7ufTB5sccnrNqELhDMafoEmZdPPng7
Save this URL. We’ll need this to mint our NFT.
0x05 Creating your own token.
For ease and security, we’ll use the 0xcert/ethereum-erc721 contract to create our NFT. With 0xcert/ethereum-erc721, we don’t need to write the whole ERC-721 interface. Instead, we can import the library contract and use its functions.
Head over to the Ethereum Remix IDE and make a new Solidity file, for example — nft.sol
Remix IDE is an online smart contract compilation, testing and deployment tool. Use it to easily interact with smart contracts.
Paste the following code into your new Solidity script:
Specifying SPDX license type, which is an addition after Solidity ^0.6.8. Whenever the source code of a smart contract is made available to the public, these licenses can help resolve/avoid copyright issues. If you do not wish to specify any license type, you can use a special value UNLICENSED or simply skip the whole comment (it won’t result in an error, just a warning).
Injected Web3: This is the option to use for the browser plugin (MetaMask). Here, you are telling Remix to defer all control of the blockchain integration to the MetaMask plugin. At this point, MetaMask controls which network you are connecting to.
Line 2: Declaring the solidity version.
Line 4–5: Importing 0xcert/ethereum-erc721 contracts.
Line 7: Starting our Contract named newNFT and mentioning it’s extending NFTokenMetadata and Ownable contracts.
Line 9–12: Initializing the constructor and setting name, a symbol of our token.
Line 14: Declaring function mint with three arguments, variable _to of type address which will store the address of the receiver of NFT token, variable _tokenId of uint256 type which will hold the token id, variable _uri of type string which will store the URI of the JSON file. Declaring mint as external means, it can be accessed from other smart contracts and outside the self scope.
Line 15: Minting token using the address of the receiver and token id.
Line 16: Setting token URI using token id and URI of JSON file.
Compile the smart-contract and deploy it using injected Web3 (make sure to select Ropsten testnet on Metamask before compiling the contract). Approve the transaction from metamask.
If you receive an error message before deployment, “This contract may be abstract,” make sure to select the appropriate contract under the Contract tab.
Now go to the “Deployed Contracts” section in Remix and expand the deployed contract. You’ll see a bunch of functions/methods. Expand the mint function and add the following details:
- Add your Ropsten address in the _to the field.
- Enter any Big number value in the _tokenid field (we suggest 1 since it’s the first).
- Add URI of JSON file in the _uri field, which we obtained in the previous section.
Click on transact and confirm the transaction from metamask. Now you have the token on the Ropsten chain.
You can check other details like name, symbol, owner, or tokenuri by entering the token id we mentioned earlier.Confirm the transaction in Metamask
Bravo! now you have your own NFT ,KEEP IT UP !
ref：https://ipfs.io/#install ——installation guide in IPFS docs.
https://remix.ethereum.org/：Ethereum Remix IDE
SPDX License List: https://spdx.org/licenses/
How to create and deploy an ERC-721 (NFT)
QuickNode guides are designed with beginners in mind. Learn all about developing on Web3.
- 3Commas Review | Pionex Review | Coinrule review
- Ledger vs Ngrave | Ledger nano s vs x | Binance Review
- Bybit Exchange Review | Bityard Review | Jet-Bot Review
- 3Commas vs Cryptohopper | Earn crypto interest
- The Best Bitcoin Hardware wallet | BitBox02 Review
- BlockFi vs Celsius | Hodlnaut Review | KuCoin Review