|
| 1 | +import * as fs from 'node:fs'; |
| 2 | +import { join, parse as parsePath } from 'node:path'; |
| 3 | + |
| 4 | +import type { AddressLike } from 'ethers'; |
| 5 | + |
| 6 | +export interface DeploymentAddressEntry { |
| 7 | + address: AddressLike; |
| 8 | + constructorArgs?: any[]; |
| 9 | + constructorArgTypes?: string[]; |
| 10 | +} |
| 11 | + |
| 12 | +export interface ChainDeploymentAddresses { |
| 13 | + [chainId: string]: DeploymentAddressEntry; |
| 14 | +} |
| 15 | + |
| 16 | +export interface AllDeploymentAddresses { |
| 17 | + [deploymentName: string]: ChainDeploymentAddresses; |
| 18 | +} |
| 19 | + |
| 20 | +/** |
| 21 | + * Reads deployment artifacts from the `deployments` directory (specific to data-feed-proxy-combinators) |
| 22 | + * and aggregates contract addresses, constructor arguments, and types by deployment name and chain ID. |
| 23 | + * @returns A stringified JSON object of deployment addresses. |
| 24 | + */ |
| 25 | +export function getDeploymentAddresses(): string { |
| 26 | + const allAddresses: AllDeploymentAddresses = {}; |
| 27 | + const deploymentsRoot = join(__dirname, '..', '..', 'deployments'); |
| 28 | + |
| 29 | + if (!fs.existsSync(deploymentsRoot)) { |
| 30 | + throw new Error(`Deployments directory not found at ${deploymentsRoot}.`); |
| 31 | + } |
| 32 | + |
| 33 | + const networkDirs = fs |
| 34 | + .readdirSync(deploymentsRoot, { withFileTypes: true }) |
| 35 | + .filter((dirent) => dirent.isDirectory() && dirent.name !== 'localhost' && dirent.name !== 'hardhat') |
| 36 | + .map((dirent) => dirent.name); |
| 37 | + |
| 38 | + for (const networkName of networkDirs) { |
| 39 | + const networkPath = join(deploymentsRoot, networkName); |
| 40 | + const chainIdFilePath = join(networkPath, '.chainId'); |
| 41 | + if (!fs.existsSync(chainIdFilePath)) continue; |
| 42 | + const chainId = fs.readFileSync(chainIdFilePath, 'utf8').trim(); |
| 43 | + |
| 44 | + const deploymentFiles = fs |
| 45 | + .readdirSync(networkPath, { withFileTypes: true }) |
| 46 | + .filter((dirent) => dirent.isFile() && dirent.name.endsWith('.json')) |
| 47 | + .map((dirent) => dirent.name); |
| 48 | + |
| 49 | + for (const deploymentFile of deploymentFiles) { |
| 50 | + const deploymentName = parsePath(deploymentFile).name; |
| 51 | + const artifact = JSON.parse(fs.readFileSync(join(networkPath, deploymentFile), 'utf8')); |
| 52 | + const constructorEntry = artifact.abi.find((item: any) => item.type === 'constructor'); |
| 53 | + const constructorArgTypes = constructorEntry?.inputs?.map((input: any) => input.type) || []; |
| 54 | + |
| 55 | + if (!allAddresses[deploymentName]) allAddresses[deploymentName] = {}; |
| 56 | + allAddresses[deploymentName][chainId] = { |
| 57 | + address: artifact.address, |
| 58 | + constructorArgs: artifact.args || [], |
| 59 | + constructorArgTypes, |
| 60 | + }; |
| 61 | + } |
| 62 | + } |
| 63 | + return `${JSON.stringify(allAddresses, null, 2)}\n`; |
| 64 | +} |
0 commit comments