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)
// 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?
NFT Metadata Formatting Instructions
{ "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
Post a Comment