Deploy Your NFT Contract to Goerli Testnet
Hi Folks I’m back with another article. I hope you guys will enjoy and learn from this one too. I will tell you how to deploy the Contract to the Network in this article. Networks are different Ethereum environments You can access them for Development, Testing, and Production. Since Ethereum is a protocol then there may be multiple Ethereum networks here are some abstract lists:
Public Network: Public Network are accessible to anyone throught an internet connection. Anyone read and create transactions on a public blockchain and validate the transaction being executed.
Ethereum Mainnet: Mainnet is the primmary public Ethereum production blockchain Where the actual transaction occurs.
Ethereum Testnets: There are test networks that is used by Blockchain developer to test their smart contract and before deployment on the Mainnet. You can consider the Testnets as the stage environment.
There is a list of Ethereum Testnet but we are going for the Goerli Test network. So how do deployments? How it actually works? I will tell you guys. Connect with me. If you are interested to learn some other stuff don't hesitate to get in touch with me through LinkedIn and through mail: shahirzain100@gmail.com
so for deployment, you need a contract so we start from scratch.
We will walk through creating and deploying ERC-721, You should have
HardHat, Solidity, Metamask, and Alchemy.
Step- 01
make a directory mkdir rich-rhinos
and go into the directory cd rich-rhinos
run command npm init -y
with -y
for a default configuration. Make sure you have Node.js installed on your machine.
Then install the Hardhat command npm i hardhat
and then run npx hardhat
in your terminal, you should see a welcome message and some options, select create an empty hardhat.config.js
Step-02
create a folder for Contracts and scripts. Create file name rich_Rhinos.sol write your contract in the contract folder Like I have mine
// SPDX-License-Identifier: MITpragma solidity >= 0.7.0;
import "@openzeppelin/contracts/token/ERC721/ERC721.sol";
import "@openzeppelin/contracts/utils/Counters.sol";
import "@openzeppelin/contracts/access/Ownable.sol";
import "@openzeppelin/contracts/token/ERC721/extensions/ERC721URIStorage.sol";contract RichRhinos is ERC721URIStorage, Ownable { using Counters for Counters.Counter;
Counters.Counter private _tokenIds; constructor() ERC721("RichRhinos", "NFT"){}
function mintNFT(address recipient, string memory tokenUri) public onlyOwner returns (uint256){ _tokenIds.increment();
uint256 newItemId = _tokenIds.current();
_mint(recipient,newItemId);
_setTokenURI(newItemId,tokenUri);
return newItemId;
}
}
so, after importing statements we have our custom NFT contract. This contract is actually short. If you focus on the constructor we pass two parameters, the first one is an NFT name and the second one is the NFT symbol. Finally, we have the function mintNFT(address recipient, string memory tokenURI)
that allows us to mint an NFT!
Step-03
Go to Alchemy, and create your account if you don't have any. Once you get logged in there is an Apps tab on which you have to hover and then click on the create App
.
Enter the name of your NFT like mine have Rich Rhinos and chose network like I chose Goerli testnet
go to your app from the Apps drop-down, click on the view key
copy the API KEY and HTTPS go to the project and install dotenv npm install dotenv --save
create .env
file and paste the copied item like
API_URL="your-metamask-https-url"
PRIVATE_KEY="your-metamask-private-key"
note: don't commit your .env file please add it to your gitignore file.
Step-04
install Ether.js in your app
npm install --save-dev @nomiclabs/hardhat-ethers ethers@^5.0.0
this library provides easier interaction with the Ethereum blockchain. so far w have added multiple dependencies so we need to update our hardhat.config.js your hardhat config should look like this
require("dotenv").config();
require("@nomiclabs/hardhat-ethers");
const { API_URL, PRIVATE_KEY } = process.env;module.exports = {
solidity: "0.8.1",
defaultNetwork: "ropsten",
networks: {
hardhat: {},
ropsten: {
url: API_URL,
accounts: [`0x${PRIVATE_KEY}`],
},
},
};
Step-05
now it's time to compile and write the deployment script. To make sure everything is perfect we need to compile our contract by typing this command in the terminal
npx hardhat compile
if everything is perfect then go to the scripts folder and create a file named deploy.js
and the below statements
async function main() {const RichRhinos = await ethers.getContractFactory("RichRhinos");
const gasPrice = await RichRhinos.signer.getGasPrice();
console.log(`Current gas price: ${gasPrice}`);const estimatedGas = await RichRhinos.signer.estimateGas(
RichRhinos.getDeployTransaction()
);console.log(`Estimated gas: ${estimatedGas}`);const deploymentPrice = gasPrice.mul(estimatedGas);
const deployerBalance = await RichRhinos.signer.getBalance();console.log(`Deployer balance: ${ethers.utils.formatEther(deployerBalance)}`);console.log( `Deployment price: ${ethers.utils.formatEther(deploymentPrice)}`);if (Number(deployerBalance) < Number(deploymentPrice)) { throw new Error("You dont have enough balance to deploy.");}// Start deployment, returning a promise that resolves to a contract objectconst myNFT = await RichRhinos.deploy();await myNFT.deployed();console.log("Contract deployed to address:", myNFT.address);}main().then(() => process.exit(0)).catch((error) => {console.error("Error:", error);process.exit(1);});
but for the deployment, you should have a digital wallet like metamask, Download metamask and choose the network as goerli.
for the first time you have 0 goerli Eth so deployment needs some gas fees so go to this site https://goerli-faucet.slock.it/
for faucet eth.
Now the time has come to run the deployment command in your terminal
npx hardhat --network ropsten run scripts/deploy.js
you should see something like this.
If we go to the Goerli etherscan and search for our contract address we should be able to see that it has been deployed successfully.
Yayyyyyy! You just deployed your NFT smart contract to the Ethereum chain!
That’s it, folks! hope it was a good read for you. Thank you! ✨
👉 Contact me: shahirzain100@gmail.com
New to trading? Try crypto trading bots or copy trading