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

本地部署正常,Mumbai测试网部署Compound-Hardhat项目失败求助

问题描述

本地Hardhat网络部署Compound项目完全正常,但部署到Mumbai测试网时失败,报错cannot estimate gas; transaction may fail or may require manual gas limit,底层错误为execution reverted。

本地部署成功日志

PS D:\Work\Git\CompoundHardhat> npx hardhat run --network hardhat ./scripts/sample.ts
No need to generate any newer typings.
Duplicate definition of ActionPaused (ActionPaused(string,bool), ActionPaused(address,string,bool))
Comptroller:  0x9fE46736679d2D9a65F0992F2272dE9f3c7fa6e0
SimplePriceOralce:  0xCf7Ed3AccA5a467e9e704C703E8D87F634fB0Fc9
cUNI:  0x9A676e781A523b5d0C0e43731313A708CB607508
cUSDC:  0x68B1D87F95878fE05B998F19b66F4baba5De1aed
>>>> Deploying Local Contract
>>>> is Comptroller ? true
Compound deployed to: 0xa85233C63b9Ee964Add6F2cffe00Fd84eb32338f
>>>> cToken Name : Compound Uniswap
>>>> cToken Name : Compound USD Coin

Mumbai测试网部署报错日志

PS D:\Work\Git\CompoundHardhat> npx hardhat run --network mumbai ./scripts/sample.ts
No need to generate any newer typings.
Duplicate definition of ActionPaused (ActionPaused(string,bool), ActionPaused(address,string,bool))
Error: cannot estimate gas; transaction may fail or may require manual gas limit (error={"name":"ProviderError","code":-32000,"_isProviderError":true}, method="estimateGas", transaction={"from":"0x2b6e4066f435C758708C406f8E9c18E26DF649D4","to":"0x22DD6CA9F95F02Ab3bb7a138c6f44bf9Fb30B709","data":"0xa76b3fda0000000000000000000000002266f1536e49dfdb0a22ccbe5601b778d2efd7ef","accessList":null}, code=UNPREDICTABLE_GAS_LIMIT, version=providers/5.4.5)
    at Logger.makeError (D:\Work\Git\CompoundHardhat\node_modules\@ethersproject\logger\src.ts\index.ts:225:28)
    at Logger.throwError (D:\Work\Git\CompoundHardhat\node_modules\@ethersproject\logger\src.ts\index.ts:237:20)
    at checkError (D:\Work\Git\CompoundHardhat\node_modules\@ethersproject\providers\src.ts\json-rpc-provider.ts:80:16)
    at EthersProviderWrapper.<anonymous> (D:\Work\Git\CompoundHardhat\node_modules\@ethersproject\providers\src.ts\json-rpc-provider.ts:555:20)
    at step (D:\Work\Git\CompoundHardhat\node_modules\@ethersproject\providers\lib\json-rpc-provider.js:48:23)
    at Object.throw (D:\Work\Git\CompoundHardhat\node_modules\@ethersproject\providers\lib\json-rpc-provider.js:29:53)
    at rejected (D:\Work\Git\CompoundHardhat\node_modules\@ethersproject\providers\lib\json-rpc-provider.js:21:65)
    at processTicksAndRejections (node:internal/process/task_queues:95:5) {
  reason: 'cannot estimate gas; transaction may fail or may require manual gas limit',
  error: ProviderError: execution reverted
      at HttpProvider.request (D:\Work\Git\CompoundHardhat\node_modules\hardhat\src\internal\core\providers\http.ts:49:19)
      at LocalAccountsProvider.request (D:\Work\Git\CompoundHardhat\node_modules\hardhat\src\internal\core\providers\accounts.ts:168:34)
      at processTicksAndRejections (node:internal/process/task_queues:95:5)
      at async EthersProviderWrapper.send (D:\Work\Git\CompoundHardhat\node_modules\@nomiclabs\hardhat-ethers\src\internal\ethers-provider-wrapper.ts:13:20),
  method: 'estimateGas',
  transaction: {
    from: '0x2b6e4066f435C758708C406f8E9c18E26DF649D4',
    to: '0x22DD6CA9F95F02Ab3bb7a138c6f44bf9Fb30B709',
    data: '0xa76b3fda0000000000000000000000002266f1536e49dfdb0a22ccbe5601b778d2efd7ef',
    accessList: null
  }
}

