Skip to content

Commit

Permalink
Remove watch chains script (#68)
Browse files Browse the repository at this point in the history
* Remove the yarn dev script for watching chain data

* Expand linting checks to include prettier and tsc

* Add .ts files to prettier options

* Remove unnecessary lint:tsc script
  • Loading branch information
andreogle authored Aug 26, 2023
1 parent 866bb45 commit 1860a53
Show file tree
Hide file tree
Showing 14 changed files with 72 additions and 160 deletions.
3 changes: 0 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -161,9 +161,6 @@ The TypeScript file is generated by running any of these commands

```sh
yarn generate:chains

# Alternatively, watch the chains/ directory and regenerate on file change
yarn dev
```

### Validation
Expand Down
33 changes: 16 additions & 17 deletions chains/base.json
Original file line number Diff line number Diff line change
@@ -1,19 +1,18 @@
{
"name": "Base",
"alias": "base",
"id": "8453",
"symbol": "ETH",
"testnet": false,
"providerUrl": "https://mainnet.base.org",
"explorer": {
"api": {
"url": "https://api.basescan.org/api",
"key": {
"required": true
}
},
"browserUrl": "https://basescan.org/"
"name": "Base",
"alias": "base",
"id": "8453",
"symbol": "ETH",
"testnet": false,
"providerUrl": "https://mainnet.base.org",
"explorer": {
"api": {
"url": "https://api.basescan.org/api",
"key": {
"required": true
}
},
"blockTimeMs": 2000
}

"browserUrl": "https://basescan.org/"
},
"blockTimeMs": 2000
}
32 changes: 16 additions & 16 deletions chains/linea.json
Original file line number Diff line number Diff line change
@@ -1,18 +1,18 @@
{
"name": "Linea",
"alias": "linea",
"id": "59144",
"symbol": "ETH",
"testnet": false,
"providerUrl": "https://rpc.linea.build",
"explorer": {
"api": {
"url": "https://lineascan.build/api",
"key": {
"required": true
}
},
"browserUrl": "https://lineascan.build/"
"name": "Linea",
"alias": "linea",
"id": "59144",
"symbol": "ETH",
"testnet": false,
"providerUrl": "https://rpc.linea.build",
"explorer": {
"api": {
"url": "https://lineascan.build/api",
"key": {
"required": true
}
},
"blockTimeMs": 12099
}
"browserUrl": "https://lineascan.build/"
},
"blockTimeMs": 12099
}
32 changes: 16 additions & 16 deletions chains/mantle.json
Original file line number Diff line number Diff line change
@@ -1,18 +1,18 @@
{
"name": "Mantle",
"alias": "mantle",
"id": "5000",
"symbol": "MNT",
"testnet": false,
"providerUrl": "https://rpc.testnet.mantle.xyz/",
"explorer": {
"api": {
"url": "https://explorer.mantle.xyz/api",
"key": {
"required": false
}
},
"browserUrl": "https://explorer.mantle.xyz/"
"name": "Mantle",
"alias": "mantle",
"id": "5000",
"symbol": "MNT",
"testnet": false,
"providerUrl": "https://rpc.testnet.mantle.xyz/",
"explorer": {
"api": {
"url": "https://explorer.mantle.xyz/api",
"key": {
"required": false
}
},
"blockTimeMs": 362
}
"browserUrl": "https://explorer.mantle.xyz/"
},
"blockTimeMs": 362
}
8 changes: 4 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,13 @@
"scripts": {
"build": "yarn clean && yarn generate:chains && tsc -p tsconfig.build.json",
"clean": "rimraf ./dist",
"dev": "ts-node scripts/generate-chains.ts --watch",
"generate:chains": "ts-node scripts/generate-chains.ts",
"lint": "eslint . --ext .js,.ts",
"lint": "yarn lint:eslint && yarn lint:prettier",
"lint:eslint": "eslint . --ext .js,.ts",
"lint:prettier": "prettier --check \"./**/*.{ts,js,md,json}\"",
"prepare": "husky install",
"prepublishOnly": "yarn validate:chains",
"prettier": "prettier --write \"./**/*.{js,md,json}\"",
"prettier": "prettier --write \"./**/*.{ts,js,md,json}\"",
"providers:ping": "ts-node scripts/ping-providers.ts",
"providers:time": "ts-node scripts/calculate-average-block-times.ts",
"test": "node --test --loader ts-node/esm ./src/**/*.test.ts",
Expand All @@ -36,7 +37,6 @@
"@types/prettier": "^3.0.0",
"@typescript-eslint/eslint-plugin": "^6.2.1",
"@typescript-eslint/parser": "^6.2.1",
"chokidar": "^3.5.3",
"eslint": "^8.46.0",
"eslint-plugin-import": "^2.28.0",
"ethers": "^6.6.7",
Expand Down
2 changes: 1 addition & 1 deletion scripts/calculate-average-block-times.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { CHAINS } from '../src';
const BLOCK_LOOKBACK = 400_000;

async function calculateAverageBlockTimes(): Promise<void> {
const specifiedChain = CHAINS.find(chain => chain.alias === process.env.CHAIN);
const specifiedChain = CHAINS.find((chain) => chain.alias === process.env.CHAIN);
const chains = specifiedChain ? [specifiedChain] : CHAINS;

const results = await Promise.allSettled(
Expand Down
39 changes: 6 additions & 33 deletions scripts/generate-chains.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import fs from 'fs';
import path from 'path';
import chokidar from 'chokidar';
import prettier from 'prettier';

const PRETTIER_CONFIG = path.join(__dirname, '../.prettierrc');
Expand Down Expand Up @@ -43,35 +42,9 @@ async function mergeJsonFiles(): Promise<void> {
console.log(`Combined chains been saved as ${OUTPUT_FILE}`);
}

function watchJsonFiles(): void {
// ignored: by default we want to ignore dotfiles while watching
// persistent: continue the process as long as the directory is being watched
// See: https://github.com/paulmillr/chokidar#api
const watcher = chokidar.watch([INPUT_DIR], { ignored: /^\./, persistent: true });

watcher
.on('add', (path) => {
console.log(`File ${path} has been added`);
mergeJsonFiles();
})
.on('change', (path) => {
console.log(`File ${path} has been changed`);
mergeJsonFiles();
})
.on('unlink', (path) => {
console.log(`File ${path} has been removed`);
mergeJsonFiles();
});
}

if (process.argv.includes('--watch')) {
watchJsonFiles();
} else {
mergeJsonFiles()
.then(() => process.exit(0))
.catch((error) => {
console.log(error);
process.exit(1);
});
}

mergeJsonFiles()
.then(() => process.exit(0))
.catch((error) => {
console.log(error);
process.exit(1);
});
6 changes: 2 additions & 4 deletions scripts/ping-providers.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,14 @@
import { JsonRpcProvider } from 'ethers';
import { CHAINS } from '../src';

const specifiedChain = CHAINS.find(chain => chain.alias === process.env.CHAIN);
const specifiedChain = CHAINS.find((chain) => chain.alias === process.env.CHAIN);
const chains = specifiedChain ? [specifiedChain] : CHAINS;

chains.forEach(async (chain) => {
const provider = new JsonRpcProvider(chain.providerUrl);
const chainId = (await provider.getNetwork()).chainId;
if (chainId.toString() !== chain.id) {
throw new Error(
`${chain.alias} provider reports chain ID to be ${chainId}, while it is defined to be ${chain.id}`
);
throw new Error(`${chain.alias} provider reports chain ID to be ${chainId}, while it is defined to be ${chain.id}`);
}
const blockTimestamp = (await provider.getBlock('latest'))!.timestamp;
const deltaTime = Math.floor(new Date().getTime() / 1000) - blockTimestamp;
Expand Down
3 changes: 1 addition & 2 deletions scripts/validate-chains.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ if (CHAINS.length !== jsonChains.length) {
jsonFiles.forEach((filePath: string, index: number) => {
const chain = jsonChains[index]!;
if (filePath.replace('.json', '') !== chain.alias) {
console.log('JSON file name must match the chain\'s alias');
console.log("JSON file name must match the chain's alias");
console.log(`Current value: ${filePath}.json. Expected: ${chain.alias}.json`);
process.exit(1);
}
Expand Down Expand Up @@ -54,4 +54,3 @@ jsonChains.forEach((chain: any, index: number) => {

console.log('Successfully validated chains!');
process.exit(0);

5 changes: 3 additions & 2 deletions src/hardhat-config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@ import { toUpperSnakeCase } from './utils/strings';
import { Chain, HardhatEtherscanConfig, HardhatNetworksConfig } from './types';

export function getEnvVariableNames(): string[] {
const apiKeyEnvNames = CHAINS.filter((chain) => chain.explorer?.api?.key?.required).map((chain) => etherscanApiKeyName(chain));
const apiKeyEnvNames = CHAINS.filter((chain) => chain.explorer?.api?.key?.required).map((chain) =>
etherscanApiKeyName(chain)
);

const networkRpcUrlNames = CHAINS.map((chain) => chain.providerUrl);

Expand Down Expand Up @@ -71,4 +73,3 @@ export function networks(): HardhatNetworksConfig {
return networks;
}, {} as HardhatNetworksConfig);
}

7 changes: 3 additions & 4 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,18 +39,17 @@ export interface HardhatNetworksConfig {
accounts: { mnemonic: string };
chainId: number;
url: string;
}
};
}

// https://hardhat.org/hardhat-runner/plugins/nomicfoundation-hardhat-verify#adding-support-for-other-networks
export interface HardhatEtherscanCustomChain {
network: string;
chainId: number;
urls: { apiURL: string; browserURL: string; }
urls: { apiURL: string; browserURL: string };
}

export interface HardhatEtherscanConfig {
apiKey: { [alias: string]: string; }
apiKey: { [alias: string]: string };
customChains: HardhatEtherscanCustomChain[];
}

1 change: 0 additions & 1 deletion src/validations/deep-equal.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,4 +54,3 @@ describe('deepEqual', () => {
assert.equal(deepEqual(func1, func2), false);
});
});

1 change: 0 additions & 1 deletion src/validations/deep-equal.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,4 +30,3 @@ export function deepEqual(obj1: any, obj2: any): boolean {

return true;
}

Loading

0 comments on commit 1860a53

Please sign in to comment.