0

I am following the Patrick Collins tutorial for the hardhat-nft-marketplace. I am running a hardhat script with the terminal command: yarn hardhat run scripts/mint-and-list.js

And I am getting the following error:

$ /Users/johndoe/local/hardhat-fcc-course/hardhat-nft-marketplace-fcc/node_modules/.bin/hardhat run scripts/mint-and-list.js
Error: No Contract deployed with name NftMarketplace
    at Object.getContract (/Users/johndoe/local/hardhat-fcc-course/hardhat-nft-marketplace-fcc/node_modules/@nomiclabs/hardhat-ethers/src/internal/helpers.ts:447:11)
    at mintAndList (/Users/johndoe/local/hardhat-fcc-course/hardhat-nft-marketplace-fcc/scripts/mint-and-list.js:6:28)
error Command failed with exit code 1.

For the below script: mint-and-list.js

const { ethers } = require("hardhat")

const PRICE = ethers.utils.parseEther("0.1")

async function mintAndList() {
    const nftMarketplace = await ethers.getContract("NftMarketplace")
    // console.log(nftMarketplace.address)
    const basicNft = await ethers.getContract("BasicNft")
    // console.log(basicNft.address)
    console.log("Minting...")
    const mintTx = await basicNft.mintNft()
    const mintTxReceipt = await mintTx.wait(1)
    const tokenId = mintTxReceipt.events[0].args.tokenId
    console.log("Approving Nft...")

    const approvalTx = await basicNft.approve(nftMarketplace.address, tokenId)
    await approvalTx.wait(1)
    console.log("Listing Nft...")
    const tx = await nftMarketplace.listItem(basicNft.address, tokenId, PRICE)
    await tx.wait(1)
    console.log("Listed!")
}

mintAndList()
    .then(() => process.exit(0))
    .catch((error) => {
        console.error(error)
        process.exit(1)
    })

Here are the deploy scripts:

01-deploy-nft-marketplace.js

const { network } = require("hardhat")
const { developmentChains } = require("../helper-hardhat-config.js")
const { verify } = require("../utils/verify")

module.exports = async function ({ getNamedAccounts, deployments }) {
    const { deploy, log } = deployments
    const { deployer } = await getNamedAccounts()

    log("----------------------------")
    const args = []
    const nftMarketplace = await deploy("NftMarketplace", {
        from: deployer,
        args: args,
        log: true,
        waitConfirmations: network.config.blockConfirmations || 1,
    })
    // const nftMarketplace = await deploy("NftMarketplace", deployer)

    // Verify the deployment
    if (!developmentChains.includes(network.name) && process.env.ETHERSCAN_API_KEY) {
        log("Verifying...")
        await verify(nftMarketplace.address, args)
    }

    log("----------------------------")
}

module.exports.tags = ["all", "nftmarketplace"]

02-deploy-basic-nft.js

const { network } = require("hardhat")
const { developmentChains } = require("../helper-hardhat-config.js")
const { verify } = require("../utils/verify")

module.exports = async function ({ getNamedAccounts, deployments }) {
    const { deploy, log } = deployments
    const { deployer } = await getNamedAccounts()

    log("----------------------------")
    const args = []
    const basicNft = await deploy("BasicNft", {
        from: deployer,
        args: args,
        log: true,
        waitConfirmations: network.config.blockConfirmations || 1,
    })

    // Verify the deployment
    if (!developmentChains.includes(network.name) && process.env.ETHERSCAN_API_KEY) {
        log("Verifying...")
        await verify(basicNft.address, args)
    }

    log("----------------------------")
}

module.exports.tags = ["all", "basicnft"]

1 Answer 1

1

I eventually found out that this can be resolved by simply specifying the network --network localhost and hence running this command in the terminal:

yarn hardhat run scripts/mint-and-list.js --network localhost

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.