-
Notifications
You must be signed in to change notification settings - Fork 27
/
Copy pathstart.js
239 lines (213 loc) · 6.17 KB
/
start.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
const TaskList = require('listr')
const ncp = require('ncp')
const ganache = require('../build/ganache-core.node.cli.js')
const ethers = require('ethers')
const { promisify } = require('util')
const os = require('os')
const path = require('path')
const rimraf = require('rimraf')
const mkdirp = require('mkdirp')
const chalk = require('chalk')
const fs = require('fs')
const listrOpts = require('@aragon/cli-utils/src/helpers/listr-options')
const pjson = require('../../package.json')
const devchainStatus = require('./status')
const { BLOCK_GAS_LIMIT, MNEMONIC } = require('../helpers/ganache-vars')
exports.command = 'start'
exports.describe =
'Open a test chain for development and pass arguments to ganache'
exports.builder = yargs => {
return yargs
.option('port', {
description: 'The port to run the local chain on',
default: 8545,
alias: 'p',
})
.option('network-id', {
description: 'Network id to connect with',
alias: 'i',
})
.option('block-time', {
description: 'Specify blockTime in seconds for automatic mining',
alias: 'b',
})
.option('gas-limit', {
default: BLOCK_GAS_LIMIT,
description: 'Block gas limit. Must be specified as a hex string',
alias: 'l',
})
.option('reset', {
type: 'boolean',
default: false,
description: 'Reset devchain to snapshot',
alias: 'r',
})
.option('accounts', {
default: 2,
description: 'Number of accounts to print',
alias: 'a',
})
.option('verbose', {
default: false,
type: 'boolean',
description: 'Enable verbose devchain output',
alias: 'v',
})
}
exports.task = async function({
port = 8545,
networkId,
blockTime,
gasLimit = BLOCK_GAS_LIMIT,
verbose = false,
reset = false,
showAccounts = 2,
reporter,
silent,
debug,
}) {
const removeDir = promisify(rimraf)
const mkDir = promisify(mkdirp)
const recursiveCopy = promisify(ncp)
const snapshotPath = path.join(
os.homedir(),
`.aragon/aragen-db-${pjson.version}`
)
const tasks = new TaskList(
[
{
title: 'Check devchain status',
task: async ctx => {
const task = await devchainStatus.task({
port,
reset,
silent,
debug,
})
const { portTaken, processID } = await task.run()
if (portTaken && !reset) {
throw new Error(
`Process with ID ${chalk.red(
processID
)} already running at port ${chalk.blue(port)}`
)
}
},
},
{
title: 'Setting up a new chain from latest Aragon snapshot',
task: async (ctx, task) => {
await removeDir(snapshotPath)
await mkDir(path.resolve(snapshotPath, '..'))
const snapshot = path.join(__dirname, '../../aragon-ganache')
await recursiveCopy(snapshot, snapshotPath)
},
enabled: () => !fs.existsSync(snapshotPath) || reset,
},
{
title: 'Starting a local chain from snapshot',
task: async (ctx, task) => {
ctx.id = parseInt(networkId) || parseInt(1e8 * Math.random())
const options = {
network_id: ctx.id,
blockTime,
gasLimit,
mnemonic: MNEMONIC,
db_path: snapshotPath,
logger: verbose ? { log: reporter.info.bind(reporter) } : undefined,
debug,
}
const server = ganache.server(options)
const listen = () =>
new Promise((resolve, reject) => {
server.listen(port, err => {
if (err) return reject(err)
task.title = `Local chain started at port ${chalk.blue(port)}\n`
resolve()
})
})
await listen()
ctx.mnemonic = MNEMONIC
ctx.wallets = []
for (i = 1; i <= showAccounts; i++) {
let path = `m/44'/60'/0'/0/${i - 1}`
ctx.wallets.push(ethers.Wallet.fromMnemonic(MNEMONIC, path))
}
},
},
],
listrOpts(silent, debug)
)
return tasks
}
exports.printAccounts = (reporter, wallets) => {
const firstAccountComment =
'(account used to deploy DAOs, has more permissions)'
const formattedAccounts = wallets.map(
({ address, privateKey }, i) =>
`Address #${i + 1}: ${chalk.green(address)} ${
i === 0 ? firstAccountComment : ''
}\nPrivate key: ` +
chalk.blue(privateKey) +
'\n'
)
reporter.info(`Here are some Ethereum accounts you can use.
The first one will be used for all the actions the aragonCLI performs.
You can use your favorite Ethereum provider or wallet to import their private keys.
\n${formattedAccounts.join('\n')}`)
}
exports.printMnemonic = (reporter, mnemonic) => {
reporter.info(
`The accounts were generated from the following mnemonic phrase:\n${chalk.blue(
mnemonic
)}\n`
)
}
exports.printResetNotice = (reporter, reset) => {
if (reset) {
reporter.warning(`${chalk.yellow(
'The devchain was reset, some steps need to be done to prevent issues:'
)}
- Reset the application cache in Aragon Client by going to Settings -> Troubleshooting.
- If using Metamask: switch to a different network, and then switch back to the 'Private Network' (this will clear the nonce cache and prevent errors when sending transactions)
`)
}
}
exports.handler = async ({
reporter,
port,
networkId,
blockTime,
gasLimit,
reset,
verbose,
accounts,
silent,
debug,
}) => {
const task = await exports.task({
port,
networkId,
blockTime,
gasLimit,
reset,
verbose,
reporter,
showAccounts: accounts,
silent,
debug,
})
const { wallets, id, mnemonic } = await task.run()
exports.printAccounts(reporter, wallets)
exports.printMnemonic(reporter, mnemonic)
exports.printResetNotice(reporter, reset)
reporter.info(
'ENS instance deployed at:',
chalk.green('0x5f6f7e8cc7346a11ca2def8f827b7a0b612c56a1'),
'\n'
)
reporter.info(`Network Id: ${chalk.blue(id)}`, '\n')
reporter.info(
`Devchain running at: ${chalk.blue('http://localhost:' + port)}.`
)
}