PatrickAlphaC / hardhat-smartcontract-lottery-fcc

MIT License
117 stars 183 forks source link

TypeError: contractAddresses[chainId].includes is not a function #194

Open kihiuFrank opened 9 months ago

kihiuFrank commented 9 months ago

I'm deploying my contract and I want to update the front-end with the contract addresses and the ABI. But the update fails with the error below. What I'm I doing wrong?

Error

Updating front end...
An unexpected error occurred:

Error: ERROR processing /Users/k_frankline/projects/SaveAKid/backend/deploy/update-front-end.js:
TypeError: contractAddresses[chainId].includes is not a function
    at updateContractAddresses (/Users/k_frankline/projects/SaveAKid/backend/deploy/update-front-end.js:37:37)
    at processTicksAndRejections (node:internal/process/task_queues:96:5)

Package.json

{
  "name": "SaveAKid",
  "version": "0.1.0",
  "private": true,
  "license": "MIT",
  "dependencies": {
    "hardhat": "^2.18.1"
  },
  "devDependencies": {
    "@chainlink/contracts": "^0.4.1",
    "@ethersproject/abi": "^5.4.7",
    "@ethersproject/providers": "^5.4.7",
    "@nomicfoundation/hardhat-chai-matchers": "^1.0.0",
    "@nomicfoundation/hardhat-ethers": "^3.0.2",
    "@nomicfoundation/hardhat-network-helpers": "^1.0.0",
    "@nomicfoundation/hardhat-toolbox": "^1.0.1",
    "@nomiclabs/hardhat-ethers": "npm:hardhat-deploy-ethers",
    "@nomiclabs/hardhat-etherscan": "^3.0.0",
    "@typechain/ethers-v5": "^10.1.0",
    "@typechain/hardhat": "^6.1.2",
    "chai": "^4.2.0",
    "dotenv": "^16.0.2",
    "ethers": "^6.8.0",
    "hardhat-contract-sizer": "^2.10.0",
    "hardhat-deploy": "^0.11.14",
    "hardhat-gas-reporter": "^1.0.8",
    "prettier": "^2.7.1",
    "solhint": "^3.3.7",
    "solidity-coverage": "^0.8.2",
    "typechain": "^8.1.0"
  },
  "scripts": {
    "test": "hardhat test",
    "test:staging": "hardhat test --network rinkeby",
    "lint": "solhint 'contracts/*.sol'",
    "lint:fix": "solhint 'contracts/*.sol' --fix",
    "format": "prettier --write .",
    "coverage": "hardhat coverage"
  }
}

Update Frontend Script

const { network } = require("hardhat");
const fs = require("fs");
require("dotenv").config();

const {
  frontEndContractFile,
  frontEndAbiLocation,
} = require("../helper-hardhat-config");

module.exports = async function () {
  if (process.env.UPDATE_FRONT_END) {
    console.log("Updating front end...");
    await updateContractAddresses();
    await updateAbi();
  }
};

async function updateAbi() {
  const saveAKid = await ethers.getContract("SaveAKid");
  //   fs.writeFileSync(
  //     `${frontEndAbiLocation}SaveAKid.json`,
  //     saveAKid.interface.format(ethers.utils.FormatTypes.json)
  //   );
  fs.writeFileSync(
    frontEndAbiLocation,
    saveAKid.interface.format(ethers.FormatTypes.json)
  );
}

async function updateContractAddresses() {
  const saveAKid = await ethers.getContract("SaveAKid");
  const chainId = network.config.chainId.toString();
  const contractAddresses = JSON.parse(
    fs.readFileSync(frontEndContractFile, "utf8")
  );
  if (chainId in contractAddresses) {
    if (!contractAddresses[chainId].includes(saveAKid.address)) {
      contractAddresses[chainId].push(saveAKid.address);
    }
  } else {
    contractAddresses[chainId] = { SaveAKid: [saveAKid.address] };
  }
  fs.writeFileSync(frontEndContractFile, JSON.stringify(contractAddresses));
}

module.exports.tags = ["all", "frontend"];
PatrickAlphaC commented 9 months ago

Thank you for this!

Can you:

  1. Make this a discusson on the full repo? https://github.com/smartcontractkit/full-blockchain-solidity-course-js/
Nlferu commented 9 months ago

Issue solved here