|
| 1 | +const fs = require("fs"); |
| 2 | +const path = require("path"); |
| 3 | +const vm = require("vm"); |
| 4 | + |
| 5 | +const MIN_EXPECTED_NETWORKS = 39; |
| 6 | +const SUPPORTED_THIRD_PARTY_CHAIN_IDS = [98866]; |
| 7 | +const MULTI_SEND_VERSIONS = ["1.5.0", "1.4.1", "1.3.0"]; |
| 8 | + |
| 9 | +const apiKitBundlePath = require.resolve("@safe-global/api-kit"); |
| 10 | +const apiKitBundle = fs.readFileSync(apiKitBundlePath, "utf8"); |
| 11 | + |
| 12 | +const networksMatch = apiKitBundle.match(/var networks = (\[[\s\S]*?\n\]);\nvar getNetworkShortName =/); |
| 13 | +if (!networksMatch) { |
| 14 | + throw new Error(`Unable to locate networks config in ${apiKitBundlePath}`); |
| 15 | +} |
| 16 | + |
| 17 | +const networks = vm.runInNewContext(networksMatch[1]); |
| 18 | +if (!Array.isArray(networks) || networks.length < MIN_EXPECTED_NETWORKS) { |
| 19 | + throw new Error(`Unexpected networks config shape in ${apiKitBundlePath}`); |
| 20 | +} |
| 21 | + |
| 22 | +const chainIds = []; |
| 23 | +const seenChainIds = new Set(); |
| 24 | + |
| 25 | +for (const network of networks) { |
| 26 | + const chainId = Number(network.chainId); |
| 27 | + if (!Number.isInteger(chainId)) { |
| 28 | + throw new Error(`Invalid chainId in ${apiKitBundlePath}: ${network.chainId}`); |
| 29 | + } |
| 30 | + if (seenChainIds.has(chainId)) { |
| 31 | + throw new Error(`Duplicate chainId in ${apiKitBundlePath}: ${chainId}`); |
| 32 | + } |
| 33 | + |
| 34 | + seenChainIds.add(chainId); |
| 35 | + chainIds.push(chainId); |
| 36 | +} |
| 37 | + |
| 38 | +for (const chainId of SUPPORTED_THIRD_PARTY_CHAIN_IDS) { |
| 39 | + if (seenChainIds.has(chainId)) { |
| 40 | + throw new Error(`Duplicate supported chainId: ${chainId}`); |
| 41 | + } |
| 42 | + |
| 43 | + seenChainIds.add(chainId); |
| 44 | + chainIds.push(chainId); |
| 45 | +} |
| 46 | + |
| 47 | +const safeDeploymentsRoot = path.dirname(require.resolve("@safe-global/safe-deployments/package.json")); |
| 48 | +const multiSendDeployments = MULTI_SEND_VERSIONS.map((version) => { |
| 49 | + const deploymentPath = path.join( |
| 50 | + safeDeploymentsRoot, |
| 51 | + `src/assets/v${version}/multi_send_call_only.json`, |
| 52 | + ); |
| 53 | + return JSON.parse(fs.readFileSync(deploymentPath, "utf8")); |
| 54 | +}); |
| 55 | + |
| 56 | +const counts = []; |
| 57 | +const addresses = []; |
| 58 | + |
| 59 | +for (const chainId of chainIds) { |
| 60 | + const validAddresses = new Set(); |
| 61 | + |
| 62 | + for (const deployment of multiSendDeployments) { |
| 63 | + const addressTypes = deployment.networkAddresses[String(chainId)]; |
| 64 | + if (!addressTypes) { |
| 65 | + continue; |
| 66 | + } |
| 67 | + |
| 68 | + const normalizedAddressTypes = Array.isArray(addressTypes) ? addressTypes : [addressTypes]; |
| 69 | + for (const addressType of normalizedAddressTypes) { |
| 70 | + const address = deployment.deployments[addressType]?.address; |
| 71 | + if (typeof address !== "string" || address.length === 0) { |
| 72 | + throw new Error(`Invalid ${addressType} deployment for chain ${chainId}`); |
| 73 | + } |
| 74 | + |
| 75 | + validAddresses.add(address); |
| 76 | + } |
| 77 | + } |
| 78 | + |
| 79 | + if (validAddresses.size === 0) { |
| 80 | + throw new Error(`No MultiSendCallOnly deployment found for chain ${chainId}`); |
| 81 | + } |
| 82 | + |
| 83 | + counts.push(validAddresses.size); |
| 84 | + addresses.push(...validAddresses); |
| 85 | +} |
| 86 | + |
| 87 | +process.stdout.write(JSON.stringify({ chainIds, counts, addresses })); |
0 commit comments