Skip to content

Commit

Permalink
Extracts logic from apm commands (#871)
Browse files Browse the repository at this point in the history
* Enabling verbose in AVA tests
* Review: Extracts logic from apm commands: info command (#898)
* Extracts logic from apm commands: extract-functions command (#870)
* Extracts logic from apm commands: versions command (#891)
* Extracts logic from apm commands: packages command (#892)
* Extracts logic from apm commands: grant command (#902)
* Fix test import
  • Loading branch information
theethernaut authored and macor161 committed Nov 22, 2019
1 parent 4215e1b commit 5a3dc58
Show file tree
Hide file tree
Showing 21 changed files with 1,275 additions and 189 deletions.
58 changes: 0 additions & 58 deletions packages/aragon-cli/src/commands/apm_cmds/extract-functions.js

This file was deleted.

74 changes: 32 additions & 42 deletions packages/aragon-cli/src/commands/apm_cmds/grant.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
const APM = require('@aragon/apm')
const ACL = require('./util/acl')
const { ensureWeb3 } = require('../../helpers/web3-fallback')
const chalk = require('chalk')
const grantNewVersionsPermission = require('../../lib/apm/grantNewVersionsPermission')

exports.command = 'grant [grantees..]'
exports.describe =
Expand All @@ -20,7 +19,6 @@ exports.handler = async function({
// Globals
reporter,
gasPrice,
cwd,
network,
module,
apm: apmOptions,
Expand All @@ -30,48 +28,40 @@ exports.handler = async function({
const web3 = await ensureWeb3(network)
apmOptions.ensRegistryAddress = apmOptions['ens-registry']

const apm = await APM(web3, apmOptions)
const acl = ACL({ web3, network })

const repo = await apm.getRepository(module.appName).catch(() => null)
if (repo === null) {
throw new Error(
`Repository ${module.appName} does not exist and it's registry does not exist`
)
}

if (grantees.length === 0) {
reporter.warning('No grantee addresses provided')
const progressHandler = (step, data) => {
switch (step) {
case 1:
reporter.info(`Fetching repository`)
break
case 2:
// eslint-disable-next-line no-case-declarations
const address = data
reporter.info(
`Granting permission to publish on ${chalk.blue(
module.appName
)} for ${address}`
)
break
case 3:
// eslint-disable-next-line no-case-declarations
const txHash = data
reporter.success(`Successful transaction (${chalk.blue(txHash)})`)
break
}
}

/* eslint-disable-next-line */
for (const address of grantees) {
reporter.info(
`Granting permission to publish on ${chalk.blue(
module.appName
)} for ${chalk.green(address)}`
try {
await grantNewVersionsPermission(
web3,
module.appName,
apmOptions,
grantees,
progressHandler,
{ gasPrice: gasPrice || network.gasPrice }
)

// Decode sender
const accounts = await web3.eth.getAccounts()
const from = accounts[0]

// Build transaction
const transaction = await acl.grant(repo.options.address, address)

transaction.from = from
transaction.gasPrice = network.gasPrice || gasPrice
// the recommended gasLimit is already calculated by the ACL module

try {
const receipt = await web3.eth.sendTransaction(transaction)
reporter.success(
`Successful transaction (${chalk.blue(receipt.transactionHash)})`
)
} catch (e) {
reporter.error(`${e}\n${chalk.red('Transaction failed')}`)
process.exit(1)
}
} catch (err) {
reporter.error(`${err}\n${chalk.red('Command failed')}`)
process.exit(1)
}

process.exit(0)
Expand Down
69 changes: 41 additions & 28 deletions packages/aragon-cli/src/commands/apm_cmds/info.js
Original file line number Diff line number Diff line change
@@ -1,44 +1,57 @@
const APM = require('@aragon/apm')
const chalk = require('chalk')
const TaskList = require('listr')
const defaultAPMName = require('@aragon/cli-utils/src/helpers/default-apm')
const { ensureWeb3 } = require('../../helpers/web3-fallback')
const getRepoTask = require('../dao_cmds/utils/getRepoTask')
const getApmRepo = require('../../lib/apm/getApmRepo')

exports.command = 'info <apmRepo> [apmRepoVersion]'

exports.describe = 'Get information about a package'

exports.builder = getRepoTask.args
exports.builder = yargs => {
return yargs
.option('apmRepo', {
describe: 'Name of the aragonPM repo',
})
.option('apmRepoVersion', {
describe: 'Version of the package upgrading to',
default: 'latest',
})
}

exports.handler = async function({
apmRepo,
apm: apmOptions,
apmRepoVersion,
apm: apmOptions,
network,
}) {
const web3 = await ensureWeb3(network)
apmRepo = defaultAPMName(apmRepo)
apmOptions.ensRegistryAddress = apmOptions['ens-registry']
const apm = await APM(web3, apmOptions)

const tasks = new TaskList([
{
title: `Fetching ${chalk.bold(apmRepo)}@${apmRepoVersion}`,
task: getRepoTask.task({
apm,
apmRepo,
apmRepoVersion,
artifactRequired: false,
}),
},
])

return tasks.run().then(ctx => {
delete ctx.repo.abi
delete ctx.repo.environments

console.log(JSON.stringify(ctx.repo, null, 2))
process.exit()
})

const apmRepoName = defaultAPMName(apmRepo)

const progressHandler = step => {
switch (step) {
case 1:
console.log(`Initialize aragonPM`)
break
case 2:
// TODO: Use reporter instead of chalk? Should reporter have a 'title' function?
console.log(`Fetching ${chalk.bold(apmRepo)}@${apmRepoVersion}`)
break
}
}

const apmRepoObject = await getApmRepo(
web3,
apmRepoName,
apmRepoVersion,
apmOptions,
progressHandler
)
// TODO: Improve parsing of abi and env to display useful information
delete apmRepoObject.abi
delete apmRepoObject.environments

const apmRepoJSON = JSON.stringify(apmRepoObject, null, 2)
console.log(apmRepoJSON)
process.exit()
}
74 changes: 40 additions & 34 deletions packages/aragon-cli/src/commands/apm_cmds/packages.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
const APM = require('@aragon/apm')
const chalk = require('chalk')
const Table = require('cli-table')
const TaskList = require('listr')
const { ensureWeb3 } = require('../../helpers/web3-fallback')
const getApmRegistryPackages = require('../../lib/apm/getApmRegistryPackages')

exports.command = 'packages [apmRegistry]'

Expand All @@ -16,52 +15,59 @@ exports.builder = function(yargs) {
})
}

exports.handler = async function({
reporter,
apmRegistry,
network,
apm: apmOptions,
}) {
exports.handler = async function({ apmRegistry, network, apm: apmOptions }) {
const web3 = await ensureWeb3(network)

apmOptions.ensRegistryAddress = apmOptions['ens-registry']
const apm = APM(web3, apmOptions)
let packages

const tasks = new TaskList([
{
title: `Fetching APM Registry: ${apmRegistry}`,
task: async (ctx, task) => {
// TODO add a new method to APM to allow fetching a registry without appId
ctx.registry = await apm.getRepoRegistry(`vault.${apmRegistry}`)
},
},
{
title: 'Gathering Repos',
title: `Fetching APM packages for ${apmRegistry}`,
task: async (ctx, task) => {
const e = await ctx.registry.getPastEvents('NewRepo', { fromBlock: 0 })
task.output = `Initializing APM`

ctx.names = e.map(ev => ev.returnValues.name)
ctx.versions = await Promise.all(
e.map(async ev => apm.getLatestVersion(ev.returnValues.id))
const progressHandler = step => {
switch (step) {
case 1:
task.output = `Fetching APM Registry`
break
case 2:
task.output = `Gathering packages in registry`
break
}
}

packages = await getApmRegistryPackages(
web3,
apmRegistry,
apmOptions,
progressHandler
)
},
},
])

return tasks.run().then(ctx => {
reporter.success('Successfully fetched packages')

const rows = ctx.versions.map((info, index) => {
return [ctx.names[index], info.version]
})
await tasks.run()

const table = new Table({
head: ['App', 'Latest Version'].map(x => chalk.white(x)),
})
displayPackages(packages)
process.exit()
}

rows.forEach(r => table.push(r))
/**
* Display packages and their version in a table
*
* @param {Object[]} packages Packages
* @returns {void}
*/
function displayPackages(packages) {
const table = new Table({
head: ['App', 'Latest Version'],
})

console.log(table.toString())
process.exit()
packages.forEach(aPackage => {
const row = [aPackage.name, aPackage.version]
table.push(row)
})

console.log(table.toString())
}
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,9 @@ async function generateApplicationArtifact(

// Analyse contract functions and returns an array
// > [{ sig: 'transfer(address)', role: 'X_ROLE', notice: 'Transfers..'}]
artifact.functions = await extract(path.resolve(cwd, artifact.path))
artifact.functions = (
await extract(path.resolve(cwd, artifact.path))
).functions
// extract abi for each function
// > [{ sig: , role: , notice: , abi: }]
decorateFunctionsWithAbi(artifact.functions, artifact.abi, web3)
Expand Down
Loading

0 comments on commit 5a3dc58

Please sign in to comment.