TypeError: no matching function (argument="key", value="address", code=INVALID_ARGUMENT, version=6.5.1) getting this error while deploying contract

huangapple go评论55阅读模式
英文:

TypeError: no matching function (argument="key", value="address", code=INVALID_ARGUMENT, version=6.5.1) getting this error while deploying contract

问题

这是我的合同

// 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}`);
}

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

这是我收到的错误
部署合同的网络是ganache。

TypeError: 没有匹配的函数 (参数="key", 值="address", 代码=INVALID_ARGUMENT, 版本=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'
}

我尝试重新创建hardhat文件并更改合同,但仍然反复收到相同的错误。

英文:

here is my contract

// 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];
}
}

here is my deploy.js file

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;
});

here is the error I'm getting
Network used to deploy contract is ganache.

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'
}

I tried re-create the hardhat file and change the contract but still getting same error repeatedly

答案1

得分: 2

ethers版本6使用getAddress()函数来检索合同地址,而之前的版本5使用.address属性。

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

替换为

const address = await contract.getAddress();
console.log(`Contract Address: ${address}`);

文档链接:https://docs.ethers.org/v6/api/contract/#BaseContract-getAddress

英文:

ethers version 6 uses getAddress() function to retrieve the contract address, while the previous version 5 uses .address property.

Replace

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

with

const address = await contract.getAddress();
console.log(`Contract Address: ${address}`);

Docs: https://docs.ethers.org/v6/api/contract/#BaseContract-getAddress

huangapple
  • 本文由 发表于 2023年6月12日 15:11:09
  • 转载请务必保留本文链接:https://go.coder-hub.com/76454314.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定