Lesson 7: ethers.getContract is not a function #1503
-
I'm testing FundMe.test.js but its gives me error
here FundMe.test.js file: const { deployments, getNamedAccounts, ethers } = require("hardhat");
const { assert } = require("chai");
describe("FundMe", function () {
let fundMe;
let MockV3Aggregator;
let deployer;
beforeEach(async function () {
deployer = (await getNamedAccounts()).deployer;
await deployments.fixture(["all"]);
fundMe = await ethers.getContract("FundMe", deployer);
MockV3Aggregator = await ethers.getContract("MockV3Aggregator", deployer);
});
describe("constructor", function () {
it("sets the aggregator addresses correctly", async () => {
const response = await fundMe.getPriceFeed();
assert.equal(response, mockV3Aggregator.address);
});
});
});
|
Beta Was this translation helpful? Give feedback.
Replies: 10 comments 43 replies
-
Can you make a repo of your code and share the link here? |
Beta Was this translation helpful? Give feedback.
-
@Drakenn04 I think you are using a newer version of hardhat, try |
Beta Was this translation helpful? Give feedback.
-
Show hardhat config |
Beta Was this translation helpful? Give feedback.
-
@Drakenn04 Have you tried reinstalling ethers dependencies from hardhat & restart vscode? |
Beta Was this translation helpful? Give feedback.
-
@Drakenn04 Did you solve the error? I have the same problem like you! In both ways i mean "call revert exception" if i use getContractAt and if I use getContract "ethers.getContract is not a function" |
Beta Was this translation helpful? Give feedback.
-
Thank you a lot, i'm going to try today!
czw., 4 sie 2022 o 12:04 Drakenn04 ***@***.***> napisał(a):
… Okay 👍🏼
—
Reply to this email directly, view it on GitHub
<#1503 (reply in thread)>,
or unsubscribe
<https://github.com/notifications/unsubscribe-auth/AYN2SLUPRGR3JEEE4MTXDG3VXOIRTANCNFSM55O4EJ7Q>
.
You are receiving this because you commented.Message ID:
<smartcontractkit/full-blockchain-solidity-course-js/repo-discussions/1503/comments/3323951
@github.com>
|
Beta Was this translation helpful? Give feedback.
-
@Abeltemi I solved this issue , I posted how to solve this issue on Ethereum Stackoverflow, here https://ethereum.stackexchange.com/questions/146902/before-each-hook-for-sets-the-aggregator-addresses-correctly-typeerror-cann/146903#146903 |
Beta Was this translation helpful? Give feedback.
-
Ethers version: 6.8.0
This worked for me. I used signer and getContractAt. Still not happy with await ethers.getContractAt("FundMe", (await deployments.get("FundMe")).address, deployer);. Can we do this better? However, the test runs successfully. |
Beta Was this translation helpful? Give feedback.
-
I think I found the fix. (Ethers 6.4.0) Steps:
My {
"name": "hardhat-fund-me",
"version": "1.0.0",
"license": "MIT",
"devDependencies": {
"@chainlink/contracts": "^0.8.0",
"@nomicfoundation/hardhat-chai-matchers": "^2.0.0",
"@nomicfoundation/hardhat-ethers": "^3.0.0",
"@nomicfoundation/hardhat-network-helpers": "^1.0.0",
"@nomicfoundation/hardhat-toolbox": "^4.0.0",
"@nomicfoundation/hardhat-verify": "^2.0.0",
"@nomiclabs/hardhat-ethers": "npm:hardhat-deploy-ethers",
"@typechain/ethers-v6": "^0.5.0",
"@typechain/hardhat": "^9.0.0",
"@types/chai": "^4.3.11",
"@types/mocha": "^10.0.6",
"@types/node": "^20.10.7",
"chai": "^4.2.0",
"dotenv": "^16.3.1",
"ethers": "^6.4.0",
"hardhat": "^2.19.4",
"hardhat-deploy": "^0.11.45",
"hardhat-deploy-ethers": "^0.4.1",
"hardhat-gas-reporter": "^1.0.8",
"prettier": "^3.1.1",
"prettier-plugin-solidity": "^1.3.1",
"solhint": "^4.1.1",
"solidity-coverage": "^0.8.0",
"ts-node": "^10.9.2",
"typechain": "^8.3.0",
"typescript": "^5.3.3"
}
} My const { deployments, ethers, getNamedAccounts } = require("hardhat");
const { assert } = require("chai");
describe("FundMe", function () {
let fundMe;
let deployer;
let mockV3Aggregator;
beforeEach(async function () {
deployer = (await getNamedAccounts()).deployer;
await deployments.fixture(["all"]); // Deploy all contracts from "deploy" folder
fundMe = await ethers.getContract("FundMe", deployer);
});
describe("constructor", async function () {
it("Sets the aggregator address correctly", async function () {
const response = await fundMe.priceFeed();
// console.log(`response: ${response}`);
mockV3Aggregator = await ethers.getContract("MockV3Aggregator", deployer);
// console.log(`mockV3Aggregator: ${await mockV3Aggregator.getAddress()}`);
assert.equal(response, await mockV3Aggregator.getAddress());
});
});
}); |
Beta Was this translation helpful? Give feedback.
-
Here's my solution in import { network, deployments, ethers, getNamedAccounts } from "hardhat";
import { FundMe, MockV3Aggregator } from "../../typechain-types";
import { assert, expect } from "chai";
import { Signer } from "ethers";
describe("FundMe", async () => {
let fundMe: FundMe;
let mockV3Aggregator: MockV3Aggregator;
let deployer;
let deployerSigner: Signer;
beforeEach(async () => {
await deployments.fixture(["all"]);
deployer = (await getNamedAccounts()).deployer;
deployerSigner = await ethers.getSigner(deployer);
fundMe = await ethers.getContractAt(
"FundMe",
(await deployments.get("FundMe")).address,
deployerSigner,
);
mockV3Aggregator = await ethers.getContractAt(
"MockV3Aggregator",
(await deployments.get("MockV3Aggregator")).address,
deployerSigner,
);
});
describe("constructor", () => {
it("sets the aggregator addresses correctly", async () => {
const response = await fundMe.priceFeed();
const address = await mockV3Aggregator.getAddress();
assert.equal(response, address);
});
});
}); |
Beta Was this translation helpful? Give feedback.
@Drakenn04 I think you are using a newer version of hardhat, try
getContractAt
method.