Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 14 additions & 10 deletions yarn-project/aztec/src/bin/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand All @@ -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();
Expand Down
17 changes: 14 additions & 3 deletions yarn-project/aztec/src/cli/chain_l2_config.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { EthAddress } from '@aztec/aztec.js';
import type { EnvVar } from '@aztec/foundation/config';

import path from 'path';
Expand Down Expand Up @@ -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) {
Expand All @@ -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);
}