I have a public contract member defined like this, which is later assigned a value in the constructor
AggregatorV3Interface public priceFeed;
I want to access this variable's value and I am trying like this
fundMe = await ethers.getContractAt("FundMe", deployer);
const response = await fundMe.priceFeed();
But I am getting error:
Error: could not decode result data (value="0x", info={ "method": "priceFeed", "signature": "priceFeed()" }, code=BAD_DATA, version=6.7.1)
I am getting this error when I am running the test which is defined below
const { assert } = require("chai");
const { deployments, ethers, getNamedAccounts } = require("hardhat");
describe("FundMe", async () => {
let fundMe;
let deployer;
let MockV3Aggregator;
beforeEach(async function () {
deployer = (await getNamedAccounts()).deployer;
await deployments.fixture("all");
fundMe = await ethers.getContractAt("FundMe", deployer); // <-- change to getContract
MockV3Aggregator = await ethers.getContractAt("MockV3Aggregator", deployer); // <-- same change here
});
describe("constructor", async function () {
it("Sets the aggregator address correctly", async function () {
const response = await fundMe.priceFeed(); //<-- error here
console.log(response);
assert.equal(response, MockV3Aggregator.target);
});
});
});
The fundMe contract is
contract FundMe {
AggregatorV3Interface public priceFeed;
constructor(address customPriceFeed) {
i_owner = msg.sender;
priceFeed = AggregatorV3Interface(customPriceFeed);
}
hardhat.config.js file :
require("@nomicfoundation/hardhat-toolbox");
require("dotenv").config();
require("./tasks/block-number");
require("hardhat-deploy");
require("@nomiclabs/hardhat-ethers"); // <-- error solved by adding this
const SEPOLIA_URL = process.env.SEPOLIA_RPC_URL || "https://sepolia.com";
const PRIVATE_KEY = process.env.PRIVATE_KEY || "key";
const COINMARKETCAP_API_KEY = process.env.COINMARKETCAP_API_KEY || "key";
/** @type import('hardhat/config').HardhatUserConfig */
module.exports = {
defaultNetwork: "hardhat",
networks: {
sepolia: {
url: SEPOLIA_URL,
accounts: [PRIVATE_KEY],
chainId: 11155111,
},
},
solidity: {
compilers: [
{
version: "0.8.19",
},
{
version: "0.6.6",
},
],
},
gasReporter: {
enabled: true,
outputFile: "gas-reporter.txt",
noColors: true,
currency: "INR",
coinmarketcap: COINMARKETCAP_API_KEY,
},
namedAccounts: {
deployer: {
default: 0,
},
},
};
AggregatorV3Interface
is for chainlink price feeds. The deployment process should differ when deploying on the dev chain (hardhat and local host) from deploying to testnets like Sepolia. Could you provide the deploying js script?AggregatorV3Interface
. you could try changing that to return an address instead, thengetContractAt("AggregatorV3Interface", fundMe.callStatic.priceFeed())
getContractAt
intogetContract
, and changeMockV3Aggregator.target
intoMockV3Aggregator.address
in the test script, and see if the error disappeared or not.getContract()
by importingrequire("@nomiclabs/hardhat-ethers");
in hardhat.config.js file. Although I couldn't understand how it worked out?! Because when I imported ethers package in my test.js fileconst { ethers } = require("hardhat");
then it was showing errorethers.getContract
function is not a function, but after requiring the package @nomiclabs/hardhat-ethers in my hardhat.config.js file, I was able to call the function. Can anybody tell how they are related. It would be really helpful.