Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

import devchain from aragen #246

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions src/cli.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
require('@babel/polyfill')
const { environmentMiddleware, manifestMiddleware, moduleMiddleware } = require('./middleware')
const { findProjectRoot } = require('./util')
const ConsoleReporter = require('./reporters/ConsoleReporter')
const { ConsoleReporter, ens } = require('@aragon/aragen')
const url = require('url')

const MIDDLEWARES = [
Expand Down Expand Up @@ -67,7 +67,7 @@ cmd.option('environment', {
// APM
cmd.option('apm.ens-registry', {
description: 'Address of the ENS registry. This will be overwritten if the selected \'--environment\' from your arapp.json includes a `registry` property',
default: require('@aragon/aragen').ens
default: ens
})
cmd.group(['apm.ens-registry', 'eth-rpc'], 'APM:')

Expand Down
127 changes: 2 additions & 125 deletions src/commands/devchain.js
Original file line number Diff line number Diff line change
@@ -1,126 +1,3 @@
const TaskList = require('listr')
const ncp = require('ncp')
const ganache = require('ganache-core')
const Web3 = require('web3')
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 { devchain } = require('@aragon/aragen').commands

const { BLOCK_GAS_LIMIT, MNEMONIC } = require('../helpers/ganache-vars')

exports.command = 'devchain'
exports.describe = 'Open a test chain for development and pass arguments to ganache'
exports.builder = {
port: {
description: 'The port to run the local chain on',
default: 8545
},
reset: {
type: 'boolean',
default: false,
description: 'Reset devchain to snapshot'
},
accounts: {
default: 2,
description: 'Number of accounts to print'
}
}

exports.task = async function ({ port = 8545, reset = false, showAccounts = 2 }) {
const removeDir = promisify(rimraf)
const mkDir = promisify(mkdirp)
const recursiveCopy = promisify(ncp)

const snapshotPath = path.join(os.homedir(), `.aragon/ganache-db-${port}`)

const tasks = new TaskList([
{
title: 'Setting up a new chain from latest Aragon snapshot',
task: async (ctx, task) => {
await removeDir(snapshotPath)
await mkDir(path.resolve(snapshotPath, '..'))
const aragen = path.resolve(require.resolve('@aragon/aragen'), '../aragon-ganache')
await recursiveCopy(aragen, snapshotPath)
},
enabled: () => !fs.existsSync(snapshotPath) || reset
},
{
title: 'Starting a local chain from snapshot',
task: async (ctx, task) => {
const server = ganache.server({
// Start on a different networkID every time to avoid Metamask nonce caching issue:
// https://github.com/aragon/aragon-cli/issues/156
network_id: parseInt(1e8 * Math.random()),
gasLimit: BLOCK_GAS_LIMIT,
mnemonic: MNEMONIC,
db_path: snapshotPath
})
const listen = () => (
new Promise((resolve, reject) => {
server.listen(port, (err) => {
if (err) return reject(err)

task.title = `Local chain started at port ${port}`
resolve()
})
})
)
await listen()

ctx.web3 = new Web3(
new Web3.providers.WebsocketProvider(`ws://localhost:${port}`)
)
const accounts = await ctx.web3.eth.getAccounts()

ctx.accounts = accounts.slice(0, parseInt(showAccounts))
ctx.mnemonic = MNEMONIC

const ganacheAccounts = server.provider.manager.state.accounts
ctx.privateKeys = ctx.accounts.map((address) => (
{ key: ganacheAccounts[address.toLowerCase()].secretKey.toString('hex'), address }
))
}
}])

return tasks
}

exports.printAccounts = (reporter, privateKeys) => {
const firstAccountComment = '(this account is used to deploy DAOs, it has more permissions)'

const formattedAccounts = privateKeys.map(({ address, key }, i) =>
chalk.bold(`Address #${i + 1}: ${address} ${i === 0 ? firstAccountComment : ''}\nPrivate key: `) + key
)

reporter.info(`Here are some Ethereum accounts you can use.
The first one will be used for all the actions the CLI 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${mnemonic}\n`)
}

exports.printResetNotice = (reporter, reset) => {
if (reset) {
reporter.warning(`The devchain was reset, some steps need to be done to prevent issues:
- Reset the application cache in Aragon Core 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, reset, accounts }) => {
const task = await exports.task({ port, reset, showAccounts: accounts })
const { privateKeys, mnemonic } = await task.run()
exports.printAccounts(reporter, privateKeys)
exports.printMnemonic(reporter, mnemonic)
exports.printResetNotice(reporter, reset)

reporter.info(`Devchain running: ${chalk.bold('http://localhost:' + port)}.`)
}
Object.assign(exports, devchain)
4 changes: 0 additions & 4 deletions src/helpers/ganache-vars.js

This file was deleted.

49 changes: 0 additions & 49 deletions src/reporters/ConsoleReporter.js

This file was deleted.