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.