diff --git a/yarn-project/aztec/src/bin/index.ts b/yarn-project/aztec/src/bin/index.ts index b032f9d64714..ee13a912e714 100644 --- a/yarn-project/aztec/src/bin/index.ts +++ b/yarn-project/aztec/src/bin/index.ts @@ -12,11 +12,10 @@ import { createConsoleLogger, createLogger } from '@aztec/foundation/log'; import { Command } from 'commander'; -import { NETWORK_FLAG, aztecStartOptions } from '../cli/aztec_start_options.js'; -import { enrichEnvironmentWithChainConfig } from '../cli/chain_l2_config.js'; +import { NETWORK_FLAG } from '../cli/aztec_start_options.js'; +import { type NetworkNames, enrichEnvironmentWithChainConfig } from '../cli/chain_l2_config.js'; import { injectAztecCommands } from '../cli/index.js'; import { getCliVersion } from '../cli/release_version.js'; -import { addOptions } from '../cli/util.js'; const userLog = createConsoleLogger(); const debugLogger = createLogger('cli'); @@ -29,15 +28,20 @@ async function main() { process.once('SIGINT', shutdown); process.once('SIGTERM', shutdown); - // We create a temp command object to detect the presence of the network option - // If it has been given then we enrich the environment with the chain config - const tempCmd = new Command('aztec').allowUnknownOption(); - addOptions(tempCmd, aztecStartOptions['NETWORK']); - tempCmd.parse(process.argv); - const networkValue = tempCmd.getOptionValue(NETWORK_FLAG); + // Intercept the setting of a network and enrich the environment with defaults for that network + let networkValue: string | undefined; + + const args = process.argv.slice(2); + const networkIndex = args.findIndex(arg => arg.startsWith(`--${NETWORK_FLAG}=`) || arg === `--${NETWORK_FLAG}`); + + if (networkIndex !== -1) { + networkValue = args[networkIndex].split('=')[1] || args[networkIndex + 1]; + } + + networkValue = networkValue || process.env.NETWORK; if (networkValue !== undefined) { - await enrichEnvironmentWithChainConfig(networkValue); + await enrichEnvironmentWithChainConfig(networkValue as NetworkNames); } const cliVersion = getCliVersion(); diff --git a/yarn-project/aztec/src/cli/chain_l2_config.ts b/yarn-project/aztec/src/cli/chain_l2_config.ts index f0cf184965f7..ad128399e8dd 100644 --- a/yarn-project/aztec/src/cli/chain_l2_config.ts +++ b/yarn-project/aztec/src/cli/chain_l2_config.ts @@ -1,3 +1,4 @@ +import { EthAddress } from '@aztec/aztec.js'; import type { EnvVar } from '@aztec/foundation/config'; import path from 'path'; @@ -92,6 +93,15 @@ function enrichVar(envVar: EnvVar, value: string) { process.env[envVar] = value; } +function enrichEthAddressVar(envVar: EnvVar, value: string) { + // EthAddress doesn't like being given empty strings + if (value === '') { + enrichVar(envVar, EthAddress.ZERO.toString()); + return; + } + enrichVar(envVar, value); +} + export async function enrichEnvironmentWithChainConfig(networkName: NetworkNames) { const config = await getL2ChainConfig(networkName); if (!config) { @@ -106,12 +116,13 @@ export async function enrichEnvironmentWithChainConfig(networkName: NetworkNames enrichVar('SPONSORED_FPC', config.sponsoredFPC.toString()); enrichVar('P2P_ENABLED', config.p2pEnabled.toString()); enrichVar('L1_CHAIN_ID', config.l1ChainId.toString()); - enrichVar('REGISTRY_CONTRACT_ADDRESS', config.registryAddress); - enrichVar('SLASH_FACTORY_CONTRACT_ADDRESS', config.slashFactoryAddress); - enrichVar('FEE_ASSET_HANDLER_CONTRACT_ADDRESS', config.feeAssetHandlerAddress); enrichVar('SEQ_MIN_TX_PER_BLOCK', config.seqMinTxsPerBlock.toString()); enrichVar('SEQ_MAX_TX_PER_BLOCK', config.seqMaxTxsPerBlock.toString()); enrichVar('DATA_DIRECTORY', path.join(process.env.HOME || '~', '.aztec', networkName, 'data')); enrichVar('PROVER_REAL_PROOFS', config.realProofs.toString()); enrichVar('PXE_PROVER_ENABLED', config.realProofs.toString()); + + enrichEthAddressVar('REGISTRY_CONTRACT_ADDRESS', config.registryAddress); + enrichEthAddressVar('SLASH_FACTORY_CONTRACT_ADDRESS', config.slashFactoryAddress); + enrichEthAddressVar('FEE_ASSET_HANDLER_CONTRACT_ADDRESS', config.feeAssetHandlerAddress); }