diff --git a/packages/api/src/app.ts b/packages/api/src/app.ts index dfedc702..23e08e16 100644 --- a/packages/api/src/app.ts +++ b/packages/api/src/app.ts @@ -1,4 +1,4 @@ -import { NETWORK_NAME_TO_CHAIN_ID, NetworkName } from "types/lib"; +import { NetworkName } from "types/lib"; import { IDbController } from "types/lib"; import { Executor } from "executor/lib/executor"; import { Config } from "executor/lib/config"; @@ -58,24 +58,21 @@ export class ApiApp { private setupRoutes(): void { if (this.testingMode) { - this.server.post("/rpc/", this.setupRouteFor("dev")); + this.server.post("/rpc/", this.setupRouteFor("dev", 1337)); logger.info("Setup route for dev: /rpc/"); return; } - const networkNames: NetworkName[] = this.config.supportedNetworks; - for (const network of networkNames) { - const chainId: number | undefined = NETWORK_NAME_TO_CHAIN_ID[network]; - if (chainId == undefined) { - continue; - } - this.server.post(`/${chainId}`, this.setupRouteFor(network)); + const networkNames = this.config.supportedNetworks; + for (const [network, chainId] of Object.entries(networkNames)) { + this.server.post(`/${chainId}`, this.setupRouteFor(network, chainId)); logger.info(`Setup route for ${network}: /${chainId}/`); } } - private setupRouteFor(network: NetworkName): RouteHandler { + private setupRouteFor(network: NetworkName, chainId: number): RouteHandler { const relayer = new Executor({ + chainId, network, db: this.db, config: this.config, diff --git a/packages/cli/src/cmds/start/handler.ts b/packages/cli/src/cmds/start/handler.ts index dc263e37..04666e43 100644 --- a/packages/cli/src/cmds/start/handler.ts +++ b/packages/cli/src/cmds/start/handler.ts @@ -41,6 +41,8 @@ export async function bundlerHandler( }); } + await config.fetchChainIds(); + if (unsafeMode) { logger.warn( "WARNING: Running in unsafe mode, skips opcode check and stake check" diff --git a/packages/executor/src/config.ts b/packages/executor/src/config.ts index ff038359..d783f742 100644 --- a/packages/executor/src/config.ts +++ b/packages/executor/src/config.ts @@ -1,5 +1,6 @@ // TODO: create a new package "config" instead of this file and refactor import { Wallet, providers, utils } from "ethers"; +import { NetworkName } from "types/lib"; import { BundlerConfig, ConfigOptions, @@ -8,7 +9,9 @@ import { } from "./interfaces"; export class Config { - supportedNetworks: string[]; + supportedNetworks: { + [networkName in NetworkName]: number; + }; networks: Networks; testingMode: boolean; unsafeMode: boolean; @@ -64,20 +67,35 @@ export class Config { return config; } - private parseSupportedNetworks(): string[] { + async fetchChainIds(): Promise { + for (const networkName of Object.keys(this.supportedNetworks)) { + const provider = this.getNetworkProvider(networkName); + if (!provider) { + throw new Error(`No provider for ${networkName}`); + } + const network = await provider.getNetwork(); + this.supportedNetworks[networkName] = network.chainId; + } + } + + private parseSupportedNetworks(): { [name: string]: number } { if (this.testingMode) { - return ["dev"]; + return { dev: 1337 }; } const envNetworks = NETWORKS_ENV(); - if (envNetworks) { - return envNetworks; + let networkNames = envNetworks; + if (!networkNames) { + networkNames = Object.keys(this.config.networks); } - return Object.keys(this.config.networks); + return networkNames.reduce((networks, networkName) => { + networks[networkName] = 0; + return networks; + }, {} as { [name: string]: number }); } private parseNetworkConfigs(): Networks { const networks: Networks = {}; - for (const network of this.supportedNetworks) { + for (const network of Object.keys(this.supportedNetworks)) { const config = this.getDefaultNetworkConfig(network); networks[network] = { ...config, diff --git a/packages/executor/src/executor.ts b/packages/executor/src/executor.ts index a6ca2d63..361e815b 100644 --- a/packages/executor/src/executor.ts +++ b/packages/executor/src/executor.ts @@ -57,10 +57,9 @@ export class Executor { this.networkName ) as providers.JsonRpcProvider; - const chainId = this.provider.network.chainId; this.reputationService = new ReputationService( this.db, - chainId, + this.chainId, this.networkConfig.minInclusionDenominator, this.networkConfig.throttlingSlack, this.networkConfig.banSlack, @@ -77,7 +76,7 @@ export class Executor { ); this.mempoolService = new MempoolService( this.db, - chainId, + this.chainId, this.reputationService ); this.bundlingService = new BundlingService(