通过Ganache GUI部署智能合约失败(invalid opcode错误)
Ganache本地部署智能合约触发RuntimeError(invalid opcode)
在Ganache GUI本地环境部署智能合约,使用ethers.js编写部署脚本,执行node deploy.js时触发RuntimeError,报错信息显示“VM Exception while processing transaction: invalid opcode”,已尝试安装相关依赖但问题未解决。
部署代码
const ethers = require("ethers"); const fs = require("fs-extra"); async function main() { try { const provider = new ethers.JsonRpcProvider("http://127.0.0.1:7545"); const wallet = new ethers.Wallet( "0x3fff57a423422c9af7026a90b58be81e66bd80e7587edd7ec61060f6daaed287", provider ); const abi = fs.readFileSync( "./SimpleStorage_sol_SimpleStorage.abi", "utf8" ); const binary = fs.readFileSync( "./SimpleStorage_sol_SimpleStorage.bin", "utf8" ); const contractFactory = new ethers.ContractFactory(abi, binary, wallet); console.log("Deploying, please hang on..."); const contract = await contractFactory.deploy({ gasLimit: 6721975, gasPrice: 20000000000, }); console.log("Deployment Transaction:", contract.deployTransaction); console.log("Waiting for the contract to be mined..."); await contract.deployTransaction.wait(1); // Wait for 1 block confirmation console.log("Contract deployed successfully!"); // Call the `retrieve` function on the deployed contract console.log("Calling retrieve method..."); const favNumber = await contract.retrieve(); console.log("Favorite number:", favNumber.toString()); } catch (error) { console.error("Error during deployment or function call:", error); process.exit(1); } } main();
完整报错信息
Error during deployment or function call: Error: could not coalesce error (error={ "code": -32000, "data": { "hash": "0x71df1abf7ca0c92341bcbe285a19c83102635b35d9964bb5a1ba5da1b3456ddb", "message": "invalid opcode", "programCounter": 23, "reason": null, "result": "0x71df1abf7ca0c92341bcbe285a19c83102635b35d9964bb5a1ba5da1b3456ddb" }, "message": "VM Exception while processing transaction: invalid opcode", "name": "RuntimeError", "stack": "RuntimeError: VM Exception while processing transaction: invalid opcode at EIP1559FeeMarketTransaction.fillFromResult (/Applications/Ganache.app/Contents/Resources/static/node/node_modules/ganache/dist/node/1.js:2:12745) at Miner.<anonymous> (/Applications/Ganache.app/Contents/Resources/static/node/node_modules/ganache/dist/node/1.js:2:36703) code=UNKNOWN_ERROR, version=6.13.4) at makeError (/Users/wasimchoudhary/ethersjs blockchain/ether-ssstorage/node_modules/ethers/lib.commonjs/utils/errors.js:129:21) at JsonRpcProvider.getRpcError (/Users/wasimchoudhary/ethersjs blockchain/ether-ssstorage/node_modules/ethers/lib.commonjs/providers/provider-jsonrpc.js:730:41) at /Users/wasimchoudhary/ethersjs blockchain/ether-ssstorage/node_modules/ethers/lib.commonjs/providers/provider-jsonrpc.js:302:45) at process.processTicksAndRejections (node:internal/process/task_queues:95:5) { code: 'UNKNOWN_ERROR', error: { message: 'VM Exception while processing transaction: invalid opcode', stack: 'RuntimeError: VM Exception while processing transaction: invalid opcode\n' + ' at EIP1559FeeMarketTransaction.fillFromResult (/Applications/Ganache.app/Contents/Resources/static/node/node_modules/ganache/dist/node/1.js:2:12745)\n' + ' at Miner.<anonymous> (/Applications/Ganache.app/Contents/Resources/static/node/node_modules/ganache/dist/node/1.js:2:36703)\n' + ' at async Miner.<anonymous> (/Applications/Ganache.app/Contents/Resources/static/node/node_modules/ganache/dist/node/1.js:2:35116)\n' + ' at async Miner.mine (/Applications/Ganache.app/Contents/Resources/static/node/node_modules/ganache/dist/node/1.js:2:39680)\n' + ' at async Blockchain.mine (/Applications/Ganache.app/Contents/Resources/static/node/node_modules/ganache/dist/node/1.js:2:60063)\n' + ' at async Promise.all (index 0)\n' + ' at async TransactionPool.emit (/Applications/Ganache.app/Contents/Resources/static/node/node_modules/ganache/node_modules/emittery/index.js:303:3)', code: -32000, name: 'RuntimeError', data: { hash: '0x71df1abf7ca0c92341bcbe285a19c83102635b35d9964bb5a1ba5da1b3456ddb', programCounter: 23, result: '0x71df1abf7ca0c92341bcbe285a19c83102635b35d9964bb5a1ba5da1b3456ddb', reason: null, message: 'invalid opcode' } }, payload: { method: 'eth_sendRawTransaction', params: [ id: 9, jsonrpc: '2.0' }, shortMessage: 'could not coalesce error'
解决方法
- 检查合约编译文件:确认
.bin和.abi文件是通过Solidity编译器正确生成的,没有损坏或内容缺失。可以用命令重新编译:solcjs --bin --abi SimpleStorage.sol -o . - 核对Ganache配置:确保Ganache的RPC端口为7545,与脚本中的地址一致;同时确认使用的私钥对应的账户在Ganache中存在且有足够ETH余额。
- 移除手动gas参数:脚本中手动指定了
gasLimit和gasPrice,可能与Ganache的EIP-1559机制冲突,改为让ethers自动估算:const contract = await contractFactory.deploy(); - 检查Solidity版本兼容性:如果合约使用了高版本Solidity语法,确认Ganache的EVM版本支持该特性。建议将合约的Solidity版本指定为0.8.x稳定版后重新编译。
- 重置Ganache工作区:若Ganache链数据存在异常,新建或重置工作区,重新生成账户后再次尝试部署。
内容的提问来源于stack exchange,提问作者666ryuga
相关产品推荐
相关产品推荐

