-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathDeploymentFactory.ts
More file actions
217 lines (203 loc) · 6.89 KB
/
DeploymentFactory.ts
File metadata and controls
217 lines (203 loc) · 6.89 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
import {
TransactionReceipt,
TransactionRequest,
TransactionResponse,
} from '@ethersproject/providers';
import { ContractFactory, PayableOverrides, Signer, ethers } from 'ethers';
import { Artifact } from 'hardhat/types';
import * as zk from 'zksync-ethers';
import { Address, Deployment, DeployOptions, ExtendedArtifact } from '../types';
import { getAddress } from '@ethersproject/address';
import { keccak256 as solidityKeccak256 } from '@ethersproject/solidity';
import { hexConcat } from '@ethersproject/bytes';
import { TronContractFactory } from './tron/contract';
import { TronSigner } from './tron/signer';
import { CreateSmartContract } from './tron/types';
export class DeploymentFactory {
private factory: ContractFactory;
private artifact: Artifact | ExtendedArtifact;
private isZkSync: boolean;
private isTron: boolean;
private getArtifact: (name: string) => Promise<Artifact>;
private overrides: PayableOverrides;
private args: any[];
constructor(
getArtifact: (name: string) => Promise<Artifact>,
artifact: Artifact | ExtendedArtifact,
args: any[],
network: any,
ethersSigner?: Signer | zk.Signer | TronSigner,
overrides: PayableOverrides = {}
) {
this.overrides = overrides;
this.getArtifact = getArtifact;
this.isZkSync = network.zksync;
this.isTron = network.tron;
this.artifact = artifact;
if (this.isZkSync) {
this.factory = new zk.ContractFactory(
artifact.abi,
artifact.bytecode,
ethersSigner as zk.Signer
);
} else if (this.isTron) {
let contractName = '';
if ('contractName' in artifact) ({ contractName } = artifact);
this.factory = new TronContractFactory(
artifact.abi,
artifact.bytecode,
ethersSigner as TronSigner,
contractName
);
} else {
this.factory = new ContractFactory(
artifact.abi,
artifact.bytecode,
ethersSigner
);
}
const numArguments = this.factory.interface.deploy.inputs.length;
if (args.length !== numArguments) {
throw new Error(
`expected ${numArguments} constructor arguments, got ${args.length}`
);
}
this.args = args;
}
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];
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;
}
public async getDeployTransaction(): Promise<TransactionRequest> {
let overrides = this.overrides;
if (this.isZkSync) {
const factoryDeps = await this.extractFactoryDeps(this.artifact);
const customData = {
customData: {
factoryDeps,
feeToken: zk.utils.ETH_ADDRESS,
},
};
overrides = {
...overrides,
...customData,
};
}
return this.factory.getDeployTransaction(...this.args, overrides);
}
// TVM formula is identical than EVM except for the prefix: keccak256( 0x41 ++ address ++ salt ++ keccak256(init_code))[12:]
// https://developers.tron.network/v4.4.0/docs/vm-vs-evm#tvm-is-basically-compatible-with-evm-with-some-differences-in-details
private async calculateEvmCreate2Address(
create2DeployerAddress: Address,
salt: string,
isTron?: boolean
): Promise<Address> {
const deploymentTx = await this.getDeployTransaction();
if (typeof deploymentTx.data !== 'string') {
throw Error('unsigned tx data as bytes not supported');
}
const prefix = isTron ? '0x41' : '0xff';
return getAddress(
'0x' +
solidityKeccak256(
['bytes'],
[
`${prefix}${create2DeployerAddress.slice(2)}${salt.slice(
2
)}${solidityKeccak256(['bytes'], [deploymentTx.data]).slice(2)}`,
]
).slice(-40)
);
}
private async calculateZkCreate2Address(
create2DeployerAddress: Address,
salt: string
): Promise<Address> {
const bytecodeHash = zk.utils.hashBytecode(this.artifact.bytecode);
const constructor = this.factory.interface.encodeDeploy(this.args);
return zk.utils.create2Address(
create2DeployerAddress,
bytecodeHash,
salt,
constructor
);
}
public async getCreate2Address(
create2DeployerAddress: Address,
create2Salt: string
): Promise<Address> {
if (this.isZkSync)
return await this.calculateZkCreate2Address(
create2DeployerAddress,
create2Salt
);
return await this.calculateEvmCreate2Address(
create2DeployerAddress,
create2Salt,
this.isTron
);
}
public async compareDeploymentTransaction(
transaction: TransactionResponse,
deployment: Deployment
): Promise<boolean> {
const newTransaction = await this.getDeployTransaction();
const newData = newTransaction.data?.toString();
if (this.isZkSync) {
const currentFlattened = hexConcat(deployment.factoryDeps || []);
const newFlattened = hexConcat(newTransaction.customData?.factoryDeps);
return transaction.data !== newData || currentFlattened != newFlattened;
} else if (this.isTron) {
const tronDeployTx = newTransaction as CreateSmartContract;
const res = await (
this.factory.signer as TronSigner
).getTronWebTransaction(transaction.hash);
const contract = res.raw_data.contract[0];
const deployed_bytecode = contract.parameter.value.new_contract?.bytecode;
const newBytecode = tronDeployTx.bytecode + tronDeployTx.rawParameter;
return deployed_bytecode !== newBytecode;
} else {
return transaction.data !== newData;
}
}
getDeployedAddress(
receipt: TransactionReceipt,
options: DeployOptions,
create2Address: string | undefined
): string {
if (options.deterministicDeployment && create2Address) {
return create2Address;
}
if (this.isZkSync) {
const deployedAddresses = zk.utils
.getDeployedContracts(receipt)
.map((info) => info.deployedAddress);
return deployedAddresses[deployedAddresses.length - 1];
}
return receipt.contractAddress;
}
}