Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add script to deploy state transition manager #461

Open
wants to merge 4 commits into
base: dev
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 2 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
66 changes: 66 additions & 0 deletions l1-contracts/scripts/deploy-hyperchain.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
// hardhat import should be the first import in the file
// eslint-disable-next-line @typescript-eslint/no-unused-vars
import * as hardhat from "hardhat";
import { Command } from "commander";
import { Wallet, ethers } from "ethers";
import { Deployer } from "../src.ts/deploy";
import { formatUnits, parseUnits } from "ethers/lib/utils";
import { web3Provider, GAS_MULTIPLIER } from "./utils";
import { deployedAddressesFromEnv } from "../src.ts/deploy-utils";
import { deployHyperchain } from "../src.ts/deploy-process";
import { ethTestConfig } from "../src.ts/utils";

const provider = web3Provider();

async function main() {
const program = new Command();

program.version("0.1.0").name("deploy-hyperchain").description("deploy initial hyperchain contracts");

program
.option("--private-key <private-key>")
.option("--gas-price <gas-price>")
.option("--nonce <nonce>")
.option("--owner-address <owner-address>")
.option("--create2-salt <create2-salt>")
.action(async (cmd) => {
const deployWallet = cmd.privateKey
? new Wallet(cmd.privateKey, provider)
: Wallet.fromMnemonic(
process.env.MNEMONIC ? process.env.MNEMONIC : ethTestConfig.mnemonic,
"m/44'/60'/0'/0/1"
).connect(provider);
console.log(`Using deployer wallet: ${deployWallet.address}`);

const ownerAddress = cmd.ownerAddress ? cmd.ownerAddress : deployWallet.address;
console.log(`Using owner address: ${ownerAddress}`);

const gasPrice = cmd.gasPrice
? parseUnits(cmd.gasPrice, "gwei")
: (await provider.getGasPrice()).mul(GAS_MULTIPLIER);
console.log(`Using gas price: ${formatUnits(gasPrice, "gwei")} gwei`);

const nonce = cmd.nonce ? parseInt(cmd.nonce) : await deployWallet.getTransactionCount();
console.log(`Using nonce: ${nonce}`);

const create2Salt = cmd.create2Salt ? cmd.create2Salt : ethers.utils.hexlify(ethers.utils.randomBytes(32));

const deployer = new Deployer({
deployWallet,
addresses: deployedAddressesFromEnv(),
ownerAddress,
verbose: true,
});

await deployHyperchain(deployer, [], gasPrice, create2Salt, nonce);
});

await program.parseAsync(process.argv);
}

main()
.then(() => process.exit(0))
.catch((err) => {
console.error("Error:", err);
process.exit(1);
});
13 changes: 9 additions & 4 deletions l1-contracts/scripts/deploy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import { Deployer } from "../src.ts/deploy";
import { formatUnits, parseUnits } from "ethers/lib/utils";
import { web3Provider, GAS_MULTIPLIER } from "./utils";
import { deployedAddressesFromEnv } from "../src.ts/deploy-utils";
import { initialBridgehubDeployment } from "../src.ts/deploy-process";
import { deployHyperchain, deployVerifier, initialBridgehubDeployment } from "../src.ts/deploy-process";
import { ethTestConfig } from "../src.ts/utils";

