Skip to content

Commit

Permalink
Create build script to gen TS code for chain metadata
Browse files Browse the repository at this point in the history
  • Loading branch information
jmrossy committed Apr 7, 2024
1 parent 0cff96a commit 3528af4
Show file tree
Hide file tree
Showing 8 changed files with 186 additions and 7 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -22,4 +22,5 @@ yarn-error.log*
*.tsbuildinfo

# build artifacts
dist
dist
tmp
1 change: 1 addition & 0 deletions .prettierrc
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
"tabWidth": 2,
"singleQuote": true,
"trailingComma": "all",
"printWidth": 100,
"overrides": [
{
"files": "*.yaml",
Expand Down
80 changes: 80 additions & 0 deletions chains/core.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
/**
* An enumeration of chains managed by the Hyperlane core team.
* Hyperlane can be deployed permissionlessly to any chain but for
* convenience the core team maintains some deployments.
*/
export enum CoreChain {
alfajores = 'alfajores',
ancient8 = 'ancient8',
arbitrum = 'arbitrum',
avalanche = 'avalanche',
base = 'base',
bsc = 'bsc',
bsctestnet = 'bsctestnet',
celo = 'celo',
chiado = 'chiado',
ethereum = 'ethereum',
fuji = 'fuji',
gnosis = 'gnosis',
inevm = 'inevm',
injective = 'injective',
mantapacific = 'mantapacific',
moonbeam = 'moonbeam',
nautilus = 'nautilus',
neutron = 'neutron',
optimism = 'optimism',
plumetestnet = 'plumetestnet',
polygon = 'polygon',
polygonzkevm = 'polygonzkevm',
proteustestnet = 'proteustestnet',
scroll = 'scroll',
scrollsepolia = 'scrollsepolia',
sepolia = 'sepolia',
solana = 'solana',
solanadevnet = 'solanadevnet',
solanatestnet = 'solanatestnet',
eclipsetestnet = 'eclipsetestnet',
viction = 'viction',
}

export type CoreChainName = keyof typeof CoreChain;

export const CoreMainnets: Array<CoreChainName> = [
CoreChain.arbitrum,
CoreChain.ancient8,
CoreChain.avalanche,
CoreChain.bsc,
CoreChain.celo,
CoreChain.ethereum,
CoreChain.neutron,
CoreChain.mantapacific,
CoreChain.moonbeam,
CoreChain.optimism,
CoreChain.polygon,
CoreChain.gnosis,
CoreChain.base,
CoreChain.scroll,
CoreChain.polygonzkevm,
CoreChain.injective,
CoreChain.inevm,
CoreChain.viction,
// CoreChains.solana,
];

export const CoreTestnets: Array<CoreChainName> = [
CoreChain.alfajores,
CoreChain.bsctestnet,
CoreChain.chiado,
CoreChain.fuji,
CoreChain.plumetestnet,
CoreChain.scrollsepolia,
CoreChain.sepolia,
CoreChain.solanadevnet,
CoreChain.solanatestnet,
CoreChain.eclipsetestnet,
];

export const CoreChains: Array<CoreChainName> = [
...CoreMainnets,
...CoreTestnets,
];
1 change: 1 addition & 0 deletions chains/proteustestnet/logo.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
10 changes: 4 additions & 6 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,8 @@
"type": "module",
"exports": {
".": "./dist/index.js",
"./chains": "./dist/chains/index.js",
"./chains/js": "./dist/chains/index.js",
"./chains/json": "./dist/chains/index.json",
"./chains/yaml": "./dist/chains/index.yaml",
"./chains/svg": "./dist/chains/svg"
"./chains/*": "./dist/chains/*",
"./chains/**": "./dist/chains/**"
},
"types": "./dist/index.d.ts",
"files": [
Expand All @@ -44,7 +41,8 @@
],
"license": "MIT",
"scripts": {
"clean": "rm -rf ./dist",
"clean": "rm -rf ./dist ./tmp",
"build": "tsx ./scripts/build.ts && tsc",
"prettier": "prettier --write ./chains ./deployments",
"test": "echo 'TODO'"
},
Expand Down
67 changes: 67 additions & 0 deletions scripts/build.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
import { pick } from '@hyperlane-xyz/utils';
import fs from 'fs';
import { parse } from 'yaml';
import { CoreChains } from '../chains/core';

function genJsExport(data, exportName) {
return `export const ${exportName} = ${JSON.stringify(data, null, 2)};`;
}

let chainMetadata = {};
let chainAddresses = {};

console.log('Parsing and copying chain data');
for (const file of fs.readdirSync('./chains')) {
const inDirPath = `./chains/${file}`;
const assetOutPath = `./dist/chains/${file}`;
// generated ts files to to the tmp dir so they can be compiled along with other generated code
const tsOutPath = `./tmp/chains/${file}`;
const stat = fs.statSync(`${inDirPath}`);
if (!stat.isDirectory()) continue;

const metadata = parse(fs.readFileSync(`${inDirPath}/metadata.yaml`, 'utf8'));
chainMetadata[metadata.name] = metadata;
fs.mkdirSync(`${assetOutPath}`, { recursive: true });
fs.mkdirSync(`${tsOutPath}`, { recursive: true });
fs.copyFileSync(`${inDirPath}/metadata.yaml`, `${assetOutPath}/metadata.yaml`);
fs.writeFileSync(`${assetOutPath}/metadata.json`, JSON.stringify(metadata, null, 2), 'utf8');
fs.writeFileSync(`${tsOutPath}/metadata.ts`, genJsExport(metadata, 'metadata'), 'utf8');

if (fs.existsSync(`${inDirPath}/addresses.yaml`)) {
const addresses = parse(fs.readFileSync(`${inDirPath}/addresses.yaml`, 'utf8'));
chainAddresses[metadata.name] = addresses;
fs.copyFileSync(`${inDirPath}/addresses.yaml`, `${assetOutPath}/addresses.yaml`);
fs.writeFileSync(`${assetOutPath}/addresses.json`, JSON.stringify(addresses, null, 2), 'utf8');
fs.writeFileSync(`${tsOutPath}/addresses.ts`, genJsExport(addresses, 'addresses'), 'utf8');
}

fs.copyFileSync(`${inDirPath}/logo.svg`, `${assetOutPath}/logo.svg`);
}

console.log('Assembling typescript code');
fs.mkdirSync(`./tmp`, { recursive: true });
fs.copyFileSync(`./chains/core.ts`, `./tmp/index.ts`);
fs.writeFileSync(`./tmp/chainMetadata.ts`, genJsExport(chainMetadata, 'chainMetadata'), 'utf8');
const coreChainMetadata = pick<any>(chainMetadata, CoreChains);
fs.writeFileSync(
`./tmp/coreChainMetadata.ts`,
genJsExport(coreChainMetadata, 'coreChainMetadata'),
'utf8',
);
fs.appendFileSync(`./tmp/index.ts`, `\nexport { chainMetadata } from './chainMetadata.js';\n`);
fs.appendFileSync(
`./tmp/index.ts`,
`export { coreChainMetadata } from './coreChainMetadata.js';\n`,
);
for (const name of Object.keys(chainMetadata)) {
fs.appendFileSync(
`./tmp/index.ts`,
`export { metadata as ${name} } from './chains/${name}/metadata.js';\n`,
);
if (chainAddresses[name]) {
fs.appendFileSync(
`./tmp/index.ts`,
`export { addresses as ${name}Addresses } from './chains/${name}/addresses.js';\n`,
);
}
}
File renamed without changes.
31 changes: 31 additions & 0 deletions tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
{
"compilerOptions": {
"declaration": true,
"declarationMap": false,
"esModuleInterop": true,
"forceConsistentCasingInFileNames": true,
"incremental": false,
"lib": [
"ES2015", "ES2016", "ES2017", "ES2018",
"ES2019", "ES2020","ES2021", "DOM"
],
"module": "nodenext",
"moduleResolution": "nodenext",
"noEmitOnError": false,
"noFallthroughCasesInSwitch": true,
"noImplicitAny": true,
"noImplicitReturns": true,
"noUnusedLocals": true,
"outDir": "./dist/",
"preserveSymlinks": true,
"preserveWatchOutput": true,
"pretty": false,
"resolveJsonModule": true,
"rootDir": "./tmp",
"skipLibCheck": true,
"sourceMap": false,
"strict": true,
"target": "es2022",
},
"include": ["./tmp/**/*.ts"]
}

0 comments on commit 3528af4

Please sign in to comment.