4

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?

1
  • Can you try replacing .getContract() with .getContractFactory()? Commented Nov 15, 2022 at 7:41

3 Answers 3

4

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

1

If other solutions are not working at all, you can try implementing this workaround instead of using ethers.getContract(), and it should be the same result:

 await deployments.fixture(["MyContract"]);

 const myContract = await deployments.get("MyContract");

 const contract = await ethers.getContractAt(
      myContract.abi,
      myContract.address
    );
  });
2
  • I used the above in my random-ipfs-nft deploy script in conjunction with another deploy script to grab the mock contract where the second deploy script (exclusively for mocks) uses the deploy function, ` await deploy("VRFCoordinatorV2Mock", { from: deployer, log: true, args: args, });` So while it doesn't solve the issue of why the function ethers.getContract doesn't work, it's a solid workaround. Thank you.
    – Domini
    Commented Nov 15, 2022 at 15:09
  • You're welcome! I am not sure why but let me investigate it, I will add another comment if I find the real reason about that, maybe other people can help. @Domini Commented Nov 15, 2022 at 17:32
0

This is the way I could fix this issue using names instead of binary and ABI. First to use getContractAt() and second to use connect() to be able to benefit from TypeChain.

deployer = (await getNamedAccounts()).deployer
signer = await ethers.getSigner(deployer)
await deployments.fixture(["all"])
const deployment = await deployments.get("MyContract")
let contract = await ethers.getContractAt(
     "MyContract",
     deployment.address,
     signer
)
myContract = MyContract__factory.connect(
     await contract.getAddress(),
     signer //also works with contract.runner for read-only properties
)

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.