More
    StartKryptowährung News1. How to Develop an NFT Smart Contract (ERC721) with Alchemy

    1. How to Develop an NFT Smart Contract (ERC721) with Alchemy

    Video 1. How to Develop an NFT Smart Contract (ERC721) with Alchemy

    Developing a Smart Contract with Solidity and deploying it on the blockchain might sound daunting at first: solidity, security, gas optimization, developer environment, and gas fees, are only some of the things you’ll need to go through to host your code on the blockchain.

    Luckily though, in the last few months, a lot of tools have been released for developers to make their Smart contracts development jobs much easier.

    Tools like the OpenZeppelin Wizard that offers developers click and write functionalities to create composable and secure smart contracts in no time, used with Web3 developer tools like Alchemy, make the experience of writing a deploying code on the blockchain easy, fast, and reliable like never before.

    In this tutorial, you’re going to learn how to develop and deploy an ERC721 (NFT) smart contract using Alchemy, OpenZeppelin, Remix, and Ethereum Rinkeby.

    More precisely, you will learn:

    • How to write and modify the smart contract using OpenZeppelin and Remix
    • Get free Rinkeby ETH using rinkebyfaucet.com
    • Deploy it on the Ethereum Rinkeby testnet blockchain to save on gas fees
    • Host the NFT tokens metadata on IPFS using Filebase.
    • Mint an NFT and visualize it on OpenSea

    You can also follow the video tutorial:

    Let’s start by creating the smart contract.

    As said before, in this tutorial, you’re going to use the OpenZeppelin Wizard to create the smart contract, for two main reasons:

    • It’s secure.
    • It offers standard complaining smart contracts.

    When it comes to writing smart contracts, security is key. There are tons of examples of smart contract exploits that have seen hundreds of millions of dollars stolen by malicious actors due to bad security.

    You don’t want someone to steal all of your precious cryptos or NFTs once you’ll deploy on the blockchain right?

    OpenZeppelin serves this purpose, being one of the biggest maintainers of smart contract standards (ERC20, ERC721, etc), allowing developers to use thoroughly audited code to develop reliable contracts.

    The first thing you’ll need to do to develop our ERC721 NFT smart contract is to go on the Open Zeppelin Smart contract wizard page.

    Once on the page, you will see the following editor:

    985

    Click on the ERC721 button on the top left corner, to select the type of ERC standard to use and the kind of contract you want to write:

    997997

    Now that you’ve selected the contract standard, on the left-hand side menu you should see a number of options:

    Let’s start by choosing the name and the symbol of our Tokens. Click on the text box with “MyToken” and give it a name, do the same with the Symbol, and leave the base URI field blank (the token name will be used by OpenSea and Rarible as the name of the collection).

    516

    Now you’ll need to select the features that you want to integrate into our Smart contract, right after the “settings” section, you’ll find the “features” section where you’ll be able to select the different modules to include in your smart contract.

    258258

    In this case, you’re going to select the following integrations:

    • Mintable will create a mint function only callable by privileged accounts
    • Autoincrement IDs will automatically assign incremental IDs to your NFTs
    • Enumerable will give you access to on-chain Tokens enumeration and functions such as “totalSupply”, not present in the default ERC721 integration URI storage, to associate metadata and images to each of your NFTs
    • URI Storage to be able to associate URIs to our NFTs

    256

    For the sake of this tutorial, and because you don’t want to create any kind of Tokenomic around our NFTs, leave the following modules unticked:

    • Burnable – to burn tokens
    • Pausable – to pause tokens transfers, sales, etc
    • Votes – gives access to governance-like features like delegates and votes

    If you want to learn more about these modules, check the official OpenZeppelin documentation about the ERC721 standard.

    Now that you’ve selected the features you want, OpenZeppelin Wizard will populate the code of the Smart Contract, it should look as follow:

    It’s time to copy our code and bring it on Remix IDE to modify and deploy it on the Blockchain.

    Now that you have your ERC721 smart contract, let’s modify, and deploy it on the Rinkeby Testnet. To do so, you’ll use Remix IDE, a free and web-based integrated development environment specifically designed for smart contract development with Solidity.

    First of all, as you might have noticed, on top of the OpenZeppelin Wizard editor, there’s the button “Open in Remix”:

    960960

    Clicking on it will open REMIX IDE in a new tab of your browser.

    Starting from the top of the contract, there’s the “SPDX-License-Identifier” that specifies the type of license your code will be published under – it’s good practice in web3 applications to keep the code open source as it will ensure trustworthiness.

    Then there’s the pragma – the version of the compiler you’ll want to use to compile the smart contract code. The little “^” symbol, tells the compiler that every version between 0.8.0 to 0.8.9 is suitable to compile our code.

    Then we’re importing a bunch of libraries and initializing the smart contract.

    We’re then initializing the contract, inheriting from all the standards we’re importing from the OpenZeppelin repository:

    As you can notice, the safeMint function has the “only owner” modifier – this will allow only the owner of the smart contract (the wallet address that deployed the smart contract) to mint NFTs. Chances are you want anyone to mint NFTs, to do so you’ll need to remove the onlyOwner modifier from the Mint function.

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

    Now that everyone will be able to mint our NFTs, you’ll need to avoid people from minting more NFTs than the max number of NFTs in our collection. To do so let’s specify the max number of mintable NFTs.

    Let’s say want the users to be able to mint up to a total of 10,000 NFTs. To do so, let’s create a new uint256 variable, call it MAX_SUPPLY, and assign it 10,000.

    Next, let’s move into the safeMint function and add a require statement on line 18:

    Let’s spend a couple of words to better understand what is the „require“ statement in Solidity.

    You can read more about the Solidity „require“ statement, in the official documentation.

    Now that you’ve limited the max supply of our NFTs, is time to compile the smart contract and deploy it on the Rinkeby Testnet. To do so, you’ll need to create a free account on Alchemy.com, add it as a node provider on Metamask, and get some free Rinkeby ETH.

    First of all, let’s need to navigate to alchemy.com click on “Login” and create a new account:

    2494

    Select the Ethereum ecosystem:

    2660

    Give your application app and team a name, choose the Rinkeby network and click on create App:

    1298

    Once you’ll have completed the onboarding process, we’ll be redirected to the dashboard. Click on the application with the name you decided, in this case, “test”, click on the “VIEW KEY” button on the top right corner, and copy the HTTP URL:

    3122

    Next, you’ll need to add Alchemy to Metamask as a Rineby RPC Provider. If you don’t have Metamask installed, make sure to follow this guide to add it to your browser and create a new wallet.

    Once Metamask will be installed, click on the Network dropdown menu, and on “add network”!

    708

    You’ll be redirected to the following page, where you’ll need to fill in the Rinkeby Network and RPC URL info.

    1230

    Add the following information to the form:

    • Network name: Alchemy Rinkeby
    • New RPC URL: the HTTP URL of the Rinkeby Alchemy Application
    • Chain ID: 4
    • Currency Symbol: ETH
    • Block Explorer: https://rinkeby.etherscan.io/

    Amazing, you just added Rinkeby to Metamask, using Alchemy! 🎉

    It’s now time to deploy our Smart Contract on Rinkeby, but first, you’ll need to get some Rinkeby Test ETH.

    Getting Rinkeby Test ETH is super simple, just navigate to rinkebyfaucet.com, copy the wallet address into the text bar, and click on “Send Me ETH”:

    2028

    After 10-20 seconds you’ll see the Rinkeby ETH appearing in the Metamask Wallet.

    You’ll be able to get up to 0.1 ETH every 24 without logging in, or 0.5 with an Alchemy account.

    Now that you have the test ETH, it’s time to compile and deploy our NFT smart contract on the blockchain.

    Back to Remix, let’s click on the compiler menu on the left-hand side of the page and click on The blue “Compile” button:

    357

    Then click on the “Deploy and Run Transactions menu, click on the Environment dropdown menu and select “injected Web3”:

    Make sure the Metamask wallet is on the Alchemy Rinkeby network, select the NFT Smart contract from the Contract dropdown menu, and click on Deploy.

    366

    A Metamask pop-up window will appear, click on „sign“, and proceed to pay the gas fees.

    If everything worked as expected, after 10 seconds you should see the contract listed under Deployed Contracts:

    628

    Now that the Smart contract is deployed on the Rinkeby testnet, it’s time to mint our NFTs, but first, you’ll need to create and upload the metadata on IPFS, let’s understand what we mean by the term “metadata”.

    2075

    For OpenSea to pull in off-chain metadata for ERC721 tokens, the contract will need to return a URI pointing to the hosted metadata. To find this URI, OpenSea, Rarible and other popular marketplaces will use the tokenURI method contained in the ERC721Uristorage standard.

    The tokenURI function in the ERC721 should return an HTTP or IPFS URL, such as ipfs://bafkreig4rdq3nvyg2yra5x363gdo4xtbcfjlhshw63we7vtlldyyvwagbq. When queried, this URL should return a JSON blob of data with the metadata for your token.

    You can read more about metadata standars on the official OpenSea documentation.

    According to the OpenSea documentation, the NFT Metadata should be stored in a .json file and structured as follow:

    Here’s a brief explanation of what each property stores:

    Now that we have a brief understanding of what will be contained in your tokens metadata, let’s learn how to create it and store it on IPFS.

    First of all, navigate to filebase.com and create a new account.

    Once logged in, click on the bucket button on the left-hand side menu, and create a new bucket:

    3388

    Navigate into the bucket, click on the upload button, and upload the image you want to use for your NFT, I’ll use the following.

    Once uploaded click on it and copy the IPFS Gateway URL:

    2894

    Using any text editor, paste the following JSON code:

    And save the file as „metadata.json“. Move back to Filebase and upload the metadata.json file in the same bucket where we uploaded the Image.

    3446

    Lastly, click on the CID and copy it, we’ll need it in the next part to build the token URI when minting out NFT:

    2820

    Go back to Remix and in the Deploy & Run Transactions menu, go under “deployed contracts” – and click on the contract we just deployed, it will open up a list of all the methods contained in your Smart contact:

    Orange methods are methods that actually write on the blockchain whereas Blue methods are methods learning from the blockchain.

    Click on the safeMint method dropdown icon and paste your address and the following string into the uri field:

    ipfs://<your_metadata_cid>

    Clicking on transact will create a Metamask popup prompting you to pay the gas fees.

    Click on „sign“ and go on minting your first NFT!

    Wait a couple of seconds and, to make sure the mint went through successfully, copy and paste your address in the balanceOf method input, and run it – it should show you have 1 NFT.

    Do the same with the tokenUri method, inserting “0” as the id argument – it should display your tokenURI.

    Great! You just minted your first NFT! 🎉

    Now it’s time to move to OpenSea to check if the metadata is benign read.

    Navigate to testnets.opensea.io and log in with your Metamask wallet. Then click on your profile picture, you should see your newly minted NFT there. If the image is not yet visible, click on it, and click on the “refresh metadata” button.

    Sometimes OpenSea has hard times recognizing testnet metadata – and could take up to 6 hours to see it. After some time your NFT should be visible as follow:

    Congratulations, you have successfully created, modified, and deployed your first smart contract. Minted your first NFT, and published your image on IPFS! 🔥

    Next step? Why don’t you modify your smart contract to allow users to only mint only up to a certain number of NFTs? 5 per user should be enough, or someone might start minting thousands of NFTs!

    To do so, look into the mapping type, here there’s an amazing guide to walk you through it.

    Want the video version of this tutorial? Subscribe to the Alchemy YouTube channel and join our Discord community to find thousands of developers ready to help you out!

    We are always looking to improve this learning journey, please share any feedback you have with us! https://alchemyapi.typeform.com/roadtofeedback

    Read more: Nft nftsgravesdecrypt – Krypto-NFTs

    Source: 🔗