-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdeploy.js
207 lines (170 loc) ยท 5.96 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
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
/* global ethers */
const fs = require("fs");
const { promises } = fs
const {
createAddFacetCut
} = require('./libraries/cuts.js');
const {
setDiamondJson,
verify,
getChainIdByNetworkName
} = require('../tasks/lib/utils.js')
const {
updateDiamond
} = require('../test/libraries/diamond.js');
const { ethers } = require("hardhat");
const init = '0xe1c7392a';
// @dev reusable facets - attached facets are prefixed with 'x_';
async function createDiamond(signer, cuts, initAddr) {
const Diamond = await ethers.getContractFactory('Diamond');
const diamond = await Diamond.connect(signer).deploy(cuts, initAddr, init);
await diamond.deployed();
console.log('๐ Diamond deployed:', diamond.address);
return diamond;
}
async function create0xpm(cuts) {
const Xpm = await ethers.getContractFactory('Xpm');
const xpm = await Xpm.deploy(cuts, ethers.constants.AddressZero, '0x');
await xpm.deployed();
console.log('๐ 0xpm deployed:', xpm.address);
const x_ownership = await ethers.getContractAt('Ownership', xpm.address);
const x_readable = await ethers.getContractAt('Readable', xpm.address);
const x_writable = await ethers.getContractAt('Writable', xpm.address);
let facetAddresses = [xpm.address]
for (let cut of cuts) { facetAddresses.push(cut.target) }
const packageManagerAddr = (await x_readable.facetAddresses()).filter((x) => {
if (facetAddresses.indexOf(x) === -1) return x;
})[0];
const packagemanager = await ethers.getContractAt('PackageManager', packageManagerAddr);
console.log('๐ชฉ PackageManager deployed:', packageManagerAddr);
//deploy greeter
const Greeter = await ethers.getContractFactory('Greeter');
const greeter = await Greeter.deploy();
await greeter.deployed();
console.log('๐ Greeter deployed:', greeter.address);
//deploy git
cuts = createAddFacetCut([greeter]);
const Git = await ethers.getContractFactory('Git');
const git = await Git.deploy(cuts);
await git.deployed();
console.log('๐ Git deployed:', git.address);
//deploy factory
const DiamondFactory = await ethers.getContractFactory('DiamondFactory');
const diamondfactory = await DiamondFactory.deploy();
await diamondfactory.deployed();
console.log('๐ญ DiamondFactory deployed:', diamondfactory.address);
cuts = createAddFacetCut([git, diamondfactory]);
//deploy initializer
const XpmInit = await ethers.getContractFactory('XpmInit');
const xpminit = await XpmInit.deploy();
await xpminit.deployed();
console.log('๐ 0xpmInit deployed:', xpminit.address);
await x_writable.diamondCut(cuts, xpminit.address, init);
return [ xpm, packagemanager, git, diamondfactory, xpminit ];
}
async function deploy0xpm() {
console.log('~~~~~ C R E A T I N G O N T A P ~~~~~')
const Readable = await ethers.getContractFactory('Readable');
const readable = await Readable.deploy();
await readable.deployed();
console.log('๐ฎ Readable deployed:', readable.address);
const Writable = await ethers.getContractFactory('Writable');
const writable = await Writable.deploy();
await writable.deployed();
console.log('โ๏ธ Writable deployed:', writable.address);
const Ownership = await ethers.getContractFactory('Ownership');
const ownership = await Ownership.deploy();
await ownership.deployed();
console.log('๐ Ownership deployed:', ownership.address);
const ERC165 = await ethers.getContractFactory('Erc165');
const erc165 = await ERC165.deploy();
await erc165.deployed();
console.log('๐บ ERC165 deployed:', erc165.address);
let cuts = createAddFacetCut([readable, ownership, writable, erc165]);
const [ xpm, packagemanager, git, diamondfactory, xpminit ] = await create0xpm(cuts);
console.log('~~~~~~ O N T A P C R E A T E D ~~~~~~');
return [ xpm, readable, ownership, writable, erc165, packagemanager, git, diamondfactory, xpminit ];
}
async function deploy() {
const CHAIN_ID = getChainIdByNetworkName(hre.config.defaultNetwork);
await hre.run("clean")
await hre.run("compile")
let [ xpm, readable, ownership, writable, erc165, packagemanager, git, diamondfactory, xpminit ] = await deploy0xpm();
contractsToVerify = [
{
name: 'Xpm',
address: xpm.address
},
{
name: 'Readable',
address: readable.address
},
{
name: 'Ownership',
address: ownership.address
},
{
name: 'Writable',
address: writable.address
},
{
name: 'Erc165',
address: erc165.address
},
{
name: 'PackageManager',
address: packagemanager.address
},
{
name: 'Git',
address: git.address
},
{
name: 'DiamondFactory',
address: diamondfactory.address
},
{
name: 'XpmInit',
address: xpminit.address
}
];
await verify(contractsToVerify)
console.log('[OK] 0xpm verified')
//write addresses map
const map = {};
map[CHAIN_ID] = {
'Xpm': [xpm.address],
'Readable': [readable.address],
'Ownership': [ownership.address],
'Erc165': [erc165.address],
'Writable': [writable.address],
'PackageManager': [packagemanager.address],
'Git': [git.address],
'DiamondFactory': [diamondfactory.address],
'XpmInit': [xpminit.address]/* ,
'Multicall': [multicall2.address] */
};
const buffer = await promises.readFile('../web/src/lib/state/map.json')
const string = buffer.toString()
const json = JSON.parse(string)
// overwrite at chainid
for (const chain of Object.keys(json)) {
if (chain != String(CHAIN_ID)) {
map[chain] = json[chain]
}
}
await promises.writeFile('../web/src/lib/state/map.json', JSON.stringify(map, null, 2));
return [ xpm, readable, ownership, writable, erc165, packagemanager, git, diamondfactory, xpminit ]
}
if (require.main === module) {
deploy()
.then(() => process.exit(0))
.catch(error => {
console.error(error)
process.exit(1)
})
}
exports.deploy = deploy
exports.deploy0xpm = deploy0xpm
exports.createDiamond = createDiamond;
exports.create0xpm = create0xpm;