forked from across-protocol/contracts
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDeploymentUtils.ts
38 lines (34 loc) · 1.69 KB
/
DeploymentUtils.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
import * as deployments_ from "../deployments/deployments.json";
interface DeploymentExport {
[chainId: string]: { [contractName: string]: { address: string; blockNumber: number } };
}
const deployments: DeploymentExport = deployments_ as any;
// Returns the deployed address of any contract on any network.
export function getDeployedAddress(contractName: string, networkId: number): string {
try {
return deployments[networkId.toString()][contractName].address;
} catch (_) {
throw new Error(`Contract ${contractName} not found on ${networkId} in deployments.json`);
}
}
// Returns the deployment block number of any contract on any network.
export function getDeployedBlockNumber(contractName: string, networkId: number): number {
try {
return deployments[networkId.toString()][contractName].blockNumber;
} catch (_) {
throw new Error(`Contract ${contractName} not found on ${networkId} in deployments.json`);
}
}
// Returns the chainId and contract name for a given contract address.
export function getContractInfoFromAddress(contractAddress: string): { chainId: Number; contractName: string } {
const returnValue: { chainId: number; contractName: string }[] = [];
Object.keys(deployments).forEach((_chainId) =>
Object.keys(deployments[_chainId]).forEach((_contractName) => {
if (deployments[_chainId][_contractName].address === contractAddress)
returnValue.push({ chainId: Number(_chainId), contractName: _contractName });
})
);
if (returnValue.length === 0) throw new Error(`Contract ${contractAddress} not found in deployments.json`);
if (returnValue.length > 1) throw new Error(`Multiple deployments found for ${contractAddress}`);
return returnValue[0];
}