Merge pull request #17 from status-im/feat/gas-price
feat: specify gas price
This commit is contained in:
commit
3d9e479da0
|
@ -38,6 +38,10 @@ class Actions {
|
||||||
this.accounts = accounts || [];
|
this.accounts = accounts || [];
|
||||||
}
|
}
|
||||||
|
|
||||||
|
getWeb3() {
|
||||||
|
return this.web3;
|
||||||
|
}
|
||||||
|
|
||||||
connect(options, cb) {
|
connect(options, cb) {
|
||||||
const url = options.url;
|
const url = options.url;
|
||||||
|
|
||||||
|
@ -80,6 +84,9 @@ class Actions {
|
||||||
contracts.loadContracts();
|
contracts.loadContracts();
|
||||||
this.contracts = contracts.contracts;
|
this.contracts = contracts.contracts;
|
||||||
|
|
||||||
|
this.execute = TrxUtils.executeAndWait(this.web3);
|
||||||
|
|
||||||
|
|
||||||
cb();
|
cb();
|
||||||
}, 1000);
|
}, 1000);
|
||||||
}
|
}
|
||||||
|
@ -89,10 +96,10 @@ class Actions {
|
||||||
}
|
}
|
||||||
|
|
||||||
async addProject(params) {
|
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 () => {
|
return doAction(text, async () => {
|
||||||
const toSend = this.contracts.LiquidPledging.methods.addProject(params.name, params.url, params.account, params.parentProject, params.commitTime, params.plugin);
|
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);
|
console.dir("txHash: " + receipt.transactionHash);
|
||||||
const projectId = receipt.events.ProjectAdded.returnValues.idProject;
|
const projectId = receipt.events.ProjectAdded.returnValues.idProject;
|
||||||
console.log("Project ID: " , projectId);
|
console.log("Project ID: " , projectId);
|
||||||
|
@ -126,20 +133,20 @@ class Actions {
|
||||||
}
|
}
|
||||||
|
|
||||||
async withdraw(params) {
|
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 () => {
|
return doAction(text, async () => {
|
||||||
|
|
||||||
let toSend, receipt;
|
let toSend, receipt;
|
||||||
|
|
||||||
toSend = this.contracts.LiquidPledging.methods.withdraw(params.id.toString(), this.web3.utils.toWei(params.amount.toString(), "ether"));
|
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);
|
console.dir("txHash: " + receipt.transactionHash);
|
||||||
const paymentId = receipt.events.AuthorizePayment.returnValues.idPayment;
|
const paymentId = receipt.events.AuthorizePayment.returnValues.idPayment;
|
||||||
console.log("Payment ID: " , paymentId);
|
console.log("Payment ID: " , paymentId);
|
||||||
|
|
||||||
toSend = this.contracts.LPVault.methods.confirmPayment(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);
|
console.dir("txHash: " + receipt.transactionHash);
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
@ -171,10 +178,10 @@ class Actions {
|
||||||
}
|
}
|
||||||
|
|
||||||
async addGiver(params) {
|
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 () => {
|
return doAction(text, async () => {
|
||||||
const toSend = this.contracts.LiquidPledging.methods.addGiver(params.name, params.url, params.commitTime, params.plugin);
|
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);
|
console.dir("txHash: " + receipt.transactionHash);
|
||||||
const funderId = receipt.events.GiverAdded.returnValues.idGiver;
|
const funderId = receipt.events.GiverAdded.returnValues.idGiver;
|
||||||
console.log("Funder ID: " , funderId);
|
console.log("Funder ID: " , funderId);
|
||||||
|
@ -182,28 +189,28 @@ class Actions {
|
||||||
}
|
}
|
||||||
|
|
||||||
async mintToken(params) {
|
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 () => {
|
return doAction(text, async () => {
|
||||||
const toSend = this.contracts.StandardToken.methods.mint(params.account, this.web3.utils.toWei(params.amount.toString(), "ether"));
|
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);
|
console.dir("txHash: " + receipt.transactionHash);
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
async approveToken(params) {
|
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 () => {
|
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 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);
|
console.dir("txHash: " + receipt.transactionHash);
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
async donate(params) {
|
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 () => {
|
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 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);
|
console.dir("txHash: " + receipt.transactionHash);
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
16
src/cmd.js
16
src/cmd.js
|
@ -1,10 +1,12 @@
|
||||||
var inquirer = require('inquirer');
|
var inquirer = require('inquirer');
|
||||||
const menus = require('./menus.js');
|
const menus = require('./menus.js');
|
||||||
|
const {getGasPrice} = require('./trx-utils');
|
||||||
|
|
||||||
function mainMenu(actions) {
|
function mainMenu(actions) {
|
||||||
return new Promise(async (resolve, reject) => {
|
return new Promise(async (resolve, reject) => {
|
||||||
let action = (await menus.main()).action
|
let action = (await menus.main()).action
|
||||||
let subAction
|
let subAction;
|
||||||
|
let gasPrice = await getGasPrice(actions.getWeb3());
|
||||||
|
|
||||||
if (action === 'Projects') {
|
if (action === 'Projects') {
|
||||||
subAction = (await menus.projects()).action
|
subAction = (await menus.projects()).action
|
||||||
|
@ -14,7 +16,7 @@ function mainMenu(actions) {
|
||||||
}
|
}
|
||||||
|
|
||||||
if (subAction === 'Create Project') {
|
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);
|
await actions.addProject(params);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -24,7 +26,7 @@ function mainMenu(actions) {
|
||||||
}
|
}
|
||||||
|
|
||||||
if (subAction === 'Fund a Project') {
|
if (subAction === 'Fund a Project') {
|
||||||
let params = (await menus.donate())
|
let params = (await menus.donate(gasPrice)())
|
||||||
await actions.donate(params);
|
await actions.donate(params);
|
||||||
}
|
}
|
||||||
} else if(action === 'Pledges') {
|
} else if(action === 'Pledges') {
|
||||||
|
@ -35,7 +37,7 @@ function mainMenu(actions) {
|
||||||
}
|
}
|
||||||
|
|
||||||
if (subAction === 'Withdraw') {
|
if (subAction === 'Withdraw') {
|
||||||
let params = (await menus.withdraw())
|
let params = (await menus.withdraw(gasPrice)())
|
||||||
await actions.withdraw(params);
|
await actions.withdraw(params);
|
||||||
}
|
}
|
||||||
} else if (action === 'Funders') {
|
} else if (action === 'Funders') {
|
||||||
|
@ -44,17 +46,17 @@ function mainMenu(actions) {
|
||||||
if (subAction === 'List Funders') {
|
if (subAction === 'List Funders') {
|
||||||
await actions.listFunders();
|
await actions.listFunders();
|
||||||
} if (subAction === 'Create Funder') {
|
} if (subAction === 'Create Funder') {
|
||||||
let params = (await menus.createFunder())
|
let params = (await menus.createFunder(gasPrice)())
|
||||||
await actions.addGiver(params);
|
await 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(actions.web3Object().eth.defaultAccount))
|
let params = (await menus.mintToken(gasPrice)(actions.web3Object().eth.defaultAccount))
|
||||||
await actions.mintToken(params);
|
await actions.mintToken(params);
|
||||||
} if (subAction === 'Approve') {
|
} if (subAction === 'Approve') {
|
||||||
let params = (await menus.approveToken())
|
let params = (await menus.approveToken(gasPrice)())
|
||||||
await actions.approveToken(params);
|
await actions.approveToken(params);
|
||||||
}
|
}
|
||||||
} else if (action === 'Exit') {
|
} else if (action === 'Exit') {
|
||||||
|
|
55
src/menus.js
55
src/menus.js
|
@ -118,7 +118,7 @@ const menus = {
|
||||||
])
|
])
|
||||||
},
|
},
|
||||||
|
|
||||||
createProject: async function(defaultAccount) {
|
createProject: (gasPrice) => async function(defaultAccount) {
|
||||||
return inquirer.prompt([
|
return inquirer.prompt([
|
||||||
{
|
{
|
||||||
type: 'input',
|
type: 'input',
|
||||||
|
@ -167,6 +167,13 @@ const menus = {
|
||||||
validate: function(value) {
|
validate: function(value) {
|
||||||
return value.indexOf("0x") === 0;
|
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([
|
return inquirer.prompt([
|
||||||
{
|
{
|
||||||
type: 'input',
|
type: 'input',
|
||||||
|
@ -198,11 +205,19 @@ const menus = {
|
||||||
message: 'amount (in ether units)',
|
message: 'amount (in ether units)',
|
||||||
default: 2,
|
default: 2,
|
||||||
filter: Number
|
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([
|
return inquirer.prompt([
|
||||||
{
|
{
|
||||||
type: 'input',
|
type: 'input',
|
||||||
|
@ -235,11 +250,18 @@ const menus = {
|
||||||
validate: function(value) {
|
validate: function(value) {
|
||||||
return value.indexOf("0x") === 0;
|
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)");
|
console.dir("note: If the transaction fails it likely means this account cannot mint the token (e.g not the owner)");
|
||||||
return inquirer.prompt([
|
return inquirer.prompt([
|
||||||
{
|
{
|
||||||
|
@ -264,11 +286,18 @@ const menus = {
|
||||||
message: 'amount (in ether units)',
|
message: 'amount (in ether units)',
|
||||||
default: 2,
|
default: 2,
|
||||||
filter: Number
|
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([
|
return inquirer.prompt([
|
||||||
{
|
{
|
||||||
type: 'input',
|
type: 'input',
|
||||||
|
@ -285,6 +314,13 @@ const menus = {
|
||||||
message: 'amount (in ether units)',
|
message: 'amount (in ether units)',
|
||||||
default: 2,
|
default: 2,
|
||||||
filter: Number
|
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");
|
console.dir("note: don't forget to approve the token to be withdrawn by the LF address");
|
||||||
return inquirer.prompt([
|
return inquirer.prompt([
|
||||||
{
|
{
|
||||||
|
@ -331,6 +367,13 @@ const menus = {
|
||||||
message: 'amount (in ether units)',
|
message: 'amount (in ether units)',
|
||||||
default: 2,
|
default: 2,
|
||||||
filter: Number
|
filter: Number
|
||||||
|
},
|
||||||
|
{
|
||||||
|
type: 'input',
|
||||||
|
name: 'gasPrice',
|
||||||
|
message: 'Gas Price (in gwei)',
|
||||||
|
default: gasPrice,
|
||||||
|
filter: Number
|
||||||
}
|
}
|
||||||
])
|
])
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,6 +1,8 @@
|
||||||
const Spinner = require('cli-spinner').Spinner;
|
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) => {
|
return new Promise(async (resolve, reject) => {
|
||||||
const spinner = new Spinner('%s');
|
const spinner = new Spinner('%s');
|
||||||
spinner.setSpinnerString(18);
|
spinner.setSpinnerString(18);
|
||||||
|
@ -8,7 +10,7 @@ const executeAndWait = async (toSend, account, chain) => {
|
||||||
|
|
||||||
try {
|
try {
|
||||||
const estimatedGas = await toSend.estimateGas({from: account});
|
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") {
|
if (chain && chain !== "development") {
|
||||||
tx.on('transactionHash', (transactionHash) => {
|
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 = {
|
module.exports = {
|
||||||
executeAndWait
|
executeAndWait,
|
||||||
|
getGasPrice
|
||||||
};
|
};
|
||||||
|
|
Loading…
Reference in New Issue