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

Hardhat测试部署智能合约时owner返回0x00000000问题排查

问题:Hardhat测试中ERC20合约部署后owner()返回零地址

我用Hardhat编写单元测试部署ProperSubsetERC20智能合约,已指定Hardhat网络的signers[0]地址作为部署者,但部署后调用owner()返回的是0x0000000000000000000000000000000000000000,与预期的部署者地址不符。

测试代码

const  hre = require('hardhat')
const { expect, assert } = require('chai')

describe('ProperSubsetFund', async function () {
  // Initialize Contract Var
  let deployERC20, deployer, user1, user2, trader1, trader2
  let signers

  beforeEach(async function () {
    // Getting all the signers
    signers = await hre.ethers.getSigners()
    // console.log(signers.map(item => item.address));

    // Assign addresses to roles for later use
    deployer = signers[0].address
    user1 = signers[1].address
    user2 = signers[2].address
    trader1 = signers[3].address
    trader2 = signers[4].address

    // Read Contract ERC20
    const contractERC20 = await hre.ethers.getContractFactory('ProperSubsetERC20')
    // Read Contract Launchpad
    const contractLaunchpad = await hre.ethers.getContractFactory('ProperSubsetFactory')
    // Read Contract Funds
    const contractFunds = await hre.ethers.getContractFactory('ProperSubsetFund')

    // Deploy the three contracts
    deployERC20 = await contractERC20.connect(signers[0]).deploy(true)
    
  });

  describe('Deployment and Ownership', function () {
    it('Check if the owner is the deployer', async function () {
        expect(await deployERC20.owner()).to.equal(deployer)
    });
  });
})

报错信息

ProperSubsetFund
       Deployment and Ownership
         Deployment
           Should set the right owner:

      AssertionError: expected '0x00000000000000000000000000000000000…' to equal '0xf39Fd6e51aad88F6F4ce6aB8827279cffFb…'
      + expected - actual

      -0x0000000000000000000000000000000000000000
      +0xf39Fd6e51aad88F6F4ce6aB8827279cffFb92266
      
      at Context.<anonymous> (test/ProperSubsetFund.test.js:71:49)
      at processTicksAndRejections (node:internal/process/task_queues:96:5)
排查与解决方案

核心问题是合约的owner变量未被正确初始化,以下是几个关键排查方向:

  • 检查ProperSubsetERC20构造函数逻辑
    这是最常见的原因:

    • 如果是自定义owner实现,确保构造函数中执行了owner = msg.sender;
    • 如果继承自OpenZeppelin的Ownable合约,确认正确调用了父类构造:v5+版本用constructor() Ownable(msg.sender) {},旧版本则需在构造函数中调用_transferOwnership(msg.sender);
    • 排查部署时传入的true参数是否触发了异常逻辑,导致owner被重置为零地址
  • 等待部署交易确认
    部分测试环境下,合约实例返回后交易可能尚未上链,导致读取到未初始化的值。修改部署代码,等待交易确认:

    deployERC20 = await contractERC20.connect(signers[0]).deploy(true);
    await deployERC20.deployed(); // 等待部署交易上链完成
    
  • 验证初始化流程
    如果合约使用了Initializable(可升级合约场景),需确认部署后是否调用了initialize函数,且该函数正确设置了owner

  • 确认变量赋值正确性
    在测试代码中添加日志,验证deployer地址和合约返回的owner值:

    console.log("部署者地址:", deployer);
    console.log("合约owner:", await deployERC20.owner());
    

    排除变量赋值错误的可能

内容的提问来源于stack exchange,提问作者Samijoe Hayek

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.13 03:45:35