javascript 安全帽getNamedAccounts()无法正常工作

mlmc2os5  于 2023-01-01  发布在  Java
关注(0)|答案(3)|浏览(119)

我按照32小时的课程学习 * Blockchain,Solidity,... * 在Javascript和我卡住了一个错误that others have,但他们解决,因为错别字等。我很肯定在这一点上,问题不存在,但那么是什么问题?我有我的配置文件:

namedAccounts: {
    deployer: {
        default: 0, 
        1:0, // I even with this but nothing change
    },
  },

我在安全帽默认网络中运行所有程序,当脚本从00-deploy-mock.js调用函数getNamedAccounts()时:

module.exports = async function ({getNamedAccounts,deployments}){
    const {deploy,log} = deployments
    const {deployer} = await getNamedAccounts()
    
    log(deployer)
    if(developmentChains.includes(network.name)){
        log("Local network " + network.name +" deploying mocks....")
        await deploy("VRFCoordinatorV2Mock",{
            from: deployer,
            log: true,
            args: [BASE_FEE,GAS_PRICE_LINK] 
        })
        log("Mocks deployed !")
        log("--------------------------------------------------")
    }

}

log(deployer)打印undefined。并且它返回错误:
TypeError: Cannot read properties of undefined (reading 'length')
相同的过程,但使用ganache代替运行罚款。我有hardhat-deploy插件安装,我使用的命令hardhat deploy
有什么想法吗?

7cwmlq89

7cwmlq891#

const { ethers } = require("hardhat");

async function main() {
  const AMOUNT = ethers.utils.parseEther("0.1"); //bignumber
  const [deployer] = await ethers.getSigners();
  const iWTH = await ethers.getContractAt(
    "IWeth",
    "0xC02aaA39b223FE8D0A0e5C4F27eAD9083C756Cc2",
    deployer
  );

  const tx = await iWTH.deposit({ value: AMOUNT });
  await tx.wait(1);
  const wethBalance = await iWTH.balanceOf(deployer.address);
  console.log(`WETH OF ${deployer.address}:=  WETH ${wethBalance / 10 ** 18} `);
}

main().catch((error) => {
  console.error(error);
  process.exitCode = 1;
});

试试这个,这个对我很有效。不是getNamedAccounts使用

const [deployer] = await ethers.getSigners();
7xzttuei

7xzttuei2#

很明显,deployer没有正确加载。这通常发生在您没有设置模块的时候。在您编写“getNamedAccounts()”函数的文件中导出

woobm2wo

woobm2wo3#

在你的hardhat-config.js文件中的module.exports部分请包含以下字段

namedAccounts: {     
    deployer: {      
     default: 0,     
     1: 0,
    } } ,

也请确保以下导入存在

require("@nomiclabs/hardhat-waffle")
require("hardhat-gas-reporter")
require("@nomiclabs/hardhat-etherscan")
require("dotenv").config()
require("solidity-coverage")
require("hardhat-deploy")

虽然汽油报告和所有都不是必要的,因为你正在休31小时的课程....迟早你会需要它
我想现在应该能用了

相关问题