Hardhat Deploy TypeError – How to Resolve ethers.getContract Function Errors

ethers.jshardhathardhat-deployjavascriptsolidity

I'd look for an alternative but not being able to use the ethers module is going to drive me nuts down the road. The code was copied from Patrick's Solidity Javascript course, the NFT section, Lesson 14.

The code below is supposed to deploy mocks to hardhat's local ganache chain and prints out image locations right after I hit the console.log("——-") line.

02-deploy-random-ipfs.js

const { network, ethers } = require("hardhat");
const { developmentChains, networkConfig } = require("../helper-hardhat-config");
const { verify } = require("../utils/verify");
const { storeImages } = require("../utils/uploadToPinata");

const imagesLocation = "./images/randomNft";

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


    // get IPFS hashes of images
    let tokenUris;

    if (process.env.UPLOAD_TO_PINATA == "true") {
        tokenUris = await handleTokenUris();
    }

    let vrfCoordinatorV2Address, subscriptionId; 
    if (developmentChains.includes(network.name)) {
        //const vrfCoordinatorV2Mock = await deployments.get("VRFCoordinatorV2Mock"); // works
        const vrfCoordinatorV2Mock = await ethers.getContract("VRFCoordinatorV2Mock"); /**ERROR**/
        
        vrfCoordinatorV2Address = vrfCoordinatorV2Mock.address;
        const tx = await vrfCoordinatorV2Mock.createSubscription();
        const txReceipt = await tx.wait(1);
        subscriptionId = txReceipt.events[0].args.subId;
    } else {
        vrfCoordinatorV2Address = networkConfig[chainId].vrfCoordinatorV2;
        subscriptionId = networkConfig[chainId].subscriptionId;
    }

    console.log("------------------------------");
    await storeImages(imagesLocation);

    // const args = [
    //     vrfCoordinatorV2Address,
    //     subscriptionId,
    //     networkConfig[chainId].gasLane,
    //     networkConfig[chainId].callbackGasLimit,
    //     //tokenUris,
    //     networkConfig[chainId].mintFee,
    // ];
}

async function handleTokenUris() {
    tokenUris = [];
    // store image in IPFS
    // store metadata in IPFS

    return tokenUris
}

module.exports.tags = ["all", "randomipfs", "main", "02"];

Stack trace:

