From 5fd2841e77739ea6c5bebbb1626b4770c3994c4d Mon Sep 17 00:00:00 2001 From: Ladd Hoffman Date: Sun, 17 Mar 2024 11:48:05 -0500 Subject: [PATCH] move just contract address data to its own file --- client/src/App.jsx | 3 +-- client/src/AvailabilityStakes.jsx | 3 +-- .../index.js => client/src/contract-config.js | 8 +++---- .../addresses.json => contract-addresses.json | 0 contract-config/package.json | 11 --------- ethereum/package-lock.json | 10 ++++++++ ethereum/package.json | 1 + ethereum/scripts/automatic-staking.js | 12 +++------- ethereum/scripts/contract-config.js | 23 +++++++++++++++++++ ethereum/scripts/deploy.js | 9 ++++++++ 10 files changed, 52 insertions(+), 28 deletions(-) rename contract-config/index.js => client/src/contract-config.js (74%) rename contract-config/addresses.json => contract-addresses.json (100%) delete mode 100644 contract-config/package.json create mode 100644 ethereum/scripts/contract-config.js diff --git a/client/src/App.jsx b/client/src/App.jsx index 1e75e0f..b629124 100644 --- a/client/src/App.jsx +++ b/client/src/App.jsx @@ -11,8 +11,7 @@ import Row from 'react-bootstrap/Row'; import Col from 'react-bootstrap/Col'; import Stack from 'react-bootstrap/Stack'; -import { getContractByChainId } from 'contract-config'; - +import { getContractByChainId } from './contract-config'; import Web3Context from './Web3Context'; import DAOArtifact from './assets/DAO.json'; import work1Artifact from './assets/Work1.json'; diff --git a/client/src/AvailabilityStakes.jsx b/client/src/AvailabilityStakes.jsx index 689090f..62558e4 100644 --- a/client/src/AvailabilityStakes.jsx +++ b/client/src/AvailabilityStakes.jsx @@ -2,8 +2,7 @@ import { useCallback, useContext } from 'react'; import { PropTypes } from 'prop-types'; import Button from 'react-bootstrap/Button'; -import { getContractByChainId } from 'contract-config'; - +import { getContractByChainId } from './contract-config'; import Web3Context from './Web3Context'; const getAvailabilityStatus = (stake) => { diff --git a/contract-config/index.js b/client/src/contract-config.js similarity index 74% rename from contract-config/index.js rename to client/src/contract-config.js index b055858..ba02634 100644 --- a/contract-config/index.js +++ b/client/src/contract-config.js @@ -1,6 +1,6 @@ -import contractAddresses from './addresses.json'; +import contractAddresses from '../../contract-addresses.json'; -export const networks = { +const networks = { localhost: '0x539', sepolia: '0xaa36a7', }; @@ -12,7 +12,7 @@ export const getContractByNetworkName = (networkName, contractName) => { }; export const getContractByChainId = (chainId, contractName) => { - const network = Object.entries(networks).find(([_, id]) => id === chainId)[0]; + const network = Object.entries(networks).find(([, id]) => id === chainId)[0]; if (!network) throw new Error(`Chain ID ${chainId} not recognized`); return getContractByNetworkName(network, contractName); -}; \ No newline at end of file +}; diff --git a/contract-config/addresses.json b/contract-addresses.json similarity index 100% rename from contract-config/addresses.json rename to contract-addresses.json diff --git a/contract-config/package.json b/contract-config/package.json deleted file mode 100644 index a06a93c..0000000 --- a/contract-config/package.json +++ /dev/null @@ -1,11 +0,0 @@ -{ - "name": "contract-config", - "version": "1.0.0", - "description": "", - "main": "index.js", - "scripts": { - "test": "echo \"Error: no test specified\" && exit 1" - }, - "author": "", - "license": "ISC" -} diff --git a/ethereum/package-lock.json b/ethereum/package-lock.json index ec8ab21..3b2c6de 100644 --- a/ethereum/package-lock.json +++ b/ethereum/package-lock.json @@ -17,6 +17,7 @@ "@nomicfoundation/hardhat-toolbox": "^4.0.0", "@nomicfoundation/hardhat-verify": "^2.0.5", "chai": "^4.4.1", + "contract-config": "file://../contract-config", "eslint": "^8.57.0", "eslint-config-airbnb": "^19.0.4", "eslint-plugin-chai-friendly": "^0.7.4", @@ -30,6 +31,11 @@ "prettier-plugin-solidity": "^1.3.1" } }, + "../contract-config": { + "version": "1.0.0", + "dev": true, + "license": "ISC" + }, "node_modules/@aashutoshrathi/word-wrap": { "version": "1.2.6", "resolved": "https://registry.npmjs.org/@aashutoshrathi/word-wrap/-/word-wrap-1.2.6.tgz", @@ -3483,6 +3489,10 @@ "integrity": "sha512-JsPKdmh8ZkmnHxDk55FZ1TqVLvEQTvoByJZRN9jzI0UjxK/QgAmsphz7PGtqgPieQZ/CQcHWXCR7ATDNhGe+YA==", "dev": true }, + "node_modules/contract-config": { + "resolved": "../contract-config", + "link": true + }, "node_modules/cookie": { "version": "0.4.2", "resolved": "https://registry.npmjs.org/cookie/-/cookie-0.4.2.tgz", diff --git a/ethereum/package.json b/ethereum/package.json index b761ac3..48f9291 100644 --- a/ethereum/package.json +++ b/ethereum/package.json @@ -11,6 +11,7 @@ "author": "", "license": "ISC", "devDependencies": { + "contract-config": "file://../contract-config", "@nomicfoundation/hardhat-chai-matchers": "^2.0.6", "@nomicfoundation/hardhat-toolbox": "^4.0.0", "@nomicfoundation/hardhat-verify": "^2.0.5", diff --git a/ethereum/scripts/automatic-staking.js b/ethereum/scripts/automatic-staking.js index a75a31e..ccb484e 100644 --- a/ethereum/scripts/automatic-staking.js +++ b/ethereum/scripts/automatic-staking.js @@ -1,9 +1,5 @@ const { ethers } = require('hardhat'); - -const DAOAddress = { - localhost: '0x76Dfe9F47f06112a1b78960bf37d87CfbB6D6133', - sepolia: '0x39B7522Ee1A5B13aE5580C40114239D4cE0e7D29', -}; +const { getContractByNetworkName } = require('./contract-config'); let dao; let account; @@ -34,10 +30,8 @@ const fetchValidationPools = async () => { const initialize = async () => { const network = process.env.HARDHAT_NETWORK; - if (!DAOAddress[network]) { - throw new Error(`network '${network}' is unknown`); - } - dao = await ethers.getContractAt('DAO', DAOAddress[network]); + const DAOAddress = getContractByNetworkName(network, 'DAO'); + dao = await ethers.getContractAt('DAO', DAOAddress); [account] = await ethers.getSigners(); const address = await account.getAddress(); console.log(`account: ${address}`); diff --git a/ethereum/scripts/contract-config.js b/ethereum/scripts/contract-config.js new file mode 100644 index 0000000..af46586 --- /dev/null +++ b/ethereum/scripts/contract-config.js @@ -0,0 +1,23 @@ +const contractAddresses = require('../../contract-addresses.json'); + +const networks = { + localhost: '0x539', + sepolia: '0xaa36a7', +}; + +const getContractByNetworkName = (networkName, contractName) => { + const address = contractAddresses[networkName][contractName]; + if (!address) throw new Error(`Contract ${contractName} not recognized`); + return address; +}; + +const getContractByChainId = (chainId, contractName) => { + const network = Object.entries(networks).find(([, id]) => id === chainId)[0]; + if (!network) throw new Error(`Chain ID ${chainId} not recognized`); + return getContractByNetworkName(network, contractName); +}; + +module.exports = { + getContractByChainId, + getContractByNetworkName, +}; diff --git a/ethereum/scripts/deploy.js b/ethereum/scripts/deploy.js index 21d8a89..b53a67d 100644 --- a/ethereum/scripts/deploy.js +++ b/ethereum/scripts/deploy.js @@ -1,16 +1,25 @@ const { ethers } = require('hardhat'); +const fs = require('fs'); + +const contractAddresses = require('../../contract-addresses.json'); + +const network = process.env.HARDHAT_NETWORK; async function main() { const dao = await ethers.deployContract('DAO'); await dao.waitForDeployment(); console.log(`DAO deployed to ${dao.target}`); + contractAddresses[network].DAO = dao.target; const WORK1_PRICE = ethers.parseEther('0.001'); const work1 = await ethers.deployContract('Work1', [dao.target, WORK1_PRICE]); await work1.waitForDeployment(); console.log(`Work1 deployed to ${work1.target}`); + contractAddresses[network].Work1 = work1.target; + + fs.writeFileSync('../../contract-addresses.json', JSON.stringify(contractAddresses, null, 2)); } // We recommend this pattern to be able to use async/await everywhere