smartcontractkit / full-blockchain-solidity-course-js

Learn Blockchain, Solidity, and Full Stack Web3 Development with Javascript
12.56k stars 3k forks source link

ethers getContract function not working (reading 'getContract') #820

Closed shivam017arora closed 2 years ago

shivam017arora commented 2 years ago

Lesson

Lesson 7

Could you please leave a link to the timestamp in the video where this error occurs? (You can right click a video and "copy video URL at current time")

https://youtu.be/gyMwXuJrbJQ?t=40454

Operating System

macOS (Intel)

Describe the bug

Hi!

I am running into an issue when I run the command yarn hardhat test on this line in my test file:
await ethers.getContract("MockV3Aggregator", deployer);

Error Traceback:

FundMe
     "before each" hook for "should deploy a contract":
   TypeError: Cannot read properties of undefined (reading 'getContract')
    at Context.<anonymous> (test/unit/fundme.test.js:12:25)
    at processTicksAndRejections (node:internal/process/task_queues:96:5)
    at runNextTicks (node:internal/process/task_queues:65:3)
    at listOnTimeout (node:internal/timers:528:9)
    at processTimers (node:internal/timers:502:7)

test file

My test folder has only one file which looks like this:

const { deployments, ethers, getNamedAccounts } = require("hardhat");
const { assert } = require("chai");

describe("FundMe", async () => {
  let fundMe;
  let mock;
  beforeEach(async () => {
    //deploy fundMe using hardhat-deploy
    await deployments.fixture("all");
    //get the contract instance
    const { deployer } = await getNamedAccounts();
    mock = await ethers.getContract("MockV3Aggregator", deployer);
    fundMe = await ethers.getContract("FundMe", deployer);
  });
  describe("Constructor", async () => {
    it("should deploy a contract", async () => {
      const address = await fundMe.getPriceFeedAddress();
      assert.equal(address, mock.address);
    }).timeout(5000);
  });
});

After some debugging I found that one more person ran into the same problem and he ran this command to solve it:

npm install --save-dev @nomiclabs/hardhat-ethers@npm:hardhat-deploy-ethers ethers

when I try running this I get:

hardhat-fund-me % npm install --save-dev @nomiclabs/hardhat-ethers@npm:hardhat-deploy-ethers ethers
npm WARN ERESOLVE overriding peer dependency
npm WARN While resolving: FundMe@1.0.0
npm WARN Found: @nomiclabs/hardhat-ethers@0.3.0-beta.13
npm WARN node_modules/@nomiclabs/hardhat-ethers
npm WARN   dev @nomiclabs/hardhat-ethers@"npm:hardhat-deploy-ethers" from the root project
npm WARN 
npm WARN Could not resolve dependency:
npm WARN peer @nomiclabs/hardhat-ethers@"^2.0.0" from @nomicfoundation/hardhat-chai-matchers@1.0.0
npm WARN node_modules/@nomicfoundation/hardhat-chai-matchers
npm WARN   peer @nomicfoundation/hardhat-chai-matchers@"^1.0.0" from @nomicfoundation/hardhat-toolbox@1.0.1
npm WARN   node_modules/@nomicfoundation/hardhat-toolbox
npm WARN   1 more (the root project)
npm WARN ERESOLVE overriding peer dependency
npm WARN While resolving: FundMe@1.0.0
npm WARN Found: @nomiclabs/hardhat-ethers@0.3.0-beta.13
npm WARN node_modules/@nomiclabs/hardhat-ethers
npm WARN   dev @nomiclabs/hardhat-ethers@"npm:hardhat-deploy-ethers" from the root project
npm WARN 
npm WARN Could not resolve dependency:
npm WARN peer @nomiclabs/hardhat-ethers@"^2.0.0" from @nomicfoundation/hardhat-chai-matchers@1.0.0
npm WARN node_modules/@nomicfoundation/hardhat-chai-matchers
npm WARN   peer @nomicfoundation/hardhat-chai-matchers@"^1.0.0" from @nomicfoundation/hardhat-toolbox@1.0.1
npm WARN   node_modules/@nomicfoundation/hardhat-toolbox
npm WARN   1 more (the root project)
npm WARN ERESOLVE overriding peer dependency
npm WARN While resolving: FundMe@1.0.0
npm WARN Found: @nomiclabs/hardhat-ethers@0.3.0-beta.13
npm WARN node_modules/@nomiclabs/hardhat-ethers
npm WARN   dev @nomiclabs/hardhat-ethers@"npm:hardhat-deploy-ethers" from the root project
npm WARN 
npm WARN Could not resolve dependency:
npm WARN peer @nomiclabs/hardhat-ethers@"^2.0.0" from @nomicfoundation/hardhat-toolbox@1.0.1
npm WARN node_modules/@nomicfoundation/hardhat-toolbox
npm WARN   dev @nomicfoundation/hardhat-toolbox@"^1.0.1" from the root project
npm WARN ERESOLVE overriding peer dependency
npm WARN While resolving: FundMe@1.0.0
npm WARN Found: @nomiclabs/hardhat-ethers@0.3.0-beta.13
npm WARN node_modules/@nomiclabs/hardhat-ethers
npm WARN   dev @nomiclabs/hardhat-ethers@"npm:hardhat-deploy-ethers" from the root project
npm WARN 
npm WARN Could not resolve dependency:
npm WARN peer @nomiclabs/hardhat-ethers@"^2.0.0" from @nomiclabs/hardhat-waffle@2.0.3
npm WARN node_modules/@nomiclabs/hardhat-waffle
npm WARN   dev @nomiclabs/hardhat-waffle@"^2.0.3" from the root project

