Skip to content

Commit 0794cf8

Browse files
fix: allow updating start block with START_BLOCK env (#67)
1 parent d753027 commit 0794cf8

File tree

3 files changed

+77
-2
lines changed

3 files changed

+77
-2
lines changed

Dockerfile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ COPY package*.json .
88
COPY schema.graphql .
99
COPY subgraph.yaml .
1010
COPY networks.json .
11+
COPY update-networks.js .
1112
COPY src ./src
1213

1314
RUN npm ci

package.json

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,8 @@
1616
"create": "dotenv -e .env -- sh -c 'graph create ${NETWORK_NAME:-bellecour}/${DEPLOY_ENV:+$DEPLOY_ENV-}poco-v5 --node ${GRAPHNODE_URL:-http://localhost:8020}'",
1717
"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}'",
1818
"deploy-studio": "dotenv -e .env -- sh -c 'graph deploy ${SUBGRAPH_SLUG} --deploy-key ${SUBGRAPH_DEPLOY_KEY} --network ${SUBGRAPH_NETWORK_NAME} --version-label ${VERSION_LABEL}'",
19-
"all": "npm run build && npm run create && npm run deploy",
19+
"update-networks": "node update-networks.js",
20+
"all": "npm run update-networks && npm run build && npm run create && npm run deploy",
2021
"stop-test-stack": "cd test-stack && docker compose down --remove-orphans --volumes",
2122
"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",
2223
"check-format": "prettier \"(src|tests|test-stack)/**/*.{js,ts}\" --check",
@@ -58,4 +59,4 @@
5859
"dependencies": {
5960
"@iexec/poco": "^5.5.0"
6061
}
61-
}
62+
}

update-networks.js

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
import { existsSync, readFileSync, writeFileSync } from 'fs';
2+
import { dirname, join } from 'path';
3+
import { fileURLToPath } from 'url';
4+
5+
// Get current file directory in ESM
6+
const __filename = fileURLToPath(import.meta.url);
7+
const __dirname = dirname(__filename);
8+
9+
const { NETWORK_NAME, START_BLOCK } = process.env; // Get the network name from env
10+
11+
// Path to the networks.json file (one directory up)
12+
const networksFilePath = join(__dirname, 'networks.json');
13+
14+
/**
15+
* Update networks.json file with the current block number
16+
*/
17+
async function updateNetworksFile() {
18+
if (!NETWORK_NAME) {
19+
console.warn(
20+
'No NETWORK_NAME environment variable provided. The networks.json file will not be updated.',
21+
);
22+
return;
23+
}
24+
if (!START_BLOCK) {
25+
console.warn(
26+
'No START_BLOCK environment variable provided. The networks.json file will not be updated.',
27+
);
28+
return;
29+
}
30+
31+
if (!existsSync(networksFilePath)) {
32+
console.warn(`networks.json file not found at ${networksFilePath}`);
33+
return;
34+
}
35+
36+
try {
37+
// Read the current networks.json file
38+
const networksData = JSON.parse(readFileSync(networksFilePath, 'utf8'));
39+
40+
// Check if the specified network exists in the file
41+
if (networksData[NETWORK_NAME]) {
42+
console.log(`Updating startBlock for network '${NETWORK_NAME}' to ${START_BLOCK}`);
43+
44+
// Update all startBlock values for the specified network
45+
Object.keys(networksData[NETWORK_NAME]).forEach((contract) => {
46+
networksData[NETWORK_NAME][contract].startBlock = parseInt(START_BLOCK, 10);
47+
});
48+
49+
// Write the updated networks.json file
50+
writeFileSync(networksFilePath, JSON.stringify(networksData, null, 4));
51+
console.log(`Successfully updated ${networksFilePath}`);
52+
} else {
53+
console.warn(`Network '${NETWORK_NAME}' not found in networks.json. File unchanged.`);
54+
}
55+
} catch (error) {
56+
console.error(`Error updating networks.json file: ${error}`);
57+
}
58+
}
59+
60+
/**
61+
* Main function to orchestrate the process
62+
*/
63+
async function main() {
64+
try {
65+
await updateNetworksFile();
66+
} catch (error) {
67+
console.error(error);
68+
process.exit(1);
69+
}
70+
}
71+
72+
// Run the main function
73+
main();

0 commit comments

Comments
 (0)