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:
parent
85032353ca
commit
bd2af6ed72
@ -7,30 +7,43 @@ const Kredits = require('../lib/kredits');
|
|||||||
module.exports = function(callback) {
|
module.exports = function(callback) {
|
||||||
const Registry = artifacts.require('./Registry.sol');
|
const Registry = artifacts.require('./Registry.sol');
|
||||||
Registry.deployed().then(async (registry) => {
|
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 networkId = parseInt(web3.version.network);
|
||||||
const provider = new ethers.providers.Web3Provider(
|
const provider = new ethers.providers.Web3Provider(
|
||||||
web3.currentProvider, { chainId: networkId }
|
web3.currentProvider, { chainId: networkId }
|
||||||
);
|
);
|
||||||
const kredits = await Kredits.setup(provider, provider.getSigner());
|
const kredits = await Kredits.setup(provider, provider.getSigner());
|
||||||
|
|
||||||
const contract = kredits[contractName].contract;
|
let contractName = await promptly.prompt('Contract Name: ');
|
||||||
console.log(`Using ${contractName} at ${contract.address}`);
|
const contractWrapper = kredits[contractName];
|
||||||
console.log(`Calling ${method} with ${JSON.stringify(args)}`);
|
|
||||||
|
|
||||||
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}`));
|
callback(new Error(`Method ${method} is not defined on ${contractName}`));
|
||||||
return;
|
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("\nResult:");
|
||||||
console.log(result);
|
console.log(result);
|
||||||
|
|
||||||
@ -38,7 +51,7 @@ module.exports = function(callback) {
|
|||||||
console.log(`defined variables: result, ${contractName}, kredis`);
|
console.log(`defined variables: result, ${contractName}, kredis`);
|
||||||
let r = REPL.start();
|
let r = REPL.start();
|
||||||
r.context.result = result;
|
r.context.result = result;
|
||||||
r.context[contractName] = contract;
|
r.context[contractName] = contractWrapper;
|
||||||
r.context.kredits = kredits;
|
r.context.kredits = kredits;
|
||||||
|
|
||||||
r.on('exit', () => {
|
r.on('exit', () => {
|
||||||
|
Loading…
x
Reference in New Issue
Block a user