配置文件 hardhat.config.ts

import * as dotenv from "dotenv";

import { HardhatUserConfig, task } from "hardhat/config";
import "@nomiclabs/hardhat-etherscan";
import "@nomiclabs/hardhat-waffle";
import "@typechain/hardhat";
import "hardhat-gas-reporter";
import "solidity-coverage";

dotenv.config();

task("accounts", "Prints the list of accounts", async (taskArgs, hre) => {
  const accounts = await hre.ethers.getSigners();

  for (const account of accounts) {
    console.log(account.address);
  }
});

const config: HardhatUserConfig = {
  networks: {
    hardhat: {
      allowUnlimitedContractSize: true,
    },
    ropsten: {
      url: process.env.ROPSTEN_URL || "",
      accounts:
        process.env.PRIVATE_KEY !== undefined ? [process.env.PRIVATE_KEY] : [],
    },
    mumbai: {
      url: `https://rpc-mumbai.maticvigil.com`,
      accounts: 
      process.env.PRIVATE_KEY !== undefined ? [process.env.PRIVATE_KEY] : [],
            chainId: 80001,
    },
  },
  typechain: {
    outDir: "typechain",
    target: "ethers-v5",
  },
  gasReporter: {
    enabled: process.env.REPORT_GAS !== undefined,
    currency: "USD",
  },
  etherscan: {
    apiKey: process.env.ETHERSCAN_API_KEY ,
  },
  solidity: {
    compilers: [
      {
        version: "0.8.4",
        settings: {
          optimizer: {
            enabled: true,
            runs: 200,
          },
        },
      },
      {
        version: "0.5.16",
        settings: {
          optimizer: {
            enabled: true,
            runs: 200,
          },
        },
      },
      {
        version: "0.6.10",
        settings: {
          optimizer: {
            enabled: true,
            runs: 200,
          },
        },
      },
    ],
  },
};

export default config;
解决方案

1. 手动指定Gas参数

报错核心是无法自动估算gas导致交易回滚,可通过两种方式手动设置:

  • 在部署脚本中指定:部署合约时添加gasLimit参数,示例:
    const comptroller = await ethers.getContractFactory("Comptroller");
    const comptrollerInstance = await comptroller.deploy({ gasLimit: 30000000 });
    await comptrollerInstance.deployed();
    
  • 在Hardhat配置中全局设置:修改Mumbai网络配置,添加gas限制和价格:
    mumbai: {
      url: `https://rpc-mumbai.maticvigil.com`,
      accounts: process.env.PRIVATE_KEY !== undefined ? [process.env.PRIVATE_KEY] : [],
      chainId: 80001,
      gasLimit: 30000000,
      gasPrice: ethers.utils.parseUnits("30", "gwei") // Mumbai测试网gas价格通常在20-50gwei区间
    }
    

2. 校验外部依赖合约地址

报错中调用的0x22DD6CA9F95F02Ab3bb7a138c6f44bf9Fb30B709地址需确认:

  • 是否为Mumbai测试网的有效合约地址
  • 是否应该替换为该网络上的官方/已验证依赖合约(比如价格预言机、基础代币等)

3. 检查合约与测试网兼容性

  • 排查合约中是否有依赖链ID的逻辑,确保适配Mumbai的80001链ID
  • 确认Solidity编译器版本和优化器设置在Mumbai网络上兼容,避免本地特性依赖

4. 确认部署账户余额

确保部署账户持有足够的Mumbai测试网MATIC(可通过Polygon水龙头领取),余额不足会直接导致交易失败

5. 启用详细日志定位问题

部署时添加--verbose参数,获取更详细的执行日志,精准定位失败环节:

npx hardhat run --network mumbai --verbose ./scripts/sample.ts

内容的提问来源于stack exchange,提问作者Tomas Wolfgang

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.17 13:27:03