From d42b0fa2dd941a3622e15b3df42b7f7c35168a36 Mon Sep 17 00:00:00 2001 From: bumi Date: Wed, 4 Apr 2018 22:24:33 +0200 Subject: [PATCH] Add a general purpose contract CLI This allows to call any method on any contract using the CLI. for example: $ truffle exec scripts/cli.js Operator proposalsCount --- README.mdown | 11 +++++++++++ scripts/cli.js | 34 ++++++++++++++++++++++++++++++++++ 2 files changed, 45 insertions(+) create mode 100644 scripts/cli.js diff --git a/README.mdown b/README.mdown index ce60c37..b4e2b7d 100644 --- a/README.mdown +++ b/README.mdown @@ -68,6 +68,17 @@ At some point these should be moved into a real nice CLI. To run these scripts use `truffle exec`. For example: `truffle exec scripts/add-proposal.js` +### cli.js + +Call any function on any contract: + + $ truffle exec scripts/cli.js [ ] + + For example: + $ truffle exec scripts/cli.js Operator proposalsCount + +Please note that the contract name and the function are case sensitive. + ### add-contributor.js Adds a new core contributor, creates a proposal for the new contributor and votes for that one. diff --git a/scripts/cli.js b/scripts/cli.js new file mode 100644 index 0000000..bccb2f3 --- /dev/null +++ b/scripts/cli.js @@ -0,0 +1,34 @@ +module.exports = function(callback) { + const Registry = artifacts.require('./Registry.sol'); + Registry.deployed().then(async (registry) => { + let contractName = process.argv[4]; + let method = process.argv[5]; + let args = process.argv.slice(6); + + if(!contractName) { + console.log("Usage:") + console.log(" truffle exec scripts/cli.js [ ]"); + callback(); + return; + } + + let contractAddress = await registry.getProxyFor(contractName); + console.log(`Using ${contractName} at ${contractAddress}`); + let contract = await artifacts.require(`./${contractName}`).at(contractAddress); + + console.log(`Calling ${method} with ${JSON.stringify(args)}`); + + contract[method](...args).then((result) => { + console.log('Result:'); + console.log(result); + + callback(); + }).catch((error) => { + console.log("Call failed. Probably the contract raised an error?\n"); + console.log("..."); + callback(error); + }); + + }) + +}