TypeError: contractAddresses[chainId].includes is not a function #6234
-
I'm deploying my contract and I want to update the front-end with the contract addresses and the ABI. ErrorUpdating 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 Scriptconst { 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"]; Here is my REPO |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 4 replies
-
@Neftyr @alymurtazamemon |
Beta Was this translation helpful? Give feedback.
-
Hello @kihiuFrank The error message indicates that contractAddresses[chainId] is not an array, and that's why you're encountering the error when trying to use the async function updateContractAddresses() {
const saveAKid = await ethers.getContract("SaveAKid");
const chainId = network.config.chainId.toString();
const contractAddresses = JSON.parse(
fs.readFileSync(frontEndContractFile, "utf8")
);
if (contractAddresses[chainId]) {
// Check if it's already an array
if (Array.isArray(contractAddresses[chainId])) {
if (!contractAddresses[chainId].includes(saveAKid.address)) {
contractAddresses[chainId].push(saveAKid.address);
}
} else {
// Convert it to an array
contractAddresses[chainId] = [contractAddresses[chainId], saveAKid.address];
}
} else {
contractAddresses[chainId] = [saveAKid.address];
}
fs.writeFileSync(frontEndContractFile, JSON.stringify(contractAddresses));
} |
Beta Was this translation helpful? Give feedback.
Hello @kihiuFrank
The error message indicates that contractAddresses[chainId] is not an array, and that's why you're encountering the error when trying to use the
.includes()
method on it. To fix this, you need to ensure that contractAddresses[chainId] is an array before calling.includes()
on it. Here's how you can modify your code to handle this: