Skip to content

Commit

Permalink
fix: estimateGas support for custom chains (#1194)
Browse files Browse the repository at this point in the history
Co-authored-by: Marko Arambasic <makiarambasic@gmail.com>
  • Loading branch information
kiriyaga-txfusion and kiriyaga authored Jun 27, 2024
1 parent 2d8f2fb commit ceb9c4f
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 68 deletions.
11 changes: 0 additions & 11 deletions packages/hardhat-zksync-upgradable/src/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,14 +46,3 @@ export const verifiableContracts = {
transparentUpgradeableProxy: { event: 'AdminChanged(address,address)' },
proxyAdmin: { event: 'OwnershipTransferred(address,address)' },
};

export const defaultImplAddresses: { [chainId in number]: { contractAddress: string; beacon: string } } = {
324: {
contractAddress: '0x71CF3E1430aA920903CeF2154202902dDbBE2c98',
beacon: '0x3DbAe90affFFAC8d20285f2f53e3f2e10368c11C',
},
280: {
contractAddress: '0xCFFF2D44a3d3361f86Aa9EA5c563B95FAcA7be8c',
beacon: '0x713499530cCAc7a09FecFf05C3a1d4413E4CCd12',
},
};
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import type { HardhatRuntimeEnvironment } from 'hardhat/types';
import * as zk from 'zksync-ethers';
import * as ethers from 'ethers';
import chalk from 'chalk';
import assert from 'assert';
Expand All @@ -9,11 +8,10 @@ import { Deployer } from '@matterlabs/hardhat-zksync-deploy';
import { DeployProxyOptions } from '../utils/options';
import { ZkSyncUpgradablePluginError } from '../errors';
import { convertGasPriceToEth, getInitializerData } from '../utils/utils-general';
import { UPGRADABLE_BEACON_JSON, defaultImplAddresses } from '../constants';
import { UPGRADABLE_BEACON_JSON } from '../constants';

import { getAdminArtifact, getAdminFactory } from '../proxy-deployment/deploy-proxy-admin';
import { getAdminArtifact } from '../proxy-deployment/deploy-proxy-admin';
import { getChainId } from '../core/provider';
import { deploy } from '../proxy-deployment/deploy';

export type EstimateGasFunction = (
deployer: Deployer,
Expand All @@ -23,26 +21,6 @@ export type EstimateGasFunction = (
quiet?: boolean,
) => Promise<ethers.BigNumber>;

async function deployProxyAdminLocally(adminFactory: zk.ContractFactory) {
const mockContract = await deploy(adminFactory);
return mockContract.address;
}

async function deployBeaconLocally(impl: string, hre: HardhatRuntimeEnvironment, wallet: zk.Wallet) {
const upgradableBeaconPath = (await hre.artifacts.getArtifactPaths()).find((x) =>
x.includes(path.sep + UPGRADABLE_BEACON_JSON),
);
assert(upgradableBeaconPath, 'Upgradable beacon artifact not found');
const upgradeableBeaconContract = await import(upgradableBeaconPath);

const upgradeableBeaconFactory = new zk.ContractFactory(
upgradeableBeaconContract.abi,
upgradeableBeaconContract.bytecode,
wallet,
);
return await deploy(upgradeableBeaconFactory, impl);
}

export async function getMockedBeaconData(
deployer: Deployer,
hre: HardhatRuntimeEnvironment,
Expand All @@ -55,24 +33,24 @@ export async function getMockedBeaconData(
throw new ZkSyncUpgradablePluginError(`Chain id ${chainId} is not supported!`);
}

let mockedBeaconAddress: string;
let mockImplAddress: string;
let data: string;

if (chainId === 270) {
const adminFactory = await getAdminFactory(hre, deployer.zkWallet);
mockImplAddress = await deployProxyAdminLocally(adminFactory);
mockedBeaconAddress = (await deployBeaconLocally(mockImplAddress, hre, deployer.zkWallet)).address;
data = getInitializerData(adminFactory.interface, args, opts.initializer);
} else {
mockedBeaconAddress = defaultImplAddresses[chainId].beacon;
const mockArtifact = await getAdminArtifact(hre);
data = getInitializerData(ethers.Contract.getInterface(mockArtifact.abi), args, opts.initializer);
}
const mockedBeaconAddress = await getDeployedBeaconAddress(deployer);
const mockArtifact = await getAdminArtifact(hre);
const data = getInitializerData(ethers.Contract.getInterface(mockArtifact.abi), args, opts.initializer);

return { mockedBeaconAddress, data };
}

async function getDeployedBeaconAddress(deployer: Deployer) {
const defaultBridgeAddresses = await deployer.zkWallet.provider.getDefaultBridgeAddresses();
const sharedBridgeL2Contract = new ethers.Contract(
defaultBridgeAddresses.sharedL2,
['function l2TokenBeacon() public view returns (address)'],
deployer.zkWallet.provider,
);
const beaconAddress = await sharedBridgeL2Contract.l2TokenBeacon();
return beaconAddress;
}

export function makeEstimateGasBeacon(hre: HardhatRuntimeEnvironment): EstimateGasFunction {
return async function estimateGasBeacon(
deployer: Deployer,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import type { HardhatRuntimeEnvironment } from 'hardhat/types';
import * as zk from 'zksync-ethers';
import * as ethers from 'ethers';
import chalk from 'chalk';
import assert from 'assert';
Expand All @@ -11,10 +10,9 @@ import { ZkSyncUpgradablePluginError } from '../errors';
import { DeployProxyOptions } from '../utils/options';
import { convertGasPriceToEth, getInitializerData } from '../utils/utils-general';
import { getChainId } from '../core/provider';
import { ERC1967_PROXY_JSON, TUP_JSON, defaultImplAddresses } from '../constants';
import { ERC1967_PROXY_JSON, TUP_JSON } from '../constants';

import { getAdminArtifact, getAdminFactory } from '../proxy-deployment/deploy-proxy-admin';
import { deploy } from '../proxy-deployment/deploy';
import { getAdminArtifact } from '../proxy-deployment/deploy-proxy-admin';

export type EstimateProxyGasFunction = (
deployer: Deployer,
Expand All @@ -28,11 +26,6 @@ interface GasCosts {
proxyGasCost: ethers.BigNumber;
}

async function deployProxyAdminLocally(adminFactory: zk.ContractFactory) {
const mockContract = await deploy(adminFactory);
return mockContract.address;
}

export function makeEstimateGasProxy(hre: HardhatRuntimeEnvironment): EstimateProxyGasFunction {
return async function estimateGasProxy(
deployer: Deployer,
Expand All @@ -41,9 +34,7 @@ export function makeEstimateGasProxy(hre: HardhatRuntimeEnvironment): EstimatePr
opts: DeployProxyOptions = {},
quiet: boolean = false,
) {
let data;
let totalGasCost;
let mockImplAddress: string;

const mockArtifact = await getAdminArtifact(hre);
const kind = opts.kind;
Expand All @@ -53,14 +44,9 @@ export function makeEstimateGasProxy(hre: HardhatRuntimeEnvironment): EstimatePr
if (!chainId) {
throw new ZkSyncUpgradablePluginError(`Chain id ${chainId} is not supported!`);
}
if (chainId === 270) {
const adminFactory = await getAdminFactory(hre, deployer.zkWallet);
mockImplAddress = await deployProxyAdminLocally(adminFactory);
data = getInitializerData(adminFactory.interface, args, opts.initializer);
} else {
mockImplAddress = defaultImplAddresses[chainId].contractAddress;
data = getInitializerData(ethers.Contract.getInterface(mockArtifact.abi), args, opts.initializer);
}

const mockImplAddress = await getProxyAdminContractAddress();
const data = getInitializerData(ethers.Contract.getInterface(mockArtifact.abi), args, opts.initializer);

const implGasCost = await deployer.estimateDeployFee(artifact, []);

Expand Down Expand Up @@ -111,6 +97,11 @@ export function makeEstimateGasProxy(hre: HardhatRuntimeEnvironment): EstimatePr
};
}

async function getProxyAdminContractAddress() {
// return deployer contract address since it is used only for estimating gas as admin contract
return '0x0000000000000000000000000000000000008006';
}

async function estimateGasUUPS(
hre: HardhatRuntimeEnvironment,
deployer: Deployer,
Expand Down

0 comments on commit ceb9c4f

Please sign in to comment.