This repository has been archived by the owner on Nov 7, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
index.js
415 lines (317 loc) · 13.9 KB
/
index.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
const figlet = require('figlet');
const chalk = require('chalk');
const Web3 = require('web3');
const namehash = require('eth-ens-namehash').hash;
const { promisify } = require('util');
const RNS = require('./build/contracts/RNS');
const PublicResolver = require('./build/contracts/PublicResolver');
const MultiChainResolver = require('./build/contracts/MultiChainResolver');
const NameResolver = require('./build/contracts/NameResolver');
const ReverseRegistrar = require('./build/contracts/ReverseRegistrar');
const ReverseSetup = require('./build/contracts/ReverseSetup');
const RIF = require('./build/contracts/ERC677');
const TokenRegistrar = require('./build/contracts/TokenRegistrar');
const RSKOwner = require('./build/contracts/RSKOwner');
const NamePrice = require('./build/contracts/NamePrice');
const BytesUtils = require('./build/contracts/BytesUtils');
const FIFSRegistrar = require('./build/contracts/FIFSRegistrar');
const FIFSAddrRegistrar = require('./build/contracts/FIFSAddrRegistrar');
const Renewer = require('./build/contracts/Renewer');
const ProxyFactory = require('./build/contracts/ProxyFactory');
const ProxyAdmin = require('./build/contracts/ProxyAdmin');
const ResolverV1 = require('./build/contracts/ResolverV1');
const { encodeCall } = require('@openzeppelin/upgrades');
function link(artifact, libName, libAddress) {
console.log(chalk.cyan(`Linking ${libName} into ${artifact.contractName}`));
const linkerRegex = new RegExp(`__${libName}_+`, "g");
return Object.assign(
artifact,
{ bytecode: artifact.bytecode.replace(linkerRegex, libAddress.replace("0x", "")) }
);
}
async function executeTx(tx, options) {
const gas = await tx.estimateGas();
return new Promise((resolve, reject) => tx.send({ gas, ...options })
.on('transactionHash', transactionHash => {
console.log(chalk.cyan(`Tx hash: ${transactionHash}`));
})
.on('receipt', receipt => {
console.log(chalk.greenBright(`Success!`));
resolve(receipt)
})
.on('error', error => {
reject(error)
})
);
}
/**
* Deploy RNS suite locally and perform automatic registrations.
* @param {string|Web3Provider} provider to deploy the contracts on
* @param {string[]} registrations labels to be registered with the current registrar (don't append .rsk)
* @param {string[]} auctionRegistrations labels to be registered with the previous registrar (auction) (don't append .rsk)
*/
async function main(provider, registrations, auctionRegistrations, registrationsWithAddr) {
console.log(figlet.textSync('Deploying RNS'));
console.log(chalk.italic('\nThis can take a while...\n\n'));
const web3 = new Web3(provider);
const [from] = await web3.eth.getAccounts();
web3.eth.defaultAccount = from;
async function deployContract(artifact, arguments) {
console.log(chalk.bold(`Deploying ${artifact.contractName}`));
const contract = new web3.eth.Contract(artifact.abi, {
data: artifact.bytecode,
from
});
const gas = await contract.deploy({ arguments }).estimateGas();
return new Promise((resolve, reject) => contract.deploy({ arguments }).send({ gas })
.on('error', (error) => {
reject(error);
})
.on('transactionHash', (transactionHash) => {
console.log(chalk.cyan(`Tx hash: ${transactionHash}`));
})
.on('receipt', (receipt) => {
console.log(chalk.greenBright(`Success!`));
console.log(`Contract address: ${receipt.contractAddress}`);
})
.then((newContractInstance) => {
resolve(newContractInstance);
})
);
}
console.log(chalk.italic('The registry'));
const rns = await deployContract(RNS);
console.log()
console.log(chalk.italic('The resolvers'));
const publicResolver = await deployContract(PublicResolver, [rns.options.address]);
const multiChainResolver = await deployContract(MultiChainResolver, [rns.options.address, publicResolver.options.address]);
console.log(chalk.bold('Connecting default resolver (public)'));
await executeTx(rns.methods.setDefaultResolver(publicResolver.options.address));
console.log()
console.log(chalk.italic('The reverse suite'));
const nameResolver = await deployContract(NameResolver, [rns.options.address]);
const reverseRegistrar = await deployContract(ReverseRegistrar, [rns.options.address]);
const reverseSetup = await deployContract(
ReverseSetup,
[
rns.options.address,
nameResolver.options.address,
reverseRegistrar.options.address,
from
]
);
console.log(chalk.bold('Connecting reverse suite'));
await executeTx(rns.methods.setSubnodeOwner('0x00', web3.utils.sha3('reverse'), reverseSetup.options.address));
await executeTx(reverseSetup.methods.run());
console.log()
console.log(chalk.italic('The RIF Token'));
const rif = await deployContract(
RIF,
[
from,
web3.utils.toBN('1000000000000000000000'),
'RIF',
'RIF',
web3.utils.toBN('18')
]
);
console.log()
console.log(chalk.italic('RSK auction registrar (legacy)'));
const auctionRegistrar = await deployContract(
TokenRegistrar,
[rns.options.address, namehash('rsk'), rif.options.address]
);
console.log(chalk.bold('Connecting auction registrar'));
await executeTx(rns.methods.setSubnodeOwner('0x00', web3.utils.sha3('rsk'), auctionRegistrar.options.address));
let registeredDomainsAuction;
try {
if(auctionRegistrations && auctionRegistrations.length) {
console.log(chalk.bold('Registering domains with auction'));
async function increaseTime (duration) {
await promisify(web3.currentProvider.send.bind(web3.currentProvider))({
jsonrpc: '2.0',
method: 'evm_increaseTime',
params: [duration],
id: new Date().getTime(),
});
await promisify(web3.currentProvider.send.bind(web3.currentProvider))({
jsonrpc: '2.0',
method: 'evm_mine',
id: new Date().getTime(),
});
}
const oneToken = web3.utils.toBN('1000000000000000000');
let labels = [];
let domains = [];
auctionRegistrations.forEach(name => {
labels.push(web3.utils.sha3(name));
domains.push(`${name}.rsk`)
});
await executeTx(auctionRegistrar.methods.startAuctions(labels));
await executeTx(rif.methods.approve(auctionRegistrar.options.address, oneToken.mul(web3.utils.toBN(labels.length))));
let allBids = [];
for (let i = 0; i < labels.length; i += 1)
allBids.push(
auctionRegistrar.methods.shaBid(labels[i], from, oneToken, '0x00').call()
.then(sealedBid => executeTx(auctionRegistrar.methods.newBid(sealedBid, oneToken)))
);
await Promise.all(allBids);
await increaseTime(259200);
let allReveals = [];
for (let i = 0; i < labels.length; i += 1)
allReveals.push(
executeTx(auctionRegistrar.methods.unsealBid(labels[i], oneToken, '0x00'))
);
await Promise.all(allReveals);
await increaseTime(172800);
let allFinalizations = [];
for (let i = 0; i < labels.length; i += 1)
allFinalizations.push(
executeTx(auctionRegistrar.methods.finalizeAuction(labels[i]))
);
await Promise.all(allFinalizations);
/*
This block is useful if you want to transfer domains to your Metamask :)
let allTransfers = []
for (let i = 0; i < labels.length; i += 1)
allTransfers.push(
executeTx(auctionRegistrar.methods.transfer(labels[i], 'PASTE YOUR ADDRESS'))
);
await Promise.all(allTransfers);
*/
registeredDomainsAuction = domains;
}
} catch(error) {
console.log(chalk.redBright('Error registering domains in auction!'))
console.log(error)
}
console.log()
console.log(chalk.italic('RSK Registrar suite'));
const rskOwner = await deployContract(
RSKOwner,
[auctionRegistrar.options.address, rns.options.address, namehash('rsk')]
);
const namePrice = await deployContract(NamePrice);
const bytesUtils = await deployContract(BytesUtils);
const LinkedFIFSRegistrar = link(FIFSRegistrar, 'BytesUtils', bytesUtils.options.address);
const LinkedFIFSAddrRegistrar = link(FIFSAddrRegistrar, 'BytesUtils', bytesUtils.options.address);
const LinkedRenewer = link(Renewer, 'BytesUtils', bytesUtils.options.address);
const fifsRegistrar = await deployContract(
LinkedFIFSRegistrar,
[rif.options.address, rskOwner.options.address, from, namePrice.options.address]
)
console.log(chalk.bold('Adding FIFS Registrar'));
await executeTx(rskOwner.methods.addRegistrar(fifsRegistrar.options.address));
const fifsAddrRegistrar = await deployContract(
LinkedFIFSAddrRegistrar,
[
rif.options.address,
rskOwner.options.address,
from,
namePrice.options.address,
rns.options.address,
namehash('rsk')
]
)
console.log(chalk.bold('Adding FIFS Addr Registrar'));
await executeTx(rskOwner.methods.addRegistrar(fifsAddrRegistrar.options.address));
const renewer = await deployContract(
LinkedRenewer,
[rif.options.address, rskOwner.options.address, from, namePrice.options.address]
)
console.log(chalk.bold('Adding FIFS Addr Registrar'));
await executeTx(rskOwner.methods.addRenewer(renewer.options.address));
console.log(chalk.bold('Connecting RSK Registrar suite'));
await executeTx(rns.methods.setSubnodeOwner('0x00', web3.utils.sha3('rsk'), rskOwner.options.address));
console.log();
let registeredDomainsRSKRegistrar;
if (
(registrations && registrations.length) ||
(registrationsWithAddr && registrationsWithAddr.length)
) {
console.log(chalk.bold('Registering domains'));
if(registrationsWithAddr && registrationsWithAddr.length)
registrations = registrations.concat(registrationsWithAddr);
let labels = [];
let domains = [];
registrations.forEach(name => {
labels.push(web3.utils.sha3(name));
domains.push(`${name}.rsk`)
});
await executeTx(rskOwner.methods.addRegistrar(from));
let allRegistrations = [];
for (let i = 0; i < labels.length; i += 1)
allRegistrations.push(executeTx(rskOwner.methods.register(labels[i], from, web3.utils.toBN('99999999999999'))));
await Promise.all(allRegistrations);
registeredDomainsRSKRegistrar = domains;
console.log(`Registered: ${domains}`);
console.log();
if (registrationsWithAddr && registrationsWithAddr.length) {
console.log(chalk.bold('Setting addrs'));
const allAddrs = [];
for (let i = 0; i < registrationsWithAddr.length; i += 1)
allAddrs.push(
executeTx(publicResolver.methods.setAddr(namehash(`${registrationsWithAddr[i]}.rsk`), from))
);
await Promise.all(allAddrs);
console.log(`Set addr for: ${domains}`);
console.log();
}
}
console.log(chalk.italic('Definitive resolver (proxy deployment)'));
const proxyFactory = await deployContract(ProxyFactory);
const proxyAdmin = await deployContract(ProxyAdmin);
const resolverV1 = await deployContract(ResolverV1);
console.log(chalk.bold('Creating instance'));
const salt = '16';
const data = encodeCall('initialize', ['address'], [rns.options.address]);
await executeTx(proxyFactory.methods.deploy(salt, resolverV1.options.address, proxyAdmin.options.address, data));
const deploymentAddress = await proxyFactory.methods.getDeploymentAddress(salt, from).call();
const defintiveResolver = new web3.eth.Contract(ResolverV1.abi, deploymentAddress);
console.log()
console.log('Done! Summary:')
console.log('|===============================|============================================|')
console.log('| Contract | Address |')
console.log('|===============================|============================================|')
console.log(`| RNS registry | ${rns.options.address} |`)
console.log(`| Public resolver (legacy) | ${publicResolver.options.address} |`)
console.log(`| Multi-chain resolver (legacy) | ${multiChainResolver.options.address} |`)
console.log(`| Name resolver | ${nameResolver.options.address} |`)
console.log(`| Reverse registrar | ${reverseRegistrar.options.address} |`)
console.log(`| RIF token | ${rif.options.address} |`)
console.log(`| Auction registrar (legacy) | ${auctionRegistrar.options.address} |`)
console.log(`| RSK owner | ${rskOwner.options.address} |`)
console.log(`| Name price | ${namePrice.options.address} |`)
console.log(`| Bytes utils | ${bytesUtils.options.address} |`)
console.log(`| FIFS registrar | ${fifsRegistrar.options.address} |`)
console.log(`| FIFS addr registrar | ${fifsAddrRegistrar.options.address} |`)
console.log(`| Renewer | ${renewer.options.address} |`)
console.log(`| Definitive resolver | ${defintiveResolver.options.address} |`)
console.log('|===============================|============================================|\n')
if (registeredDomainsAuction)
console.log(`Registered domains with the auction registrar(legacy): ${registeredDomainsAuction}`);
if (!registeredDomainsAuction && auctionRegistrations && auctionRegistrations.length)
console.log(`${chalk.yellowBright('An error occurred registering domains with the auction.')} Ensure you can execute evm_increaseTime and evm_mine`);
if (registeredDomainsRSKRegistrar)
console.log(`Registered domains with the current registrar: ${registeredDomainsRSKRegistrar}`);
if(registrationsWithAddr)
console.log(`Set addr for: ${registrationsWithAddr}`);
console.log()
return {
rns,
publicResolver,
multiChainResolver,
nameResolver,
reverseRegistrar,
rif,
auctionRegistrar,
rskOwner,
namePrice,
bytesUtils,
fifsRegistrar,
fifsAddrRegistrar,
renewer,
defintiveResolver
}
}
module.exports = main;