feat: list funders + refactoring

This commit is contained in:
Richard Ramos 2019-05-22 16:59:58 -04:00
parent d5d2786b97
commit b6c56cbc55
3 changed files with 53 additions and 24 deletions

View File

@ -1,7 +1,7 @@
var inquirer = require('inquirer');
const Web3 = require("web3");
const Table = require('cli-table');
const PledgeAdminUtils = require('./pledgeadmin-utils');
const Contracts = require("./contracts.js");
@ -69,23 +69,10 @@ class Actions {
async listProjects() {
try {
let numProjects = await this.contracts.LiquidPledging.methods.numberOfPledgeAdmins().call();
const table = new Table({
head: ['Id', 'Name', 'URL', 'ParentProject', 'Status', 'Commit Time', 'Owner', 'Plugin']
});
for(let i = 1; i <= numProjects; i++){
const pledgeAdmin = await this.contracts.LiquidPledging.methods.getPledgeAdmin(i).call();
if(pledgeAdmin.adminType !== '2') continue;
table.push(
[i, pledgeAdmin.name, pledgeAdmin.url, pledgeAdmin.parentProject, pledgeAdmin.canceled ? 'Canceled' : 'Active', pledgeAdmin.commitTime, pledgeAdmin.addr, pledgeAdmin.plugin]
);
}
console.log(table.toString());
const pledgeAdmins = await PledgeAdminUtils.getPledgeAdmins(this.contracts.LiquidPledging);
PledgeAdminUtils.printTable(pledgeAdmins.filter(x => x !== PledgeAdminUtils.constants.PROJECT));
} catch(error){
console.log(error);
console.log("Couldn't obtain the list of projects: ", error.message);
}
}
@ -95,19 +82,23 @@ class Actions {
doAction(text, async () => {
try {
const pledgeAdmin = await this.contracts.LiquidPledging.methods.getPledgeAdmin(params.id).call();
const table = new Table({
head: ['Id', 'Name', 'URL', 'ParentProject', 'Status', 'Commit Time', 'Owner', 'Plugin']
});
table.push(
[params.id, pledgeAdmin.name, pledgeAdmin.url, pledgeAdmin.parentProject, pledgeAdmin.canceled ? 'Canceled' : 'Active', pledgeAdmin.commitTime, pledgeAdmin.addr, pledgeAdmin.plugin]
);
console.log(table.toString());
PledgeAdminUtils.printTable([pledgeAdmin].filter(x => x !== PledgeAdminUtils.constants.PROJECT));
} catch(error){
console.log("Couldn't obtain the project: ", error.message);
}
});
}
async listFunders() {
try {
const pledgeAdmins = await PledgeAdminUtils.getPledgeAdmins(this.contracts.LiquidPledging);
PledgeAdminUtils.printTable(pledgeAdmins.filter(x => x !== PledgeAdminUtils.constants.FUNDER));
} catch(error){
console.log(error);
console.log("Couldn't obtain the list of funders: ", error.message);
}
}
addGiver(params) {
let text = `await LiquidPledging.methods.addGiver(\"${params.name}\", \"${params.url}\", ${params.commitTime}, \"${params.plugin}\").send({from: \"${web3.eth.defaultAccount}\", gas: 2000000})`
doAction(text, async () => {

View File

@ -25,6 +25,7 @@ async function app(actions) {
subAction = (await menus.funders()).action
if (subAction === 'List Funders') {
actions.listFunders();
} if (subAction === 'Create Funder') {
let params = (await menus.createProject())
actions.addProject(params);

37
src/pledgeadmin-utils.js Normal file
View File

@ -0,0 +1,37 @@
const Table = require('cli-table');
const FUNDER = "0";
const PROJECT = "2";
const printTable = (pledgeAdmins) => {
const table = new Table({
head: ['Id', 'Name', 'URL', 'ParentProject', 'Status', 'Commit Time', 'Owner', 'Plugin']
});
for(let i = 0; i < pledgeAdmins.length; i++){
table.push(
[i, pledgeAdmins[i].name, pledgeAdmins[i].url, pledgeAdmins[i].parentProject, pledgeAdmins[i].canceled ? 'Canceled' : 'Active', pledgeAdmins[i].commitTime, pledgeAdmins[i].addr, pledgeAdmins[i].plugin]
);
}
console.log(table.toString());
};
const getPledgeAdmins = async (LiquidPledging) => {
let numProjects = await LiquidPledging.methods.numberOfPledgeAdmins().call();
let pledgeAdmins = [];
for(let i = 1; i <= numProjects; i++){
pledgeAdmins.push(LiquidPledging.methods.getPledgeAdmin(i).call());
}
return Promise.all(pledgeAdmins);
}
module.exports = {
printTable,
getPledgeAdmins,
constants: {
FUNDER,
PROJECT
}
};