1
const { ethers, network } = require("hardhat")
const fs = require("fs")

const FRONT_END_ADDRESS_FILE = `../../nextjs-frontend-lottery/constants/contractAddresses.json`
const FRONT_END_ABI_FILE = `../../nextjs-frontend-lottery/constants/abi.json`


module.exports = async function () {
  if (process.env.UPDATE_FRONT_END) {
    console.log("updating frontend...")
    updateContractAddresses()
    updateAbi()
  }
}

async function updateAbi() {
  const lottery = await ethers.getContract("Lottery")
  fs.writeFileSync(
    FRONT_END_ABI_FILE,
    lottery.interface.format(ethers.utils.FormatTypes.json)
  )
}

async function updateContractAddresses() {
  const lottery = await ethers.getContract("Lottery")
  const chainId = network.config.chainId.toString()
  const currentAddresses = JSON.parse(
    fs.readFileSync(FRONT_END_ADDRESS_FILE, "utf8") // utf-8 a way to convert to ASCII
  )
  if (chainId in contractAddress) {
    if (!contractAddress[chainId].includes(lottery.address)) {
      currentAddresses[chainId].push(lottery.address)
    }
  }
  {
    currentAddresses[chainId] = [lottery.address]
  }
  fs.writeFileSync(FRONT_END_ADDRESS_FILE, JSON.stringify(currentAddresses))
}

module.exports.tags = ["all", "frontend"]
Error: ENOENT: no such file or directory, open '../../nextjs-frontend-lottery/constants/contractAddresses.json'
    at Object.openSync (node:fs:585:3)
    at Object.readFileSync (node:fs:453:35)
    at updateContractAddresses (C:\Users\user\OneDrive\Desktop\BlockChain\hardhat-lottery\deploy\99-update-front-end.js:28:8)

My file path is correct I have checked many times.. but still the same error is coming. This code is from Patrick Collins Blockchain Course. Can anyone help me with this error?

1 Answer 1

0

If both frontend and hardhat lottery code fall under same directory then in 4 and 5th line, instead of giving path as:

const FRONT_END_ADDRESS_FILE = `../../nextjs-frontend-lottery/constants/contractAddresses.json`
const FRONT_END_ABI_FILE = `../../nextjs-frontend-lottery/constants/abi.json`

Give path as:

const FRONT_END_ADDRESS_FILE = `../nextjs-frontend-lottery/constants/contractAddresses.json`
const FRONT_END_ABI_FILE = `../nextjs-frontend-lottery/constants/abi.json`

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.