In this next part of the tutorial, we'll cover all the basics needed to mint an NFT to users wallets
Minting from new NFT contract
First, define some common helpers that we can reuse in later pieces of code. Create a new helpers.js file and add following code:
const { ethers } =require("ethers");const { getContractAt } =require("@nomiclabs/hardhat-ethers/internal/helpers");// Helper method for fetching environment variables from .envfunctiongetEnvVariable(key, defaultValue) {if (process.env[key]) {returnprocess.env[key]; }if (!defaultValue) {throw`${key} is not defined and no default value was provided`; }return defaultValue;}// Helper method for fetching a connection provider to the Ethereum networkfunctiongetProvider() {returnethers.getDefaultProvider(getEnvVariable("NETWORK","rinkeby"), { alchemy:getEnvVariable("ALCHEMY_KEY"), });}// Helper method for fetching a wallet account using an environment variable for the PKfunctiongetAccount() {returnnewethers.Wallet(getEnvVariable("ACCOUNT_PRIVATE_KEY"),getProvider());}// Helper method for fetching a contract instance at a given addressfunctiongetContract(contractName, hre) {constaccount=getAccount();returngetContractAt(hre, contractName,getEnvVariable("NFT_CONTRACT_ADDRESS"), account);}module.exports= { getEnvVariable, getProvider, getAccount, getContract,}
Open up the .env file you created in the last part of the tutorial and add a new variable NFT_CONTRACT_ADDRESS and set it to the
Add a new mint.js file to your scripts folder and write the task:
const { task } =require("hardhat/config");const { getContract } =require("./helpers");task("mint","Mints from the NFT contract").addParam("address","The address to receive a token").setAction(asyncfunction (taskArguments, hre) {constcontract=awaitgetContract("NFT", hre);consttransactionResponse=awaitcontract.mintTo(taskArguments.address, { gasLimit:500_000, });console.log(`Transaction Hash: ${transactionResponse.hash}`);});
Hardhat allows users to create preconfigured tasks in their projects that can execute common user actions using the Hardhat CLI directly. To do that, we will import the new mint.js file to our hardhat.config.js configuration so that it is picked up by Hardhat
> npx hardhat mint --address 0xb9720BE63Ea8896956A06d2dEd491De125fD705E=========NOTICE=========Request-RateExceeded (this messagewillnotberepeated)ThedefaultAPIkeysforeachserviceareprovidedasahighly-throttled,communityresourceforlow-trafficprojectsandearlyprototyping.Whileyourapplicationwillcontinuetofunction,wehighlyrecommendedsigningupforyourownAPIkeystoimproveperformance,increaseyourrequestrate/limitandenableotherperks,suchasmetricsandadvancedAPIs.Formoredetails:https://docs.ethers.io/api-keys/==========================TransactionHash:0x16ea832e486f115fb828dbec20e578a008675998256c807682d8cbdca17264d3
If you take that transaction hash over to Etherscan, you should see something like this:
This transaction record shows us who called the contract function, what the outcome was (Tokens Transferred) and how much the transaction cost the caller in both gas fees and cost to transact.