L7: Hardhat-Fund-me: No contract deployed with name FundMe error #5000
Answered
by
jatin-choubey
Mayank-Pandey1
asked this question in
Q&A
-
Here is the error:
deploy-fund-me.js code: const { network } = require("hardhat");
const {
networkConfig,
developmentChains,
} = require("../helper-hardhat-config");
const { verify } = require("../utils/verify");
require("dotenv").config();
module.exports = async ({ getNamedAccounts, deployments }) => {
const { deploy, log } = deployments; //getting variables from deployments object
const { deployer } = await getNamedAccounts();
const chainId = network.config.chainId;
let ethUsdPriceFeedAddress;
//console.log(chainId);
if (developmentChains.includes(network.name)) {
const ethUsdAggregator = await deployments.get("MockV3Aggregator");
ethUsdPriceFeedAddress = ethUsdAggregator.address;
} else {
ethUsdPriceFeedAddress = networkConfig[chainId]["ethUsdPriceFeed"];
}
//when working with localhost or hardhat we will create a mock.
//Mocking is primarily used in unit testing. An object under test may have dependencies on other (complex) objects.
//To isolate the behaviour of the object you want to test you replace the other objects by mocks that simulate
//the behaviour of the real objects. This is useful if the real objects are impractical to incorporate into the
//unit test.
//In short, mocking is creating objects that simulate the behaviour of real objects.
const args = [ethUsdPriceFeedAddress];
const fundMe = await deploy("FundMe", {
from: deployer,
args: args, //put priceFeed Address here
log: true,
waitConfirmations: network.config.blockConfirmations || 1,
});
if (
!developmentChains.includes(network.name) &&
process.env.ETHERSCAN_API_KEY
) {
await verify(fundMe.address, args);
}
};
module.exports.tags = ["all", "fundme"]; deploy-mocks.js code: const { network } = require("hardhat");
const {
developmentChains,
DECIMALS,
INITIAL_ANSWER,
} = require("../helper-hardhat-config");
module.exports = async ({ getNamedAccounts, deployments }) => {
const { deploy, log } = deployments; //geting variables from deployments object
const { deployer } = await getNamedAccounts();
const chainId = network.config.chainId;
if (developmentChains.includes(network.name)) {
log("Local network detected... Deploying mocks!");
await deploy("MockV3Aggregator", {
from: deployer,
args: [DECIMALS, INITIAL_ANSWER],
log: true,
});
log("Mocks Deployed!");
log("---------------------------------");
}
};
module.exports.tags = ["all", "mocks"]; FundMe.test.js code: const { deployments, ethers, getNamedAccounts } = require("hardhat");
const { assert } = require("chai");
describe("FundMe", async function () {
let fundMe;
let deployer;
let mockV3Aggregator;
beforeEach(async function () {
deployer = (await getNamedAccounts()).deployer;
//specifying which account we want connected to our deployed fundMe contract since we will be making transactions
//while testing
deployments.fixture(["all"]); //using fixture we can deploy our contracts with as many tags as we want
//running all the deploy scripts using this line
fundMe = await ethers.getContract("FundMe", deployer);
});
describe("constructor", async function () {
it("sets the priceFeed addresses correctly", async function () {
const response = await fundMe.priceFeed();
mockV3Aggregator = ethers.getContract("MockV3Aggregator", deployer);
assert.equal(mockV3Aggregator.address, response);
});
});
}); FundMe.sol contract: // SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;
import "@chainlink/contracts/src/v0.8/interfaces/AggregatorV3Interface.sol";
import "./PriceConvertor.sol"; //PriceConvertor library
error FundMe__NotOwner();
//NatSpec: Ethereum Natural Language Specification Format
/** @author Mayank Pandey
* @title A contract for crowdfunding
* @notice The contract demonstrates how people can fund to this contract and only the owner can withdraw the donated funds
*/
contract FundMe {
address public immutable i_owner;
AggregatorV3Interface public priceFeed;
constructor(address priceFeedAddress) {
i_owner = msg.sender;
priceFeed = AggregatorV3Interface(priceFeedAddress);
}
using PriceConvertor for uint256;
address[] public funders;
mapping(address => uint256) addressToAmountFunded;
uint256 public constant MINIMUM_USD = 50 * 1e18;
//view functions: cost only applies when called by a contract
//23,471 * 20000000000 = $0.75 without constant
//21,371 * 20000000000 = $0.69 with constant
modifier onlyOwner() {
// require(msg.sender == i_owner, "You are not the owner");
if (msg.sender != i_owner) revert FundMe__NotOwner();
_;
}
function fund() public payable {
//we can send tokens to this function or to the contract
//using this function
require(
msg.value.getConversionRate(priceFeed) >= MINIMUM_USD,
"Didn't send enough"
); //1e18 is in wei 1*10^18 wei = 1 ether
//18 decimals
funders.push(msg.sender);
addressToAmountFunded[msg.sender] = msg.value;
}
function withdraw() public onlyOwner {
for (uint256 i = 0; i < funders.length; i++) {
addressToAmountFunded[funders[i]] = 0;
}
//reset the array
funders = new address[](0);
(bool callSuccess /*bytes memory functionReturnedData*/, ) = payable(
msg.sender
).call{value: address(this).balance}("");
require(callSuccess, "Withdraw failed");
} |
Beta Was this translation helpful? Give feedback.
Answered by
jatin-choubey
Mar 3, 2023
Replies: 1 comment 1 reply
-
Hi @Mayank-Pandey1 describe("FundMe", function() {
let fundMe
let deployer
let mockV3Aggregator
beforeEach(async function() {
deployer = (await getNamedAccounts()).deployer
await deployments.fixture(["all"]) // Please dont forget the await here
fundMe = await ethers.getContract("FundMe", deployer)
mockV3Aggregator = await ethers.getContract( // you have placed this Line inside the constructor
"MockV3Aggregator",
deployer
)
})
describe("constructor", async function() {
it("sets the priceFeed addresses correctly", async function() {
const response = await fundMe.getPriceFeed()
assert.equal(response, mockV3Aggregator.address)
})
})
}) |
Beta Was this translation helpful? Give feedback.
1 reply
Answer selected by
Mayank-Pandey1
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Hi @Mayank-Pandey1
You can consider doing some changes in your FundMe.test.js file that I have mentioned below in the comments.