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

feat: register hyperchain through governance EVM-597 #443

Merged
merged 6 commits into from
May 20, 2024
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
7 changes: 5 additions & 2 deletions l1-contracts/scripts/register-hyperchain.ts
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ async function main() {
.option("--validium-mode")
.option("--base-token-name <base-token-name>")
.option("--base-token-address <base-token-address>")
.option("--use-governance <use-governance>")
.action(async (cmd) => {
const deployWallet = cmd.privateKey
? new Wallet(cmd.privateKey, provider)
Expand Down Expand Up @@ -99,11 +100,13 @@ async function main() {
await checkTokenAddress(baseTokenAddress);
console.log(`Using base token address: ${baseTokenAddress}`);

const useGovernance = cmd.useGovernance ? cmd.useGovernance == "true" : false;
ischasny marked this conversation as resolved.
Show resolved Hide resolved

if (!(await deployer.bridgehubContract(deployWallet).tokenIsRegistered(baseTokenAddress))) {
await deployer.registerToken(baseTokenAddress);
await deployer.registerToken(baseTokenAddress, useGovernance);
}

await deployer.registerHyperchain(baseTokenAddress, cmd.validiumMode, null, gasPrice);
await deployer.registerHyperchain(baseTokenAddress, cmd.validiumMode, null, gasPrice, useGovernance);
});

await program.parseAsync(process.argv);
Expand Down
15 changes: 12 additions & 3 deletions l1-contracts/src.ts/deploy-process.ts
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,8 @@ export async function registerHyperchain(
extraFacets: FacetCut[],
gasPrice: BigNumberish,
baseTokenName?: string,
chainId?: string
chainId?: string,
useGovernance: boolean = false
) {
const testnetTokens = getTokens();

Expand All @@ -85,7 +86,15 @@ export async function registerHyperchain(
: ADDRESS_ONE;

if (!(await deployer.bridgehubContract(deployer.deployWallet).tokenIsRegistered(baseTokenAddress))) {
await deployer.registerToken(baseTokenAddress);
await deployer.registerToken(baseTokenAddress, useGovernance);
}
await deployer.registerHyperchain(baseTokenAddress, validiumMode, extraFacets, gasPrice, null, chainId);
await deployer.registerHyperchain(
baseTokenAddress,
validiumMode,
extraFacets,
gasPrice,
null,
chainId,
useGovernance
);
}
72 changes: 58 additions & 14 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 { Contract, Overrides } from "@ethersproject/contracts";

let L2_BOOTLOADER_BYTECODE_HASH: string;
let L2_DEFAULT_ACCOUNT_BYTECODE_HASH: string;
Expand Down Expand Up @@ -442,21 +443,52 @@ export class Deployer {
}
}

public async executeDirectOrGovernance(
useGovernance: boolean,
contract: Contract,
fname: string,
// eslint-disable-next-line @typescript-eslint/no-explicit-any
fargs: any[],
value: BigNumberish,
overrides?: Overrides,
printFileName?: string
ischasny marked this conversation as resolved.
Show resolved Hide resolved
): Promise<ethers.ContractReceipt> {
if (useGovernance) {
const cdata = contract.interface.encodeFunctionData(fname, fargs);
return this.executeUpgrade(contract.address, value, cdata, printFileName);
} else {
const tx: ethers.ContractTransaction = await contract[fname](...fargs, ...(overrides ? [overrides] : []));
return await tx.wait();
}
}

/// this should be only use for local testing
public async executeUpgrade(targetAddress: string, value: BigNumberish, callData: string) {
public async executeUpgrade(targetAddress: string, value: BigNumberish, callData: string, printFileName?: string) {
const governance = IGovernanceFactory.connect(this.addresses.Governance, this.deployWallet);
const operation = {
calls: [{ target: targetAddress, value: value, data: callData }],
predecessor: ethers.constants.HashZero,
salt: ethers.utils.hexlify(ethers.utils.randomBytes(32)),
};
if (printFileName) {
console.log("Operation:", operation);
console.log(
"Schedule operation: ",
governance.interface.encodeFunctionData("scheduleTransparent", [operation, 0])
);
console.log(
`Execute operation value: ${value}, calldata`,
governance.interface.encodeFunctionData("execute", [operation])
);
return;
}
const scheduleTx = await governance.scheduleTransparent(operation, 0);
await scheduleTx.wait();
if (this.verbose) {
console.log("Upgrade scheduled");
}
const executeTX = await governance.execute(operation, { value: value });
await executeTX.wait();
const receipt = await executeTX.wait();
if (this.verbose) {
console.log(
"Upgrade with target ",
Expand All @@ -465,6 +497,7 @@ export class Deployer {
await governance.isOperationDone(await governance.hashOperation(operation))
);
}
return receipt;
}

// used for testing, mimics original deployment process.
Expand Down Expand Up @@ -654,7 +687,8 @@ export class Deployer {
extraFacets?: FacetCut[],
gasPrice?: BigNumberish,
nonce?,
predefinedChainId?: string
predefinedChainId?: string,
useGovernance: boolean = false
) {
const gasLimit = 10_000_000;

Expand All @@ -668,24 +702,34 @@ export class Deployer {
const diamondCutData = await this.initialZkSyncHyperchainDiamondCut(extraFacets);
const initialDiamondCut = new ethers.utils.AbiCoder().encode([DIAMOND_CUT_DATA_ABI_STRING], [diamondCutData]);

const tx = await bridgehub.createNewChain(
inputChainId,
this.addresses.StateTransition.StateTransitionProxy,
baseTokenAddress,
Date.now(),
admin,
initialDiamondCut,
const receipt = await this.executeDirectOrGovernance(
useGovernance,
bridgehub,
"createNewChain",
[
inputChainId,
this.addresses.StateTransition.StateTransitionProxy,
baseTokenAddress,
Date.now(),
admin,
initialDiamondCut,
],
0,
{
gasPrice,
nonce,
gasLimit,
}
);
const receipt = await tx.wait();

const chainId = receipt.logs.find((log) => log.topics[0] == bridgehub.interface.getEventTopic("NewChain"))
.topics[1];

nonce++;
if (useGovernance) {
// deploying through governance requires two transactions
nonce++;
}

this.addresses.BaseToken = baseTokenAddress;

Expand Down Expand Up @@ -755,11 +799,11 @@ export class Deployer {
}
}

public async registerToken(tokenAddress: string) {
public async registerToken(tokenAddress: string, useGovernance: boolean = false) {
const bridgehub = this.bridgehubContract(this.deployWallet);
const tx = await bridgehub.addToken(tokenAddress);

const receipt = await tx.wait();
const receipt = await this.executeDirectOrGovernance(useGovernance, bridgehub, "addToken", [tokenAddress], 0);

if (this.verbose) {
console.log(`Token ${tokenAddress} was registered, gas used: ${receipt.gasUsed.toString()}`);
}
Expand Down