const provider = web3Provider();
Expand All @@ -24,7 +24,6 @@ async function main() {
.option("--nonce <nonce>")
.option("--owner-address <owner-address>")
.option("--create2-salt <create2-salt>")
.option("--diamond-upgrade-init <version>")
.option("--only-verifier")
.action(async (cmd) => {
const deployWallet = cmd.privateKey
Expand All @@ -43,7 +42,7 @@ async function main() {
: (await provider.getGasPrice()).mul(GAS_MULTIPLIER);
console.log(`Using gas price: ${formatUnits(gasPrice, "gwei")} gwei`);

const nonce = cmd.nonce ? parseInt(cmd.nonce) : await deployWallet.getTransactionCount();
let nonce = cmd.nonce ? parseInt(cmd.nonce) : await deployWallet.getTransactionCount();
console.log(`Using nonce: ${nonce}`);

const create2Salt = cmd.create2Salt ? cmd.create2Salt : ethers.utils.hexlify(ethers.utils.randomBytes(32));
Expand All @@ -55,7 +54,13 @@ async function main() {
verbose: true,
});

await initialBridgehubDeployment(deployer, [], gasPrice, cmd.onlyVerifier, create2Salt, nonce);
if (cmd.onlyVerifier) {
await deployVerifier(deployer, gasPrice, create2Salt, nonce);
} else {
await initialBridgehubDeployment(deployer, gasPrice, create2Salt, nonce);
nonce = await deployWallet.getTransactionCount();
await deployHyperchain(deployer, [], gasPrice, create2Salt, nonce);
}
});

await program.parseAsync(process.argv);
Expand Down
28 changes: 16 additions & 12 deletions l1-contracts/src.ts/deploy-process.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,7 @@ export const L2_DEFAULT_ACCOUNT_BYTECODE_HASH = "0x10010000000000000000000000000

export async function initialBridgehubDeployment(
deployer: Deployer,
extraFacets: FacetCut[],
gasPrice: BigNumberish,
onlyVerifier: boolean,
create2Salt?: string,
nonce?: number
) {
Expand All @@ -37,11 +35,6 @@ export async function initialBridgehubDeployment(
nonce++;
}

if (onlyVerifier) {
await deployer.deployVerifier(create2Salt, { gasPrice, nonce });
return;
}

await deployer.deployDefaultUpgrade(create2Salt, {
gasPrice,
nonce,
Expand All @@ -54,22 +47,33 @@ export async function initialBridgehubDeployment(
});
nonce++;

await deployer.deployValidatorTimelock(create2Salt, { gasPrice, nonce });
nonce++;

await deployer.deployGovernance(create2Salt, { gasPrice, nonce });
await deployer.deployTransparentProxyAdmin(create2Salt, { gasPrice });
await deployer.deployBridgehubContract(create2Salt, gasPrice);
await deployer.deployBlobVersionedHashRetriever(create2Salt, { gasPrice });
await deployer.deployStateTransitionManagerContract(create2Salt, extraFacets, gasPrice);
await deployer.setStateTransitionManagerInValidatorTimelock({ gasPrice });

await deployer.deploySharedBridgeContracts(create2Salt, gasPrice);
await deployer.deployERC20BridgeImplementation(create2Salt, { gasPrice });
await deployer.deployERC20BridgeProxy(create2Salt, { gasPrice });
await deployer.setParametersSharedBridge();
}

export async function deployVerifier(deployer: Deployer, gasPrice: BigNumberish, create2Salt?: string, nonce?: number) {
await deployer.deployVerifier(create2Salt, { gasPrice, nonce });
}

export async function deployHyperchain(
thomas-nguy marked this conversation as resolved.
Show resolved Hide resolved
deployer: Deployer,
extraFacets: FacetCut[],
gasPrice: BigNumberish,
create2Salt?: string,
nonce?: number
) {
await deployer.deployValidatorTimelock(create2Salt, { gasPrice, nonce });
await deployer.deployStateTransitionManagerContract(create2Salt, extraFacets, gasPrice);
await deployer.setStateTransitionManagerInValidatorTimelock({ gasPrice });
}

export async function registerHyperchain(
deployer: Deployer,
validiumMode: boolean,
Expand Down
12 changes: 8 additions & 4 deletions l1-contracts/src.ts/deploy-test-process.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ import {
L2_DEFAULT_ACCOUNT_BYTECODE_HASH,
initialBridgehubDeployment,
registerHyperchain,
deployVerifier,
deployHyperchain,
} from "./deploy-process";
import { deployTokens, getTokens } from "./deploy-token";

Expand Down Expand Up @@ -103,8 +105,9 @@ export async function initialTestnetDeploymentProcess(
fs.writeFileSync(testnetTokenPath, JSON.stringify(result, null, 2));

// deploy the verifier first
await initialBridgehubDeployment(deployer, extraFacets, gasPrice, true);
await initialBridgehubDeployment(deployer, extraFacets, gasPrice, false);
await deployVerifier(deployer, gasPrice);
await initialBridgehubDeployment(deployer, gasPrice);
await deployHyperchain(deployer, [], gasPrice);
await registerHyperchain(deployer, false, extraFacets, gasPrice, baseTokenName);
return deployer;
}
Expand Down Expand Up @@ -187,10 +190,11 @@ export async function initialEraTestnetDeploymentProcess(
fs.writeFileSync(testnetTokenPath, JSON.stringify(result, null, 2));

// deploy the verifier first
await initialBridgehubDeployment(deployer, extraFacets, gasPrice, true);
await deployVerifier(deployer, gasPrice);
await deployer.deployDiamondProxy(extraFacets, {});
// deploy normal contracts
await initialBridgehubDeployment(deployer, extraFacets, gasPrice, false);
await initialBridgehubDeployment(deployer, gasPrice);
await deployHyperchain(deployer, extraFacets, gasPrice);
// for Era we first deploy the DiamondProxy manually, set the vars manually, and register it in the system via bridgehub.createNewChain(ERA_CHAIN_ID, ..)
if (deployer.verbose) {
console.log("Applying DiamondCut");
Expand Down
7 changes: 3 additions & 4 deletions l1-contracts/src.ts/deploy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ import type { FacetCut } from "./diamondCut";
import { diamondCut, getCurrentFacetCutsForAdd } from "./diamondCut";

import { ERC20Factory } from "../typechain";
import type { IBridgehub} from "zksync-ethers/typechain/IBridgehub";

let L2_BOOTLOADER_BYTECODE_HASH: string;
let L2_DEFAULT_ACCOUNT_BYTECODE_HASH: string;
Expand Down Expand Up @@ -627,7 +628,6 @@ export class Deployer {
await this.deployStateTransitionDiamondFacets(create2Salt, gasPrice, nonce);
await this.deployStateTransitionManagerImplementation(create2Salt, { gasPrice });
await this.deployStateTransitionManagerProxy(create2Salt, { gasPrice }, extraFacets);
await this.registerStateTransitionManager();
}

public async deployStateTransitionDiamondFacets(create2Salt: string, gasPrice?: BigNumberish, nonce?) {
Expand All @@ -640,9 +640,7 @@ export class Deployer {
await this.deployStateTransitionDiamondInit(create2Salt, { gasPrice, nonce: nonce + 4 });
}

public async registerStateTransitionManager() {
const bridgehub = this.bridgehubContract(this.deployWallet);

public async registerStateTransitionManager(bridgehub: IBridgehub) {
const tx = await bridgehub.addStateTransitionManager(this.addresses.StateTransition.StateTransitionProxy);

const receipt = await tx.wait();
Expand All @@ -666,6 +664,7 @@ export class Deployer {
const bridgehub = this.bridgehubContract(this.deployWallet);
const stateTransitionManager = this.stateTransitionManagerContract(this.deployWallet);

await this.registerStateTransitionManager(bridgehub);
const inputChainId = predefinedChainId || getNumberFromEnv("CHAIN_ETH_ZKSYNC_NETWORK_ID");
const admin = process.env.CHAIN_ADMIN_ADDRESS || this.ownerAddress;
const diamondCutData = await this.initialZkSyncHyperchainDiamondCut(extraFacets);
Expand Down