You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Hardhat本地部署智能合约后无法交互,已获取合约地址

Hardhat本地部署合约后无法交互的问题

问题描述

在Hardhat本地环境成功部署智能合约并获取地址,但调用合约函数时触发revert异常,无法正常交互。

相关代码与报错

  • deploy.js代码:
const { ethers } = require("hardhat");

async function main() {
  const Voting = await ethers.getContractFactory("Voting");
  const voting = await Voting.deploy();
  await voting.deployed();
  console.log("Contract deployed to:", voting.address);
}

main()
  .then(() => process.exit(0))
  .catch((error) => {
    console.error(error);
    process.exit(1);
  });
  • 报错信息:
Error: call revert exception [ See: https://links.ethers.org/v5-errors-CALL_EXCEPTION ]
(reason: null, methodName: 'VotesOfBJP', errorArgs: null, errorName: null, errorSignature: null, reasonCode: null, version: abi/5.7.0)

智能合约代码

pragma solidity >= 0.6.0;

//Here the contract will start
contract Voting{

//Struct function for the voter
struct Voter{
    bool isVote; //checker variable to determine whether the voter has voted before
    uint id; //ID of the voter        
    string towhom; //to whom the voter has voted
}

//Declaring and Initialising the variables for votes
uint bjp = 0;
uint congress = 0;
uint aap = 0;
uint nota = 0;

//Initializing the time variable to the time of deployment
uint time_inital = block.timestamp;
uint time_final;
//Setting total count of the variable to be zero
uint counter = 0;

//Declaring the static array to initialize the number of the vote
Voter[20] voters;

//Initializing the ids of the voter via construction during the deployment
constructor() {
    for (uint i = 0;i < 20;i++){
        voters[i].id = i+1000;
        voters[i].isVote = false;
        voters[i].towhom = "";
    }
    time_inital = block.timestamp;
}

//Modifiers

//Modifier used for comparing the current time and the
//time of the declaration of the result
modifier Time(){
    require(block.timestamp >= time_inital + 30 seconds);
    _;
}

//Modifier used for checking whether the voter is
//giving his vote again
modifier IsVote(uint i) {
    require(voters[i].isVote == false);
    _;
}

//Functions

//Function to increase the vote count of BJP
function BJP(uint i) public IsVote(i){
    bjp++; //bjp counter will increases by one
    counter++; //Total count will increase by one
    voters[i].isVote = true; //Made the voter ineligible to vote
    voters[i].towhom = "BJP"; //Keeps the record of the voter list to whom the voter has voted
}

function Congress(uint i) public IsVote(i){
    congress++;
    counter++; //Total count will increase by one
    voters[i].isVote = true; //Made the voter ineligible to vote
    voters[i].towhom = "Congress"; //Keeps the record of the voter list to whom the voter has voted       
}

function AAP(uint i) public IsVote(i){
    aap++;
    counter++; //Total count will increase by one
    voters[i].isVote = true; //Made the voter ineligible to vote
    voters[i].towhom = "AAP"; //Keeps the record of the voter list to whom the voter has voted    
}

function NOTA(uint i) public IsVote(i){
    nota++;
    counter++; //Total count will increase by one
    voters[i].isVote = true; //Made the voter ineligible to vote
    voters[i].towhom = "None of the above"; //Keeps the record of the voter list to whom the voter has voted      
}

//This function will return the votes of BJP
function VotesOfBJP() external view Time() returns(uint) {
    return bjp;
}

//This function will return the votes of Congress
function VotesOfCongress() external view Time() returns(uint) {
    return congress;
}

//This function will return the votes of AAP
function VotesOfAAP() external view Time() returns(uint) {
    return aap;
}

//This function will return the votes of None of the above
function VotesOfNOTA() external view Time() returns(uint) {
    return nota;
}

//This function finally declares the result of the all the candidates
//registered to be voted and the total voting of the elections
function Declaration() external view Time() returns(uint){
    return counter;
}

问题解决思路

  1. 处理时间限制校验:
    所有查询类函数(如VotesOfBJP)带有Time修饰器,要求当前时间需晚于合约部署时间30秒。如果部署后立即调用,会触发require失败导致revert。

    • 解决方式:要么等待30秒后再调用;要么在本地测试时用Hardhat的时间操控接口快进时间:
      await ethers.provider.send("evm_increaseTime", [30]);
      await ethers.provider.send("evm_mine");
      
  2. 验证合约实例有效性:
    确保调用函数时使用的合约地址、ABI与最新部署的合约完全匹配。若重启过Hardhat本地节点,之前的合约地址会失效,需重新部署。

  3. 检查投票函数参数范围:
    调用BJP(uint i)等投票函数时,i必须在0-19之间(因为合约内voters是长度为20的数组),否则会触发数组越界导致revert。

  4. 查看详细报错原因:
    运行npx hardhat console或编写测试脚本时,添加错误捕获逻辑,或者在Hardhat配置中开启详细日志,定位具体的revert原因。


内容的提问来源于stack exchange,提问作者Sahitya aryan

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.17 18:57:56