Improve helper scripts #41

Merged
bumi merged 7 commits from feature/improve-scripts into master 2018-04-29 07:43:23 +00:00
7 changed files with 111 additions and 51 deletions

View File

@ -81,30 +81,29 @@ 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>]
$ truffle exec scripts/cli.js
For example:
$ truffle exec scripts/cli.js Operator proposalsCount
### repl.js
Similar to cli.js but only provides a REPL with an initialized `kredits` instance.
Please note that the contract name and the function are case sensitive.
$ truffle exec scripts/repl.js
### add-contributor.js
Adds a new core contributor, creates a proposal for the new contributor and votes for that one.
$ truffle exec scripts/add-contributor.js <ethereum address> [<profile IPFS hash>]
$ truffle exec scripts/add-contributor.js
### add-proposal.js
Adds a new proposal for an existing contributor
$ truffle exec scripts/add-proposal.js <ethereum address> [<proposal IPFS hash>]
$ truffle exec scripts/add-proposal.js
### send-funds.js
Sends funds to an address. Helpful in development mode to for example fund a metamask account.
$ truffle exec scripts/send-funds.js <ethereum address>
$ truffle exec scripts/send-funds.js
### seeds.js
Run seeds defined in `config/seeds.js`.

View File

@ -6,4 +6,8 @@ let contractCalls = [
['Operator', 'addProposal', [{contributorId: 3, amount: 100, kind: 'code', description: 'hacks on kredits', url: '' }, {gasLimit: 350000}]],
['Operator', 'vote', ['1', {gasLimit: 250000}]]
];
module.exports = { contractCalls };
let funds = [
'0x7e8f313c56f809188313aa274fa67ee58c31515d',
'0xa502eb4021f3b9ab62f75b57a94e1cfbf81fd827'
];
module.exports = { contractCalls, funds };

View File

