Search code examples
javascriptsolidityhardhat

hardhat getNamedAccounts() does not work properly


I'm following the course of 32 hours Learn Blockchain, Solidity, .. in Javascript and I'm stucked with an error that others have but they solve because typos ecc. I'm pretty sure at this point that the problem is not there but so what is the problem? I have my configuration file:

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

And I'm running everything in the hardhat default network, and when from the 00-deploy-mock.js the script calls the function 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) prints undefined. and It returns the error:

TypeError: Cannot read properties of undefined (reading 'length')

The same process but using ganache instead run fine. I have the hardhat-deploy plugin installed and i'm using the command hardhat deploy.

Any ideas ?


Solution

  • 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;
    });
    Try this this worked for me .instead of getNamedAccounts use

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