Skip to main content

Build a NFT Smart Contract ( ERC721 ) and deloy it on chain

 

earn-money-with-writing


Blockchain is here to stay. This is one of the biggest opportunities for the current generation. It is a time to learn more about this. 

In this tutorial, we're going to learn about nft smart contracts. This is one of the simple smart contracts.

We're gonna use

  • Solidity
  • OpenZeppelin
  • Metamask
  • Alchemy ( For node)
If you're not familiar with these terms then don't skip from here. I will introduce each along the way.

We're going to use Open Zeppelin. Open Zeppelin is a library of erc contracts maintained by high professionals. We're going to use their code for this project. They maintained a standard of ERC contracts. So everyone who develops coins or nfts has the same properties. Another reason their code is well written so it is also more gas efficient. 
Go to Open Zeppelin Website and select ERC-721






Then select ERC-721 in a given online editor. Then select Mintable, Auto Increment, Enumerable, and URI Storage from the left bar. 
I know you're feeling lost. But I will explain you step by step what is going on here. 



Also, give a name and symbol. These ones are for your nfts there is no need to provide Base URI for this one. 

// SPDX-License-Identifier : MIT

We're providing License Certificate for our project. There are a lot of Liceses but MIT means it is open source and everyone can use this Smart Contract.

// pragma solidity ^0.8.0

We're defining the solidity version for our project. Here we're telling the compiler that only use 0.8.0 for this. Solidity is changing so rapidly so it is necessary to specify the version you're going to use. 

import "@openzeppelin/contracts/token/ERC721/ERC721.sol"; import "@openzeppelin/contracts/token/ERC721/extensions/ERC721Enumerable.sol"; import "@openzeppelin/contracts/token/ERC721/extensions/ERC721URIStorage.sol"; import "@openzeppelin/contracts/access/Ownable.sol"; import "@openzeppelin/contracts/utils/Counters.sol";

We're also importing a bunch of files. These are from the OpenZeppelin library.  

contract Alchemy is ERC721, ERC721Enumerable, ERC721URIStorage, Ownable {...}

We're also using inheritance for that. We're inheriting all the functions of some openzeppelin libraries.


function safeMint(address to, string memory uri) public { uint256 tokenId = _tokenIdCounter.current(); _tokenIdCounter.increment(); _safeMint(to, tokenId); _setTokenURI(tokenId, uri); }

As you may have seen, the safeMint function has the "only owner" modification, which restricts who is permitted to mint NFTs to the owner of the smart contract (the wallet address from which the smart contract was deployed). You should delete the onlyOwner modifier from the Mint function because it's likely that you want anyone to mint NFTs.

You can also remove it from the contract declaration "Ownable", and the library imports

 You must prevent anyone from minting more NFTs than the maximum allowed in our collection now that anyone can mint our NFTs. Let's start by defining the maximum number of mintable NFTs.


Let's imagine you want users to be able to mint up to 10,000 NFTs altogether. Let's do this by making a new uint256 variable called MAX SUPPLY and setting its value to 10,000.

Counters.Counter private _tokenIdCounter; uint256 MAX_SUPPLY = 100000; constructor() ERC721("Alchemy", "ALCH") {}

Let's take a moment to clarify what the "need" declaration in Solidity does.


In the official documentation, you may read more about the Solidity "need" statement.


It's time to put the smart contract together and launch it on the Goerli Testnet now that you've restricted the maximum supply of our NFTs. To do this, you'll need to get some free Goerli ETH, register for a free account on Alchemy.com, then add it as a node provider on Metamask.

Include the following details on the form:


the Alchemy Goerli network

The HTTP URL of the Goerli Alchemy Application Chain ID: 5 is the new RPC URL.

Currency GoerliETH Block Explorer is accessible at https://goerli.etherscan.io.

Amazing, you just used Alchemy to add Goerli to Metamask.


It's time to launch our Goerli Smart Contract, but first, you must acquire some Goerli Test ETH.


Purchase Free Goerli Test ETH

It's quite easy to get Goerli Test ETH; just go to goerlifaucet.com, paste the

The Goerli ETH will start to appear in the Metamask Wallet after 10–20 seconds.


You can receive up to 0.1 ETH per day without signing in or 0.5 ETH per day if you have an Alchemy account.


It's time to create and launch our NFT smart contract on the blockchain now that you have the test Ethereum.


Install the NFT Smart Contract on the Goerli Testnet after compiling it.

Let's return to Remix and select the compiler option on the page's left side by clicking the blue "Compile" button:

Then click on deploy and run transactions on the remix ide. On the top dropdown select injected web3 option for this.

Make sure the Goerli network is selected in the metamask account. Other testnets are deprecated.

Now we need to add meta deta for our nft.

What is nft metadeta?

The contract must return a URI linking to the hosted metadata in order for OpenSea to import it for ERC721 tokens. OpenSea, Rarible, and other well-known marketplaces will employ the tokenURI method specified in the ERC721Uristorage standard to locate this URI.

An HTTP or IPFS URL, such as ipfs:/bafkreig4rdq3nvyg2yra5x363gdo4xtbcfjlhshw63we7vtlldyyvwagbq, should be returned by the tokenURI function in the ERC721. This URL should provide a JSON blob of information when queried, along with the token's metadata.


NFT Metadata Formatting Instructions

