|
1 | 1 | import * as deployments_ from "../deployments/deployments.json"; |
2 | 2 |
|
3 | | -interface DeploymentExport { |
4 | | - [chainId: string]: { [contractName: string]: { address: string; blockNumber: number } }; |
5 | | -} |
6 | | -const deployments: DeploymentExport = deployments_ as any; |
| 3 | +/** Mapping: chainId -> contractName -> { address, blockNumber }. */ |
| 4 | +export type Deployments = Record<string, Record<string, { address: string; blockNumber: number }>>; |
| 5 | + |
| 6 | +export const DEPLOYMENTS: Readonly<Deployments> = deployments_ as Deployments; |
7 | 7 |
|
8 | | -// Returns the deployed address of any contract on any network. |
| 8 | +/** |
| 9 | + * Returns the deployed address of any contract on any network. |
| 10 | + */ |
9 | 11 | export function getDeployedAddress( |
10 | 12 | contractName: string, |
11 | 13 | networkId: number | string, |
12 | 14 | throwOnError = true |
13 | 15 | ): string | undefined { |
14 | | - const address = deployments[networkId.toString()]?.[contractName]?.address; |
| 16 | + const address = DEPLOYMENTS[networkId.toString()]?.[contractName]?.address; |
15 | 17 | if (!address && throwOnError) { |
16 | 18 | throw new Error(`Contract ${contractName} not found on ${networkId} in deployments.json`); |
17 | 19 | } |
18 | 20 |
|
19 | 21 | return address; |
20 | 22 | } |
21 | 23 |
|
22 | | -// Returns the deployment block number of any contract on any network. |
| 24 | +/** |
| 25 | + * Returns all active deployments for a given contract name across all chains. |
| 26 | + * Each result contains chainId, address, and blockNumber. |
| 27 | + */ |
| 28 | +export function getAllDeployedAddresses( |
| 29 | + contractName: string |
| 30 | +): Array<{ chainId: number; address: string; blockNumber: number }> { |
| 31 | + const results: Array<{ chainId: number; address: string; blockNumber: number }> = []; |
| 32 | + Object.keys(DEPLOYMENTS).forEach((_chainId) => { |
| 33 | + const info = DEPLOYMENTS[_chainId]?.[contractName]; |
| 34 | + if (info?.address) { |
| 35 | + results.push({ chainId: Number(_chainId), address: info.address, blockNumber: info.blockNumber }); |
| 36 | + } |
| 37 | + }); |
| 38 | + return results; |
| 39 | +} |
| 40 | + |
| 41 | +/** |
| 42 | + * Returns the deployment block number of any contract on any network. |
| 43 | + */ |
23 | 44 | export function getDeployedBlockNumber(contractName: string, networkId: number): number { |
24 | 45 | try { |
25 | | - return deployments[networkId.toString()][contractName].blockNumber; |
| 46 | + return DEPLOYMENTS[networkId.toString()][contractName].blockNumber; |
26 | 47 | } catch (_) { |
27 | 48 | throw new Error(`Contract ${contractName} not found on ${networkId} in deployments.json`); |
28 | 49 | } |
29 | 50 | } |
30 | 51 |
|
31 | | -// Returns the chainId and contract name for a given contract address. |
| 52 | +/** |
| 53 | + * Returns the chainId and contract name for a given contract address. |
| 54 | + */ |
32 | 55 | export function getContractInfoFromAddress(contractAddress: string): { chainId: Number; contractName: string } { |
33 | 56 | const returnValue: { chainId: number; contractName: string }[] = []; |
34 | 57 |
|
35 | | - Object.keys(deployments).forEach((_chainId) => |
36 | | - Object.keys(deployments[_chainId]).forEach((_contractName) => { |
37 | | - if (deployments[_chainId][_contractName].address === contractAddress) |
| 58 | + Object.keys(DEPLOYMENTS).forEach((_chainId) => |
| 59 | + Object.keys(DEPLOYMENTS[_chainId]).forEach((_contractName) => { |
| 60 | + if (DEPLOYMENTS[_chainId][_contractName].address === contractAddress) |
38 | 61 | returnValue.push({ chainId: Number(_chainId), contractName: _contractName }); |
39 | 62 | }) |
40 | 63 | ); |
|
0 commit comments