From d42b0fa2dd941a3622e15b3df42b7f7c35168a36 Mon Sep 17 00:00:00 2001 From: bumi Date: Wed, 4 Apr 2018 22:24:33 +0200 Subject: [PATCH 1/3] 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); + }); + + }) + +} From 1488862b42eada408b2e4e58835518d63c1953ca Mon Sep 17 00:00:00 2001 From: bumi Date: Wed, 4 Apr 2018 22:42:16 +0200 Subject: [PATCH 2/3] Add check if method is defined on the contract --- scripts/cli.js | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/scripts/cli.js b/scripts/cli.js index bccb2f3..6fb2e19 100644 --- a/scripts/cli.js +++ b/scripts/cli.js @@ -16,6 +16,10 @@ module.exports = function(callback) { console.log(`Using ${contractName} at ${contractAddress}`); let contract = await artifacts.require(`./${contractName}`).at(contractAddress); + if(!contract[method]) { + callback(new Error(`Method ${method} is not defined on ${contractName}`)); + return; + } console.log(`Calling ${method} with ${JSON.stringify(args)}`); contract[method](...args).then((result) => { From ebb26f78c237fddd14160fb2c957864633c705f5 Mon Sep 17 00:00:00 2001 From: bumi Date: Sat, 7 Apr 2018 19:41:39 +0200 Subject: [PATCH 3/3] code style --- scripts/cli.js | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/scripts/cli.js b/scripts/cli.js index 6fb2e19..c617354 100644 --- a/scripts/cli.js +++ b/scripts/cli.js @@ -5,8 +5,8 @@ module.exports = function(callback) { let method = process.argv[5]; let args = process.argv.slice(6); - if(!contractName) { - console.log("Usage:") + if (!contractName) { + console.log("Usage:"); console.log(" truffle exec scripts/cli.js [ ]"); callback(); return; @@ -16,14 +16,14 @@ module.exports = function(callback) { console.log(`Using ${contractName} at ${contractAddress}`); let contract = await artifacts.require(`./${contractName}`).at(contractAddress); - if(!contract[method]) { + if (!contract[method]) { callback(new Error(`Method ${method} is not defined on ${contractName}`)); return; } console.log(`Calling ${method} with ${JSON.stringify(args)}`); contract[method](...args).then((result) => { - console.log('Result:'); + console.log("\nResult:"); console.log(result); callback();