Skip to content

Commit e78b1e9

Browse files
committed
feat: Generated the constants file
Signed-off-by: Faisal Usmani <[email protected]>
1 parent 1d7173c commit e78b1e9

File tree

4 files changed

+549
-265
lines changed

4 files changed

+549
-265
lines changed

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,8 @@
4747
"generate-evm-artifacts": "rm -rf typechain && TYPECHAIN=ethers yarn hardhat typechain",
4848
"process-hardhat-export": "hardhat export --export-all ./cache/massExport.json && ts-node ./scripts/processHardhatExport.ts && prettier --write ./deployments/deployments.json",
4949
"pre-commit-hook": "sh scripts/preCommitHook.sh",
50-
"extract-addresses": "./script/utils/extract_foundry_addresses.sh"
50+
"extract-addresses": "./script/utils/extract_foundry_addresses.sh",
51+
"generate-constants-json": "ts-node ./scripts/generate-constants-json.ts"
5152
},
5253
"dependencies": {
5354
"@across-protocol/constants": "^3.1.69",

script/utils/ExtractDeployedFoundryAddresses.ts

Lines changed: 0 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@
1010

1111
import * as fs from "fs";
1212
import * as path from "path";
13-
import { getAddress } from "ethers/lib/utils";
1413

1514
import { PUBLIC_NETWORKS, MAINNET_CHAIN_IDs, TESTNET_CHAIN_IDs } from "../../utils/constants";
1615

@@ -192,37 +191,6 @@ function getChainName(chainId: number): string {
192191
return PUBLIC_NETWORKS[chainId]?.name || `Chain ${chainId}`;
193192
}
194193

195-
function toChecksumAddress(address: string): string {
196-
// Check if this looks like an Ethereum address (0x followed by 40 hex characters)
197-
if (/^0x[a-fA-F0-9]{40}$/.test(address)) {
198-
// Use ethers.js to get the checksummed address for valid Ethereum addresses
199-
try {
200-
return getAddress(address);
201-
} catch (error) {
202-
// If ethers validation fails, return the original address
203-
console.warn(`Warning: Invalid Ethereum address format: ${address}`);
204-
return address;
205-
}
206-
} else {
207-
// For non-Ethereum addresses (like Solana), return as-is
208-
return address;
209-
}
210-
}
211-
212-
function sanitizeContractName(name: string): string {
213-
// Remove special characters and replace with underscores
214-
let sanitized = name.replace(/[^a-zA-Z0-9]/g, "_");
215-
// Remove multiple consecutive underscores
216-
sanitized = sanitized.replace(/_+/g, "_");
217-
// Remove leading/trailing underscores
218-
sanitized = sanitized.replace(/^_+|_+$/g, "");
219-
// Ensure it starts with a letter
220-
if (sanitized && /^\d/.test(sanitized)) {
221-
sanitized = "CONTRACT_" + sanitized;
222-
}
223-
return sanitized.toUpperCase();
224-
}
225-
226194
function generateAddressesFile(broadcastFiles: BroadcastFile[], outputFile: string): void {
227195
const allContracts: AllContracts = {};
228196

script/utils/GenerateConstantsJson.ts

Lines changed: 255 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,255 @@
1+
#!/usr/bin/env ts-node
2+
3+
import * as fs from "fs";
4+
import * as path from "path";
5+
6+
// Import the constants from the TypeScript files
7+
import { CHAIN_IDs } from "../../utils/constants";
8+
import {
9+
ZERO_ADDRESS,
10+
USDC,
11+
USDCe,
12+
WETH,
13+
WGHO,
14+
QUOTE_TIME_BUFFER,
15+
FILL_DEADLINE_BUFFER,
16+
ARBITRUM_MAX_SUBMISSION_COST,
17+
AZERO_GAS_PRICE,
18+
CIRCLE_UNINITIALIZED_DOMAIN_ID,
19+
ZK_L1_GAS_TO_L2_GAS_PER_PUBDATA_LIMIT,
20+
ZK_L2_GAS_LIMIT,
21+
ZK_MAX_GASPRICE,
22+
L1_ADDRESS_MAP,
23+
OP_STACK_ADDRESS_MAP,
24+
L2_ADDRESS_MAP,
25+
CIRCLE_DOMAIN_IDs,
26+
OFT_EIDs,
27+
} from "../../deploy/consts";
28+
29+
// Helper function to convert chain IDs object to the expected format
30+
function convertChainIdsToObject(chainIds: any): { [key: string]: number } {
31+
const result: { [key: string]: number } = {};
32+
for (const [key, value] of Object.entries(chainIds)) {
33+
if (typeof value === "number") {
34+
result[key] = value;
35+
}
36+
}
37+
return result;
38+
}
39+
40+
// Helper function to extract wrapped native tokens from TOKEN_SYMBOLS_MAP
41+
function extractWrappedNativeTokens(): { [key: string]: string } {
42+
const result: { [key: string]: string } = {};
43+
44+
// Extract WETH addresses for each chain
45+
for (const [chainId, addresses] of Object.entries(WETH)) {
46+
const chainName = Object.keys(CHAIN_IDs).find(
47+
(key) => CHAIN_IDs[key as keyof typeof CHAIN_IDs] === Number(chainId)
48+
);
49+
if (chainName) {
50+
result[chainName] = addresses;
51+
}
52+
}
53+
54+
return result;
55+
}
56+
57+
// Helper function to extract USDC addresses
58+
function extractUsdcAddresses(): { [key: string]: string } {
59+
const result: { [key: string]: string } = {};
60+
61+
for (const [chainId, address] of Object.entries(USDC)) {
62+
const chainName = Object.keys(CHAIN_IDs).find(
63+
(key) => CHAIN_IDs[key as keyof typeof CHAIN_IDs] === Number(chainId)
64+
);
65+
if (chainName) {
66+
result[chainName] = address;
67+
}
68+
}
69+
70+
return result;
71+
}
72+
73+
// Helper function to extract USDCe addresses
74+
function extractUsdceAddresses(): { [key: string]: string } {
75+
const result: { [key: string]: string } = {};
76+
77+
for (const [chainId, address] of Object.entries(USDCe)) {
78+
const chainName = Object.keys(CHAIN_IDs).find(
79+
(key) => CHAIN_IDs[key as keyof typeof CHAIN_IDs] === Number(chainId)
80+
);
81+
if (chainName) {
82+
result[chainName] = address;
83+
}
84+
}
85+
86+
return result;
87+
}
88+
89+
// Helper function to extract WGHO addresses
90+
function extractWghoAddresses(): { [key: string]: string } {
91+
const result: { [key: string]: string } = {};
92+
93+
for (const [chainId, address] of Object.entries(WGHO)) {
94+
const chainName = Object.keys(CHAIN_IDs).find(
95+
(key) => CHAIN_IDs[key as keyof typeof CHAIN_IDs] === Number(chainId)
96+
);
97+
if (chainName) {
98+
result[chainName] = address;
99+
}
100+
}
101+
102+
return result;
103+
}
104+
105+
// Helper function to convert L1_ADDRESS_MAP to the expected format
106+
function convertL1Addresses(): { [key: string]: { [key: string]: string } } {
107+
const result: { [key: string]: { [key: string]: string } } = {};
108+
109+
for (const [chainId, addresses] of Object.entries(L1_ADDRESS_MAP)) {
110+
const chainName = Object.keys(CHAIN_IDs).find(
111+
(key) => CHAIN_IDs[key as keyof typeof CHAIN_IDs] === Number(chainId)
112+
);
113+
if (chainName) {
114+
result[chainName] = addresses;
115+
}
116+
}
117+
118+
return result;
119+
}
120+
121+
// Helper function to convert L2_ADDRESS_MAP to the expected format
122+
function convertL2Addresses(): { [key: string]: { [key: string]: string } } {
123+
const result: { [key: string]: { [key: string]: string } } = {};
124+
125+
for (const [chainId, addresses] of Object.entries(L2_ADDRESS_MAP)) {
126+
const chainName = Object.keys(CHAIN_IDs).find(
127+
(key) => CHAIN_IDs[key as keyof typeof CHAIN_IDs] === Number(chainId)
128+
);
129+
if (chainName) {
130+
result[chainName] = addresses;
131+
}
132+
}
133+
134+
return result;
135+
}
136+
137+
// Helper function to convert OP_STACK_ADDRESS_MAP to the expected format
138+
function convertOpStackAddresses(): { [key: string]: { [key: string]: { [key: string]: string } } } {
139+
const result: { [key: string]: { [key: string]: { [key: string]: string } } } = {};
140+
141+
for (const [hubChainId, spokeChains] of Object.entries(OP_STACK_ADDRESS_MAP)) {
142+
const hubChainName = Object.keys(CHAIN_IDs).find(
143+
(key) => CHAIN_IDs[key as keyof typeof CHAIN_IDs] === Number(hubChainId)
144+
);
145+
if (hubChainName) {
146+
result[hubChainName] = {};
147+
for (const [spokeChainId, addresses] of Object.entries(spokeChains)) {
148+
const spokeChainName = Object.keys(CHAIN_IDs).find(
149+
(key) => CHAIN_IDs[key as keyof typeof CHAIN_IDs] === Number(spokeChainId)
150+
);
151+
if (spokeChainName) {
152+
result[hubChainName][spokeChainName] = addresses;
153+
}
154+
}
155+
}
156+
}
157+
158+
return result;
159+
}
160+
161+
// Helper function to convert OFT_EIDs to the expected format
162+
function convertOftEids(): { [key: string]: number } {
163+
const result: { [key: string]: number } = {};
164+
165+
for (const [chainId, eid] of OFT_EIDs.entries()) {
166+
const chainName = Object.keys(CHAIN_IDs).find(
167+
(key) => CHAIN_IDs[key as keyof typeof CHAIN_IDs] === Number(chainId)
168+
);
169+
if (chainName) {
170+
result[chainName] = eid;
171+
}
172+
}
173+
174+
return result;
175+
}
176+
177+
// Helper function to convert CIRCLE_DOMAIN_IDs to the expected format
178+
function convertCircleDomainIds(): { [key: string]: number } {
179+
const result: { [key: string]: number } = {};
180+
181+
for (const [chainId, domainId] of Object.entries(CIRCLE_DOMAIN_IDs)) {
182+
const chainName = Object.keys(CHAIN_IDs).find(
183+
(key) => CHAIN_IDs[key as keyof typeof CHAIN_IDs] === Number(chainId)
184+
);
185+
if (chainName) {
186+
result[chainName] = domainId;
187+
}
188+
}
189+
190+
return result;
191+
}
192+
193+
// Generate the constants.json structure
194+
function generateConstantsJson() {
195+
const constants = {
196+
chainIds: convertChainIdsToObject(CHAIN_IDs),
197+
oftEids: convertOftEids(),
198+
wrappedNativeTokens: extractWrappedNativeTokens(),
199+
l2Addresses: convertL2Addresses(),
200+
l1Addresses: convertL1Addresses(),
201+
opStackAddresses: convertOpStackAddresses(),
202+
circleDomainIds: convertCircleDomainIds(),
203+
timeConstants: {
204+
QUOTE_TIME_BUFFER: QUOTE_TIME_BUFFER,
205+
FILL_DEADLINE_BUFFER: FILL_DEADLINE_BUFFER,
206+
},
207+
usdcAddresses: extractUsdcAddresses(),
208+
usdceAddresses: extractUsdceAddresses(),
209+
wghoAddresses: extractWghoAddresses(),
210+
otherConstants: {
211+
ZERO_ADDRESS: ZERO_ADDRESS,
212+
ARBITRUM_MAX_SUBMISSION_COST: ARBITRUM_MAX_SUBMISSION_COST,
213+
AZERO_GAS_PRICE: AZERO_GAS_PRICE,
214+
CIRCLE_UNINITIALIZED_DOMAIN_ID: CIRCLE_UNINITIALIZED_DOMAIN_ID,
215+
ZK_L1_GAS_TO_L2_GAS_PER_PUBDATA_LIMIT: ZK_L1_GAS_TO_L2_GAS_PER_PUBDATA_LIMIT,
216+
ZK_L2_GAS_LIMIT: ZK_L2_GAS_LIMIT,
217+
ZK_MAX_GASPRICE: ZK_MAX_GASPRICE,
218+
},
219+
};
220+
221+
return constants;
222+
}
223+
224+
// Main function
225+
function main() {
226+
try {
227+
console.log("Generating constants.json...");
228+
229+
const constants = generateConstantsJson();
230+
231+
// Write to script/utils/constants.json
232+
const outputPath = path.join(__dirname, "../script/utils/constants.json");
233+
const outputDir = path.dirname(outputPath);
234+
235+
// Ensure the directory exists
236+
if (!fs.existsSync(outputDir)) {
237+
fs.mkdirSync(outputDir, { recursive: true });
238+
}
239+
240+
fs.writeFileSync(outputPath, JSON.stringify(constants, null, 2));
241+
242+
console.log(`✅ Successfully generated constants.json at ${outputPath}`);
243+
console.log(`📊 Generated ${Object.keys(constants.chainIds).length} chain IDs`);
244+
console.log(`📊 Generated ${Object.keys(constants.l1Addresses).length} L1 address mappings`);
245+
console.log(`📊 Generated ${Object.keys(constants.l2Addresses).length} L2 address mappings`);
246+
} catch (error) {
247+
console.error("❌ Error generating constants.json:", error);
248+
process.exit(1);
249+
}
250+
}
251+
252+
// Run the script
253+
if (require.main === module) {
254+
main();
255+
}

0 commit comments

Comments
 (0)