Skip to content

Commit

Permalink
New command: change-controller (#339)
Browse files Browse the repository at this point in the history
  • Loading branch information
kernelwhisperer authored Jan 16, 2019
1 parent ed54c40 commit 92344f4
Show file tree
Hide file tree
Showing 4 changed files with 259 additions and 1 deletion.
26 changes: 26 additions & 0 deletions src/commands/dao_cmds/token.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
const {
manifestMiddleware,
moduleMiddleware,
environmentMiddleware,
} = require('../../middleware')

const MIDDLEWARES = [
manifestMiddleware,
moduleMiddleware,
environmentMiddleware,
]

exports.command = 'token <command>'

exports.describe = 'Create and interact with MiniMe tokens'

exports.builder = function(yargs) {
return yargs
.commandDir('token_cmds', {
visit: cmd => {
cmd.middlewares = MIDDLEWARES
return cmd
},
})
.demandCommand(1, 'You need to specify a command')
}
82 changes: 82 additions & 0 deletions src/commands/dao_cmds/token_cmds/change-controller.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
const TaskList = require('listr')
const { ensureWeb3 } = require('../../../helpers/web3-fallback')
const { getContract } = require('../../../util')
const listrOpts = require('../../../helpers/listr-options')
const chalk = require('chalk')

exports.command = 'change-controller <token-address> <new-controller>'

exports.describe = 'Change the controller of a MiniMe token'

exports.builder = yargs => {
return yargs
.positional('token-address', {
description: 'Address of the MiniMe token',
})
.positional('new-controller', {
description: 'Address of the new controller',
})
}

exports.task = async ({ web3, tokenAddress, newController, silent, debug }) => {
// Decode sender
const accounts = await web3.eth.getAccounts()
const from = accounts[0]

return new TaskList(
[
{
title: 'Changing the MiniMe token controller',
task: async (ctx, task) => {
let artifact = getContract('@aragon/os', 'MiniMeToken')
let contract = new web3.eth.Contract(artifact.abi, tokenAddress)

const tx = contract.methods.changeController(newController)
// this fails if from is not passed
const gas = await tx.estimateGas({ from })

const sendPromise = tx.send({ from, gas })
sendPromise
.on('transactionHash', transactionHash => {
ctx.txHash = transactionHash
})
.on('error', function(error) {
throw error
})

task.output = `Waiting for the transaction to be mined...`
return sendPromise
},
},
],
listrOpts(silent, debug)
)
}

exports.handler = async function({
reporter,
network,
tokenAddress,
newController,
silent,
debug,
}) {
const web3 = await ensureWeb3(network)

const task = await exports.task({
web3,
reporter,
tokenAddress,
newController,
silent,
debug,
})
return task.run().then(ctx => {
reporter.success(
`Successfully changed the controller of ${tokenAddress} to ${newController}`
)
reporter.info(`Transaction hash: ${chalk.bold(ctx.txHash)}`)

process.exit()
})
}
150 changes: 150 additions & 0 deletions src/commands/dao_cmds/token_cmds/new.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,150 @@
const TaskList = require('listr')
const { ensureWeb3 } = require('../../../helpers/web3-fallback')
const { getContract } = require('../../../util')
const listrOpts = require('../../../helpers/listr-options')
const chalk = require('chalk')

const ZERO_ADDR = '0x0000000000000000000000000000000000000000'

exports.command = 'new <token-name> <symbol> [decimal-units] [transfer-enabled]'

exports.describe = 'Create a new MiniMe token'

exports.builder = yargs => {
return yargs
.positional('token-name', {
description: 'Full name of the new Token',
})
.positional('symbol', {
description: 'Symbol of the new Token',
})
.option('transfer-enabled', {
description: 'Whether the new token will have transfers enabled',
default: true,
})
.option('decimal-units', {
description: 'Total decimal units the new token will use',
default: 18,
})
}

exports.task = async ({
web3,
reporter,
tokenName,
symbol,
transferEnabled,
decimalUnits,
silent,
debug,
}) => {
// Decode sender
const accounts = await web3.eth.getAccounts()
const from = accounts[0]

return new TaskList(
[
{
title: 'Deploy the MiniMeTokenFactory contract',
task: async (ctx, task) => {
let artifact = getContract('@aragon/os', 'MiniMeTokenFactory')
let contract = new web3.eth.Contract(artifact.abi)

const deployTx = contract.deploy({ data: artifact.bytecode })
const gas = await deployTx.estimateGas()

const deployPromise = deployTx.send({ from, gas })
deployPromise
.on('receipt', function(receipt) {
ctx.factoryAddress = receipt.contractAddress
})
.on('transactionHash', transactionHash => {
ctx.factoryTxHash = transactionHash
})
.on('error', function(error) {
throw error
})

task.output = `Waiting for the transaction to be mined...`
return deployPromise
},
},
{
title: 'Deploy the MiniMeToken contract',
task: async (ctx, task) => {
let artifact = getContract('@aragon/os', 'MiniMeToken')
let contract = new web3.eth.Contract(artifact.abi)

const deployTx = contract.deploy({
data: artifact.bytecode,
arguments: [
ctx.factoryAddress,
ZERO_ADDR,
0,
tokenName,
decimalUnits,
symbol,
transferEnabled,
],
})
const gas = await deployTx.estimateGas()

const deployPromise = deployTx.send({ from, gas: gas + 1e6 })
deployPromise
.on('receipt', function(receipt) {
ctx.tokenAddress = receipt.contractAddress
})
.on('transactionHash', transactionHash => {
ctx.tokenTxHash = transactionHash
})
.on('error', function(error) {
throw error
})

task.output = `Waiting for the transaction to be mined...`
return deployPromise
},
},
],
listrOpts(silent, debug)
)
}

exports.handler = async function({
reporter,
network,
tokenName,
symbol,
transferEnabled,
decimalUnits,
silent,
debug,
}) {
const web3 = await ensureWeb3(network)

const task = await exports.task({
web3,
reporter,
tokenName,
symbol,
transferEnabled,
decimalUnits,
silent,
debug,
})
return task.run().then(ctx => {
reporter.success(
`Successfully deployed the token at ${chalk.bold(ctx.tokenAddress)}`
)
reporter.info(`Token transaction hash: ${ctx.tokenTxHash}`)

reporter.success(
`Successfully deployed the token factory at ${chalk.bold(
ctx.factoryAddress
)}`
)
reporter.info(`Token factory transaction hash: ${ctx.factoryTxHash}`)

process.exit()
})
}
2 changes: 1 addition & 1 deletion src/commands/dao_cmds/utils/daoArg.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ const isValidAragonID = dao => /[a-z0-9]+\.eth/.test(dao)

module.exports = yargs => {
return yargs.positional('dao', {
description: 'Address of the Kernel or AragonID)',
description: 'Address of the Kernel or AragonID',
type: 'string',
coerce: dao =>
!isAddress(dao) && !isValidAragonID(dao)
Expand Down

0 comments on commit 92344f4

Please sign in to comment.