refactor: added spinner to wait for trxs, and varios transaction related refactors
This commit is contained in:
parent
b6c56cbc55
commit
de51217160
|
@ -44,12 +44,15 @@
|
||||||
"homepage": ".",
|
"homepage": ".",
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
"@aragon/os": "3.1.9",
|
"@aragon/os": "3.1.9",
|
||||||
|
"clear": "^0.1.0",
|
||||||
|
"cli-spinner": "^0.2.10",
|
||||||
"cli-table": "^0.3.1",
|
"cli-table": "^0.3.1",
|
||||||
"colors": "^1.3.3",
|
"colors": "^1.3.3",
|
||||||
"commander": "^2.20.0",
|
"commander": "^2.20.0",
|
||||||
"embark": "^4.1.0-beta.0",
|
"embark": "^4.1.0-beta.0",
|
||||||
"embarkjs-connector-web3": "^4.0.0",
|
"embarkjs-connector-web3": "^4.0.0",
|
||||||
"inquirer": "^6.3.1"
|
"inquirer": "^6.3.1",
|
||||||
|
"readline": "^1.3.0"
|
||||||
},
|
},
|
||||||
"browserslist": [
|
"browserslist": [
|
||||||
">0.2%",
|
">0.2%",
|
||||||
|
|
|
@ -1,8 +1,7 @@
|
||||||
var inquirer = require('inquirer');
|
var inquirer = require('inquirer');
|
||||||
const Web3 = require("web3");
|
const Web3 = require("web3");
|
||||||
const Table = require('cli-table');
|
|
||||||
const PledgeAdminUtils = require('./pledgeadmin-utils');
|
const PledgeAdminUtils = require('./pledgeadmin-utils');
|
||||||
|
const TrxUtils = require('./trx-utils');
|
||||||
const Contracts = require("./contracts.js");
|
const Contracts = require("./contracts.js");
|
||||||
|
|
||||||
const web3 = new Web3();
|
const web3 = new Web3();
|
||||||
|
@ -59,18 +58,18 @@ class Actions {
|
||||||
addProject(params) {
|
addProject(params) {
|
||||||
let text = `await LiquidPledging.methods.addProject(\"${params.name}\", \"${params.url}\", \"${params.account}\", ${params.parentProject}, ${params.commitTime}, \"${params.plugin}\").send({from: \"${web3.eth.defaultAccount}\", gas: 2000000})`
|
let text = `await LiquidPledging.methods.addProject(\"${params.name}\", \"${params.url}\", \"${params.account}\", ${params.parentProject}, ${params.commitTime}, \"${params.plugin}\").send({from: \"${web3.eth.defaultAccount}\", gas: 2000000})`
|
||||||
doAction(text, async () => {
|
doAction(text, async () => {
|
||||||
let projectReceipt = await this.contracts.LiquidPledging.methods.addProject(params.name, params.url, params.account, params.parentProject, params.commitTime, params.plugin).send({from: web3.eth.defaultAccount, gas: 2000000});
|
const toSend = this.contracts.LiquidPledging.methods.addProject(params.name, params.url, params.account, params.parentProject, params.commitTime, params.plugin);
|
||||||
console.dir("txHash: " + projectReceipt.transactionHash)
|
const receipt = await TrxUtils.executeAndWait(toSend, web3.eth.defaultAccount);
|
||||||
var projectId = projectReceipt.events.ProjectAdded.returnValues.idProject;
|
console.dir("txHash: " + receipt.transactionHash);
|
||||||
|
const projectId = receipt.events.ProjectAdded.returnValues.idProject;
|
||||||
console.log(projectId);
|
console.log("Project ID: " , projectId);
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
async listProjects() {
|
async listProjects() {
|
||||||
try {
|
try {
|
||||||
const pledgeAdmins = await PledgeAdminUtils.getPledgeAdmins(this.contracts.LiquidPledging);
|
const pledgeAdmins = await PledgeAdminUtils.getPledgeAdmins(this.contracts.LiquidPledging);
|
||||||
PledgeAdminUtils.printTable(pledgeAdmins.filter(x => x !== PledgeAdminUtils.constants.PROJECT));
|
PledgeAdminUtils.printTable(pledgeAdmins.filter(x => x.adminType === PledgeAdminUtils.constants.PROJECT));
|
||||||
} catch(error){
|
} catch(error){
|
||||||
console.log(error);
|
console.log(error);
|
||||||
console.log("Couldn't obtain the list of projects: ", error.message);
|
console.log("Couldn't obtain the list of projects: ", error.message);
|
||||||
|
@ -82,7 +81,7 @@ class Actions {
|
||||||
doAction(text, async () => {
|
doAction(text, async () => {
|
||||||
try {
|
try {
|
||||||
const pledgeAdmin = await this.contracts.LiquidPledging.methods.getPledgeAdmin(params.id).call();
|
const pledgeAdmin = await this.contracts.LiquidPledging.methods.getPledgeAdmin(params.id).call();
|
||||||
PledgeAdminUtils.printTable([pledgeAdmin].filter(x => x !== PledgeAdminUtils.constants.PROJECT));
|
PledgeAdminUtils.printTable([pledgeAdmin].filter(x => x.adminType === PledgeAdminUtils.constants.PROJECT));
|
||||||
} catch(error){
|
} catch(error){
|
||||||
console.log("Couldn't obtain the project: ", error.message);
|
console.log("Couldn't obtain the project: ", error.message);
|
||||||
}
|
}
|
||||||
|
@ -92,7 +91,7 @@ class Actions {
|
||||||
async listFunders() {
|
async listFunders() {
|
||||||
try {
|
try {
|
||||||
const pledgeAdmins = await PledgeAdminUtils.getPledgeAdmins(this.contracts.LiquidPledging);
|
const pledgeAdmins = await PledgeAdminUtils.getPledgeAdmins(this.contracts.LiquidPledging);
|
||||||
PledgeAdminUtils.printTable(pledgeAdmins.filter(x => x !== PledgeAdminUtils.constants.FUNDER));
|
PledgeAdminUtils.printTable(pledgeAdmins.filter(x => x.adminType === PledgeAdminUtils.constants.FUNDER));
|
||||||
} catch(error){
|
} catch(error){
|
||||||
console.log(error);
|
console.log(error);
|
||||||
console.log("Couldn't obtain the list of funders: ", error.message);
|
console.log("Couldn't obtain the list of funders: ", error.message);
|
||||||
|
@ -102,34 +101,38 @@ class Actions {
|
||||||
addGiver(params) {
|
addGiver(params) {
|
||||||
let text = `await LiquidPledging.methods.addGiver(\"${params.name}\", \"${params.url}\", ${params.commitTime}, \"${params.plugin}\").send({from: \"${web3.eth.defaultAccount}\", gas: 2000000})`
|
let text = `await LiquidPledging.methods.addGiver(\"${params.name}\", \"${params.url}\", ${params.commitTime}, \"${params.plugin}\").send({from: \"${web3.eth.defaultAccount}\", gas: 2000000})`
|
||||||
doAction(text, async () => {
|
doAction(text, async () => {
|
||||||
let funderReceipt = await this.contracts.LiquidPledging.methods.addGiver(params.name, params.url, params.commitTime, params.plugin).send({from: web3.eth.defaultAccount, gas: 2000000})
|
const toSend = this.contracts.LiquidPledging.methods.addGiver(params.name, params.url, params.commitTime, params.plugin);
|
||||||
console.dir("txHash: " + funderReceipt.transactionHash)
|
const receipt = await TrxUtils.executeAndWait(toSend, web3.eth.defaultAccount);
|
||||||
var funderId = funderReceipt.events.GiverAdded.returnValues.idGiver;
|
console.dir("txHash: " + receipt.transactionHash);
|
||||||
console.log(funderId);
|
const funderId = receipt.events.GiverAdded.returnValues.idGiver;
|
||||||
|
console.log("Funder ID: " , funderId);
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
mintToken(params) {
|
mintToken(params) {
|
||||||
let text = `await StandardToken.methods.mint(\"${params.account}\", web3.utils.toWei(\"${params.amount}\", \"ether\")).send({gas: 2000000})`
|
let text = `await StandardToken.methods.mint(\"${params.account}\", web3.utils.toWei(\"${params.amount}\", \"ether\")).send({gas: 2000000})`
|
||||||
doAction(text, async () => {
|
doAction(text, async () => {
|
||||||
let mintReceipt = await this.contracts.StandardToken.methods.mint(params.account, web3.utils.toWei(params.amount, "ether")).send({gas: 2000000})
|
const toSend = this.contracts.StandardToken.methods.mint(params.account, web3.utils.toWei(params.amount.toString(), "ether"));
|
||||||
console.dir("txHash: " + mintReceipt.transactionHash)
|
const receipt = await TrxUtils.executeAndWait(toSend, web3.eth.defaultAccount);
|
||||||
|
console.dir("txHash: " + receipt.transactionHash);
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
approveToken(params) {
|
approveToken(params) {
|
||||||
let text = `await StandardToken.methods.approve(\"${this.contracts.LiquidPledging.options.address}\", web3.utils.toWei(\"${params.amount}\", \"ether\")).send({gas: 2000000})`
|
let text = `await StandardToken.methods.approve(\"${this.contracts.LiquidPledging.options.address}\", web3.utils.toWei(\"${params.amount}\", \"ether\")).send({gas: 2000000})`
|
||||||
doAction(text, async () => {
|
doAction(text, async () => {
|
||||||
let mintReceipt = await this.contracts.StandardToken.methods.approve(this.contracts.LiquidPledging.options.address, web3.utils.toWei(params.amount, "ether")).send({gas: 2000000})
|
const toSend = this.contracts.StandardToken.methods.approve(this.contracts.LiquidPledging.options.address, web3.utils.toWei(params.amount.toString(), "ether"));
|
||||||
console.dir("txHash: " + mintReceipt.transactionHash)
|
const receipt = await TrxUtils.executeAndWait(toSend, web3.eth.defaultAccount);
|
||||||
|
console.dir("txHash: " + receipt.transactionHash);
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
donate(params) {
|
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}, \"${this.contracts.LiquidPledging.options.address}\", web3.utils.toWei(\"${params.amount}\", \"ether\")).send({gas: 2000000});`
|
||||||
doAction(text, async () => {
|
doAction(text, async () => {
|
||||||
let donateReceipt = await this.contracts.LiquidPledging.methods.donate(params.funderId, params.projectId, this.contracts.LiquidPledging.options.address, web3.utils.toWei(params.amount, "ether")).send({gas: 2000000});
|
const toSend = this.contracts.LiquidPledging.methods.donate(params.funderId, params.projectId, this.contracts.LiquidPledging.options.address, web3.utils.toWei(params.amount.toString(), "ether"));
|
||||||
console.dir("txHash: " + donateReceipt.transactionHash)
|
const receipt = await TrxUtils.executeAndWait(toSend, web3.eth.defaultAccount);
|
||||||
|
console.dir("txHash: " + receipt.transactionHash);
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
10
src/cmd.js
10
src/cmd.js
|
@ -27,18 +27,18 @@ async function app(actions) {
|
||||||
if (subAction === 'List Funders') {
|
if (subAction === 'List Funders') {
|
||||||
actions.listFunders();
|
actions.listFunders();
|
||||||
} if (subAction === 'Create Funder') {
|
} if (subAction === 'Create Funder') {
|
||||||
let params = (await menus.createProject())
|
let params = (await menus.createFunder())
|
||||||
actions.addProject(params);
|
actions.addGiver(params);
|
||||||
}
|
}
|
||||||
} else if (action === 'Tokens') {
|
} else if (action === 'Tokens') {
|
||||||
subAction = (await menus.tokens()).action
|
subAction = (await menus.tokens()).action
|
||||||
|
|
||||||
if (subAction === 'Mint') {
|
if (subAction === 'Mint') {
|
||||||
let params = (await menus.mintToken())
|
let params = (await menus.mintToken())
|
||||||
action.mintToken(params);
|
actions.mintToken(params);
|
||||||
} if (subAction === 'Aprove') {
|
} if (subAction === 'Approve') {
|
||||||
let params = (await menus.approveToken())
|
let params = (await menus.approveToken())
|
||||||
action.approveToken(params);
|
actions.approveToken(params);
|
||||||
}
|
}
|
||||||
} else if (action === 'Exit') {
|
} else if (action === 'Exit') {
|
||||||
process.exit()
|
process.exit()
|
||||||
|
|
|
@ -70,7 +70,7 @@ const menus = {
|
||||||
message: 'Tokens> ',
|
message: 'Tokens> ',
|
||||||
choices: [
|
choices: [
|
||||||
'Mint',
|
'Mint',
|
||||||
'Aprove',
|
'Approve',
|
||||||
// new inquirer.Separator(),
|
// new inquirer.Separator(),
|
||||||
// 'Back',
|
// 'Back',
|
||||||
// new inquirer.Separator(),
|
// new inquirer.Separator(),
|
||||||
|
|
|
@ -0,0 +1,22 @@
|
||||||
|
const Spinner = require('cli-spinner').Spinner;
|
||||||
|
|
||||||
|
const executeAndWait = async (toSend, account) => {
|
||||||
|
const spinner = new Spinner('%s');
|
||||||
|
spinner.setSpinnerString(18);
|
||||||
|
spinner.start();
|
||||||
|
|
||||||
|
try {
|
||||||
|
const estimatedGas = await toSend.estimateGas({from: account});
|
||||||
|
const receipt = await toSend.send({from: account, gas: estimatedGas + 10000});
|
||||||
|
|
||||||
|
spinner.stop(true);
|
||||||
|
return receipt;
|
||||||
|
} catch(error) {
|
||||||
|
console.log("Error minting tokens: ", error.message);
|
||||||
|
spinner.stop(true);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
module.exports = {
|
||||||
|
executeAndWait
|
||||||
|
};
|
15
yarn.lock
15
yarn.lock
|
@ -2644,6 +2644,11 @@ class-utils@^0.3.5:
|
||||||
isobject "^3.0.0"
|
isobject "^3.0.0"
|
||||||
static-extend "^0.1.1"
|
static-extend "^0.1.1"
|
||||||
|
|
||||||
|
clear@^0.1.0:
|
||||||
|
version "0.1.0"
|
||||||
|
resolved "https://registry.yarnpkg.com/clear/-/clear-0.1.0.tgz#b81b1e03437a716984fd7ac97c87d73bdfe7048a"
|
||||||
|
integrity sha512-qMjRnoL+JDPJHeLePZJuao6+8orzHMGP04A8CdwCNsKhRbOnKRjefxONR7bwILT3MHecxKBjHkKL/tkZ8r4Uzw==
|
||||||
|
|
||||||
cli-cursor@^2.1.0:
|
cli-cursor@^2.1.0:
|
||||||
version "2.1.0"
|
version "2.1.0"
|
||||||
resolved "https://registry.yarnpkg.com/cli-cursor/-/cli-cursor-2.1.0.tgz#b35dac376479facc3e94747d41d0d0f5238ffcb5"
|
resolved "https://registry.yarnpkg.com/cli-cursor/-/cli-cursor-2.1.0.tgz#b35dac376479facc3e94747d41d0d0f5238ffcb5"
|
||||||
|
@ -2651,6 +2656,11 @@ cli-cursor@^2.1.0:
|
||||||
dependencies:
|
dependencies:
|
||||||
restore-cursor "^2.0.0"
|
restore-cursor "^2.0.0"
|
||||||
|
|
||||||
|
cli-spinner@^0.2.10:
|
||||||
|
version "0.2.10"
|
||||||
|
resolved "https://registry.yarnpkg.com/cli-spinner/-/cli-spinner-0.2.10.tgz#f7d617a36f5c47a7bc6353c697fc9338ff782a47"
|
||||||
|
integrity sha512-U0sSQ+JJvSLi1pAYuJykwiA8Dsr15uHEy85iCJ6A+0DjVxivr3d+N2Wjvodeg89uP5K6TswFkKBfAD7B3YSn/Q==
|
||||||
|
|
||||||
cli-spinners@^1.1.0:
|
cli-spinners@^1.1.0:
|
||||||
version "1.3.1"
|
version "1.3.1"
|
||||||
resolved "https://registry.yarnpkg.com/cli-spinners/-/cli-spinners-1.3.1.tgz#002c1990912d0d59580c93bd36c056de99e4259a"
|
resolved "https://registry.yarnpkg.com/cli-spinners/-/cli-spinners-1.3.1.tgz#002c1990912d0d59580c93bd36c056de99e4259a"
|
||||||
|
@ -8685,6 +8695,11 @@ readdirp@^2.0.0, readdirp@^2.2.1:
|
||||||
micromatch "^3.1.10"
|
micromatch "^3.1.10"
|
||||||
readable-stream "^2.0.2"
|
readable-stream "^2.0.2"
|
||||||
|
|
||||||
|
readline@^1.3.0:
|
||||||
|
version "1.3.0"
|
||||||
|
resolved "https://registry.yarnpkg.com/readline/-/readline-1.3.0.tgz#c580d77ef2cfc8752b132498060dc9793a7ac01c"
|
||||||
|
integrity sha1-xYDXfvLPyHUrEySYBg3JeTp6wBw=
|
||||||
|
|
||||||
recache@0.3.x:
|
recache@0.3.x:
|
||||||
version "0.3.1"
|
version "0.3.1"
|
||||||
resolved "https://registry.yarnpkg.com/recache/-/recache-0.3.1.tgz#30037fcb09c594e4e07960342dd5a6ff0a914ddf"
|
resolved "https://registry.yarnpkg.com/recache/-/recache-0.3.1.tgz#30037fcb09c594e4e07960342dd5a6ff0a914ddf"
|
||||||
|
|
Loading…
Reference in New Issue