Solidity

HardhatError:未找到契約“CertData.sol”的工件

  • November 2, 2022

我正在嘗試從我的 Solidity 智能合約創建一個 ABI。

當我執行時npx hardhat run --network testnet scripts/deploy.js,它給了我 HardhatError: Artifact for contract “CertData” not found。

我很確定我沒有在智能合約名稱中犯任何拼寫錯誤。

我的 deploy.js 腳本位於 scripts/deploy.js

腳本內容:

const hre = require("hardhat");

async function main() {
 const CertData = await hre.ethers.getContractFactory("CertData");
 const certData = await CertData.deploy();
 await certData.deployed();

 const txHash = certData.deployTransaction.hash;
 const txReceipt = await hre.ethers.provider.waitForTransaction(txHash);
 console.log(
   `check your contract: https://mumbai.polygonscan.com/address/${txReceipt.contractAddress}`
 );
 console.log("contract address:", txReceipt.contractAddress);
}

main()
 .then(() => process.exit(0))
 .catch((error) => {
   console.error(error);
   process.exit(1);
 });

還有我的 CertData.sol 文件:

// SPDX-License-Identifier: MIT
pragma solidity >=0.7.0 <0.9.0;

contract CertData {
 Cert[] private certs;
 mapping(address => Cert[]) private authorToCerts;

 struct Cert {
   string title;
   string description;
   string owner;
 }

 function store(string memory title, string memory description, string memory owner) public {
   Cert memory cert = Cert(title, description, owner);

   certs.push(cert);
   authorToCerts[msg.sender].push(cert);
 }

 function retrieveAllCerts() public view returns (Cert[] memory) {
   return certs;
 }

 function retrieveCertsByAuthor() public view returns (Cert[] memory) {
   return authorToCerts[msg.sender];
 }
}

hardhat.config.js 文件內容:

require("@nomiclabs/hardhat-waffle");
require("dotenv").config();

const privateKey = process.env.PRIVATE_KEY;

module.exports = {
 networks: {
   hardhat: {
     chainId: 1337,
   },
   testnet: {
     url: "https://rpc-mumbai.maticvigil.com",
     accounts: [privateKey],
   },
   mainnet: {
     url: "https://polygon-rpc.com",
     accounts: [privateKey],
   },
 },
 solidity: "0.8.4",
};

我的文件夾結構:

在此處輸入圖像描述

謝謝大家

嘗試將文件夾重命名/contract/contracts,因為安全帽path.sources預設為/contracts. 或paths在您hardhat.config.js的以下內容中另行指定:

 paths: {
   sources: "./contract",
 },

查看paths文件中的完整屬性列表:

https://hardhat.org/hardhat-runner/docs/config#path-configuration

引用自:https://ethereum.stackexchange.com/questions/138613