0

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,
    },
  },
};
6
  • This contract interface 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?
    – Al-Qa'qa'
    Sep 27 at 15:05
  • probably your interface is not aware of type AggregatorV3Interface. you could try changing that to return an address instead, then getContractAt("AggregatorV3Interface", fundMe.callStatic.priceFeed())
    – high_byte
    Sep 27 at 19:40
  • @Al-Qa'qa' I am deploying the contracts on hardhat default network and I have two deploy scripts one for mocks and other for contract. I have edited and added the deploy scripts. Sep 30 at 14:23
  • Try to change getContractAt into getContract, and change MockV3Aggregator.target into MockV3Aggregator.address in the test script, and see if the error disappeared or not.
    – Al-Qa'qa'
    Oct 1 at 7:52
  • @Al-Qa'qa' Thanks! I was able to run the function getContract() by importing require("@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 file const { ethers } = require("hardhat"); then it was showing error ethers.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. Oct 1 at 12:14

1 Answer 1

0
  • Change getContractAt by getContract
  • in testing use MockV3Aggregator.address instead of MockV3Aggregator.target

Don't forget to import the hardhat ethers, and hardhat deploy modules in hardhat config file, this part was added in the question itself.

// hardhat.config.js
require("hardhat-deploy");
require("@nomiclabs/hardhat-ethers");

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.