0

I have a working contract deployed on rinkeby, I'm trying to add there a function to get the current ETH/USD price using ChainLink's AggregatorV3Interface .

When testing the needed function in Remix, (environment=Injected Provider - Metamask(Rinkeby)) everything works fine:

pragma solidity 0.8.7;
import "@chainlink/contracts/src/v0.8/interfaces/AggregatorV3Interface.sol";

contract Testing {
    function getPrice() public view returns (uint256) {
        AggregatorV3Interface priceFeed = AggregatorV3Interface(0x8A753747A1Fa494EC906cE90E9f37563A8AF630e);
        (, int256 answer, , , ) = priceFeed.latestRoundData();
        // ETH/USD rate in 18 digit
        return uint256(answer * 10000000000);}}

However when I take the same function and put in my contract, deploy the whole thing on Rinkeby, then on my front-end i call it and get this error:

Error: cannot estimate gas; transaction may fail or may require manual gas limit [ See: https://links.ethers.org/v5-errors-UNPREDICTABLE_GAS_LIMIT ] (error={"code":-32000,"message":"execution reverted"}, method="call", transaction={"from":"0x34aaAa5Ccfa7A0899C1FeC04C43BF0D87Ea0F3fB","to":"0xee42aa31f9E9f0AA063DE5a6A773c6e345bef783","data":"0x98d5fdca","accessList":null}, code=UNPREDICTABLE_GAS_LIMIT, version=providers/5.6.0)

  1. Why would a public view function need any gas to execute itself?
  2. What do I change in the function to make it work inside of my contract?
2
  • Have you deployed the full contract via remix to ensure it works when calling there?
    – Richard G
    Jul 21 at 15:54
  • @RichardG yes, just tried it, all working in Remix. Seems like I've got a problem with how i'm actually calling the function. At least I know the direction of digging now, thanks! Jul 21 at 17:47

2 Answers 2

1

For the next guy who will search for this: if you change your contract and redeploy it NOT on the local network, contract address is going to change. Do not forget to update contract address and the ABI's when calling an updated contract's function.

0

You can add the manual gas like this: gas: 6000000,

Add this inside network object like this example

rinkeby: {
  url: process.env.RPC_URL,
  accounts: [process.env.PRIVATE_KEY],
  chainId: 4,
  blockConfirmations: 6,
  gas: 6000000,
},

Why would a public view function need any gas to execute itself?

View functions are read-only functions and do not change the state of the blockchain that's why they do not cost gas.

What do I change in the function to make it work inside of my contract?

You can pass the manual gas, but for gas error, there could be many reasons, maybe an issue in code or like that.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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