The NFT Metadata should be kept in a.json file and organised as follows, per the OpenSea documentation:

{ "description": "YOUR DESCRIPTION", "external_url": "YOUR URL", "image": "IMAGE URL", "name": "TITLE", "attributes": [ { "trait_type": "Base", "value": "Starfish" }, { "trait_type": "Eyes", "value": "Big" }, { "trait_type": "Mouth", "value": "Surprised" }, { "trait_type": "Level", "value": 5 }, { "trait_type": "Stamina", "value": 1.4 }, { "trait_type": "Personality", "value": "Sad" }, { "display_type": "boost_number", "trait_type": "Aqua Power", "value": 40 }, { "display_type": "boost_percentage", "trait_type": "Stamina Increase", "value": 10 }, { "display_type": "number", "trait_type": "Generation", "value": 2 }] }


Making and Uploading the IPFS Metadata

Go to filebase.com and register for a new account first. After logging in, select the bucket option from the left-hand menu to start a new bucket: 

You can upload the image you wish to use for your NFT by navigating to the bucket, clicking the upload button, and doing so. I'll use the following image. Click it once it has been uploaded, then copy the IPFS Gateway URL:



{ "description": "This NFT proves I've created and deployed my first ERC20 smart contract on Goerli with Alchemy Road to Web3", "external_url": "Alchemy.com/?a=roadtoweb3weekone", "image": "https://ipfs.filebase.io/ipfs/bafybeihyvhgbcov2nmvbnveunoodokme5eb42uekrqowxdennt2qyeculm", "name": "A cool NFT", "attributes": [ { "trait_type": "Base", "value": "Starfish" }, { "trait_type": "Eyes", "value": "Big" }, { "trait_type": "Mouth", "value": "Surprised" }, { "trait_type": "Level", "value": 5 }, { "trait_type": "Stamina", "value": 1.4 }, { "trait_type": "Personality", "value": "Sad" }, { "display_type": "boost_number", "trait_type": "Aqua Power", "value": 40 }, { "display_type": "boost_percentage", "trait_type": "Stamina Increase", "value": 10 }, { "display_type": "number", "trait_type": "Generation", "value": 2 }] }

Save the file under the name "metadata.json." Reopen Filebase and upload the metadata.json file to the same bucket as the Image was previously posted.

Your Goerli NFT, mint

Return to Remix and click on the contract we just deployed under "deployed contracts" under the Deploy & Run Transactions menu. This will bring up a list of all the methods in your Smart contact.

Blue methods are those that learn from the blockchain, while Orange methods actively publish to the blockchain. Paste your address and the string shown below into the uri field after selecting the safeMint method option icon: ipfs:/\ A Metamask popup asking you to pay the gas fees will appear when you click the transact button. To continue minting your first NFT, click "sign." After a little delay, copy and paste your address into the balanceOf method field to verify that the mint transaction was successful. Run the method, and it should display 1 NFT. Use the tokenUri method to display your tokenURI by putting "0" as the id argument. Great! You recently produced your first NFT! Picture Your NFT on the Open Sea Go to testnets.opensea.io and enter your Metamask wallet to sign in. When you do so, your freshly created NFT should appear when you click on your profile image. Click the image if it is not now visible, then select "refresh metadata."


OpenSea occasionally has trouble identifying testnet metadata, and it could take up to 6 hours for it to appear. Your NFT should become visible after some time as shown below:

Congratulations! Your first smart contract has been successfully generated, altered, and deployed. published your image on IPFS and issued your first NFT!


The next step Why not change your smart contract such that users can only mint a specific number of NFTs? 5 should be adequate for each user, or else someone might start producing thousands of NFTs!


To do this, investigate the mapping type; this site has a fantastic guide that will walk you through it.



Comments

Popular posts from this blog

University of Engineering and Technology(uet), Taxila

University of Engineering and technology, taxila is one of the top engineering universities of Pakistan.It is a public sector university in taxila, Punjab.It is founded in 1975 as a campus of uet lahore. but separated as a independent university in 1993. In the recent ranking of HEC uet ranked in 10th position of top ten engineering universities of Pakistan. Indeed uet has a high merit as years passing uet taxila losses its position. It falls from 5th position to 10th. It losses its fame and position. In this years uet announces spring admission as well to fill up their seats. Chakwal  campus of uet taxila fails to fill up their seats.  We are hoping that uet taxila will regain its position win future. It offers Engineering and computer science degree programs. Engineering             >Civil Engineering             >Mechanical Engineering             >Electrical Eng...

Dropshipping

Drop shipping                                       Drop shipping is now best growing business in this world people generaed a huge revenue from this business and earned a huge profit though in that helps them to grow their business and money. The dropshipping is stated as   the product invented somewhere else and sell somewhere else. Product reach to the end user that passes from different phases and then reach to end user. But in dropshipping we directly send our product from company to end user without a single bug in our  pocket. Yes it is true that you can earn a huge rofit thorough it without a single penny. 1.Offline                       In the offline market we buy products in our own country  and then sell it on the online platform like  www.shopify.com  and many more you can generate h...

How to learn and remember things Easily

A study was conducted in Princeton university in 2014. Where they separated class in two groups the researchers that had each groups take notes for a class in two ways the first group was told to take notes by hands you know to old school method. but the results really shocked us because the people who are taking notes with pen and paper are more efficient they remember things much better than others