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

使用Hardhat在Goerli网络部署合约返回0x0000地址的问题

合约跨部署问题:Goerli测试网返回空地址,本地节点正常

问题描述

在Solidity中通过Factory合约部署Test合约时,本地Hardhat节点能正常返回Test合约的有效地址,但在Goerli测试网调用后,getAddress()返回的是0x0000000000000000000000000000000000000000。

合约代码

Test合约

contract Test {
    function test() external pure returns(uint256) {
        return 10;
    }
}

Factory合约

contract Factory {
    address private addr;

    constructor() {}

    function DeployContract() public {
        addr = address(new Test());
    }

    function getAddress() external view returns (address) {
        return addr;
    }
}

部署脚本

const FactorySeed = await hre.ethers.getContractFactory("Factory");
const FactorySeedInstance = await FactorySeed.deploy();
await FactorySeedInstance.deployed();
        
console.log(`Deployed factory at ${FactorySeedInstance.address}`);

await FactorySeedInstance.DeployContract();
const address = await FactorySeedInstance.getAddress();
console.log("Test address " + address);

运行结果

  • 本地Hardhat节点:

    Deployed factory at 0xb7278A61aa25c888815aFC32Ad3cC52fF24fE575
    Test address 0x9FA5072032B7a7DCc49533401cec1C47bfcf350d
    
  • Goerli测试网:

    Deployed factory at 0x742970731639e0830B2c7cD579d71018Fe93C683
    Test address 0x0000000000000000000000000000000000000000
    

原因分析与解决方法

1. 交易未等待确认就读取状态

本地Hardhat节点是单节点即时出块,调用DeployContract()后交易立即确认,状态同步更新。但Goerli测试网需要等待区块打包确认,脚本中调用DeployContract()后直接读取getAddress(),此时交易可能还未上链,addr仍为初始的0地址。

解决方法:调用DeployContract()后等待交易确认:

const deployTx = await FactorySeedInstance.DeployContract();
await deployTx.wait(); // 等待交易被区块确认
const address = await FactorySeedInstance.getAddress();

2. Gas设置不足导致交易失败

Goerli测试网的Gas价格波动比本地节点大,如果脚本未手动设置足够的Gas,DeployContract()交易可能因Gas不足被矿工拒绝,导致addr从未被赋值。

解决方法:调用DeployContract()时显式设置Gas参数:

const deployTx = await FactorySeedInstance.DeployContract({
  gasLimit: 1000000, // 根据实际部署需求调整
  gasPrice: await hre.ethers.provider.getGasPrice()
});
await deployTx.wait();

3. 验证交易状态

可以在Goerli区块浏览器输入Factory合约地址,查看DeployContract交易的状态是否成功。如果交易失败,可查看具体失败原因(如Gas不足、合约执行异常等)。


内容的提问来源于stack exchange,提问作者Yohan Béjannin

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.25 11:58:02