As I embark on developing my inaugural Solidity DApp using Next.js and Hardhat, I've encountered a perplexing error. After successfully deploying my contract on a local blockchain via npx hardhat node
, the issue arises when calling the getProposalCount()
function. Solving this problem has proven to be quite challenging.
If anyone is interested in replicating the error, you can access my GitHub repository for this project here: https://github.com/blakelucey/voting-dapp-project
Here's the specific error message:
Unhandled Runtime Error
Error: call revert exception [ See: https://links.ethers.org/v5-errors-CALL_EXCEPTION ] (method="getProposalCount()", data="0x", errorArgs=null, errorName=null, errorSignature=null, reason=null, code=CALL_EXCEPTION, version=abi/5.7.0)
During the deployment of VotingContract.sol, I could only use the input "1"
for proposalNames
. Despite several attempts with arrays of strings or BigNumbers, only "1"
worked as expected.
This is the Smart Contract I am working with:
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.18;
import "@openzeppelin/contracts/access/Ownable.sol";
contract VotingContract is Ownable {
struct Proposal {
string name;
uint256 voteCount;
}
Proposal[] public proposals;
uint256 public proposalCount;
constructor(string[] memory proposalNames) {
proposalCount = proposalNames.length;
for (uint256 i = 0; i < proposalNames.length; i++) {
proposals.push(Proposal({name: proposalNames[i], voteCount: 0}));
}
}
function getProposalCount() public view returns (uint256) {
return proposalCount;
}
function getProposal(
uint256 proposalId
) public view returns (string memory, uint256) {
require(proposalId < proposals.length, "Invalid proposal ID");
Proposal storage proposal = proposals[proposalId];
return (proposal.name, proposal.voteCount);
}
function vote(uint256 proposalId) public {
require(proposalId < proposals.length, "Invalid proposal ID");
Proposal storage proposal = proposals[proposalId];
proposal.voteCount++;
}
}
I have not been able to log anything within my useEffect
in index.tsx, so do keep that in mind.
This is my current index.tsx implementation:
import React, { useState, useEffect } from "react";
import { ethers } from "ethers";
import {
Typography,
Button,
Container,
ThemeProvider,
createTheme,
Stack,
} from "@mui/material";
import VotingContract from "../../bin/src/contracts/VotingContract.json";
const contractAddress = "0xDc64a140Aa3E981100a9becA4E685f962f0cF6C9"; // Replace with your contract address
const contractAbi = VotingContract.abi;
interface Proposal {
id: number;
title: string;
description: string;
}
const theme = createTheme({
palette: {
mode: "dark",
primary: {
main: "#0f0",
},
background: {
default: "#111111",
paper: "#212121",
},
},
typography: {
fontFamily: "Open Sans",
h1: {
fontFamily: "Ubuntu Mono",
},
...
});
function VotingApp() {
...
And below is the output in my terminal post-contract deployment:
scripts % npx hardhat run deploy.js --network localhost
VotingContract deployed to: 0xDc64a140Aa3E981100a9becA4E685f962f0cF6C9
Contract deployment: Lock
Contract address: 0xdc64a140aa3e981100a9beca4e685f962f0cf6c9
Transaction: 0x30ea6a068de0112a4d504fcc9dc4b918b437a0c89251ab070971f9b4e502768a
From: 0xf39fd6e51aad88f6f4ce6ab8827279cfffb92266
Value: 0 ETH
Gas used: 426897 of 2000000
Block #5: 0x58979de60048e74231335845ec625bd0d9925c5b67dd72bdcfd36166e4a4eb55
Your assistance in resolving this matter would be highly appreciated. Thank you!