😇

【Hardhat】deployエラーの備忘録

2023/06/22に公開

現象

npx hardhat run scripts/deploy.js --network sepolia

で下記エラーが発生

TypeError: no matching function (argument="key", value="deployed", code=INVALID_ARGUMENT, version=6.6.0)
    at makeError (~~/dapps/udemy-blockchain-app/contract/node_modules/ethers/src.ts/utils/errors.ts:670:21)
    at assert (~~/dapps/udemy-blockchain-app/contract/node_modules/ethers/src.ts/utils/errors.ts:694:25)
    at assertArgument (~~/dapps/udemy-blockchain-app/contract/node_modules/ethers/src.ts/utils/errors.ts:706:5)
    at Interface.getFunctionName (~~/dapps/udemy-blockchain-app/contract/node_modules/ethers/src.ts/abi/interface.ts:542:23)
    at buildWrappedMethod (~~/dapps/udemy-blockchain-app/contract/node_modules/ethers/src.ts/contract/contract.ts:334:34)
    at BaseContract.getFunction (~~/dapps/udemy-blockchain-app/contract/node_modules/ethers/src.ts/contract/contract.ts:849:22)
    at Object.get (~~/dapps/udemy-blockchain-app/contract/node_modules/ethers/src.ts/contract/contract.ts:747:39)
    at deploy (~~/dapps/udemy-blockchain-app/contract/scripts/deploy.js:10:21)
    at processTicksAndRejections (node:internal/process/task_queues:95:5)
    at runDeploy (~~/dapps/udemy-blockchain-app/contract/scripts/deploy.js:17:3) {
  code: 'INVALID_ARGUMENT',
  argument: 'key',
  value: 'deployed'
}

deploy.jsの、await transactions.deployed(); でエラーが出ているとのこと

解決策

下記の方法でうまくいきました。

deploy.js(旧ver)
// import { ethers } from "hardhat";
const ethers = require("hardhat").ethers;

const deploy = async () => {
	const Transactions = await ethers.getContractFactory("Transactions");
	const transactions = await Transactions.deploy();
	await transactions.deployed();

	console.log("Transaction deployed to:", transactions.address);
}

const runDeploy = async () => {
	try {
		await deploy();
		process.exit(0);
	} catch (err) {
		console.log(err);
		process.exit(1);
	}
}

runDeploy();

↓↓↓↓

deploy.js(2023/06/22現在)
const {ethers} = require("hardhat");

const deploy = async () => {
	const transactions = await ethers.deployContract("Transactions");
	await transactions.waitForDeployment();
	console.log("成功! Transaction deployed to:", transactions.target);
}

const runDeploy = async () => {
	try {
		await deploy();
		process.exit(0);
	} catch (err) {
		console.log(err);
		process.exit(1);
	}
}

runDeploy();

原因

どこかのタイミングで、書き方が変わったようですね

	const Transactions = await ethers.getContractFactory("Transactions");
	const transactions = await Transactions.deploy();
	await transactions.deployed();
	const transactions = await ethers.deployContract("Transactions");
	await transactions.waitForDeployment();

新しい記法の方が圧倒的にシンプルで良いですね。

参考

  • hardhat公式ドキュメント

https://hardhat.org/hardhat-runner/docs/guides/deploying

  • Sinn Codeさんの講座
    フロントエンドエンジニアへの、実践ブロックチェーン入門としてめちゃオススメです。

https://www.udemy.com/course/web3-blockchain-app-udemy/

Discussion