Error: ERROR processing /Users/user/Documents/Programs/link/hh-fcc/hardhat-nft-fcc/deploy/02-deploy-random-ipfs.js:
TypeError: ethers.getContract is not a function
    at Object.module.exports [as func] (/Users/user/Documents/Programs/link/hh-fcc/hardhat-nft-fcc/deploy/02-deploy-random-ipfs.js:25:51)
    at processTicksAndRejections (node:internal/process/task_queues:96:5)
    at DeploymentsManager.executeDeployScripts (/Users/user/Documents/Programs/link/hh-fcc/hardhat-nft-fcc/node_modules/hardhat-deploy/src/DeploymentsManager.ts:1220:22)
    at DeploymentsManager.runDeploy (/Users/user/Documents/Programs/link/hh-fcc/hardhat-nft-fcc/node_modules/hardhat-deploy/src/DeploymentsManager.ts:1053:5)
    at SimpleTaskDefinition.action (/Users/user/Documents/Programs/link/hh-fcc/hardhat-nft-fcc/node_modules/hardhat-deploy/src/index.ts:422:5)
    at Environment._runTaskDefinition (/Users/user/Documents/Programs/link/hh-fcc/hardhat-nft-fcc/node_modules/hardhat/src/internal/core/runtime-environment.ts:308:14)
    at Environment.run (/Users/user/Documents/Programs/link/hh-fcc/hardhat-nft-fcc/node_modules/hardhat/src/internal/core/runtime-environment.ts:156:14)
    at SimpleTaskDefinition.action (/Users/user/Documents/Programs/link/hh-fcc/hardhat-nft-fcc/node_modules/hardhat-deploy/src/index.ts:568:32)
    at Environment._runTaskDefinition (/Users/user/Documents/Programs/link/hh-fcc/hardhat-nft-fcc/node_modules/hardhat/src/internal/core/runtime-environment.ts:308:14)
    at Environment.run (/Users/user/Documents/Programs/link/hh-fcc/hardhat-nft-fcc/node_modules/hardhat/src/internal/core/runtime-environment.ts:156:14)
    at DeploymentsManager.executeDeployScripts (/Users/user/Documents/Programs/link/hh-fcc/hardhat-nft-fcc/node_modules/hardhat-deploy/src/DeploymentsManager.ts:1223:19)
    at processTicksAndRejections (node:internal/process/task_queues:96:5)
    at DeploymentsManager.runDeploy (/Users/user/Documents/Programs/link/hh-fcc/hardhat-nft-fcc/node_modules/hardhat-deploy/src/DeploymentsManager.ts:1053:5)
    at SimpleTaskDefinition.action (/Users/user/Documents/Programs/link/hh-fcc/hardhat-nft-fcc/node_modules/hardhat-deploy/src/index.ts:422:5)
    at Environment._runTaskDefinition (/Users/user/Documents/Programs/link/hh-fcc/hardhat-nft-fcc/node_modules/hardhat/src/internal/core/runtime-environment.ts:308:14)
    at Environment.run (/Users/user/Documents/Programs/link/hh-fcc/hardhat-nft-fcc/node_modules/hardhat/src/internal/core/runtime-environment.ts:156:14)
    at SimpleTaskDefinition.action (/Users/user/Documents/Programs/link/hh-fcc/hardhat-nft-fcc/node_modules/hardhat-deploy/src/index.ts:568:32)
    at Environment._runTaskDefinition (/Users/user/Documents/Programs/link/hh-fcc/hardhat-nft-fcc/node_modules/hardhat/src/internal/core/runtime-environment.ts:308:14)
    at Environment.run (/Users/user/Documents/Programs/link/hh-fcc/hardhat-nft-fcc/node_modules/hardhat/src/internal/core/runtime-environment.ts:156:14)
    at SimpleTaskDefinition.action (/Users/user/Documents/Programs/link/hh-fcc/hardhat-nft-fcc/node_modules/hardhat-deploy/src/index.ts:653:5)

package.json

{
  "devDependencies": {
    "@chainlink/contracts": "^0.5.1",
    "@nomiclabs/hardhat-ethers": "npm:hardhat-deploy-ethers",
    "@nomiclabs/hardhat-etherscan": "^3.1.2",
    "@nomiclabs/hardhat-waffle": "^2.0.3",
    "@openzeppelin/contracts": "^4.8.0",
    "@pinata/sdk": "^2.1.0",
    "chai": "^4.3.7",
    "dotenv": "^16.0.3",
    "ethereum-waffle": "^3.4.4",
    "ethers": "5.5.4",
    "hardhat": "^2.12.2",
    "hardhat-contract-sizer": "^2.6.1",
    "hardhat-deploy": "0.10.5",
    "hardhat-gas-reporter": "^1.0.9",
    "prettier-plugin-solidity": "^1.0.0-rc.1",
    "solhint": "^3.3.7",
    "solidity-coverage": "^0.8.2"
  },
  "dependencies": {}
}

I've seen questions with this problem where it was identified that there are conflicts between modules but their solutions aren't working.

That's because I am trying to delete my node_modules folder and reinstalling with yarn install from the package.json from the github but still no dice. Any suggestions?

Best Answer

I recognize that deploy script anywhere. It's what i learned from Patrick and just like you i got this problem too. Its a hardhat-ethers problem and you will need to install hardhat-deploy-ethers as an alias and force download hardhat-waffle.

Do this: first install hardhat-deploy-ethers by adding this line

 "@nomiclabs/hardhat-ethers": "npm:hardhat-deploy-ethers@^0.3.0-beta.10"

to your package.json, then

npm i

to install. Next force hardhat waffle using

npm install -D @nomiclabs/hardhat-waffle --force

you're done and getContract becomes a function. Using the workaround helps, but Patrick uses that deploy script a lot and you'd want to use it too. The work around "getContractAt" is great but i mostly use it when i'm just running scripts not deploying. You'll realize this too following Patrick, so you might as well get the err solved and be done with it. Ps save this page, you'll most likely run into the error again.

For more clarity see Wighawags'explanation here

Related Topic