@ -1,6 +1,5 @@
const Registry = artifacts.require('./Registry.sol');
const promptly = require('promptly');
const bs58 = require('bs58');
const ethers = require('ethers');
const Kredits = require('../lib/kredits');
@ -37,7 +36,7 @@ module.exports = function(callback) {
console.log("\nAdding contributor:");
console.log(contributorAttributes);
kredits.Contributor.add(contributorAttributes, {gasLimit: 250000}).then((result) => {
kredits.Contributor.add(contributorAttributes, { gasLimit: 250000 }).then((result) => {
console.log("\n\nResult:");
console.log(result);
callback();

View File

@ -1,38 +1,46 @@
fsmanuel commented 2018-04-25 21:20:35 +00:00 (Migrated from github.com)
Review

Where does contributors come from?

Where does `contributors` come from?
fsmanuel commented 2018-04-25 21:20:35 +00:00 (Migrated from github.com)
Review

Where does contributors come from?

Where does `contributors` come from?
const Registry = artifacts.require('./Registry.sol');
const Operator = artifacts.require('./Operator.sol');
fsmanuel commented 2018-04-25 21:20:35 +00:00 (Migrated from github.com)
Review

Where does contributors come from?

Where does `contributors` come from?
const Contributors = artifacts.require('./Contributors.sol');
fsmanuel commented 2018-04-25 21:20:35 +00:00 (Migrated from github.com)
Review

Where does contributors come from?

Where does `contributors` come from?
const promptly = require('promptly');
const bs58 = require('bs58');
fsmanuel commented 2018-04-25 21:20:35 +00:00 (Migrated from github.com)
Review

Where does contributors come from?

Where does `contributors` come from?
fsmanuel commented 2018-04-25 21:20:35 +00:00 (Migrated from github.com)
Review

Where does contributors come from?

Where does `contributors` come from?
function getBytes32FromMultiash(multihash) {
fsmanuel commented 2018-04-25 21:20:35 +00:00 (Migrated from github.com)
Review

Where does contributors come from?

Where does `contributors` come from?
const decoded = bs58.decode(multihash);
fsmanuel commented 2018-04-25 21:20:35 +00:00 (Migrated from github.com)
Review

Where does contributors come from?

Where does `contributors` come from?
fsmanuel commented 2018-04-25 21:20:35 +00:00 (Migrated from github.com)
Review

Where does contributors come from?

Where does `contributors` come from?
return {
fsmanuel commented 2018-04-25 21:20:35 +00:00 (Migrated from github.com)
Review

Where does contributors come from?

Where does `contributors` come from?
digest: `0x${decoded.slice(2).toString('hex')}`,
fsmanuel commented 2018-04-25 21:20:35 +00:00 (Migrated from github.com)
Review

Where does contributors come from?

Where does `contributors` come from?
hashFunction: decoded[0],
fsmanuel commented 2018-04-25 21:20:35 +00:00 (Migrated from github.com)
Review

Where does contributors come from?

Where does `contributors` come from?
size: decoded[1],
fsmanuel commented 2018-04-25 21:20:35 +00:00 (Migrated from github.com)
Review

Where does contributors come from?

Where does `contributors` come from?
};
fsmanuel commented 2018-04-25 21:20:35 +00:00 (Migrated from github.com)
Review

Where does contributors come from?

Where does `contributors` come from?
}
fsmanuel commented 2018-04-25 21:20:35 +00:00 (Migrated from github.com)
Review

Where does contributors come from?

Where does `contributors` come from?
const ethers = require('ethers');
fsmanuel commented 2018-04-25 21:20:35 +00:00 (Migrated from github.com)
Review

Where does contributors come from?

Where does `contributors` come from?
const Kredits = require('../lib/kredits');
fsmanuel commented 2018-04-25 21:20:35 +00:00 (Migrated from github.com)
Review

Where does contributors come from?

Where does `contributors` come from?
module.exports = function(callback) {
Registry.deployed().then(async (registry) => {
var operatorAddress = await registry.getProxyFor('Operator');
fsmanuel commented 2018-04-25 21:20:35 +00:00 (Migrated from github.com)
Review

Where does contributors come from?

Where does `contributors` come from?
var contributorsAddress = await registry.getProxyFor('Contributors');
fsmanuel commented 2018-04-25 21:20:35 +00:00 (Migrated from github.com)
Review

Where does contributors come from?

Where does `contributors` come from?
const networkId = parseInt(web3.version.network);
fsmanuel commented 2018-04-25 21:20:35 +00:00 (Migrated from github.com)
Review

Where does contributors come from?

Where does `contributors` come from?
const provider = new ethers.providers.Web3Provider(
fsmanuel commented 2018-04-25 21:20:35 +00:00 (Migrated from github.com)
Review

Where does contributors come from?

Where does `contributors` come from?
web3.currentProvider, { chainId: networkId }
fsmanuel commented 2018-04-25 21:20:35 +00:00 (Migrated from github.com)
Review

Where does contributors come from?

Where does `contributors` come from?
);
fsmanuel commented 2018-04-25 21:20:35 +00:00 (Migrated from github.com)
Review

Where does contributors come from?

Where does `contributors` come from?
const kredits = await Kredits.setup(provider, provider.getSigner());
fsmanuel commented 2018-04-25 21:20:35 +00:00 (Migrated from github.com)
Review

Where does contributors come from?

Where does `contributors` come from?
var operator = await Operator.at(operatorAddress);
fsmanuel commented 2018-04-25 21:20:35 +00:00 (Migrated from github.com)
Review

Where does contributors come from?

Where does `contributors` come from?
var contributors = await Contributors.at(contributorsAddress);
fsmanuel commented 2018-04-25 21:20:35 +00:00 (Migrated from github.com)
Review

Where does contributors come from?

Where does `contributors` come from?
console.log(`Using operator at: ${kredits.Operator.contract.address}`);
fsmanuel commented 2018-04-25 21:20:35 +00:00 (Migrated from github.com)
Review

Where does contributors come from?

Where does `contributors` come from?
let recipientAddress = await promptly.prompt('Contributor address: ');
fsmanuel commented 2018-04-25 21:20:35 +00:00 (Migrated from github.com)
Review

Where does contributors come from?

Where does `contributors` come from?
let ipfsHash = await promptly.prompt('IPFS hash (blank for default): ', { default: 'QmQNA1hhVyL1Vm6HiRxXe9xmc6LUMBDyiNMVgsjThtyevs' });
fsmanuel commented 2018-04-25 21:20:35 +00:00 (Migrated from github.com)
Review

Where does contributors come from?

Where does `contributors` come from?
let contributor = await promptly.prompt('Contributor (address or id): ');
fsmanuel commented 2018-04-25 21:20:35 +00:00 (Migrated from github.com)
Review

Where does contributors come from?

Where does `contributors` come from?
let contributorId;
fsmanuel commented 2018-04-25 21:20:35 +00:00 (Migrated from github.com)
Review

Where does contributors come from?

Where does `contributors` come from?
if (contributor.length < 5) {
fsmanuel commented 2018-04-25 21:20:35 +00:00 (Migrated from github.com)
Review

Where does contributors come from?

Where does `contributors` come from?
contributorId = contributor;
fsmanuel commented 2018-04-25 21:20:35 +00:00 (Migrated from github.com)
Review

Where does contributors come from?

Where does `contributors` come from?
} else {
fsmanuel commented 2018-04-25 21:20:35 +00:00 (Migrated from github.com)
Review

Where does contributors come from?

Where does `contributors` come from?
contributorId = await kredits.Contributor.functions.getContributorIdByAddress(contributor);
fsmanuel commented 2018-04-25 21:20:35 +00:00 (Migrated from github.com)
Review

Where does contributors come from?

Where does `contributors` come from?
}
fsmanuel commented 2018-04-25 21:20:35 +00:00 (Migrated from github.com)
Review

Where does contributors come from?

Where does `contributors` come from?
console.log(`Creating a proposal for contributor ID #${contributorId}`);
fsmanuel commented 2018-04-25 21:20:35 +00:00 (Migrated from github.com)
Review

Where does contributors come from?

Where does `contributors` come from?
let multihash = getBytes32FromMultiash(ipfsHash);
fsmanuel commented 2018-04-25 21:20:35 +00:00 (Migrated from github.com)
Review

Where does contributors come from?

Where does `contributors` come from?
let contributionAttributes = {
fsmanuel commented 2018-04-25 21:20:35 +00:00 (Migrated from github.com)
Review

Where does contributors come from?

Where does `contributors` come from?
contributorId,
fsmanuel commented 2018-04-25 21:20:35 +00:00 (Migrated from github.com)
Review

Where does contributors come from?

Where does `contributors` come from?
amount: await promptly.prompt('Amount: '),
fsmanuel commented 2018-04-25 21:20:35 +00:00 (Migrated from github.com)
Review

Where does contributors come from?

Where does `contributors` come from?
description: await promptly.prompt('Description: '),
fsmanuel commented 2018-04-25 21:20:35 +00:00 (Migrated from github.com)
Review

Where does contributors come from?

Where does `contributors` come from?
kind: await promptly.prompt('Kind: ', { default: 'dev' }),
fsmanuel commented 2018-04-25 21:20:35 +00:00 (Migrated from github.com)
Review

Where does contributors come from?

Where does `contributors` come from?
url: await promptly.prompt('URL: ', { default: '' })
fsmanuel commented 2018-04-25 21:20:35 +00:00 (Migrated from github.com)
Review

Where does contributors come from?

Where does `contributors` come from?
}
fsmanuel commented 2018-04-25 21:20:35 +00:00 (Migrated from github.com)
Review

Where does contributors come from?

Where does `contributors` come from?
let contributorId = await contributors.getContributorIdByAddress(recipientAddress);
fsmanuel commented 2018-04-25 21:20:35 +00:00 (Migrated from github.com)
Review

Where does contributors come from?

Where does `contributors` come from?
fsmanuel commented 2018-04-25 21:20:35 +00:00 (Migrated from github.com)
Review

Where does contributors come from?

Where does `contributors` come from?
let result = await operator.addProposal(contributorId.toNumber(), 23, multihash.digest, multihash.hashFunction, multihash.size);
fsmanuel commented 2018-04-25 21:20:35 +00:00 (Migrated from github.com)
Review

Where does contributors come from?

Where does `contributors` come from?
console.log('Proposal added, tx: ', result.tx);
fsmanuel commented 2018-04-25 21:20:35 +00:00 (Migrated from github.com)
Review

Where does contributors come from?

Where does `contributors` come from?
console.log("\nAdding proposal:");
fsmanuel commented 2018-04-25 21:20:35 +00:00 (Migrated from github.com)
Review

Where does contributors come from?

Where does `contributors` come from?
console.log(contributionAttributes);
fsmanuel commented 2018-04-25 21:20:35 +00:00 (Migrated from github.com)
Review

Where does contributors come from?

Where does `contributors` come from?
kredits.Operator.addProposal(contributionAttributes, { gasLimit: 300000 }).then((result) => {
fsmanuel commented 2018-04-25 21:20:35 +00:00 (Migrated from github.com)
Review

Where does contributors come from?

Where does `contributors` come from?
console.log("\n\nResult:");
fsmanuel commented 2018-04-25 21:20:35 +00:00 (Migrated from github.com)
Review

Where does contributors come from?

Where does `contributors` come from?
console.log(result);
fsmanuel commented 2018-04-25 21:20:35 +00:00 (Migrated from github.com)
Review

Where does contributors come from?

Where does `contributors` come from?
callback();
}).catch((error) => {
fsmanuel commented 2018-04-25 21:20:35 +00:00 (Migrated from github.com)
Review

Where does contributors come from?

Where does `contributors` come from?
console.log('Failed to create proposal');
fsmanuel commented 2018-04-25 21:20:35 +00:00 (Migrated from github.com)
Review

Where does contributors come from?

Where does `contributors` come from?
console.log(error);
fsmanuel commented 2018-04-25 21:20:35 +00:00 (Migrated from github.com)
Review

Where does contributors come from?

Where does `contributors` come from?
});
fsmanuel commented 2018-04-25 21:20:35 +00:00 (Migrated from github.com)
Review

Where does contributors come from?

Where does `contributors` come from?
});
}

fsmanuel commented 2018-04-25 21:20:35 +00:00 (Migrated from github.com)
Review

Where does contributors come from?

Where does `contributors` come from?
fsmanuel commented 2018-04-25 21:20:35 +00:00 (Migrated from github.com)
Review

Where does contributors come from?

Where does `contributors` come from?

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]) {
fsmanuel commented 2018-04-25 21:31:18 +00:00 (Migrated from github.com)
Review

So good that we went with upper case contract names and the properties instead of the functions!
But it will breaks on Contributor, wouldn’t it? I think we should rename that contract next and align the module code to it. Will make the future much brighter!

So good that we went with upper case contract names and the properties instead of the functions! But it will breaks on `Contributor`, wouldn’t it? I think we should rename that contract next and align the module code to it. Will make the future much brighter!
bumi commented 2018-04-26 00:34:37 +00:00 (Migrated from github.com)
Review

yes, agree on renaming it! and maybe adding an alias for Contributor <=> Contributors

yes, agree on renaming it! and maybe adding an alias for Contributor <=> Contributors
fsmanuel commented 2018-04-26 10:43:41 +00:00 (Migrated from github.com)
Review

I opened #44 for the alias

I opened #44 for the alias
callback(new Error(`Method ${method} is not defined on ${contractName}`));
return;
}
let argumentInput = await promptly.prompt('Arguments (comma separated): ', { default: '' });
let args = [];
fsmanuel commented 2018-04-25 21:32:13 +00:00 (Migrated from github.com)
Review

👀 nice!

👀 nice!
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:");
fsmanuel commented 2018-04-25 21:38:20 +00:00 (Migrated from github.com)
Review

Haha master of es6. I think you don’t need the if and the initial args
let args = argumentInput.split(',') should return an empty array.

Haha master of es6. I think you don’t need the if and the initial `args` `let args = argumentInput.split(',')` should return an empty array.
bumi commented 2018-04-26 00:29:37 +00:00 (Migrated from github.com)
Review

oh yeah, true.

oh yeah, true.
bumi commented 2018-04-26 00:33:31 +00:00 (Migrated from github.com)
Review

ah noo. it returns an array with an empty string, that was the reason for the if. or what am I missing?

ah noo. it returns an array with an empty string, that was the reason for the if. or what am I missing?
fsmanuel commented 2018-04-26 10:35:51 +00:00 (Migrated from github.com)
Review

Ah ok.

Ah ok.
raucao commented 2018-04-26 10:40:37 +00:00 (Migrated from github.com)
Review

But if input is already not an empty string, wouldn't it return an array with one argument string?

But if input is already not an empty string, wouldn't it return an array with one argument string?
bumi commented 2018-04-26 11:22:18 +00:00 (Migrated from github.com)
Review

https://jsbin.com/qarikabane/edit?js,console

it would return [""] but if no arguments are given I need [].

https://jsbin.com/qarikabane/edit?js,console it would return `[""]` but if no arguments are given I need `[]`.
raucao commented 2018-04-26 11:36:42 +00:00 (Migrated from github.com)
Review

I was actually asking about the trim inside, but no idea why. It's for removing whitespace, so that's still needed.

I was actually asking about the `trim` inside, but no idea why. It's for removing whitespace, so that's still needed.
bumi commented 2018-04-26 21:19:06 +00:00 (Migrated from github.com)
Review

yep, trim() for potential whitespace as for example in "first, second"

yep, trim() for potential whitespace as for example in `"first, second"`
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', () => {

25
scripts/repl.js Normal file
View File

@ -0,0 +1,25 @@
const REPL = require('repl');
const promptly = require('promptly');
const ethers = require('ethers');
const Kredits = require('../lib/kredits');
module.exports = function(callback) {
const Registry = artifacts.require('./Registry.sol');
Registry.deployed().then(async (registry) => {
const networkId = parseInt(web3.version.network);
const provider = new ethers.providers.Web3Provider(
web3.currentProvider, { chainId: networkId }
);
const kredits = await Kredits.setup(provider, provider.getSigner());
console.log(`defined variables: kredits, web3`);
let r = REPL.start();
r.context.kredits = kredits;
r.context.web3 = web3;
r.on('exit', () => {
console.log('Bye');
callback();
});
});
}

View File

@ -17,6 +17,18 @@ module.exports = function(callback) {
);
const kredits = await Kredits.setup(provider, provider.getSigner());
let fundingAmount = 2;
each(seeds.funds, (address, next) => {
console.log(`funding ${address} with 2 ETH`);
web3.eth.sendTransaction({
to: address,
value: web3.toWei(fundingAmount),
from: web3.eth.accounts[0]
},
(result) => { next(); }
)
});
each(seeds.contractCalls, (call, next) => {
let [contractName, method, args] = call;
let contractWrapper = kredits[contractName];