How to create and deploy an NFT smart contracts
Learn how to create and deploy NFT smart contracts with our step-by-step guide. Unlock the potential of blockchain and monetize your digital assets today.
NFTs have gained traction since the first token surfaced in 2014. In fact, according to a report from DappRadar, NFT sales volume grew from $94.9 Million in 2020 to $24.9 billion in 2021, The wallets trading them grew from over 500,000 to 26.6 Million within the year!
Despite the steady momentum, about 70% of the US Population still know nothing about NFTs.
The good news is you can create your own NFT smart contract and deploy it on the Ethereum network. You don’t need to be a programming wizard to get started.
In this guide, we will show you how to build an NFT using the ERC-721 standard and deploy it on the Ethereum main net (or testnet) using Alchemy and Metamask!
Set Up a Development Environment
The development environment is the software that you use to create your smart contract.
Smart contracts are the next layer of the blockchain. They allow developers to create applications that run on top of the blockchain, but with a more user-friendly interface.
The development environment can be either a local computer or an online service, such as Amazon Web Services (AWS).
Local Environment - Local environments are software programs that run directly on your computer.
Why do you need a development environment?
It is not always easy to deploy your smart contract on the Ethereum network using just your browser. You'll need some kind of basic programming language and access to other tools like MetaMask so that you can interact with the Ethereum network from within the Web3 tooling of your choice.
Install and Set Up Metamask
Setting up MetaMask lets you store and swap ether and ether-related tokens.
You can install Metamask on your computer by downloading the Metamask plugin for your browser. Then, create your MetaMask wallet.
Now that you have a wallet, you can begin setting up. To deploy NFT Smart Contract to the test network, you need to add ETH from a faucet.
You need to do this because you are required a Gas fee which you pay through cryptocurrencies or tokens whenever you want to deploy a code on the Blockchain.
You can do these by following two easy steps:
- Once you have installed MetaMask, change the network to Goerli Test Network.
- Locate Goerli Faucet, enter your wallet address, and transfer some test GoerliETH.
Your wallet should reflect a new balance when you are done.
Next, open up a new window in the browser and go to this link:
This will show all of the transactions made using that particular address since it was created, each one with the amount that was sent and received in ETH, as well as the token contract address where those tokens were sent.
Prepare your Alchemy Account
Alchemy provides an array of developer tools and infrastructures for blockchain companies.
You will need to create an account on the Alchemy platform. This will be used to create an ERC20 token. This account is separate from your Metamask account and uses a different wallet address.
You can do this by clicking "Create New Account" in the upper right-hand corner of your browser.
Once you have created an account, you have to create an app, input a name, and choose Network as Gorelik.
You'll need to import your wallet so that we can transfer funds from it into our smart contract. The easiest way to do this is through MetaMask. If you're using MetaMask, click "Import Wallet" in its top menu bar and then select the wallet you made from there.
After importing any necessary private keys/addresses that belong within each party's respective wallets (if any), make sure there is enough Ether available within both accounts before moving forward with creating our smart contract—this gas fee must be paid before being able to deploy anything onto the blockchain network.
Once you've confirmed that there is enough Ether available for deployment, click on "Create Contract" and fill out the form that appears. This can be done by selecting a name for your contract and choosing whether or not it should be open source (if you're creating a smart contract with another party). Once this information has been filled out, select "Create" at the bottom of the page.
Creating an Nft Smart Contract
Create a new file called "NFT.sol" in the contracts/ directory of the newly created nft-tutorial directory in your preferred IDE.// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;
import "@openzeppelin/contracts/token/ERC721/ERC721.sol";
import "@openzeppelin/contracts/utils/Counters.sol";
contract NFT is ERC721 {
using Counters for Counters.Counter;
Counters.Counter private currentTokenId;
constructor() ERC721("NFTTutorial", "NFT") {}
function mintTo(address recipient)
public
returns (uint256)
{
currentTokenId.increment();
uint256 newItemId = currentTokenId.current();
_safeMint(recipient, newItemId);
return newItemId;
}
Let's try to put our (basic) contract together and make sure it is prepared for deployment now that it has been written.
Since deploying the contract will cost us ETH, we will need to incorporate the MetaMask account we previously generated into our project.
Let's first create a new file called ".env" in the project's root folder. Both our Alchemy API key and our account's private key will be kept in this file.
When your private key and Alchemy API key are prepared, paste them into .env using the following format:
ALCHEMY_KEY = "alchemy-api-key"
ACCOUNT_PRIVATE_KEY = "private-key"
Next, update the hardhat.config.js file by adding the following:
/**
* @type import('hardhat/config').HardhatUserConfig
*/
require('dotenv').config();
require("@nomiclabs/hardhat-ethers");
const { ALCHEMY_KEY, ACCOUNT_PRIVATE_KEY } = process.env;
module.exports = {
solidity: "0.8.0",
defaultNetwork: "rinkeby",
networks: {
hardhat: {},
rinkeby: {
url: `https://eth-rinkeby.alchemyapi.io/v2/${ALCHEMY_KEY}`,
accounts: [`0x${ACCOUNT_PRIVATE_KEY}`]
},
ethereum: {
chainId: 1,
url: `https://eth-mainnet.alchemyapi.io/v2/${ALCHEMY_KEY}`,
accounts: [`0x${ACCOUNT_PRIVATE_KEY}`]
},
},
}
These modifications ought to be rather simple:
- Several imports from dotenv and hardhat-ethers are necessary in order to use the variables we previously set and to later deploy our contract.
- To deploy the same code to Rinkeby testnet, Ethereum mainnet, or any other network in the future, a module configuration specifies our default Solidity version, default network, and some network parameters.
Compile The Contract
To compile our program we use the following command:
npx hardhat compile
And the result should look something like this:
Compiling 1 file with 0.8.0
Compilation finished successfully
If done well, this should run freely without errors.
Deploy The Contract
When you deploy your smart contract, it is compiled and converted into bytecodes before it will be stored in the Blockchain and an address assigned to it.
The next step is to deploy the contract. This can be done by sending a transaction to the address of your smart contract and setting some parameters.
To do this, you'll need to create a transaction with Metamask.
- First, set the address of your newly created NFT smart contract as an input value in this transaction,
- Next, set the Gas Price and Gas Limit for each operation that should be performed by this smart contract,
- Sign off on this transaction using Metamask by clicking "Sign" or hitting enter on your keyboard.
There is just one thing left to do at this point: construct a straightforward JavaScript deploy script and run it through Hardhat.
Make a "deploy.js" file in the "scripts/" folder and put the following code in it:
async function main() {
// Get our account (as deployer) to verify that a minimum wallet balance is available
const [deployer] = await ethers.getSigners();
console.log(`Deploying contracts with the account: ${deployer.address}`);
console.log(`Account balance: ${(await deployer.getBalance()).toString()}`);
// Fetch the compiled contract using ethers.js
const NFT = await ethers.getContractFactory("NFT");
// calling deploy() will return an async Promise that we can await on
const nft = await NFT.deploy();
console.log(`Contract deployed to address: ${nft.address}`);
}
main()
.then(() => process.exit(0))
.catch((error) => {
console.error(error);
process.exit(1);
});
Validate The Smart Contract
Validating the smart contract is crucial as it protects the users and contributors to the Blockchain.
You can verify the contract's source code, bytecode and deployed address.
To verify the contract's source code, copy and paste your contract into a text editor (like Notepad), then run it through the Etherscan Javascript API.
You should see "Verified" next to a button that says "View Contract Source." Click this button and you'll be able to view your smart contract's source code.
Another option is to use Arcana software development kits(SDKs) to create and transact Private NFTs.
Here’s what you need to use the Arcana option:
- The Developer Dashboard on Arcana so you can register and configure your dApp.
- Deploy your own collection of NFTs if you don’t have a public AERC721 smart contract address
- Integrate your dApp with MetaMask so you can get an Ethereum provider to get a signer for NFT minting
Now, here’s how to create private NFTs on Arcana:
- Integrate Arcana Storage SDK: If you’ve met the required steps listed above, you may need to deploy your own NFT collection and mint it.
- Select Arcana Network: Inform the dApp user to choose "Arcana Network" when setting up their MetaMask wallet.
- Upload the NFT asset to Arcana Store: Once your dApp has been integrated with Arcana Storage SDK, use the file uploader function to upload the NFT asset data file to Arcana Store.Then, save the DID assigned to this NFT asset.
- Obtain makeMetaDataURL from Storage SDK: You can do this by specifying the title, description, uploading NFT asset's DID and a preview image, and using the makeMetaDataURL function.
- Change the Network in MetaMask: Inform the dApp user to change the network setting in MetaMask to the blockchain where you would like to mint the NFT.
- Mint the NFT: To do this, you’ll need a blockchain signer using ethers.js library
- Change the Network in MetaMask: Change the network back to Arcana Network before linking the tokenID you received after minting with Arcana Store DID.
- Link NFT to DID in Arcana Store: You can use the linkNFT function of the Arcana Storage SDK to link your NFT
- Verify that the private NFT has been created by using Arcana NFT Viewer