tit*_*ito 8 javascript solidity
如果不是很清楚,第一个问题对我来说就很简单,但我会尽力而为。
我目前正在通过 YouTube 视频来测试我与安全帽、以太币和华夫饼的合同(https://www.youtube.com/watch?v=oTpmNEYV8iQ&list=PLw-9a9yL-pt3sEhicr6gmuOQdcmWXhCx4&index=6)。
这是合同:
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.9;
import "@openzeppelin/contracts/token/ERC721/ERC721.sol";
contract MyContract is ERC721 {
constructor(string memory name, string memory symbol)
ERC721(name, symbol) {
}
}
Run Code Online (Sandbox Code Playgroud)
这是 test.js:
const { expect } = require('chai');
describe("MyContract", function() {
it("should return correct name", async function() {
const MyContract = hre.ethers.getContractFactory("MyContract");
const myContractDeployed = await MyContract.deploy("MyContractName", "MCN");
await myContractDeployed.deployed();
expect(await myContractDeployed.name()).to.equal("MyContractName");
});
});
Run Code Online (Sandbox Code Playgroud)
当我在终端中运行“npx Hardhat test”时,它返回:
MyContract
1) should return correct name
0 passing (7ms)
1 failing
1) MyContract
should return correct name:
TypeError: Cannot read properties of undefined (reading 'getContractFactory')
at Context.<anonymous> (test\test.js:7:35)
at processImmediate (node:internal/timers:464:21)
Run Code Online (Sandbox Code Playgroud)
我的代码与视频中的代码匹配,我很难理解为什么我在这里收到类型错误。非常感谢任何指导!
编辑:
我以某种方式修复了它,我不明白它到底是如何修复的,但确实如此。而不是仅仅安装
npm install @nomiclabs/hardhat-waffle ethereum-waffle chai @nomiclabs/hardhat-ethers ethers
Run Code Online (Sandbox Code Playgroud)
我安装了
npm install --save-dev @nomiclabs/hardhat-waffle ethereum-waffle chai @nomiclabs/hardhat-ethers ethers
Run Code Online (Sandbox Code Playgroud)
然后终端打印出
npm WARN idealTree Removing dependencies.@nomiclabs/hardhat-waffle in favor of devDependencies.@nomiclabs/hardhat-waffle
npm WARN idealTree Removing dependencies.ethereum-waffle in favor of devDependencies.ethereum-waffle
npm WARN idealTree Removing dependencies.@nomiclabs/hardhat-ethers in favor of devDependencies.@nomiclabs/hardhat-ethers
npm WARN idealTree Removing dependencies.ethers in favor of devDependencies.ethers
Run Code Online (Sandbox Code Playgroud)
然后我删除了 ethers.getContractFactory("MyContract") 前面的 hre 并且它起作用了!如果有人想解释为什么这可能已经解决了它,我很乐意阅读它,否则我会继续前进。
pri*_*dal 11
在 Hardhat.config.js 文件顶部添加以下代码片段
require("@nomiclabs/hardhat-waffle");
Run Code Online (Sandbox Code Playgroud)