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

部署Solidity合约遇TypeError:address参数无匹配函数(ethers.js 6.5.1)

问题:Hardhat部署合约到Ganache时触发TypeError错误

使用Hardhat将Solidity合约部署至Ganache网络时,遇到TypeError错误,提示TypeError: no matching function (argument="key", value="address", code=INVALID_ARGUMENT, version=6.5.1)。已尝试重建Hardhat配置文件、更换合约,但重复出现相同错误。

合约代码

// SPDX-License-Identifier: MIT 
pragma solidity ^0.8;
contract Project {
 
struct Card { uint256 id; string title; string about; string skills; }

event CardCreated(uint256 indexed id, string title, string about, string skills);

mapping(uint256 => Card) private cards;
uint256 private cardCount;

function createCard(string memory title, string memory about, string memory skills) public {
    cardCount++;
    cards[cardCount] = Card(cardCount, title, about, skills);
    emit CardCreated(cardCount, title, about, skills);
}

function getCard(uint256 id) public view returns (Card memory) {
    return cards[id];
}
}

部署脚本(deploy.js)

const hre = require("hardhat");

async function main() {
  const Contract = await hre.ethers.getContractFactory("Project");

  // console.log(Contract);
  const contract = await Contract.deploy();
  // console.log("this is contract:", contract);
  await contract.waitForDeployment();

  console.log(`Contract Address: ${contract.address}`);
}

main().catch((error) => {
  console.error(error);
  process.exitCode = 1;
});

完整错误信息

TypeError: no matching function (argument="key", value="address", code=INVALID_ARGUMENT, version=6.5.1)
    at makeError (C:\Users\susha\OneDrive\Desktop\practice\checking\node_modules\ethers\src.ts\utils\errors.ts:670:21)
    at assert (C:\Users\susha\OneDrive\Desktop\practice\checking\node_modules\ethers\src.ts\utils\errors.ts:694:25)
    at assertArgument (C:\Users\susha\OneDrive\Desktop\practice\checking\node_modules\ethers\src.ts\utils\errors.ts:706:5)
    at Interface.getFunctionName (C:\Users\susha\OneDrive\Desktop\practice\checking\node_modules\ethers\src.ts\abi\interface.ts:542:23)
    at buildWrappedMethod (C:\Users\susha\OneDrive\Desktop\practice\checking\node_modules\ethers\src.ts\contract\contract.ts:334:34)
    at BaseContract.getFunction (C:\Users\susha\OneDrive\Desktop\practice\checking\node_modules\ethers\src.ts\contract\contract.ts:841:22)
    at Object.get (C:\Users\susha\OneDrive\Desktop\practice\checking\node_modules\ethers\src.ts\contract\contract.ts:747:39)
    at main (C:\Users\susha\OneDrive\Desktop\practice\checking\scripts\deploy.js:11:45)
    at processTicksAndRejections (node:internal/process/task_queues:96:5) {
  code: 'INVALID_ARGUMENT',
  argument: 'key',
  value: 'address'
}

解决方案

核心问题

错误源于ethers.js v6版本的API变更:合约实例不再提供直接的.address属性,而是需要通过异步方法getAddress()获取合约地址。直接访问.address会被ethers当作合约函数查找,导致找不到匹配函数,触发该错误。

修复步骤

  1. 修改deploy.js中的地址获取代码:
    将原代码中的:
console.log(`Contract Address: ${contract.address}`);

替换为:

console.log(`Contract Address: ${await contract.getAddress()}`);
  1. 验证Hardhat网络配置(确保与Ganache匹配)
    在hardhat.config.js中确认Ganache网络配置正确:
module.exports = {
  solidity: "0.8.0", // 与合约pragma版本一致
  networks: {
    ganache: {
      url: "http://127.0.0.1:7545", // Ganache RPC地址
      chainId: 1337 // Ganache默认链ID
    }
  }
};
  1. 重新执行部署命令:
npx hardhat run scripts/deploy.js --network ganache

内容的提问来源于stack exchange,提问作者Rishabh Pandey

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.19 11:37:21