解决Mumbai测试网调用智能合约时的'missing revert data'错误
问题:Next.js Web3 DApp调用Mumbai测试网合约触发CALL_EXCEPTION错误
我开发的基于Next.js的Web3 DApp,在与部署于Mumbai测试网的Solidity智能合约交互时,持续出现以下错误:
Error retrieving election details: Error: missing revert data in call exception; Transaction reverted without a reason string [ See: https://links.ethers.org/v5-errors-CALL_EXCEPTION ] (transaction={"to":"0xb2D4AFb50C98D582968cAcD24D6432Fdb0a45a82","data":"0x379b721d","accessList":null}, code=CALL_EXCEPTION, version=providers/5.7.2) at Logger.makeError (index.js?dd68:224:1) at Logger.throwError (index.js?dd68:233:1) at eval (fallback-provider.js?e405:559:1) at Array.forEach (<anonymous>) at FallbackProvider.eval (fallback-provider.js?e405:539:1) at Generator.next (<anonymous>) at fulfilled (fallback-provider.js?e405:5:43)
合约端调用函数
Solidity合约中被调用的allElections函数:
function allElections() public view returns(electionInfo []memory) { uint currentIndex = 0; electionInfo[] memory allCreatedElections = new electionInfo[] (electionCount); for(uint i = 0; i < electionCount; i++){ uint currentElectionId = i + 1; // reference current election details electionInfo storage currentElection = electionIdToDetails[currentElectionId]; // store current election in myElections array allCreatedElections[currentIndex] = currentElection; // increase index currentIndex += 1; } return allCreatedElections; }
前端调用代码
Next.js中调用合约的代码:
// import { useProvider } from "wagmi"; const wagmiProvider = useProvider(); const electionTest = async () => { const ethersVote = new ethers.Contract( ElectionContract, Electionabi.abi, wagmiProvider // provider ); try { const details = await ethersVote.allElections(); const elections = details.map(async (election) => ({ ElectionId: election.electionId.toNumber(), ElectionName: ethers.utils.parseBytes32String(election.electioName), Creator: election.electionCreator, Address: election.electionAddress, })); setElectionInfo(elections); } catch (error) { console.error("Error retrieving election details:", error); } };
MUI表格渲染代码
用于展示数据的MUI表格组件:
<TableContainer component={Paper}> <Table aria-label="All Elctions Table" stickyHeader> <TableHead> <TableRow> <TableCell align="right"> Election Id</TableCell> <TableCell align="right"> Election Name</TableCell> <TableCell align="right"> Election Address</TableCell> </TableRow> </TableHead> <TableBody> {electionInfo.map((election, index) => ( <TableRow key={index} hover onClick={() => { handleNext(election.Address); }} style={{ cursor: "pointer" }} > <TableCell align="right" role="checkbox"> {election.ElectionId} </TableCell> <TableCell align="right">{election.ElectionName}</TableCell> <TableCell align="right">{election.Address}</TableCell> </TableRow> ))} </TableBody> </Table> </TableContainer>
补充信息
- 该功能在本地Hardhat环境运行正常
- 怀疑问题出在Provider上,当前使用的是在
_app.js中配置了Alchemy凭证的rainbowkit/wagmi Provider,Provider版本为providers/5.7.2 - 尚未尝试的解决方案:更新Provider库
- 预期结果:获取平台上创建的选举数组
排查建议
- 校验合约地址与ABI:确认Mumbai测试网部署的合约地址
0xb2D4AFb50C98D582968cAcD24D6432Fdb0a45a82正确,前端使用的ABI与部署的合约完全一致,尤其是allElections函数的签名和返回结构体electionInfo的字段定义。 - 检查Provider网络配置:确认wagmi Provider已正确配置为Mumbai测试网(Chain ID:80001),Alchemy API密钥有效且未过期,网络连接无异常。
- 直接在链上测试合约:通过Polygonscan Mumbai区块浏览器,找到目标合约并调用
allElections函数,查看是否能正常返回数据。如果链上调用也失败,说明合约在测试网的状态存在问题(比如electionCount数值与实际存储的选举条目不匹配,导致循环中访问了不存在的electionIdToDetails索引)。 - 处理前端数据错误:注意到前端
details.map中使用了async,这会导致elections是Promise数组,直接赋值给electionInfo会让渲染组件无法读取有效数据。应修改为:
const elections = await Promise.all(details.map((election) => ({ ElectionId: election.electionId.toNumber(), ElectionName: ethers.utils.parseBytes32String(election.electionName), // 修正拼写错误:electioName → electionName Creator: election.electionCreator, Address: election.electionAddress, })));
同时注意代码中election.electioName存在拼写错误,应改为election.electionName,否则会导致解析选举名称失败。
5. 更新依赖库版本:尝试更新ethers.js到v5最新稳定版,或升级到v6版本,同时确保rainbowkit和wagmi的版本与ethers版本兼容。
内容的提问来源于stack exchange,提问作者Tame.nation
相关产品推荐
相关产品推荐

