feat: view pledges

This commit is contained in:
Richard Ramos 2019-05-23 14:25:26 -04:00
parent 26f404261b
commit 7f6169bb7a
5 changed files with 118 additions and 7 deletions

View File

@ -1,6 +1,7 @@
var inquirer = require('inquirer');
const Web3 = require("web3");
const PledgeAdminUtils = require('./pledgeadmin-utils');
const PledgeUtils = require('./pledge-utils');
const TrxUtils = require('./trx-utils');
const Contracts = require("./contracts.js");
@ -88,6 +89,7 @@ class Actions {
return doAction(text, async () => {
try {
const pledgeAdmin = await this.contracts.LiquidPledging.methods.getPledgeAdmin(params.id).call();
pledgeAdmin.id = params.id;
PledgeAdminUtils.printTable([pledgeAdmin].filter(x => x.adminType === PledgeAdminUtils.constants.PROJECT));
} catch(error){
console.log("Couldn't obtain the project: ", error.message);
@ -95,6 +97,19 @@ class Actions {
});
}
async viewPledges(params) {
return new Promise(async (resolve, reject) => {
try {
const pledges = await PledgeUtils.getPledges(this.contracts.LiquidPledging);
PledgeUtils.printTable(pledges, web3);
} catch(error){
console.log(error);
console.log("Couldn't obtain the list of pledges: ", error.message);
}
resolve();
});
}
async listFunders() {
return new Promise(async (resolve, reject) => {
try {
@ -138,9 +153,9 @@ class Actions {
}
async donate(params) {
let text = `await LiquidPledging.methods.donate(${params.funderId}, ${params.projectId}, \"${this.contracts.LiquidPledging.options.address}\", web3.utils.toWei(\"${params.amount}\", \"ether\")).send({gas: 2000000});`
let text = `await LiquidPledging.methods.donate(${params.funderId}, ${params.projectId}, \"${params.tokenAddress}\", web3.utils.toWei(\"${params.amount}\", \"ether\")).send({gas: 2000000});`
return doAction(text, async () => {
const toSend = this.contracts.LiquidPledging.methods.donate(params.funderId, params.projectId, this.contracts.LiquidPledging.options.address, web3.utils.toWei(params.amount.toString(), "ether"));
const toSend = this.contracts.LiquidPledging.methods.donate(params.funderId, params.projectId, params.tokenAddress, web3.utils.toWei(params.amount.toString(), "ether"));
const receipt = await TrxUtils.executeAndWait(toSend, web3.eth.defaultAccount);
console.dir("txHash: " + receipt.transactionHash);
});

View File

@ -11,16 +11,28 @@ function mainMenu(actions) {
if (subAction === 'List Projects') {
await actions.listProjects();
} if (subAction === 'Create Project') {
}
if (subAction === 'Create Project') {
let params = (await menus.createProject(actions.web3().eth.defaultAccount))
await actions.addProject(params);
} if (subAction === 'View Project') {
}
if (subAction === 'View Project') {
let params = (await menus.viewProject())
await actions.viewProject(params);
} if (subAction === 'Donate to Project') {
}
if (subAction === 'Fund a Project') {
let params = (await menus.donate())
await actions.donate(params);
}
} else if(action === 'Pledges') {
subAction = (await menus.pledges()).action
if (subAction === 'View Pledges') {
await actions.viewPledges();
}
} else if (action === 'Funders') {
subAction = (await menus.funders()).action

View File

@ -12,6 +12,7 @@ const menus = {
choices: [
'Projects',
'Funders',
'Pledges',
new inquirer.Separator(),
'Tokens',
new inquirer.Separator(),
@ -61,6 +62,24 @@ const menus = {
])
},
pledges: async function() {
return inquirer
.prompt([
{
type: 'list',
name: 'action',
message: 'Pledges> ',
choices: [
'View Pledges',
new inquirer.Separator(),
'Back',
// new inquirer.Separator(),
// 'Exit'
]
}
])
},
tokens: async function() {
return inquirer
.prompt([
@ -150,6 +169,7 @@ const menus = {
}
])
},
viewProject: async function() {
return inquirer.prompt([
{
@ -247,6 +267,18 @@ const menus = {
])
},
viewDonations: async function() {
return inquirer.prompt([
{
type: 'input',
name: 'id',
message: 'What is the Funder/Delegate/Project ID?',
filter: Number,
default: 0
}
])
},
donate: async function(lpAddress) {
console.dir("note: don't forget to approve the token to be withdrawn by the LF address");
return inquirer.prompt([

48
src/pledge-utils.js Normal file
View File

@ -0,0 +1,48 @@
const Table = require('cli-table');
const getPledges = async (LiquidPledging) => {
let numPledges = await LiquidPledging.methods.numberOfPledges().call();
let pledges = [];
let pledgeAdmins = [];
for(let i = 1; i <= numPledges; i++){
pledges.push(LiquidPledging.methods.getPledge(i).call());
}
pledges = await Promise.all(pledges);
for(let i = 0; i < pledges.length; i++){
pledges[i].id = i+1;
pledgeAdmins.push(LiquidPledging.methods.getPledgeAdmin(pledges[i].owner).call())
}
pledgeAdmins = await Promise.all(pledgeAdmins);
for(let i = 0; i < pledges.length; i++){
pledges[i].ownerData = pledgeAdmins[i];
}
return pledges;
}
const PledgeState = {
'0': "Pledged",
'1': "Paying",
'2': "Paid"
};
const printTable = (pledges, web3) => {
const table = new Table({
head: ['Id', 'Owner', 'Token', 'Amount', 'nDelegates', 'IntededProject (?)', 'Commit Time (?)', 'Old Pledge Id', 'Pledge State']
});
for(let i = pledges.length - 1; i >= 0; i--){
table.push(
[pledges[i].id, `${pledges[i].owner} (${pledges[i].ownerData.name})`, pledges[i].token, web3.utils.fromWei(pledges[i].amount, "ether"), pledges[i].nDelegates, pledges[i].intendedProject, pledges[i].commitTime, pledges[i].oldPledge, PledgeState[pledges[i].pledgeState]]
);
}
console.log(table.toString());
};
module.exports = {
getPledges,
printTable
};

View File

@ -9,7 +9,7 @@ const printTable = (pledgeAdmins) => {
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]
[pledgeAdmins[i].id, pledgeAdmins[i].name, pledgeAdmins[i].url, pledgeAdmins[i].parentProject, pledgeAdmins[i].canceled ? 'Canceled' : 'Active', pledgeAdmins[i].commitTime, pledgeAdmins[i].addr, pledgeAdmins[i].plugin]
);
}
@ -23,7 +23,11 @@ const getPledgeAdmins = async (LiquidPledging) => {
for(let i = 1; i <= numProjects; i++){
pledgeAdmins.push(LiquidPledging.methods.getPledgeAdmin(i).call());
}
return Promise.all(pledgeAdmins);
const results = await Promise.all(pledgeAdmins);
for(let i = 0; i < results.length; i++){
results[i].id = i+1;
}
return results;
}