added 19 packages, changed 10 packages, and audited 1725 packages in 20s

162 packages are looking for funding
  run `npm fund` for details

44 vulnerabilities (11 moderate, 33 high)

To address issues that do not require attention, run:
  npm audit fix

Some issues need review, and may require choosing
a different dependency.

Run `npm audit` for details.
(base) hardhat-fund-me % 

My contract FundMe.sol

// SPDX-License-Identifier: MIT
pragma solidity ^0.8.8;

import "@chainlink/contracts/src/v0.8/interfaces/AggregatorV3Interface.sol";
import "./PriceConverter.sol";

error NotOwner();

/**
 * @title FundMe
 * @author Shivam Arora
 * @dev FundMe is a contract that allows users to donate to a project.
 */
contract FundMe {
    using PriceConverter for uint256;
    address priceFeedAddress;

    mapping(address => uint256) public addressToAmountFunded;
    address[] public funders;

    address public immutable i_owner;
    uint256 public constant MINIMUM_USD = 50 * 10**18;

    constructor(address _priceFeedAddress) {
        priceFeedAddress = _priceFeedAddress;
        i_owner = msg.sender;
    }

    function fund() public payable {
        require(
            msg.value.getConversionRate(priceFeedAddress) >= MINIMUM_USD,
            "You need to spend more ETH!"
        );
        // require(PriceConverter.getConversionRate(msg.value) >= MINIMUM_USD, "You need to spend more ETH!");
        addressToAmountFunded[msg.sender] += msg.value;
        funders.push(msg.sender);
    }

    modifier onlyOwner() {
        // require(msg.sender == owner);
        if (msg.sender != i_owner) revert NotOwner();
        _;
    }

    function withdraw() public payable onlyOwner {
        for (
            uint256 funderIndex = 0;
            funderIndex < funders.length;
            funderIndex++
        ) {
            address funder = funders[funderIndex];
            addressToAmountFunded[funder] = 0;
        }
        funders = new address[](0);
        (bool callSuccess, ) = payable(msg.sender).call{
            value: address(this).balance
        }("");
        require(callSuccess, "Call failed");
    }

    fallback() external payable {
        fund();
    }

    receive() external payable {
        fund();
    }

    function getPriceFeedAddress() public view returns (address) {
        return priceFeedAddress;
    }
}

Please help! Thanks in advance!

PatrickAlphaC commented 2 years ago

Could you ask this in the discussions tab? Thank you!