-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathdeploy.js
167 lines (150 loc) · 5.04 KB
/
deploy.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
/* global ethers */
/* eslint prefer-const: "off" */
const hre = require("hardhat");
const { getSelectors, FacetCutAction } = require("./libraries/diamond.js");
// console.log("this is env", process.env.WALLET);
async function deployDiamond() {
const accounts = await ethers.getSigners();
const contractOwner = accounts[0];
// Deploy DiamondInit
// DiamondInit provides a function that is called when the diamond is upgraded or deployed to initialize state variables
// Read about how the diamondCut function works in the EIP2535 Diamonds standard
const DiamondInit = await ethers.getContractFactory("DiamondInit");
const diamondInit = await DiamondInit.deploy();
await diamondInit.deployed();
console.log("DiamondInit deployed:", diamondInit.address);
// Deploy facets and set the `facetCuts` variable
console.log("");
console.log("Deploying facets");
const FacetNames = [
"DiamondCutFacet",
"DiamondLoupeFacet",
"OwnershipFacet",
// "ERC721Facet",
"PlayerFacet",
"QuestFacet",
//"CraftFacet",
//'TrainFacet',
//'EquipFacet',
//'ShopFacet',
// 'ArenaFacet',
//'ExchangeFacet',
//'MonsterFacet',
// 'TreasureFacet',
//'PlayerDropFacet',
//'ScriptFacet',
//'BridgeFacet',
//'OmniFacet',
//'TreasureDropFacet'
];
// The `facetCuts` variable is the FacetCut[] that contains the functions to add during diamond deployment
const facetCuts = [];
for (const FacetName of FacetNames) {
const Facet = await ethers.getContractFactory(FacetName);
const facet = await Facet.deploy();
await facet.deployed();
console.log(`${FacetName} deployed: ${facet.address}`);
facetCuts.push({
facetAddress: facet.address,
action: FacetCutAction.Add,
functionSelectors: getSelectors(facet),
});
//await verifyContract(facet, FacetName);
}
//console.log("Facet Cuts = ", facetCuts);
// Creating a function call
// This call gets executed during deployment and can also be executed in upgrades
// It is executed with delegatecall on the DiamondInit address.
let functionCall = diamondInit.interface.encodeFunctionData("init");
console.log("function call", functionCall);
// Setting arguments that will be used in the diamond constructor
const diamondArgs = {
owner: contractOwner.address,
init: diamondInit.address,
initCalldata: functionCall,
};
// deploy Diamond
console.log("Deploying Diamond now...");
const Diamond = await ethers.getContractFactory("Diamond");
const diamond = await Diamond.deploy(facetCuts, diamondArgs);
await diamond.deployed();
console.log();
console.log("Diamond deployed:", diamond.address);
//await verifyDiamond(diamond, facetCuts, diamondArgs);
// returning the address of the diamond
return diamond.address;
}
// We recommend this pattern to be able to use async/await everywhere
// and properly handle errors.
if (require.main === module) {
// === This is for deploying a new diamond ===
deployDiamond()
.then(() => process.exit(0))
.catch((error) => {
console.error(error);
process.exit(1);
});
}
// exports.deployDiamond = deployDiamond;
async function verifyContract(diamond, FacetName, constructorArguments = []) {
const liveNetworks = [
"mainnet",
"goerli",
"mumbai",
"scroll",
"scroll_sepolia",
"scroll_test",
"arbitrumGoerli",
"arbg",
"fuji",
"mantle"
];
if (!liveNetworks.includes(hre.network.name)) {
return; // Don't verify on test networks
}
try {
console.log("Waiting for 10 blocks to be mined...");
await diamond.deployTransaction.wait(10);
console.log("Running verification");
await hre.run("verify:verify", {
address: diamond.address,
contract: `contracts/facets/${FacetName}.sol:${FacetName}`,
network: hardhatArguments.network,
arguments: constructorArguments ? constructorArguments : [],
});
} catch (e) {
console.log("Verification failed: ", JSON.stringify(e, null, 2));
console.log(e);
}
// hre.run('verify:verify', {
// address: diamond.address,
// constructorArguments
// })
}
async function verifyDiamond(diamond, facetCuts, diamondArgs) {
const liveNetworks = ["mainnet", "goerli", "mumbai", "scroll, mantle, scroll_sepolia"];
if (!liveNetworks.includes(hre.network.name)) {
return; // Don't verify on test networks
}
try {
console.log("Waiting for 10 blocks to be mined...");
console.log("---------------");
console.log("Facet cuts = ", facetCuts);
console.log("Diamond Args = ", diamondArgs);
console.log("---------------");
// await diamond.deployTransaction.wait(10);
console.log("Running verification");
await hre.run("verify:verify", {
address: diamond.address,
contract: "contracts/Diamond.sol:Diamond",
network: hardhatArguments.network,
arguments: [facetCuts, diamondArgs],
});
} catch (e) {
console.log("Verification failed: ", JSON.stringify(e, null, 2));
}
// hre.run('verify:verify', {
// address: diamond.address,
// constructorArguments
// })
}