-
Notifications
You must be signed in to change notification settings - Fork 14
/
0_deploy.ts
283 lines (275 loc) · 11.4 KB
/
0_deploy.ts
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
// SPDX-FileCopyrightText: 2023-2024 IEXEC BLOCKCHAIN TECH <contact@iex.ec>
// SPDX-License-Identifier: Apache-2.0
import { SignerWithAddress } from '@nomiclabs/hardhat-ethers/signers';
import fs from 'fs';
import hre, { ethers } from 'hardhat';
import path from 'path';
import { getFunctionSignatures } from '../migrations/utils/getFunctionSignatures';
import {
AppRegistry__factory,
DatasetRegistry__factory,
ENSIntegrationDelegate__factory,
ERC1538Proxy__factory,
ERC1538Query,
ERC1538QueryDelegate__factory,
ERC1538Query__factory,
ERC1538Update,
ERC1538UpdateDelegate__factory,
ERC1538Update__factory,
IexecAccessorsABILegacyDelegate__factory,
IexecAccessorsDelegate__factory,
IexecAccessors__factory,
IexecCategoryManagerDelegate__factory,
IexecCategoryManager__factory,
IexecERC20Delegate__factory,
IexecEscrowNativeDelegate__factory,
IexecEscrowTokenDelegate__factory,
IexecLibOrders_v5__factory,
IexecMaintenanceDelegate__factory,
IexecMaintenanceExtraDelegate__factory,
IexecOrderManagementDelegate__factory,
IexecPoco1Delegate__factory,
IexecPoco2Delegate__factory,
IexecPocoAccessorsDelegate__factory,
IexecPocoBoostAccessorsDelegate__factory,
IexecPocoBoostDelegate__factory,
IexecRelayDelegate__factory,
RLC__factory,
WorkerpoolRegistry__factory,
} from '../typechain';
import { Ownable__factory } from '../typechain/factories/@openzeppelin/contracts/access';
import { FactoryDeployerHelper } from '../utils/FactoryDeployerHelper';
import { getBaseNameFromContractFactory } from '../utils/deploy-tools';
import { Category } from '../utils/poco-tools';
const CONFIG = require('../config/config.json');
/**
* @dev Deploying contracts with `npx hardhat deploy` task brought by
* `hardhat-deploy` plugin.
* Previous deployments made with `npx hardhat run scripts/deploy.ts` used to
* hang at the end of deployments (terminal did not return at the end).
*
* Note:
* The`hardhat-deploy` plugin is currently being under used compared to all
* features available in it.
*/
module.exports = async function () {
console.log('Deploying PoCo..');
const chainId = (await ethers.provider.getNetwork()).chainId;
const [owner] = await hre.ethers.getSigners();
const deploymentOptions = CONFIG.chains[chainId] || CONFIG.chains.default;
const salt = process.env.SALT || deploymentOptions.v5.salt || ethers.constants.HashZero;
const factoryDeployer = new FactoryDeployerHelper(owner, salt);
// Deploy RLC
const isTokenMode = deploymentOptions.asset == 'Token';
let rlcInstanceAddress = isTokenMode
? await getOrDeployRlc(deploymentOptions.token, owner) // token
: ethers.constants.AddressZero; // native
console.log(`RLC: ${rlcInstanceAddress}`);
// Deploy ERC1538 proxy contracts
const erc1538UpdateAddress = await factoryDeployer.deployWithFactory(
new ERC1538UpdateDelegate__factory(),
);
const transferOwnershipCall = await Ownable__factory.connect(
ethers.constants.AddressZero, // any is fine
owner, // any is fine
)
.populateTransaction.transferOwnership(owner.address)
.then((tx) => tx.data)
.catch(() => {
throw new Error('Failed to prepare transferOwnership data');
});
const erc1538ProxyAddress = await factoryDeployer.deployWithFactory(
new ERC1538Proxy__factory(),
[erc1538UpdateAddress],
transferOwnershipCall,
);
// Save addresses of deployed PoCo contracts for later use
saveDeployedAddress('ERC1538Proxy', erc1538ProxyAddress);
const erc1538: ERC1538Update = ERC1538Update__factory.connect(erc1538ProxyAddress, owner);
console.log(`IexecInstance found at address: ${erc1538.address}`);
// Deploy library & modules
const iexecLibOrdersAddress = await factoryDeployer.deployWithFactory(
new IexecLibOrders_v5__factory(),
);
const iexecLibOrders = {
['contracts/libs/IexecLibOrders_v5.sol:IexecLibOrders_v5']: iexecLibOrdersAddress,
};
const modules = [
new ERC1538QueryDelegate__factory(),
new IexecAccessorsDelegate__factory(),
new IexecAccessorsABILegacyDelegate__factory(),
new IexecCategoryManagerDelegate__factory(),
new IexecERC20Delegate__factory(),
isTokenMode
? new IexecEscrowTokenDelegate__factory()
: new IexecEscrowNativeDelegate__factory(),
new IexecMaintenanceDelegate__factory(iexecLibOrders),
new IexecOrderManagementDelegate__factory(iexecLibOrders),
new IexecPoco1Delegate__factory(iexecLibOrders),
new IexecPoco2Delegate__factory(),
new IexecRelayDelegate__factory(),
new ENSIntegrationDelegate__factory(),
new IexecMaintenanceExtraDelegate__factory(),
new IexecPocoAccessorsDelegate__factory(iexecLibOrders),
new IexecPocoBoostDelegate__factory(iexecLibOrders),
new IexecPocoBoostAccessorsDelegate__factory(),
];
for (const module of modules) {
const address = await factoryDeployer.deployWithFactory(module);
await linkContractToProxy(erc1538, address, module);
}
// Verify linking on ERC1538Proxy
const erc1538QueryInstance: ERC1538Query = ERC1538Query__factory.connect(
erc1538ProxyAddress,
owner,
);
const functionCount = await erc1538QueryInstance.totalFunctions();
console.log(`The deployed ERC1538Proxy now supports ${functionCount} functions:`);
for (let i = 0; i < functionCount.toNumber(); i++) {
const [method, , contract] = await erc1538QueryInstance.functionByIndex(i);
console.log(`[${i}] ${contract} ${method}`);
}
const appRegistryAddress = await factoryDeployer.deployWithFactory(
new AppRegistry__factory(),
[],
transferOwnershipCall,
);
const datasetRegistryAddress = await factoryDeployer.deployWithFactory(
new DatasetRegistry__factory(),
[],
transferOwnershipCall,
);
const workerpoolRegistryAddress = await factoryDeployer.deployWithFactory(
new WorkerpoolRegistry__factory(),
[],
transferOwnershipCall,
);
const appRegistryInstance = AppRegistry__factory.connect(appRegistryAddress, owner);
const datasetRegistryInstance = DatasetRegistry__factory.connect(datasetRegistryAddress, owner);
const workerpoolRegistryInstance = WorkerpoolRegistry__factory.connect(
workerpoolRegistryAddress,
owner,
);
// Base URI configuration from config.json
const baseURIApp = CONFIG.registriesBaseUri.app;
const baseURIDataset = CONFIG.registriesBaseUri.dataset;
const baseURIWorkerpool = CONFIG.registriesBaseUri.workerpool;
// Check if registries have been initialized and set base URIs
if (!(await appRegistryInstance.initialized())) {
await appRegistryInstance
.initialize(deploymentOptions.v3.AppRegistry || ethers.constants.AddressZero)
.then((tx) => tx.wait());
await appRegistryInstance.setBaseURI(`${baseURIApp}/${chainId}/`).then((tx) => tx.wait());
}
if (!(await datasetRegistryInstance.initialized())) {
await datasetRegistryInstance
.initialize(deploymentOptions.v3.DatasetRegistry || ethers.constants.AddressZero)
.then((tx) => tx.wait());
await datasetRegistryInstance
.setBaseURI(`${baseURIDataset}/${chainId}/`)
.then((tx) => tx.wait());
}
if (!(await workerpoolRegistryInstance.initialized())) {
await workerpoolRegistryInstance
.initialize(deploymentOptions.v3.WorkerpoolRegistry || ethers.constants.AddressZero)
.then((tx) => tx.wait());
await workerpoolRegistryInstance
.setBaseURI(`${baseURIWorkerpool}/${chainId}/`)
.then((tx) => tx.wait());
}
// Set main configuration
const iexecAccessorsInstance = IexecAccessors__factory.connect(erc1538ProxyAddress, owner);
const iexecInitialized =
(await iexecAccessorsInstance.eip712domain_separator()) != ethers.constants.HashZero;
if (!iexecInitialized) {
await IexecMaintenanceDelegate__factory.connect(erc1538ProxyAddress, owner)
.configure(
rlcInstanceAddress,
'Staked RLC',
'SRLC',
9, // TODO: generic ?
appRegistryAddress,
datasetRegistryAddress,
workerpoolRegistryAddress,
ethers.constants.AddressZero,
)
.then((tx) => tx.wait());
}
// Set categories
const catCountBefore = await iexecAccessorsInstance.countCategory();
const categories = CONFIG.categories as Category[];
for (let i = catCountBefore.toNumber(); i < categories.length; i++) {
const category = categories[i];
await IexecCategoryManager__factory.connect(erc1538ProxyAddress, owner)
.createCategory(
category.name,
JSON.stringify(category.description),
category.workClockTimeRef,
)
.then((tx) => tx.wait());
}
const catCountAfter = await iexecAccessorsInstance.countCategory();
console.log(`countCategory is now: ${catCountAfter} (was ${catCountBefore})`);
for (let i = 0; i < catCountAfter.toNumber(); i++) {
console.log(`Category ${i}: ${await iexecAccessorsInstance.viewCategory(i)}`);
}
};
async function getOrDeployRlc(token: string, owner: SignerWithAddress) {
return token // token
? token
: await new RLC__factory()
.connect(owner)
.deploy()
.then((contract) => {
contract.deployed();
return contract.address;
});
}
/**
* Link a contract to an ERC1538 proxy.
* @param proxy contract to ERC1538 proxy.
* @param contractAddress The contract address to link to the proxy.
* @param contractFactory The contract factory to link to the proxy.
*/
async function linkContractToProxy(
proxy: ERC1538Update,
contractAddress: string,
contractFactory: any,
) {
const contractName = getBaseNameFromContractFactory(contractFactory);
await proxy
.updateContract(
contractAddress,
// TODO: Use contractFactory.interface.functions when moving to ethers@v6
// https://github.com/ethers-io/ethers.js/issues/1069
getFunctionSignatures(contractFactory.constructor.abi),
'Linking ' + contractName,
)
.then((tx) => tx.wait())
.catch(() => {
throw new Error(`Failed to link ${contractName}`);
});
}
// TODO [optional]: Use hardhat-deploy to save addresses automatically
// https://github.com/wighawag/hardhat-deploy/tree/master#hardhat-deploy-in-a-nutshell
/**
* Save addresses of deployed contracts (since hardhat does not do it for us).
* @param contractName contract name to deploy
* @param deployedAddress address where contract where deployed
*/
function saveDeployedAddress(contractName: string, deployedAddress: string) {
const chainId = hre.network.config.chainId || 0;
const BUILD_DIR = '../build';
fs.writeFileSync(
path.resolve(__dirname, BUILD_DIR, `${contractName}.json`),
JSON.stringify({
networks: {
[chainId]: {
address: deployedAddress,
},
},
}),
);
console.log(`Saved deployment at ${deployedAddress} for ${contractName}`);
}
module.exports.tags = ['IexecPocoBoostDelegate'];