-
Notifications
You must be signed in to change notification settings - Fork 0
/
deploy_blockchain_2.js
479 lines (398 loc) · 13.6 KB
/
deploy_blockchain_2.js
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
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
// We require the Hardhat Runtime Environment explicitly here. This is optional
// but useful for running the script in a standalone fashion through `node <script>`.
//
// You can also run a script with `npx hardhat run <script>`. If you do that, Hardhat
// will compile your contracts, add the Hardhat Runtime Environment's members to the
// global scope, and execute the script.
const hre = require("hardhat");
const fs = require("fs");
const path = require("path");
class Utils {
static dapp_contracts_info_folder_path;
static contracts_setup_outputs = {};
static {
Utils.dapp_contracts_info_folder_path = Utils.setup_dapp_contracts_info();
}
static async display_hardhat_network_info() {
let provider = hre.ethers.provider;
const hardhat_network_info = {
name: provider._networkName,
url:
"url" in hre.config.networks[provider._networkName]
? hre.config.networks[provider._networkName].url
: "",
chainId: parseInt((await provider.getNetwork()).chainId),
};
console.log("\n---------------- Hardhat Network Info ----------------");
console.log(`${JSON.stringify(hardhat_network_info, null, 2)}`);
console.log("------------------------------------------------------\n");
}
static setup_dapp_contracts_info() {
const folder_path = path.join(__dirname, "..", "dapp_contracts_info/");
if (!fs.existsSync(folder_path)) {
fs.mkdirSync(folder_path);
}
return folder_path;
}
static async generate_dapp_contract_info(
blockchainName,
contractName,
contractInstances
) {
const artifact = await hre.artifacts.readArtifact(contractName);
const dapp_contract_info = {
contractName: artifact.contractName,
sourceName: artifact.sourceName,
contractInstances: contractInstances,
abi: artifact.abi,
};
const folder_path = path.join(
Utils.dapp_contracts_info_folder_path,
blockchainName,
"/"
);
if (!fs.existsSync(folder_path)) {
fs.mkdirSync(folder_path);
}
fs.writeFileSync(
path.join(folder_path, `${dapp_contract_info.contractName}.json`),
JSON.stringify(dapp_contract_info, null, 2)
);
}
}
class BaseContract {
constructor(contract_name, contract_instance_name) {
this.contract_name = contract_name;
this.contract_instance_name = contract_instance_name;
this.contract_address = "";
this.contract = null;
this.contract_constructor_args = [];
if (!(this.contract_name in Utils.contracts_setup_outputs)) {
Utils.contracts_setup_outputs[this.contract_name] = {};
}
Utils.contracts_setup_outputs[this.contract_name][
this.contract_instance_name
] = {};
}
async deployContract() {
const maxRetries = 6;
const retryDelaySeconds = 10;
let retries = 0;
while (retries < maxRetries) {
try {
const Contract = await hre.ethers.getContractFactory(
this.contract_name
);
this.contract = await Contract.deploy(
...this.contract_constructor_args
);
await this.contract.waitForDeployment();
break;
} catch (error) {
if ("code" in error && error.code === "UND_ERR_HEADERS_TIMEOUT") {
console.error(
`Error UND_ERR_HEADERS_TIMEOUT (${this.contract_name} contract - ${this.contract_instance_name} contract_instance). Retrying in ${retryDelaySeconds} seconds ...`
);
retries++;
await new Promise((resolve) =>
setTimeout(resolve, retryDelaySeconds * 1000)
);
} else {
throw error;
}
}
}
if (retries === maxRetries) {
console.error(
`Error UND_ERR_HEADERS_TIMEOUT (${this.contract_name} contract - ${this.contract_instance_name} contract_instance). Failed to deploy after ${maxRetries} retries.`
);
process.exitCode = 1;
}
this.contract_address = this.contract.target;
hre.ethernalUploadAst = true;
await hre.ethernal.push({
name: this.contract_name,
address: this.contract_address,
});
Utils.contracts_setup_outputs[this.contract_name][
this.contract_instance_name
]["address"] = this.contract_address;
}
async attachContract() {
const Contract = await hre.ethers.getContractFactory(this.contract_name);
this.contract = Contract.attach(this.contract_address);
Utils.contracts_setup_outputs[this.contract_name][
this.contract_instance_name
]["address"] = this.contract_address;
}
}
class Token extends BaseContract {
constructor(contract_instance_name, contract_constructor_args) {
super("Token", contract_instance_name);
this.symbol = contract_constructor_args.symbol;
this.contract_constructor_args = [
contract_constructor_args.name,
contract_constructor_args.symbol,
contract_constructor_args.maxSupply,
];
}
async mint(to, amount) {
await (await this.contract.mint(to, amount)).wait();
if (parseInt(await this.contract.balanceOf(to)) !== amount) {
throw new Error(
`Error in ${this.mint.name}() method while setting up ${this.contract_name} contract - ${this.contract_instance_name} contract_instance`
);
}
}
}
class BridgeNFTMinter extends BaseContract {
constructor(contract_instance_name, output_nft_info) {
super("BridgeNFTMinter", contract_instance_name);
this.output_nft_info = output_nft_info;
this.contract_constructor_args = [
output_nft_info.nft_collection_name,
output_nft_info.symbol,
];
}
async setBaseURI(NFTMetadataFolderCID) {
await (await this.contract.setBaseURI(NFTMetadataFolderCID)).wait();
if ((await this.contract.baseURI()) !== `ipfs://${NFTMetadataFolderCID}/`) {
throw new Error(
`Error in ${this.setBaseURI.name}() method while setting up ${this.contract_name} contract - ${this.contract_instance_name} contract_instance`
);
}
}
}
class NFTBridge extends BaseContract {
constructor(contract_instance_name) {
super("NFTBridge", contract_instance_name);
}
async setNFTMinter(newNFTMinter) {
await (await this.contract.setNFTMinter(newNFTMinter)).wait();
const nftMinter = await this.contract.nftMinter();
if (nftMinter !== newNFTMinter) {
throw new Error(
`Error in ${this.setNFTMinter.name}() method while setting up ${this.contract_name} contract - ${this.contract_instance_name} contract_instance`
);
}
}
async setFeeToken(newFeeToken) {
await (await this.contract.setFeeToken(newFeeToken)).wait();
const feeToken = await this.contract.feeToken();
if (feeToken !== newFeeToken) {
throw new Error(
`Error in ${this.setFeeToken.name}() method while setting up ${this.contract_name} contract - ${this.contract_instance_name} contract_instance`
);
}
}
}
class BaseDeploy {
constructor() {
this.tokens = [];
this.nft_collections = [];
this.nft_bridges = [];
}
async deploy() {
const token_alp = new Token("token_alpha", {
name: "Token Alpha",
symbol: "TKN-ALP",
maxSupply: 1000000,
});
this.tokens = [token_alp];
for (const token of this.tokens) {
await token.deployContract();
}
const hash_wallet_accounts = JSON.parse(
fs.readFileSync(
path.join(__dirname, "..", "hash_wallet_accounts.json"),
"utf8"
)
);
for (const token of this.tokens) {
for (const account of hash_wallet_accounts) {
await token.mint(account.address, 10000);
}
}
const output_nfts_info = await this.get_output_nfts_info();
this.nft_collections = [];
for (let output_nft_info in output_nfts_info) {
output_nft_info = output_nfts_info[output_nft_info];
const nft_collection = new BridgeNFTMinter(
output_nft_info.nft_collection_id,
output_nft_info
);
this.nft_collections.push(nft_collection);
}
for (const nft_collection of this.nft_collections) {
await nft_collection.deployContract();
}
const [nft_collection_tnj] = this.nft_collections;
const nft_bridge = new NFTBridge("nft_bridge");
this.nft_bridges = [nft_bridge];
await nft_bridge.deployContract();
await nft_bridge.setNFTMinter(nft_collection_tnj.contract_address);
await nft_bridge.setFeeToken(token_alp.contract_address);
const dapp_contracts_info = [
{
contractName: this.tokens[0].contract_name,
contractInstances: this.tokens.map((token) => ({
name: token.contract_instance_name,
address: token.contract_address,
})),
},
{
contractName: this.nft_collections[0].contract_name,
contractInstances: this.nft_collections.map((nft_collection) => ({
name: nft_collection.contract_instance_name,
address: nft_collection.contract_address,
nftCollection: nft_collection.output_nft_info.name,
})),
},
{
contractName: this.nft_bridges[0].contract_name,
contractInstances: this.nft_bridges.map((nft_bridge) => ({
name: nft_bridge.contract_instance_name,
address: nft_bridge.contract_address,
})),
},
];
for (const dapp_contract_info of dapp_contracts_info) {
await Utils.generate_dapp_contract_info(
"blockchain_2",
dapp_contract_info.contractName,
dapp_contract_info.contractInstances
);
}
}
async setBaseURI() {
for (const nft_collection of this.nft_collections) {
await nft_collection.setBaseURI(
nft_collection.output_nft_info.nft_metadata_folder_cid
);
}
}
}
class DeploySetup extends BaseDeploy {
async deploySetup() {
await this.deploy();
await this.setup();
}
async get_output_nfts_info() {
return JSON.parse(
fs.readFileSync(
path.join(__dirname, "../..", "nft/outputs/output_nfts_info.json"),
"utf8"
)
);
}
async setup() {
await this.setBaseURI();
}
}
class DeployE2E extends BaseDeploy {
async deployE2E() {
await this.deploy();
}
async get_output_nfts_info() {
const output_nfts_info = {
tom_and_jerry: {
nft_collection_id: "tom_and_jerry",
nft_collection_name: "NFT Collection TomAndJerry",
name: "Tom and Jerry",
symbol: "COL-TNJ",
image_name: "tom_and_jerry.png",
num_copies: 0,
ipfs_node_rpc_api: "/ip4/127.0.0.1/tcp/5001",
nft_image_folder_cid: "",
nft_metadata_folder_cid: "",
},
};
return output_nfts_info;
}
}
class SetupE2E extends BaseDeploy {
async setupE2E() {
await this.setup();
}
async setup() {
await this.setupPrerequisites();
await this.setBaseURI();
}
async setupPrerequisites() {
const all_contracts_setup_inputs = JSON.parse(
fs.readFileSync(
path.join(__dirname, "..", "contracts_setup_inputs.json"),
"utf8"
)
);
for (let contracts_setup_inputs in all_contracts_setup_inputs) {
contracts_setup_inputs =
all_contracts_setup_inputs[contracts_setup_inputs];
for (let contract_setup_inputs in contracts_setup_inputs) {
contract_setup_inputs = contracts_setup_inputs[contract_setup_inputs];
if (!contract_setup_inputs.address) {
throw new Error(`contracts_setup_inputs.json file is invalid.`);
}
}
}
const contracts_setup_inputs = all_contracts_setup_inputs.NFTMinter;
this.nft_collections = [];
for (let contract_setup_inputs in contracts_setup_inputs) {
contract_setup_inputs = contracts_setup_inputs[contract_setup_inputs];
const output_nft_info = {
nft_metadata_folder_cid: contract_setup_inputs.nft_metadata_folder_cid,
};
const nft_collection = new NFTMinter(
contract_setup_inputs.contract_instance_name,
output_nft_info
);
nft_collection.contract_address = contract_setup_inputs.address;
await nft_collection.attachContract();
this.nft_collections.push(nft_collection);
}
}
}
async function main() {
const DEPLOY_MODES = ["DeploySetup", "DeployE2E", "SetupE2E"];
const DEPLOY_MODE = process.env.DEPLOY_MODE;
if (!DEPLOY_MODE || !DEPLOY_MODES.includes(DEPLOY_MODE)) {
throw new Error("Invalid DEPLOY_MODE");
}
await hre.run("compile");
await Utils.display_hardhat_network_info();
console.log("-----------------------------------------------------");
console.log("------------- Contracts Deployment Info -------------");
console.log("-----------------------------------------------------");
if (DEPLOY_MODE === "DeploySetup") {
const deploy_setup = new DeploySetup();
await deploy_setup.deploySetup();
} else if (DEPLOY_MODE === "DeployE2E") {
const deploy_e2e = new DeployE2E();
await deploy_e2e.deployE2E();
} else if (DEPLOY_MODE === "SetupE2E") {
const setup_e2e = new SetupE2E();
await setup_e2e.setupE2E();
} else {
throw new Error("Invalid DEPLOY_MODE");
}
console.log(`\n${JSON.stringify(Utils.contracts_setup_outputs, null, 2)}`);
console.log("-----------------------------------------------------");
console.log("\nSUCCESS: contracts deployment ... DONE");
}
// We recommend this pattern to be able to use async/await everywhere
// and properly handle errors.
main().catch((error) => {
console.log(
"\n--------------------------- ERROR --------------------------\n"
);
console.error(error);
console.log(
"\n------------------------------------------------------------\n"
);
console.log(
"ERROR NOTE:\n \
1) Make sure hardhat network is running.\n \
2) Make sure you have properly updated contracts_setup_inputs.json file."
);
process.exitCode = 1;
});