为什么'醚'.getSigners '返回长度仅为1的signers数组?


import { ethers } from "hardhat";
...
const [owner, user1, user2, user3] = await ethers.getSigners();
console.log(user1, user2, user3);

我在硬帽测试场景中使用ethers.getSigners。它应该返回许多签名者(我不知道具体有多少),但现在我只能得到一个签名者,即owner。我尝试打印user,user2user3,并且可以在我的控制台中看到三个undefined。有人知道这个解决方案吗?

我一直使用google,githubstackoverflow来解决这个问题。但我找不到确切的解,这驱使我尝试自己解决这个问题。我还有另一个问题,在我的硬帽测试环境中,固态文件中的console.log根本不起作用。固体文件中没有错误,它编译没有任何错误。那么为什么呢?我查看了我的配置文件

import * as dotenv from "dotenv";
import { HardhatUserConfig, task } from "hardhat/config";
import "@nomiclabs/hardhat-ethers";
import "@nomiclabs/hardhat-etherscan";
import "@nomiclabs/hardhat-waffle";
import "@typechain/hardhat";
import "hardhat-gas-reporter";
import "solidity-coverage";
dotenv.config();
// This is a sample Hardhat task. To learn how to create your own go to
// https://hardhat.org/guides/create-task.html
task("accounts", "Prints the list of accounts", async (taskArgs, hre) => {
const accounts = await hre.ethers.getSigners();
for (const account of accounts) {
console.log(account.address);
}
});
// You need to export an object to set up your config
// Go to https://hardhat.org/config/ to learn more
const config: HardhatUserConfig = {
solidity: {
version: "0.8.4",
settings: {
optimizer: {
enabled: true,
runs: 1000,
},
},
},
defaultNetwork: 'rinkeby',
networks: {
ropsten: {
url: process.env.ROPSTEN_URL || "",
accounts:
process.env.PRIVATE_KEY !== undefined ? [process.env.PRIVATE_KEY] : [],
},
rinkeby: {
url: process.env.RINKEBY_URL || "",
accounts:
process.env.PRIVATE_KEY !== undefined ? [process.env.PRIVATE_KEY] : [],
},
},
gasReporter: {
enabled: process.env.REPORT_GAS !== undefined,
currency: "USD",
},
etherscan: {
apiKey: process.env.ETHERSCAN_API_KEY,
},
mocha: {
timeout: 150000
}
};
export default config;

最后,我找到了原因。那是因为我的安全帽没有在本地网络上运行。在rinkeby网络中运行

事实上,这是两个问题的正确解决方案。

我建议你不要在硬帽配置中更新defaultNetwork。您应该在命令行中提到network (--network rinkeby)。

如果使用await,则必须在async函数中使用:

const { ethers } = require("hardhat")
async function main() {
const [owner, user1, user2, user3] = await ethers.getSigners()
console.log(`user1:`,user1); // or whatever output you want
}
main()

最新更新