Skip to content

Commit 3c1cd19

Browse files
authored
refactor(core): reduce cognitive complexity below biome limit (#22)
Extract helpers to bring two functions under the max-complexity 10 limit (no behavior change): - sdk-chain.ts deployRollupViaSdk: extract buildRollupParams + buildChainDeployment - chain-steps.ts createL3Steps: lift deploy-l3-rollup step body into module-scope deployL3Rollup pnpm validate now passes (lint + build + typecheck + 176 tests).
1 parent ac10d17 commit 3c1cd19

2 files changed

Lines changed: 122 additions & 99 deletions

File tree

packages/core/src/init/chain-steps.ts

Lines changed: 74 additions & 68 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ import { patchGeneratedL2NodeConfig, patchGeneratedL3NodeConfig } from "../node-
1515
import { inboxAbi, publicClient, rollupAbi, walletClient } from "../rpc.js";
1616
import { startL1Container } from "../runtime.js";
1717
import { deployRollupViaSdk, prepareNodeConfigFromDeployment } from "../sdk-chain.js";
18+
import type { InitState } from "../state.js";
1819
import { markStepDone } from "../state.js";
1920
import {
2021
deployL1L2TokenBridge,
@@ -580,81 +581,86 @@ async function depositFeeTokenToL3Inbox(nativeToken: Address, inbox: Address): P
580581
await l2Pub.waitForTransactionReceipt({ hash: depositHash });
581582
}
582583

584+
async function deployL3Rollup(
585+
state: InitState,
586+
runtime: InitRuntime,
587+
feeTokenDecimals: number | undefined,
588+
isV21: boolean,
589+
): Promise<InitState> {
590+
await fundL3DeployerAccounts();
591+
writeChainConfig(runtime.configDir, "l3_chain_config.json", {
592+
chainId: 333333,
593+
owner: accounts.l3owner.address,
594+
...(isV21 ? { dataAvailabilityCommittee: true } : {}),
595+
});
596+
await applyGasEstimationWorkaround();
597+
598+
// If custom fee token is requested, deploy an ERC20 (+ pricer for v3.2) on L2
599+
const { feeTokenAddress, feeTokenPricerAddress } = await deployCustomFeeToken(
600+
feeTokenDecimals,
601+
!isV21,
602+
);
603+
604+
const rollupCreatorDeployment = await deployRollupCreatorViaDocker(runtime, {
605+
hostParentRpc: L2_RPC,
606+
dockerParentRpc: L2_RPC_DOCKER,
607+
deployerKey: accounts.l3owner.privateKey,
608+
maxDataSize: "104857",
609+
...(isV21
610+
? {
611+
image: "nitro-testnode-contract-deployer-v2.1:latest",
612+
dockerfile: "docker/contract-deployer-v2.1.Dockerfile",
613+
}
614+
: {}),
615+
});
616+
await deployRollupViaSdk({
617+
chainConfigPath: resolve(runtime.configDir, "l3_chain_config.json"),
618+
chainId: 333333,
619+
chainName: "orbit-dev-test",
620+
parentChainId: 412346,
621+
parentChainIsArbitrum: true,
622+
parentRpcUrl: L2_RPC,
623+
ownerAddress: accounts.l3owner.address,
624+
ownerKey: accounts.l3owner.privateKey,
625+
batchPosterAddress: accounts.l3sequencer.address,
626+
batchPosterKey: accounts.l3sequencer.privateKey,
627+
validatorAddress: accounts.l3owner.address,
628+
validatorKey: accounts.l3owner.privateKey,
629+
maxDataSize: 104857n,
630+
wasmModuleRoot: WASM_MODULE_ROOT as `0x${string}`,
631+
deploymentOutputPath: resolve(runtime.configDir, "l3_deployment.json"),
632+
chainInfoOutputPath: resolve(runtime.configDir, "l3_chain_info.json"),
633+
rawNodeConfigOutputPath: resolve(runtime.configDir, "l3-nodeConfig.raw.json"),
634+
rollupCreatorAddress: rollupCreatorDeployment.rollupCreator,
635+
stakeToken: rollupCreatorDeployment.stakeToken,
636+
nitroContractsVersion: isV21 ? "v2.1" : "v3.2",
637+
...(feeTokenAddress ? { nativeToken: feeTokenAddress as `0x${string}` } : {}),
638+
...(feeTokenPricerAddress ? { feeTokenPricer: feeTokenPricerAddress as `0x${string}` } : {}),
639+
});
640+
641+
copyConfigFile(runtime, "l3_deployment.json", "l3deployment.json");
642+
const deployment = readDeployment(runtime, "l3_deployment.json");
643+
return markStepDone(state, "deploy-l3-rollup", {
644+
rollup: deployment["rollup"],
645+
inbox: deployment["inbox"],
646+
bridge: deployment["bridge"],
647+
sequencerInbox: deployment["sequencer-inbox"],
648+
upgradeExecutor: deployment["upgrade-executor"],
649+
validatorWalletCreator: deployment["validator-wallet-creator"] ?? ZERO_ADDRESS,
650+
stakeToken: deployment["stake-token"] ?? ZERO_ADDRESS,
651+
...(feeTokenAddress ? { feeTokenAddress } : {}),
652+
...(feeTokenDecimals !== undefined ? { feeTokenDecimals } : {}),
653+
});
654+
}
655+
583656
function createL3Steps(
584657
runtime: InitRuntime,
585658
feeTokenDecimals?: number,
586659
nitroContractsVersion?: string,
587660
): Record<string, StepRunner> {
588661
const isV21 = nitroContractsVersion === "v2.1";
589662
return {
590-
"deploy-l3-rollup": async (state) => {
591-
await fundL3DeployerAccounts();
592-
writeChainConfig(runtime.configDir, "l3_chain_config.json", {
593-
chainId: 333333,
594-
owner: accounts.l3owner.address,
595-
...(isV21 ? { dataAvailabilityCommittee: true } : {}),
596-
});
597-
await applyGasEstimationWorkaround();
598-
599-
// If custom fee token is requested, deploy an ERC20 (+ pricer for v3.2) on L2
600-
const { feeTokenAddress, feeTokenPricerAddress } = await deployCustomFeeToken(
601-
feeTokenDecimals,
602-
!isV21,
603-
);
604-
605-
const rollupCreatorDeployment = await deployRollupCreatorViaDocker(runtime, {
606-
hostParentRpc: L2_RPC,
607-
dockerParentRpc: L2_RPC_DOCKER,
608-
deployerKey: accounts.l3owner.privateKey,
609-
maxDataSize: "104857",
610-
...(isV21
611-
? {
612-
image: "nitro-testnode-contract-deployer-v2.1:latest",
613-
dockerfile: "docker/contract-deployer-v2.1.Dockerfile",
614-
}
615-
: {}),
616-
});
617-
await deployRollupViaSdk({
618-
chainConfigPath: resolve(runtime.configDir, "l3_chain_config.json"),
619-
chainId: 333333,
620-
chainName: "orbit-dev-test",
621-
parentChainId: 412346,
622-
parentChainIsArbitrum: true,
623-
parentRpcUrl: L2_RPC,
624-
ownerAddress: accounts.l3owner.address,
625-
ownerKey: accounts.l3owner.privateKey,
626-
batchPosterAddress: accounts.l3sequencer.address,
627-
batchPosterKey: accounts.l3sequencer.privateKey,
628-
validatorAddress: accounts.l3owner.address,
629-
validatorKey: accounts.l3owner.privateKey,
630-
maxDataSize: 104857n,
631-
wasmModuleRoot: WASM_MODULE_ROOT as `0x${string}`,
632-
deploymentOutputPath: resolve(runtime.configDir, "l3_deployment.json"),
633-
chainInfoOutputPath: resolve(runtime.configDir, "l3_chain_info.json"),
634-
rawNodeConfigOutputPath: resolve(runtime.configDir, "l3-nodeConfig.raw.json"),
635-
rollupCreatorAddress: rollupCreatorDeployment.rollupCreator,
636-
stakeToken: rollupCreatorDeployment.stakeToken,
637-
nitroContractsVersion: isV21 ? "v2.1" : "v3.2",
638-
...(feeTokenAddress ? { nativeToken: feeTokenAddress as `0x${string}` } : {}),
639-
...(feeTokenPricerAddress
640-
? { feeTokenPricer: feeTokenPricerAddress as `0x${string}` }
641-
: {}),
642-
});
643-
644-
copyConfigFile(runtime, "l3_deployment.json", "l3deployment.json");
645-
const deployment = readDeployment(runtime, "l3_deployment.json");
646-
return markStepDone(state, "deploy-l3-rollup", {
647-
rollup: deployment["rollup"],
648-
inbox: deployment["inbox"],
649-
bridge: deployment["bridge"],
650-
sequencerInbox: deployment["sequencer-inbox"],
651-
upgradeExecutor: deployment["upgrade-executor"],
652-
validatorWalletCreator: deployment["validator-wallet-creator"] ?? ZERO_ADDRESS,
653-
stakeToken: deployment["stake-token"] ?? ZERO_ADDRESS,
654-
...(feeTokenAddress ? { feeTokenAddress } : {}),
655-
...(feeTokenDecimals !== undefined ? { feeTokenDecimals } : {}),
656-
});
657-
},
663+
"deploy-l3-rollup": (state) => deployL3Rollup(state, runtime, feeTokenDecimals, isV21),
658664
"generate-l3-config": async (state) => {
659665
const rollupData = state.steps["deploy-l3-rollup"]?.data;
660666
if (!rollupData) {

packages/core/src/sdk-chain.ts

Lines changed: 48 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,52 @@ export function prepareNodeConfigFromDeployment(input: {
116116
});
117117
}
118118

119+
type DeploymentConfig = ReturnType<typeof createRollupPrepareDeploymentParamsConfig>;
120+
121+
function buildRollupParams(
122+
version: "v2.1" | "v3.2",
123+
deploymentConfig: DeploymentConfig,
124+
params: DeployRollupViaSdkParams,
125+
): CreateRollupParams<"v2.1"> | CreateRollupParams<"v3.2"> {
126+
// feeTokenPricer is a top-level createRollup param (read as params.feeTokenPricer
127+
// by the SDK's custom-gas validation), not a deployment-config field. v2.1's
128+
// CreateRollupParams has no feeTokenPricer field, so omit it entirely there.
129+
const baseRollupParams = {
130+
config: deploymentConfig,
131+
batchPosters: [params.batchPosterAddress],
132+
validators: [params.validatorAddress],
133+
maxDataSize: Number(params.maxDataSize),
134+
...(params.nativeToken ? { nativeToken: params.nativeToken } : {}),
135+
};
136+
if (version === "v2.1") {
137+
return baseRollupParams as CreateRollupParams<"v2.1">;
138+
}
139+
return {
140+
...baseRollupParams,
141+
...(params.feeTokenPricer ? { feeTokenPricer: params.feeTokenPricer } : {}),
142+
} as CreateRollupParams<"v3.2">;
143+
}
144+
145+
function buildChainDeployment(
146+
result: CreateRollupResults,
147+
stakeToken: Address,
148+
): CreateChainDeployment {
149+
return {
150+
bridge: result.coreContracts.bridge,
151+
inbox: result.coreContracts.inbox,
152+
"sequencer-inbox": result.coreContracts.sequencerInbox,
153+
"deployed-at": result.coreContracts.deployedAtBlockNumber,
154+
rollup: result.coreContracts.rollup,
155+
"native-token": result.coreContracts.nativeToken,
156+
"upgrade-executor": result.coreContracts.upgradeExecutor,
157+
...(result.coreContracts.validatorUtils
158+
? { "validator-utils": result.coreContracts.validatorUtils }
159+
: {}),
160+
"validator-wallet-creator": result.coreContracts.validatorWalletCreator,
161+
"stake-token": stakeToken,
162+
};
163+
}
164+
119165
export async function deployRollupViaSdk(params: DeployRollupViaSdkParams): Promise<void> {
120166
const chainConfig = JSON.parse(readFileSync(params.chainConfigPath, "utf-8")) as Record<
121167
string,
@@ -136,23 +182,7 @@ export async function deployRollupViaSdk(params: DeployRollupViaSdkParams): Prom
136182
version,
137183
);
138184

139-
// feeTokenPricer is a top-level createRollup param (read as params.feeTokenPricer
140-
// by the SDK's custom-gas validation), not a deployment-config field. v2.1's
141-
// CreateRollupParams has no feeTokenPricer field, so omit it entirely there.
142-
const baseRollupParams = {
143-
config: deploymentConfig,
144-
batchPosters: [params.batchPosterAddress],
145-
validators: [params.validatorAddress],
146-
maxDataSize: Number(params.maxDataSize),
147-
...(params.nativeToken ? { nativeToken: params.nativeToken } : {}),
148-
};
149-
const rollupParams: CreateRollupParams<"v2.1"> | CreateRollupParams<"v3.2"> =
150-
version === "v2.1"
151-
? (baseRollupParams as CreateRollupParams<"v2.1">)
152-
: ({
153-
...baseRollupParams,
154-
...(params.feeTokenPricer ? { feeTokenPricer: params.feeTokenPricer } : {}),
155-
} as CreateRollupParams<"v3.2">);
185+
const rollupParams = buildRollupParams(version, deploymentConfig, params);
156186

157187
const result = params.rollupCreatorAddress
158188
? await createRollupWithCreatorOverride({
@@ -173,20 +203,7 @@ export async function deployRollupViaSdk(params: DeployRollupViaSdkParams): Prom
173203
parentChainPublicClient,
174204
(deploymentConfig as { stakeToken?: Address }).stakeToken as Address | undefined,
175205
);
176-
const deployment: CreateChainDeployment = {
177-
bridge: result.coreContracts.bridge,
178-
inbox: result.coreContracts.inbox,
179-
"sequencer-inbox": result.coreContracts.sequencerInbox,
180-
"deployed-at": result.coreContracts.deployedAtBlockNumber,
181-
rollup: result.coreContracts.rollup,
182-
"native-token": result.coreContracts.nativeToken,
183-
"upgrade-executor": result.coreContracts.upgradeExecutor,
184-
...(result.coreContracts.validatorUtils
185-
? { "validator-utils": result.coreContracts.validatorUtils }
186-
: {}),
187-
"validator-wallet-creator": result.coreContracts.validatorWalletCreator,
188-
"stake-token": stakeToken,
189-
};
206+
const deployment = buildChainDeployment(result, stakeToken);
190207
const nodeConfig: NodeConfig = prepareNodeConfig({
191208
chainName: params.chainName,
192209
chainConfig: chainConfig as PrepareNodeConfigParams["chainConfig"],

0 commit comments

Comments
 (0)