以下代码找不到“以太”
import { HardhatUserConfig, task } from "hardhat/config";
import "@nomicfoundation/hardhat-toolbox";
task('read',async () => {
const contract = ethers.getContractFactory('AwesomeContract');
// ...
})
const config: HardhatUserConfig = {
solidity: "0.8.15",
};
export default config;当然,开发人员不能这样做:
import { ethers } from 'hardhat';因为它抛出了HH9。
是否可以在类型记录任务中使用hardhat.ethers?
发布于 2022-08-06 14:14:20
在运行任务之前,Hardhat会将Hardhad运行时环境注入全局范围,因此您需要从其中获取ethers。
检查文档示例
task(
"hello",
"Prints 'Hello, World!'",
async function (taskArguments, hre, runSuper) {
console.log("Hello, World!");
}
);还有另一个更真实的例子:
hardhat.config.ts
import { HardhatUserConfig, task } from "hardhat/config"
import { updateItem } from "./scripts"
task("updateItem", "Update a listed NFT price")
.addParam("id", "token ID")
.addParam("price", "token new listing price")
.setAction(async (args, hre) => {
const tokenId = Number(args.id)
const newPrice = String(args.price)
await updateItem(hre, tokenId, newPrice)
})
...updateItem.ts
import { HardhatRuntimeEnvironment } from "hardhat/types"
import { NFTMarketplace } from "../typechain"
async function updateItem(hre: HardhatRuntimeEnvironment, tokenId: number, newPrice: string) {
const nftMarketplace = (await hre.ethers.getContract("NFTMarketplace")) as NFTMarketplace
...
}
export default updateItemhttps://stackoverflow.com/questions/73223712
复制相似问题