英文:
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
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论