0

I have this contract which is written in solidity and one of the functions (getMyActivity()) returns an array of structs and im trying to call it from my frontend which is written in ReactJS but i get this error:

MetaMask - RPC Error: execution reverted {code: -32000, message: 'execution reverted'} Error: cannot estimate gas; transaction may fail or may require manual gas limit [ See: https://links.ethers.org/v5-errors-UNPREDICTABLE_GAS_LIMIT ]

So i set the manually the gas price like this:

const options = { gasLimit: 1000000 };
const _activities = await contract.getMyActivity(options);

But when i try to execute the code the error doesn't go away.

Frontend code:

  const getActivities = async () => {
    try {
      const { ethereum } = window;
      if (ethereum) {
        const provider = new ethers.providers.Web3Provider(ethereum, "any");
        const signer = provider.getSigner();
        const contract = new ethers.Contract(contractAddress, abi, signer);
        const options = { gasLimit: 1000000 };
        const _activities = await contract.getMyActivity(options);
        setActivities(_activities);
        console.log(`Activities: ${_activities}`);
      }
    } catch (error) {
      console.log(error);
    }
  };

Contract Code:

function getMyActivity() public view returns (Activity[] memory) {
        Activity[] memory temporary = new Activity[](activities.length);
        uint counter = 0;
        for (uint i; i < activities.length; i++) {
            if (activityToOwner[i] == msg.sender) {
                temporary[counter] = activities[i];
                counter++;
            }
        }
        Activity[] memory result = new Activity[](counter);
        for (uint i; i < counter; i++) {
            result[i] = temporary[i];
        }
        return result;
    }

If you have any suggestions please let me know.

1 Answer 1

0

Error: cannot estimate gas; transaction may fail ... suggests that it is your solidity code that is reverting. Seeing the rest of the contract would help further debugging.

Otherwise, I suggest you first test this function thoroughly with a framework like foundry or hardhat. Make sure that getMyActivity() works before integrating with a frontend.

You can also try to debug your function by removing piece by piece. Try returning first the entire raw activities, or a slice of it. Then try doing the filtering steps o your function

Your Answer

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

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