From bd2af6ed727dbf0ab30a377bed1c99342c2125a9 Mon Sep 17 00:00:00 2001 From: bumi Date: Mon, 23 Apr 2018 12:10:20 +0200 Subject: [PATCH] Smarter cli script It now allows you to list available functions and allows to call functions on the wrapper or on the contract directly. --- scripts/cli.js | 41 +++++++++++++++++++++++++++-------------- 1 file changed, 27 insertions(+), 14 deletions(-) diff --git a/scripts/cli.js b/scripts/cli.js index f1c733f..4b10ee3 100644 --- a/scripts/cli.js +++ b/scripts/cli.js @@ -7,30 +7,43 @@ const Kredits = require('../lib/kredits'); module.exports = function(callback) { const Registry = artifacts.require('./Registry.sol'); Registry.deployed().then(async (registry) => { - let contractName = await promptly.prompt('Contract Name: '); - let method = await promptly.prompt('Function: '); - let argumentInput = await promptly.prompt('Arguments (comma separated): ', { default: '' }); - let args = []; - if (argumentInput !== '') { - args = argumentInput.split(',').map(a => a.trim()); - } - const networkId = parseInt(web3.version.network); const provider = new ethers.providers.Web3Provider( web3.currentProvider, { chainId: networkId } ); const kredits = await Kredits.setup(provider, provider.getSigner()); - const contract = kredits[contractName].contract; - console.log(`Using ${contractName} at ${contract.address}`); - console.log(`Calling ${method} with ${JSON.stringify(args)}`); + let contractName = await promptly.prompt('Contract Name: '); + const contractWrapper = kredits[contractName]; - if (!contract[method]) { + let method; + method = await promptly.prompt('Function (? for available functions): '); + while (method === '?') { + console.log(`Contract functions: ${JSON.stringify(Object.keys(contractWrapper.functions))}`); + console.log(`\nWrapper functions: ${JSON.stringify(Object.getOwnPropertyNames(Object.getPrototypeOf(contractWrapper)))}`); + console.log("\n"); + + method = await promptly.prompt('Function: '); + } + if (!contractWrapper[method] && !contractWrapper.functions[method]) { callback(new Error(`Method ${method} is not defined on ${contractName}`)); return; } + let argumentInput = await promptly.prompt('Arguments (comma separated): ', { default: '' }); + let args = []; + if (argumentInput !== '') { + args = argumentInput.split(',').map(a => a.trim()); + } + console.log(`Using ${contractName} at ${contractWrapper.contract.address}`); + console.log(`Calling ${method} with ${JSON.stringify(args)}`); - contract[method](...args).then((result) => { + let func; + if (contractWrapper[method]) { + func = contractWrapper[method]; + } else { + func = contractWrapper.functions[method]; + } + func.apply(contractWrapper, args).then((result) => { console.log("\nResult:"); console.log(result); @@ -38,7 +51,7 @@ module.exports = function(callback) { console.log(`defined variables: result, ${contractName}, kredis`); let r = REPL.start(); r.context.result = result; - r.context[contractName] = contract; + r.context[contractName] = contractWrapper; r.context.kredits = kredits; r.on('exit', () => {