Skip to content

Commit 06b3dce

Browse files
authored
improve: export deployments (#1076)
* export deployments mapping for consumer projects Signed-off-by: Ihor Farion <[email protected]> * change to DEPLOYMENTS Signed-off-by: Ihor Farion <[email protected]> --------- Signed-off-by: Ihor Farion <[email protected]>
1 parent f2415a2 commit 06b3dce

File tree

2 files changed

+36
-13
lines changed

2 files changed

+36
-13
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@across-protocol/contracts",
3-
"version": "4.1.2",
3+
"version": "4.1.3",
44
"author": "UMA Team",
55
"license": "AGPL-3.0-only",
66
"repository": {

src/DeploymentUtils.ts

Lines changed: 35 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,40 +1,63 @@
11
import * as deployments_ from "../deployments/deployments.json";
22

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;
77

8-
// Returns the deployed address of any contract on any network.
8+
/**
9+
* Returns the deployed address of any contract on any network.
10+
*/
911
export function getDeployedAddress(
1012
contractName: string,
1113
networkId: number | string,
1214
throwOnError = true
1315
): string | undefined {
14-
const address = deployments[networkId.toString()]?.[contractName]?.address;
16+
const address = DEPLOYMENTS[networkId.toString()]?.[contractName]?.address;
1517
if (!address && throwOnError) {
1618
throw new Error(`Contract ${contractName} not found on ${networkId} in deployments.json`);
1719
}
1820

1921
return address;
2022
}
2123

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+
*/
2344
export function getDeployedBlockNumber(contractName: string, networkId: number): number {
2445
try {
25-
return deployments[networkId.toString()][contractName].blockNumber;
46+
return DEPLOYMENTS[networkId.toString()][contractName].blockNumber;
2647
} catch (_) {
2748
throw new Error(`Contract ${contractName} not found on ${networkId} in deployments.json`);
2849
}
2950
}
3051

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+
*/
3255
export function getContractInfoFromAddress(contractAddress: string): { chainId: Number; contractName: string } {
3356
const returnValue: { chainId: number; contractName: string }[] = [];
3457

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)
3861
returnValue.push({ chainId: Number(_chainId), contractName: _contractName });
3962
})
4063
);

0 commit comments

Comments
 (0)