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