Merge pull request #11 from 67P/features/cli

Add a general purpose contract CLI
This commit is contained in:
fsmanuel 2018-04-07 22:02:31 +02:00 committed by GitHub
commit 3f79c35635
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 49 additions and 0 deletions

View File

@ -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 <Contract Name> <Function> [<optional> <arguments>]
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.

38
scripts/cli.js Normal file
View File

@ -0,0 +1,38 @@
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 <Contract name> <method to call> [<optional> <arguments>]");
callback();
return;
}
let contractAddress = await registry.getProxyFor(contractName);
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) => {
console.log("\nResult:");
console.log(result);
callback();
}).catch((error) => {
console.log("Call failed. Probably the contract raised an error?\n");
console.log("...");
callback(error);
});
})
}