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
1 change: 0 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,6 @@ pnpm validate # Full validation (lint + build + typecheck + test)

- [Anvil](https://book.getfoundry.sh/anvil/) (Foundry) for L1
- [Nitro](https://github.com/OffchainLabs/nitro) node Docker images for L2/L3
- `arbitrum` CLI for rollup deployment and config generation
- Docker for running Nitro nodes

## License
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
"vitest": "^2.1.0"
},
"dependencies": {
"@arbitrum/chain-sdk": "0.26.0",
"incur": "^0.2.2",
"viem": "^2.47.5",
"zod": "^3.24.0"
Expand Down
1,778 changes: 1,760 additions & 18 deletions pnpm-lock.yaml

Large diffs are not rendered by default.

78 changes: 27 additions & 51 deletions src/commands/init.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import { accounts } from "../accounts.js";
import { writeChainConfig } from "../chain-config.js";
import { clampDepositAmount } from "../deposit-amount.js";
import { composeRestart, composeUp, waitForRpc } from "../docker.js";
import { arbitrum, execOrThrow } from "../exec.js";
import { execOrThrow } from "../exec.js";
import { deployTestErc20 } from "../fee-token.js";
import { ZERO_ADDRESS } from "../init-helpers.js";
import { getL3ParentChainFundingPlan } from "../l3-parent-chain-funding.js";
Expand All @@ -31,7 +31,7 @@ import {
updateRunStep,
} from "../run-logger.js";
import { startAnvilWithState, startNitroFromSnapshot, stopRuntime } from "../runtime.js";
import { deployRollupViaSdk } from "../sdk-chain.js";
import { deployRollupViaSdk, prepareNodeConfigFromDeployment } from "../sdk-chain.js";
import { installSnapshotRelease } from "../snapshot-release.js";
import {
DEFAULT_SNAPSHOT_ID,
Expand Down Expand Up @@ -361,6 +361,10 @@ function readDeployment(name: string): DeploymentJson {
return JSON.parse(raw) as DeploymentJson;
}

function readJsonFile<T>(name: string): T {
return JSON.parse(readFileSync(resolve(CONFIG_DIR, name), "utf-8")) as T;
}

function createL1Steps(): Record<string, StepRunner> {
return {
"start-l1": async (state) => {
Expand Down Expand Up @@ -414,31 +418,17 @@ function createL2DeploySteps(): Record<string, StepRunner> {
if (!rollupData) {
throw new Error("Missing l2 rollup deployment data");
}
arbitrum(
[
"deploy",
"generate-config",
"--rollup",
rollupData["rollup"] as string,
"--chain-name",
"arb-dev-test",
"--parent-rpc",
L1_RPC,
"--parent-chain-id",
"1337",
"--parent-beacon-rpc",
"http://localhost:5555",
"--batch-poster-key",
accounts.sequencer.privateKey,
"--validator-key",
accounts.validator.privateKey,
"--output-dir",
CONFIG_DIR,
"--type",
"node",
],
{ timeout: 60_000 },
);
const nodeConfig = prepareNodeConfigFromDeployment({
chainName: "arb-dev-test",
chainConfig: readJsonFile("l2_chain_config.json"),
deployment: readJsonFile("l2_deployment.json"),
parentChainId: 1337,
parentChainRpcUrl: L1_RPC,
parentChainBeaconRpcUrl: "http://localhost:5555",
batchPosterPrivateKey: accounts.sequencer.privateKey,
validatorPrivateKey: accounts.validator.privateKey,
});
writeFileSync(resolve(CONFIG_DIR, "nodeConfig.json"), JSON.stringify(nodeConfig, null, 2));
// Patch parent RPC URL for Docker networking
patchConfigUrl(resolve(CONFIG_DIR, "nodeConfig.json"), L1_RPC, L1_RPC_DOCKER);
const src = resolve(CONFIG_DIR, "nodeConfig.json");
Expand Down Expand Up @@ -596,7 +586,6 @@ function createL3Steps(feeTokenDecimals?: number): Record<string, StepRunner> {
}

await deployRollupViaSdk({
configDir: CONFIG_DIR,
chainConfigPath: resolve(CONFIG_DIR, "l3_chain_config.json"),
chainId: 333333,
chainName: "orbit-dev-test",
Expand Down Expand Up @@ -636,29 +625,16 @@ function createL3Steps(feeTokenDecimals?: number): Record<string, StepRunner> {
if (!rollupData) {
throw new Error("Missing l3 rollup deployment data");
}
arbitrum(
[
"deploy",
"generate-config",
"--rollup",
rollupData["rollup"] as string,
"--chain-name",
"orbit-dev-test",
"--parent-rpc",
L2_RPC,
"--parent-chain-id",
"412346",
"--batch-poster-key",
accounts.l3sequencer.privateKey,
"--validator-key",
accounts.l3owner.privateKey,
"--output-dir",
CONFIG_DIR,
"--type",
"node",
],
{ timeout: 60_000 },
);
const nodeConfig = prepareNodeConfigFromDeployment({
chainName: "orbit-dev-test",
chainConfig: readJsonFile("l3_chain_config.json"),
deployment: readJsonFile("l3_deployment.json"),
parentChainId: 412346,
parentChainRpcUrl: L2_RPC,
batchPosterPrivateKey: accounts.l3sequencer.privateKey,
validatorPrivateKey: accounts.l3owner.privateKey,
});
writeFileSync(resolve(CONFIG_DIR, "nodeConfig.json"), JSON.stringify(nodeConfig, null, 2));
patchConfigUrl(resolve(CONFIG_DIR, "nodeConfig.json"), L2_RPC, L2_RPC_DOCKER);
const src = resolve(CONFIG_DIR, "nodeConfig.json");
const dest = resolve(CONFIG_DIR, "l3-nodeConfig.json");
Expand Down
5 changes: 0 additions & 5 deletions src/exec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -68,11 +68,6 @@ export function execOrThrow(
return result.stdout;
}

/** Run an `arbitrum` CLI command. Throws on failure. */
export function arbitrum(args: string[], options?: { cwd?: string; timeout?: number }): string {
return execOrThrow("arbitrum", args, options);
}

/** Run an `anvil` command (non-blocking not supported here — use spawn separately). */
export function anvil(args: string[]): ExecResult {
return exec("anvil", args);
Expand Down
182 changes: 116 additions & 66 deletions src/sdk-chain.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,16 @@
import { existsSync, readFileSync, writeFileSync } from "node:fs";
import { resolve } from "node:path";
import { pathToFileURL } from "node:url";
import { readFileSync, writeFileSync } from "node:fs";
import {
type NodeConfig,
type PrepareNodeConfigParams,
createRollup,
createRollupPrepareDeploymentParamsConfig,
prepareNodeConfig,
} from "@arbitrum/chain-sdk";
import type { Address } from "viem";
import { privateKeyToAccount } from "viem/accounts";
import { ZERO_ADDRESS } from "./init-helpers.js";
import { publicClient } from "./rpc.js";

interface CreateChainModule {
createChain: (params: Record<string, unknown>) => Promise<CreateChainResult>;
}

export interface CreateChainDeployment {
bridge: Address;
inbox: Address;
Expand All @@ -22,15 +24,7 @@ export interface CreateChainDeployment {
"stake-token"?: Address;
}

export interface CreateChainResult {
deployment: CreateChainDeployment;
chainInfo: Array<Record<string, unknown>>;
nodeConfig: Record<string, unknown>;
rollupCreatorAddress: Address;
}

export interface DeployRollupViaSdkParams {
configDir: string;
chainConfigPath: string;
chainId: number;
chainName: string;
Expand All @@ -53,75 +47,131 @@ export interface DeployRollupViaSdkParams {
feeTokenPricer?: Address;
}

function getChainSdkEntryPath(): string {
const override = process.env["ARBITRUM_CHAIN_SDK_ENTRY"];
if (override) {
return override;
}

return resolve(import.meta.dirname, "../../arbitrum-chain-sdk/src/dist/index.js");
}

async function loadCreateChainModule(): Promise<CreateChainModule> {
const entryPath = getChainSdkEntryPath();
if (!existsSync(entryPath)) {
throw new Error(
`Chain SDK build not found at ${entryPath}. Build ../arbitrum-chain-sdk first or set ARBITRUM_CHAIN_SDK_ENTRY.`,
);
}

const moduleUrl = pathToFileURL(entryPath).href;
const loaded = (await import(moduleUrl)) as {
createChain?: CreateChainModule["createChain"];
default?: { createChain?: CreateChainModule["createChain"] };
};
const createChain = loaded.createChain ?? loaded.default?.createChain;
if (!createChain) {
throw new Error(`createChain export not found in ${entryPath}`);
}
type DeploymentForNodeConfig = Partial<CreateChainDeployment> &
Pick<CreateChainDeployment, "rollup" | "bridge" | "inbox" | "sequencer-inbox">;

return { createChain };
export function prepareNodeConfigFromDeployment(input: {
chainName: string;
chainConfig: Record<string, unknown> & { chainId: number };
deployment: DeploymentForNodeConfig;
parentChainId: 1337 | 412346;
parentChainIsArbitrum?: boolean;
parentChainRpcUrl: string;
parentChainBeaconRpcUrl?: string;
batchPosterPrivateKey: string;
validatorPrivateKey: string;
dasServerUrl?: string;
}) {
return prepareNodeConfig({
chainName: input.chainName,
chainConfig: input.chainConfig as PrepareNodeConfigParams["chainConfig"],
coreContracts: {
rollup: input.deployment.rollup,
bridge: input.deployment.bridge,
inbox: input.deployment.inbox,
sequencerInbox: input.deployment["sequencer-inbox"],
validatorUtils: input.deployment["validator-utils"],
validatorWalletCreator: input.deployment["validator-wallet-creator"] ?? ZERO_ADDRESS,
nativeToken: input.deployment["native-token"] ?? ZERO_ADDRESS,
deployedAtBlockNumber: input.deployment["deployed-at"] ?? 0,
} as PrepareNodeConfigParams["coreContracts"],
batchPosterPrivateKey: input.batchPosterPrivateKey,
validatorPrivateKey: input.validatorPrivateKey,
stakeToken: input.deployment["stake-token"] ?? ZERO_ADDRESS,
parentChainId: input.parentChainId,
...(input.parentChainIsArbitrum !== undefined
? { parentChainIsArbitrum: input.parentChainIsArbitrum }
: {}),
parentChainRpcUrl: input.parentChainRpcUrl,
...(input.parentChainBeaconRpcUrl
? { parentChainBeaconRpcUrl: input.parentChainBeaconRpcUrl }
: {}),
...(input.dasServerUrl ? { dasServerUrl: input.dasServerUrl } : {}),
});
}

export async function deployRollupViaSdk(
params: DeployRollupViaSdkParams,
): Promise<CreateChainResult> {
export async function deployRollupViaSdk(params: DeployRollupViaSdkParams): Promise<void> {
const chainConfig = JSON.parse(readFileSync(params.chainConfigPath, "utf-8")) as Record<
string,
unknown
>;
const account = privateKeyToAccount(params.ownerKey);
const { createChain } = await loadCreateChainModule();
const parentChainPublicClient = publicClient(params.parentRpcUrl);
const deploymentConfig = createRollupPrepareDeploymentParamsConfig(parentChainPublicClient, {
chainId: BigInt(params.chainId),
owner: params.ownerAddress,
chainConfig: chainConfig as PrepareNodeConfigParams["chainConfig"],
wasmModuleRoot: params.wasmModuleRoot,
...(params.feeTokenPricer ? { feeTokenPricer: params.feeTokenPricer } : {}),
});

const result = await createChain({
const result = await createRollup({
params: {
config: deploymentConfig,
batchPosters: [params.batchPosterAddress],
validators: [params.validatorAddress],
maxDataSize: Number(params.maxDataSize),
...(params.nativeToken ? { nativeToken: params.nativeToken } : {}),
},
account,
parentChainPublicClient: publicClient(params.parentRpcUrl),
chainId: params.chainId,
parentChainPublicClient,
});

const stakeToken =
((deploymentConfig as { stakeToken?: Address }).stakeToken as Address | undefined) ??
ZERO_ADDRESS;
const deployment: CreateChainDeployment = {
bridge: result.coreContracts.bridge,
inbox: result.coreContracts.inbox,
"sequencer-inbox": result.coreContracts.sequencerInbox,
"deployed-at": result.coreContracts.deployedAtBlockNumber,
rollup: result.coreContracts.rollup,
"native-token": result.coreContracts.nativeToken,
"upgrade-executor": result.coreContracts.upgradeExecutor,
...(result.coreContracts.validatorUtils
? { "validator-utils": result.coreContracts.validatorUtils }
: {}),
"validator-wallet-creator": result.coreContracts.validatorWalletCreator,
"stake-token": stakeToken,
};
const nodeConfig: NodeConfig = prepareNodeConfig({
chainName: params.chainName,
owner: params.ownerAddress,
batchPoster: params.batchPosterAddress,
chainConfig: chainConfig as PrepareNodeConfigParams["chainConfig"],
coreContracts: result.coreContracts,
batchPosterPrivateKey: params.batchPosterKey,
validator: params.validatorAddress,
validatorPrivateKey: params.validatorKey,
parentChainId: params.parentChainId,
stakeToken,
parentChainId: params.parentChainId as PrepareNodeConfigParams["parentChainId"],
parentChainIsArbitrum: params.parentChainIsArbitrum,
parentChainRpcUrl: params.parentRpcUrl,
...(params.parentBeaconRpcUrl ? { parentChainBeaconRpcUrl: params.parentBeaconRpcUrl } : {}),
chainConfig,
maxDataSize: params.maxDataSize,
wasmModuleRoot: params.wasmModuleRoot,
// omit stakeToken — createChain defaults to WETH for non-custom chains
...(params.nativeToken ? { nativeToken: params.nativeToken } : {}),
...(params.feeTokenPricer ? { feeTokenPricer: params.feeTokenPricer } : {}),
});

const deploymentWithCreator = {
...result.deployment,
"rollup-creator": result.rollupCreatorAddress,
...deployment,
"rollup-creator": result.transaction.to ?? ZERO_ADDRESS,
};
writeFileSync(params.deploymentOutputPath, JSON.stringify(deploymentWithCreator, null, 2));
writeFileSync(params.chainInfoOutputPath, JSON.stringify(result.chainInfo, null, 2));
writeFileSync(params.rawNodeConfigOutputPath, JSON.stringify(result.nodeConfig, null, 2));

return result;
writeFileSync(
params.chainInfoOutputPath,
JSON.stringify(
[
{
"chain-name": params.chainName,
"parent-chain-id": params.parentChainId,
"parent-chain-is-arbitrum": params.parentChainIsArbitrum,
"sequencer-url": "",
"secondary-forwarding-target": "",
"feed-url": "",
"secondary-feed-url": "",
"das-index-url": "",
"has-genesis-state": false,
"chain-config": chainConfig,
rollup: deployment,
},
],
null,
2,
),
);
writeFileSync(params.rawNodeConfigOutputPath, JSON.stringify(nodeConfig, null, 2));
}
10 changes: 1 addition & 9 deletions test/exec.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { describe, expect, it } from "vitest";
import { arbitrum, exec, execOrThrow } from "../src/exec.js";
import { exec, execOrThrow } from "../src/exec.js";

describe("exec", () => {
it("runs a command and captures stdout", () => {
Expand Down Expand Up @@ -30,11 +30,3 @@ describe("execOrThrow", () => {
expect(() => execOrThrow("sh", ["-c", "echo fail >&2; exit 1"])).toThrow("sh failed");
});
});

describe("arbitrum", () => {
it("calls exec with 'arbitrum' as the command", () => {
// arbitrum --version returns version string
const stdout = arbitrum(["--version"]);
expect(stdout).toContain("0.1.0");
});
});
Loading