Smarter cli script

It now allows you to list available functions and allows to call
functions on the wrapper or on the contract directly.
This commit is contained in:
bumi 2018-04-23 12:10:20 +02:00
parent 85032353ca
commit bd2af6ed72

View File

@ -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', () => {