Minting from your new contract

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 .env
function getEnvVariable(key, defaultValue) {
    if (process.env[key]) {
        return process.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 network
function getProvider() {
    return ethers.getDefaultProvider(getEnvVariable("NETWORK", "rinkeby"), {
        alchemy: getEnvVariable("ALCHEMY_KEY"),

// Helper method for fetching a wallet account using an environment variable for the PK
function getAccount() {
    return new ethers.Wallet(getEnvVariable("ACCOUNT_PRIVATE_KEY"), getProvider());

// Helper method for fetching a contract instance at a given address
function getContract(contractName, hre) {
    const account = getAccount();
    return getContractAt(hre, contractName, getEnvVariable("NFT_CONTRACT_ADDRESS"), account);

module.exports = {

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

ALCHEMY_KEY = "alchemy-api-key"
ACCOUNT_PRIVATE_KEY = "private-key"

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(async function (taskArguments, hre) {
    const contract = await getContract("NFT", hre);
    const transactionResponse = await contract.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

* @type import('hardhat/config').HardhatUserConfig


const { ALCHEMY_KEY, ACCOUNT_PRIVATE_KEY } = process.env;

module.exports = {
   solidity: "0.8.0",
   defaultNetwork: "rinkeby",
   networks: {
    hardhat: {},
    rinkeby: {
      url: `${ALCHEMY_KEY}`,
      accounts: [`0x${ACCOUNT_PRIVATE_KEY}`]
    ethereum: {
      chainId: 1,
      url: `${ALCHEMY_KEY}`,
      accounts: [`0x${ACCOUNT_PRIVATE_KEY}`]

We are now ready to go! To mint tokens, call our new mint task:

npx hardhat mint --address 0x0dD768A47eA2395bbE5574E3252505fe1202C4A8
> npx hardhat mint --address 0xb9720BE63Ea8896956A06d2dEd491De125fD705E
========= NOTICE =========
Request-Rate Exceeded  (this message will not be repeated)

The default API keys for each service are provided as a highly-throttled,
community resource for low-traffic projects and early prototyping.

While your application will continue to function, we highly recommended
signing up for your own API keys to improve performance, increase your
request rate/limit and enable other perks, such as metrics and advanced APIs.

For more details:
Transaction Hash: 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.

Last updated