From 5270622967d5333a7dea122f689792ea6ae7ef5c Mon Sep 17 00:00:00 2001 From: Pierre Jeanjacquot <26487010+PierreJeanjacquot@users.noreply.github.com> Date: Tue, 16 Dec 2025 15:30:31 +0100 Subject: [PATCH] fix: allow updating start block with START_BLOCK env --- Dockerfile | 1 + package.json | 5 ++-- update-networks.js | 73 ++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 77 insertions(+), 2 deletions(-) create mode 100644 update-networks.js diff --git a/Dockerfile b/Dockerfile index 782f7da..f36f5f7 100644 --- a/Dockerfile +++ b/Dockerfile @@ -8,6 +8,7 @@ COPY package*.json . COPY schema.graphql . COPY subgraph.yaml . COPY networks.json . +COPY update-networks.js . COPY src ./src RUN npm ci diff --git a/package.json b/package.json index 0c26971..c787c33 100644 --- a/package.json +++ b/package.json @@ -16,7 +16,8 @@ "create": "dotenv -e .env -- sh -c 'graph create ${NETWORK_NAME:-bellecour}/${DEPLOY_ENV:+$DEPLOY_ENV-}poco-v5 --node ${GRAPHNODE_URL:-http://localhost:8020}'", "deploy": "dotenv -e .env -- sh -c 'graph deploy ${NETWORK_NAME:-bellecour}/${DEPLOY_ENV:+$DEPLOY_ENV-}poco-v5 --node ${GRAPHNODE_URL:-http://localhost:8020} --ipfs ${IPFS_URL:-http://localhost:5001} --network ${NETWORK_NAME:-bellecour} --version-label ${VERSION_LABEL:-dev}'", "deploy-studio": "dotenv -e .env -- sh -c 'graph deploy ${SUBGRAPH_SLUG} --deploy-key ${SUBGRAPH_DEPLOY_KEY} --network ${SUBGRAPH_NETWORK_NAME} --version-label ${VERSION_LABEL}'", - "all": "npm run build && npm run create && npm run deploy", + "update-networks": "node update-networks.js", + "all": "npm run update-networks && npm run build && npm run create && npm run deploy", "stop-test-stack": "cd test-stack && docker compose down --remove-orphans --volumes", "start-test-stack": "npm run stop-test-stack && cd test-stack && export NETWORK_NAME=${NETWORK_NAME:-bellecour} && tsx prepare-test-env.ts && docker compose build && docker compose up -d", "check-format": "prettier \"(src|tests|test-stack)/**/*.{js,ts}\" --check", @@ -58,4 +59,4 @@ "dependencies": { "@iexec/poco": "^5.5.0" } -} \ No newline at end of file +} diff --git a/update-networks.js b/update-networks.js new file mode 100644 index 0000000..6ce19df --- /dev/null +++ b/update-networks.js @@ -0,0 +1,73 @@ +import { existsSync, readFileSync, writeFileSync } from 'fs'; +import { dirname, join } from 'path'; +import { fileURLToPath } from 'url'; + +// Get current file directory in ESM +const __filename = fileURLToPath(import.meta.url); +const __dirname = dirname(__filename); + +const { NETWORK_NAME, START_BLOCK } = process.env; // Get the network name from env + +// Path to the networks.json file (one directory up) +const networksFilePath = join(__dirname, 'networks.json'); + +/** + * Update networks.json file with the current block number + */ +async function updateNetworksFile() { + if (!NETWORK_NAME) { + console.warn( + 'No NETWORK_NAME environment variable provided. The networks.json file will not be updated.', + ); + return; + } + if (!START_BLOCK) { + console.warn( + 'No START_BLOCK environment variable provided. The networks.json file will not be updated.', + ); + return; + } + + if (!existsSync(networksFilePath)) { + console.warn(`networks.json file not found at ${networksFilePath}`); + return; + } + + try { + // Read the current networks.json file + const networksData = JSON.parse(readFileSync(networksFilePath, 'utf8')); + + // Check if the specified network exists in the file + if (networksData[NETWORK_NAME]) { + console.log(`Updating startBlock for network '${NETWORK_NAME}' to ${START_BLOCK}`); + + // Update all startBlock values for the specified network + Object.keys(networksData[NETWORK_NAME]).forEach((contract) => { + networksData[NETWORK_NAME][contract].startBlock = parseInt(START_BLOCK, 10); + }); + + // Write the updated networks.json file + writeFileSync(networksFilePath, JSON.stringify(networksData, null, 4)); + console.log(`Successfully updated ${networksFilePath}`); + } else { + console.warn(`Network '${NETWORK_NAME}' not found in networks.json. File unchanged.`); + } + } catch (error) { + console.error(`Error updating networks.json file: ${error}`); + } +} + +/** + * Main function to orchestrate the process + */ +async function main() { + try { + await updateNetworksFile(); + } catch (error) { + console.error(error); + process.exit(1); + } +} + +// Run the main function +main();