Skip to content

Commit

Permalink
Deploy account implementation and proxy factory with create2
Browse files Browse the repository at this point in the history
  • Loading branch information
hbriese committed Nov 28, 2023
1 parent f22d17b commit ed4b2b1
Show file tree
Hide file tree
Showing 24 changed files with 55 additions and 202 deletions.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file removed .yarn/cache/nan-npm-2.16.0-cac314a230-cb16937273.zip
Binary file not shown.
Binary file not shown.
Binary file not shown.
4 changes: 3 additions & 1 deletion contracts/hardhat.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@ import { CHAINS } from 'chains';
import '@nomiclabs/hardhat-ethers';
import '@nomicfoundation/hardhat-viem';
import '@nomiclabs/hardhat-solhint'; // https://github.com/protofire/solhint/blob/master/docs/rules.md
import '@matterlabs/hardhat-zksync-toolbox';
import '@matterlabs/hardhat-zksync-chai-matchers';
import '@matterlabs/hardhat-zksync-solc';
import '@matterlabs/hardhat-zksync-verify';
import 'hardhat-gas-reporter';
import 'hardhat-abi-exporter';
import 'hardhat-tracer';
Expand Down
2 changes: 0 additions & 2 deletions contracts/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,7 @@
},
"devDependencies": {
"@matterlabs/hardhat-zksync-chai-matchers": "^1.0.0",
"@matterlabs/hardhat-zksync-deploy": "^1.0.0",
"@matterlabs/hardhat-zksync-solc": "^1.0.2",
"@matterlabs/hardhat-zksync-toolbox": "^1.0.0",
"@matterlabs/hardhat-zksync-verify": "^1.1.0",
"@matterlabs/zksync-contracts": "^0.6.1",
"@nomicfoundation/hardhat-chai-matchers": "^2.0.2",
Expand Down
2 changes: 1 addition & 1 deletion contracts/scripts/deployAccountImplementation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ const main = async () => {
await hre.run(TASK_COMPILE);

const { address, deployTx } = await deploy('Account');
if (deployTx) await displayTx(address, deployTx);
await displayTx(address, deployTx);

await verify({ contract: 'contracts/Account.sol:Account', address });
};
Expand Down
2 changes: 1 addition & 1 deletion contracts/scripts/deployProxyFactory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ const main = async () => {
await hre.run(TASK_COMPILE);

const { address, deployTx, constructorArgs } = await deployFactory('AccountProxy');
if (deployTx) await displayTx(address, deployTx);
await displayTx(address, deployTx);

await verify({
contract: 'contracts/Factory.sol:Factory',
Expand Down
13 changes: 11 additions & 2 deletions contracts/scripts/util/display.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,16 @@ import { CONFIG } from '../../config';
import { ContractTransactionResponse } from 'ethers';
import { network } from '../../test/util';

export const displayTx = async (address: Address, tx: ContractTransactionResponse) => {
export async function displayTx(address: Address, tx: ContractTransactionResponse | null) {
if (!tx) {
console.log(`
====== Deployment ======
Address: ${address}
========================
`);
return;
}

const receipt = await tx.wait();
if (!receipt) return;

Expand All @@ -29,4 +38,4 @@ export const displayTx = async (address: Address, tx: ContractTransactionRespons
}
========================
`);
};
}
2 changes: 1 addition & 1 deletion contracts/test/deployment.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ describe('Deployment', () => {

describe('Proxy factory', () => {
it('should deploy', async () => {
const { address } = await deployFactory('ERC1967Proxy');
const { address } = await deployFactory('AccountProxy');
expect((await network.getBytecode({ address }))?.length ?? 0).to.be.gt(0);
});
});
Expand Down
54 changes: 35 additions & 19 deletions contracts/test/util/deploy.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import * as hre from 'hardhat';
import { Deployer } from '@matterlabs/hardhat-zksync-deploy';
import {
asAddress,
asPolicy,
Expand All @@ -10,16 +9,12 @@ import {
TxOptions,
} from 'lib';
import { network, wallet, wallets } from './network';
import { BytesLike, Overrides } from 'ethers';
import { BytesLike, hexlify, Interface, Overrides } from 'ethers';
import * as zk from 'zksync2-js';
import { getApprovals } from './approval';
import { Address, parseEther } from 'viem';
import { Address, parseEther, zeroHash } from 'viem';
import { CONFIG } from '../../config';

function getDeployer() {
return new Deployer(hre, new zk.Wallet(CONFIG.walletPrivateKey));
}

type AccountContractName = 'Account' | 'TestAccount';
type ContractName =
| AccountContractName
Expand All @@ -32,22 +27,44 @@ type ContractName =
interface DeployOptions<ConstructorArgs extends unknown[] = unknown[]> {
constructorArgs?: ConstructorArgs;
overrides?: Overrides;
additionalFactoryDeps?: BytesLike[];
factoryDeps?: BytesLike[];
}

const zkProvider = new zk.Provider(CONFIG.chain.rpcUrls.default.http[0]);

export async function deploy(
contractName: ContractName,
{ constructorArgs, overrides, additionalFactoryDeps }: DeployOptions = {},
{ constructorArgs, overrides, factoryDeps }: DeployOptions = {},
) {
const deployer = getDeployer();
const artifact = await deployer.loadArtifact(contractName);
const sender = new zk.Wallet(CONFIG.walletPrivateKey, zkProvider);
const artifact = await hre.artifacts.readArtifact(contractName);

const contract = await deployer.deploy(
artifact,
constructorArgs,
overrides,
additionalFactoryDeps,
const factory = new zk.ContractFactory(artifact.abi, artifact.bytecode, sender, 'create2');

const salt = zeroHash;

const encodedConstructorArgs = new Interface(artifact.abi).encodeDeploy(constructorArgs ?? []);

// const constructorAbiParams =
// (artifact.abi as Abi).find((x): x is AbiConstructor => 'type' in x && x.type === 'constructor')
// ?.inputs ?? [];
// const encodedConstructorArgs = encodeAbiParameters(constructorAbiParams, constructorArgs ?? []);

const potentialAddress = asAddress(
zk.utils.create2Address(
sender.address,
hexlify(zk.utils.hashBytecode(artifact.bytecode)),
salt,
encodedConstructorArgs,
),
);

const isDeployed = !!(await network.getBytecode({ address: potentialAddress }))?.length;
if (isDeployed) return { address: potentialAddress, deployTx: null, constructorArgs };

const contract = await factory.deploy(encodedConstructorArgs, {
customData: { ...overrides, salt, factoryDeps },
});
await contract.waitForDeployment();

return {
Expand All @@ -60,13 +77,12 @@ export async function deploy(
export type DeployResult = Awaited<ReturnType<typeof deploy>>;

export const deployFactory = async (childContractName: 'AccountProxy') => {
const deployer = getDeployer();
const childContractArtifact = await deployer.loadArtifact(childContractName);
const childContractArtifact = await hre.artifacts.readArtifact(childContractName);
const childContractBytecodeHash = zk.utils.hashBytecode(childContractArtifact.bytecode);

return deploy('Factory', {
constructorArgs: [childContractBytecodeHash],
additionalFactoryDeps: [childContractArtifact.bytecode],
factoryDeps: [childContractArtifact.bytecode],
});
};

Expand Down
4 changes: 2 additions & 2 deletions packages/lib/src/contract.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ export const TEST_VERIFIER_ABI = testVerifierArtifact.abi;
export const ACCOUNT_IMPLEMENTATION = {
...accountArtifact,
address: addresses({
'zksync-goerli': '0xc3380460A7D89981536A9ecA83e289DD0EF0c3D4',
'zksync-goerli': '0x59B6FBe744C13121FDfb304A4012f43A4aafbf3A',
}),
} as const;

Expand All @@ -22,7 +22,7 @@ export const ACCOUNT_PROXY = {
export const ACCOUNT_PROXY_FACTORY = {
abi: accountProxyFactoryArtifact.abi,
address: addresses({
'zksync-goerli': '0x2f04b94F5a39891C10E666992f08f1a69774b764',
'zksync-goerli': '0x1251D2CDaB43cb7b9b6610D0531aEB9b80fFE9ad',
}),
};

Expand Down
Loading

0 comments on commit ed4b2b1

Please sign in to comment.