From 8f8546e05bf3add8d3fe310405cfdf02cb6c5ec5 Mon Sep 17 00:00:00 2001 From: Richard Ramos Date: Fri, 24 May 2019 18:00:06 -0400 Subject: [PATCH] feat: specify gas price --- src/actions.js | 33 +++++++++++++++++------------ src/cmd.js | 16 ++++++++------ src/menus.js | 55 ++++++++++++++++++++++++++++++++++++++++++------ src/trx-utils.js | 13 +++++++++--- 4 files changed, 88 insertions(+), 29 deletions(-) diff --git a/src/actions.js b/src/actions.js index 318dffd..4b9a0b3 100644 --- a/src/actions.js +++ b/src/actions.js @@ -38,6 +38,10 @@ class Actions { this.accounts = accounts || []; } + getWeb3() { + return this.web3; + } + connect(options, cb) { const url = options.url; @@ -80,6 +84,9 @@ class Actions { contracts.loadContracts(); this.contracts = contracts.contracts; + this.execute = TrxUtils.executeAndWait(this.web3); + + cb(); }, 1000); } @@ -89,10 +96,10 @@ class Actions { } async addProject(params) { - let text = `await LiquidPledging.methods.addProject(\"${params.name}\", \"${params.url}\", \"${params.account}\", ${params.parentProject}, ${params.commitTime}, \"${params.plugin}\").send({gas: 2000000})` + let text = `await LiquidPledging.methods.addProject(\"${params.name}\", \"${params.url}\", \"${params.account}\", ${params.parentProject}, ${params.commitTime}, \"${params.plugin}\").send({gas: 2000000, gasPrice: web3.utils.toWei(${params.gasPrice}, "gwei")})` return doAction(text, async () => { const toSend = this.contracts.LiquidPledging.methods.addProject(params.name, params.url, params.account, params.parentProject, params.commitTime, params.plugin); - const receipt = await TrxUtils.executeAndWait(toSend, this.web3.eth.defaultAccount, this.chain); + const receipt = await this.execute(toSend, this.web3.eth.defaultAccount, this.chain, params.gasPrice); console.dir("txHash: " + receipt.transactionHash); const projectId = receipt.events.ProjectAdded.returnValues.idProject; console.log("Project ID: " , projectId); @@ -126,20 +133,20 @@ class Actions { } async withdraw(params) { - let text = `await LiquidPledging.methods.withdraw(\"${params.id}\", web3.utils.toWei(\"${params.amount}\", "ether")).send({gas: 2000000});\nawait LPVault.methods.confirmPayment(paymentId).send({gas: 2000000})`; + let text = `await LiquidPledging.methods.withdraw(\"${params.id}\", web3.utils.toWei(\"${params.amount}\", "ether")).send({gas: 2000000, gasPrice: web3.utils.toWei(${params.gasPrice}, "gwei")});\nawait LPVault.methods.confirmPayment(paymentId).send({gas: 2000000, gasPrice: web3.utils.toWei(${params.gasPrice}, "gwei")})`; return doAction(text, async () => { let toSend, receipt; toSend = this.contracts.LiquidPledging.methods.withdraw(params.id.toString(), this.web3.utils.toWei(params.amount.toString(), "ether")); - receipt = await TrxUtils.executeAndWait(toSend, this.web3.eth.defaultAccount, this.chain); + receipt = await this.execute(toSend, this.web3.eth.defaultAccount, this.chain, params.gasPrice); console.dir("txHash: " + receipt.transactionHash); const paymentId = receipt.events.AuthorizePayment.returnValues.idPayment; console.log("Payment ID: " , paymentId); toSend = this.contracts.LPVault.methods.confirmPayment(paymentId); - receipt = await TrxUtils.executeAndWait(toSend, this.web3.eth.defaultAccount, this.chain); + receipt = await this.execute(toSend, this.web3.eth.defaultAccount, this.chain, params.gasPrice); console.dir("txHash: " + receipt.transactionHash); }); } @@ -171,10 +178,10 @@ class Actions { } async addGiver(params) { - let text = `await LiquidPledging.methods.addGiver(\"${params.name}\", \"${params.url}\", ${params.commitTime}, \"${params.plugin}\").send({gas: 2000000})` + let text = `await LiquidPledging.methods.addGiver(\"${params.name}\", \"${params.url}\", ${params.commitTime}, \"${params.plugin}\").send({gas: 2000000, gasPrice: web3.utils.toWei(${params.gasPrice}, "gwei")})` return doAction(text, async () => { const toSend = this.contracts.LiquidPledging.methods.addGiver(params.name, params.url, params.commitTime, params.plugin); - const receipt = await TrxUtils.executeAndWait(toSend, this.web3.eth.defaultAccount, this.chain); + const receipt = await this.execute(toSend, this.web3.eth.defaultAccount, this.chain, params.gasPrice); console.dir("txHash: " + receipt.transactionHash); const funderId = receipt.events.GiverAdded.returnValues.idGiver; console.log("Funder ID: " , funderId); @@ -182,28 +189,28 @@ class Actions { } async 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, gasPrice: web3.utils.toWei(${params.gasPrice}, "gwei")})` return doAction(text, async () => { const toSend = this.contracts.StandardToken.methods.mint(params.account, this.web3.utils.toWei(params.amount.toString(), "ether")); - const receipt = await TrxUtils.executeAndWait(toSend, this.web3.eth.defaultAccount, this.chain); + const receipt = await this.execute(toSend, this.web3.eth.defaultAccount, this.chain, params.gasPrice); console.dir("txHash: " + receipt.transactionHash); }); } async 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, gasPrice: web3.utils.toWei(${params.gasPrice}, "gwei")})` return doAction(text, async () => { const toSend = this.contracts.StandardToken.methods.approve(this.contracts.LiquidPledging.options.address, this.web3.utils.toWei(params.amount.toString(), "ether")); - const receipt = await TrxUtils.executeAndWait(toSend, this.web3.eth.defaultAccount, this.chain); + const receipt = await this.execute(toSend, this.web3.eth.defaultAccount, this.chain, params.gasPrice); console.dir("txHash: " + receipt.transactionHash); }); } async donate(params) { - let text = `await LiquidPledging.methods.donate(${params.funderId}, ${params.projectId}, \"${params.tokenAddress}\", 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, gasPrice: web3.utils.toWei(${params.gasPrice}, "gwei")});` return doAction(text, async () => { const toSend = this.contracts.LiquidPledging.methods.donate(params.funderId, params.projectId, params.tokenAddress, this.web3.utils.toWei(params.amount.toString(), "ether")); - const receipt = await TrxUtils.executeAndWait(toSend, this.web3.eth.defaultAccount, this.chain); + const receipt = await this.execute(toSend, this.web3.eth.defaultAccount, this.chain, params.gasPrice); console.dir("txHash: " + receipt.transactionHash); }); } diff --git a/src/cmd.js b/src/cmd.js index e56dabe..c218f8d 100644 --- a/src/cmd.js +++ b/src/cmd.js @@ -1,10 +1,12 @@ var inquirer = require('inquirer'); const menus = require('./menus.js'); +const {getGasPrice} = require('./trx-utils'); function mainMenu(actions) { return new Promise(async (resolve, reject) => { let action = (await menus.main()).action - let subAction + let subAction; + let gasPrice = await getGasPrice(actions.getWeb3()); if (action === 'Projects') { subAction = (await menus.projects()).action @@ -14,7 +16,7 @@ function mainMenu(actions) { } if (subAction === 'Create Project') { - let params = (await menus.createProject(actions.web3Object().eth.defaultAccount)) + let params = (await menus.createProject(gasPrice)(actions.web3Object().eth.defaultAccount)) await actions.addProject(params); } @@ -24,7 +26,7 @@ function mainMenu(actions) { } if (subAction === 'Fund a Project') { - let params = (await menus.donate()) + let params = (await menus.donate(gasPrice)()) await actions.donate(params); } } else if(action === 'Pledges') { @@ -35,7 +37,7 @@ function mainMenu(actions) { } if (subAction === 'Withdraw') { - let params = (await menus.withdraw()) + let params = (await menus.withdraw(gasPrice)()) await actions.withdraw(params); } } else if (action === 'Funders') { @@ -44,17 +46,17 @@ function mainMenu(actions) { if (subAction === 'List Funders') { await actions.listFunders(); } if (subAction === 'Create Funder') { - let params = (await menus.createFunder()) + let params = (await menus.createFunder(gasPrice)()) await actions.addGiver(params); } } else if (action === 'Tokens') { subAction = (await menus.tokens()).action if (subAction === 'Mint') { - let params = (await menus.mintToken(actions.web3Object().eth.defaultAccount)) + let params = (await menus.mintToken(gasPrice)(actions.web3Object().eth.defaultAccount)) await actions.mintToken(params); } if (subAction === 'Approve') { - let params = (await menus.approveToken()) + let params = (await menus.approveToken(gasPrice)()) await actions.approveToken(params); } } else if (action === 'Exit') { diff --git a/src/menus.js b/src/menus.js index 5549c87..2f390ae 100644 --- a/src/menus.js +++ b/src/menus.js @@ -118,7 +118,7 @@ const menus = { ]) }, - createProject: async function(defaultAccount) { + createProject: (gasPrice) => async function(defaultAccount) { return inquirer.prompt([ { type: 'input', @@ -167,6 +167,13 @@ const menus = { validate: function(value) { return value.indexOf("0x") === 0; } + }, + { + type: 'input', + name: 'gasPrice', + message: 'Gas Price (in gwei)', + default: gasPrice, + filter: Number } ]) }, @@ -183,7 +190,7 @@ const menus = { ]) }, - withdraw: async function() { + withdraw: gasPrice => async function() { return inquirer.prompt([ { type: 'input', @@ -198,11 +205,19 @@ const menus = { message: 'amount (in ether units)', default: 2, filter: Number + }, + { + type: 'input', + name: 'gasPrice', + message: 'Gas Price (in gwei)', + default: gasPrice, + filter: Number } ]) }, - createFunder: async function() { + + createFunder: gasPrice => async function() { return inquirer.prompt([ { type: 'input', @@ -235,11 +250,18 @@ const menus = { validate: function(value) { return value.indexOf("0x") === 0; } + }, + { + type: 'input', + name: 'gasPrice', + message: 'Gas Price (in gwei)', + default: gasPrice, + filter: Number } ]) }, - mintToken: async function(defaultAccount) { + mintToken: gasPrice => async function(defaultAccount) { console.dir("note: If the transaction fails it likely means this account cannot mint the token (e.g not the owner)"); return inquirer.prompt([ { @@ -264,11 +286,18 @@ const menus = { message: 'amount (in ether units)', default: 2, filter: Number + }, + { + type: 'input', + name: 'gasPrice', + message: 'Gas Price (in gwei)', + default: gasPrice, + filter: Number } ]) }, - approveToken: async function(lpAddress) { + approveToken: gasPrice => async function(lpAddress) { return inquirer.prompt([ { type: 'input', @@ -285,6 +314,13 @@ const menus = { message: 'amount (in ether units)', default: 2, filter: Number + }, + { + type: 'input', + name: 'gasPrice', + message: 'Gas Price (in gwei)', + default: gasPrice, + filter: Number } ]) }, @@ -301,7 +337,7 @@ const menus = { ]) }, - donate: async function(lpAddress) { + donate: gasPrice => async function(lpAddress) { console.dir("note: don't forget to approve the token to be withdrawn by the LF address"); return inquirer.prompt([ { @@ -331,6 +367,13 @@ const menus = { message: 'amount (in ether units)', default: 2, filter: Number + }, + { + type: 'input', + name: 'gasPrice', + message: 'Gas Price (in gwei)', + default: gasPrice, + filter: Number } ]) } diff --git a/src/trx-utils.js b/src/trx-utils.js index f95dfc5..c529578 100644 --- a/src/trx-utils.js +++ b/src/trx-utils.js @@ -1,6 +1,8 @@ const Spinner = require('cli-spinner').Spinner; -const executeAndWait = async (toSend, account, chain) => { +const executeAndWait = web3 => async (toSend, account, chain, gasPrice) => { + gasPrice = web3.utils.toWei(gasPrice.toString(), "gwei"); + return new Promise(async (resolve, reject) => { const spinner = new Spinner('%s'); spinner.setSpinnerString(18); @@ -8,7 +10,7 @@ const executeAndWait = async (toSend, account, chain) => { try { const estimatedGas = await toSend.estimateGas({from: account}); - const tx = toSend.send({from: account, gas: estimatedGas + 10000}); + const tx = toSend.send({from: account, gas: estimatedGas + 10000, gasPrice}); if (chain && chain !== "development") { tx.on('transactionHash', (transactionHash) => { @@ -32,6 +34,11 @@ const executeAndWait = async (toSend, account, chain) => { }); } +const getGasPrice = async (web3) => { + return web3.utils.fromWei(await web3.eth.getGasPrice(), "gwei"); +} + module.exports = { - executeAndWait + executeAndWait, + getGasPrice };