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

Fix factory deps and re-deployment #541

Merged
merged 5 commits into from
Apr 28, 2024
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
47 changes: 30 additions & 17 deletions src/DeploymentFactory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import {
import { ContractFactory, PayableOverrides, Signer, ethers } from 'ethers';
import { Artifact } from 'hardhat/types';
import * as zk from 'zksync-ethers';
import { Address, DeployOptions, ExtendedArtifact } from '../types';
import { Address, Deployment, DeployOptions, ExtendedArtifact } from '../types';
import { getAddress } from '@ethersproject/address';
import { keccak256 as solidityKeccak256 } from '@ethersproject/solidity';
import { hexConcat } from '@ethersproject/bytes';
Expand Down Expand Up @@ -52,19 +52,33 @@ export class DeploymentFactory {
this.args = args;
}

// TODO add ZkSyncArtifact
private async extractFactoryDeps(artifact: any): Promise<string[]> {
public async extractFactoryDeps(artifact: any): Promise<string[]> {
const visited = new Set<string>();
visited.add(`${artifact.sourceName}:${artifact.contractName}`);
return await this._extractFactoryDepsRecursive(artifact, visited);
}

private async _extractFactoryDepsRecursive(
artifact: any,
visited: Set<string>
): Promise<string[]> {
// Load all the dependency bytecodes.
// We transform it into an array of bytecodes.
const factoryDeps: string[] = [];
for (const dependencyHash in artifact.factoryDeps) {
if (!dependencyHash) continue;
const dependencyContract = artifact.factoryDeps[dependencyHash];
const dependencyBytecodeString = (
await this.getArtifact(dependencyContract)
).bytecode;
factoryDeps.push(dependencyBytecodeString);
if (!visited.has(dependencyContract)) {
const dependencyArtifact = await this.getArtifact(dependencyContract);
factoryDeps.push(dependencyArtifact.bytecode);
visited.add(dependencyContract);
const transitiveDeps = await this._extractFactoryDepsRecursive(
dependencyArtifact,
visited
);
factoryDeps.push(...transitiveDeps);
}
}

return factoryDeps;
}

Expand Down Expand Up @@ -137,17 +151,16 @@ export class DeploymentFactory {
}

public async compareDeploymentTransaction(
transaction: TransactionResponse
transaction: TransactionResponse,
deployment: Deployment
): Promise<boolean> {
const newTransaction = await this.getDeployTransaction();
const newData = newTransaction.data?.toString();
if (this.isZkSync) {
const deserialize = zk.utils.parseTransaction(transaction.data) as any;
const desFlattened = hexConcat(deserialize.customData.factoryDeps);
const factoryDeps = await this.extractFactoryDeps(this.artifact);
const newFlattened = hexConcat(factoryDeps);
const currentFlattened = hexConcat(deployment.factoryDeps || []);
const newFlattened = hexConcat(newTransaction.customData?.factoryDeps);

return deserialize.data !== newData || desFlattened != newFlattened;
return transaction.data !== newData || currentFlattened != newFlattened;
} else {
return transaction.data !== newData;
}
Expand All @@ -163,9 +176,9 @@ export class DeploymentFactory {
}

if (this.isZkSync) {
const deployedAddresses = zk.utils.getDeployedContracts(receipt).map(
(info) => info.deployedAddress,
);
const deployedAddresses = zk.utils
.getDeployedContracts(receipt)
.map((info) => info.deployedAddress);

return deployedAddresses[deployedAddresses.length - 1];
}
Expand Down
1 change: 1 addition & 0 deletions src/DeploymentsManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -890,6 +890,7 @@ export class DeploymentsManager {
if (deployment.factoryDeps?.length) {
obj.factoryDeps = deployment.factoryDeps;
}

this.db.deployments[name] = obj;
if (obj.address === undefined && obj.transactionHash !== undefined) {
let receiptFetched;
Expand Down
6 changes: 5 additions & 1 deletion src/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -633,13 +633,16 @@ export function addHelpers(
options,
create2Address
);

const deployment = {
...preDeployment,
address,
receipt,
transactionHash: receipt.transactionHash,
libraries: options.libraries,
factoryDeps: unsignedTx.customData?.factoryDeps || [],
};

await saveDeployment(name, deployment);
if (options.log || hardwareWallet) {
print(
Expand Down Expand Up @@ -878,7 +881,8 @@ export function addHelpers(

if (transaction) {
const differences = await factory.compareDeploymentTransaction(
transaction
transaction,
deployment
);
return {differences, address: deployment.address};
} else {
Expand Down