1

I'm fairly new to javascript and I'm trying to automate pulling the gas price from an api and using it into the truffle.js used to execute via truffle.

Basically, I can pull the gasprice via the API but the variable is not recognized within the defined networks. I can pull the api and use it by sending and calling transactions via web3, but I need to add this to all my scripts(I will eventually but I need a quick fix for now).

I've tried:

equire('dotenv').config()
var HDWalletProvider = require("truffle-hdwallet-provider");
var NonceTrackerSubprovider = require("web3-provider-engine/subproviders/nonce-tracker");

var mnemonic = process.env.ETH_MNEMONIC;
var accessToken = process.env.INFURA_ACCESS_TOKEN;

/*
***********************Fetch tests*******************************/
const Web3 = require('web3');
const fetch = require('node-fetch-polyfill');
var web3 = new Web3(new HDWalletProvider(mnemonic,"https://rinkeby.infura.io/"+ accessToken));

//test vea Async fx
//https://ethgasstation.info/json/ethgasAPI.json
//https://www.etherchain.org/api/gasPriceOracle
async function fetchGasPrice() {
  const URL = `https://www.etherchain.org/api/gasPriceOracle`;
  try {
    const fetchResult = fetch(URL);
    const response = await fetchResult;
    const jsonData = await response.json();
    const gasPriceNow = await jsonData.standard*1;
    const gasPriceNow2 = await (gasPriceNow + 1)*1000000000;
    console.log(jsonData);
    console.log("gasPriceNow", gasPriceNow);
    console.log("gasPriceNow2", gasPriceNow2);
    return(gasPriceNow2);
  } catch(e){
    throw Error(e);
  }
}

var gasP = fetchGasPrice();
console.log("gasP1", gasP);

//test via fx
function fetchOHLC(){
  const URL = `https://www.etherchain.org/api/gasPriceOracle`;
    return fetch(URL)
    .then(response => response.json())
    .then(function(response) {
        var l = response.safeLow;
        var s = response.standard;
        var f = response.fast;
        var ft = response.fastest;
        var gp = response.standard*1;
        var gasPrice = (gp+1)*1000000000;
        console.log("gasprice",gasPrice);
    return gasPrice;
    })
    .catch(function(error) {
        console.log(error);
    });    
}

var fetchData = fetchOHLC();
console.log("fetchData:",fetchData);
var price = fetchData.then(function(result){});
console.log("new Gas Price", price);


//test via web3
var gasPrice2 = web3.eth.gasPrice;
console.log(gasPrice2);
var gasPrice2 = gasPrice.toString(10);
console.log(gasPrice2); // "10000000000000"
/********************************************Fetch test end****************
*/

module.exports = {
  networks: {
    development: {
      host: "localhost",
      port: 8545,
      network_id: "*", // Match any network id
      gasPrice: 1, // Specified in Wei
      gas:4700000
    },
    ropsten: {
      provider: new HDWalletProvider(mnemonic, "https://ropsten.infura.io/"+ accessToken),
      network_id: 3,
      gas: 4700000,
      gasPrice: 17e9
    },

    mainnet: {
      network_id: "1",
      provider: function () {
        var wallet = new HDWalletProvider(mnemonic, 'https://mainnet.infura.io/' + accessToken)
        var nonceTracker = new NonceTrackerSubprovider()
        wallet.engine._providers.unshift(nonceTracker)
        nonceTracker.setEngine(wallet.engine)
        return wallet
      },
      network_id: 1,
      gas: 4700000,
      gasPrice: gasP /*************right here, gasP or gasPrice2 are not recognized*********/
    },
    rinkeby: {
      network_id: "4",
      provider: function () {
        var wallet = new HDWalletProvider(mnemonic, 'https://rinkeby.infura.io/' + accessToken)
        var nonceTracker = new NonceTrackerSubprovider()
        wallet.engine._providers.unshift(nonceTracker)
        nonceTracker.setEngine(wallet.engine)
        return wallet
      },
      network_id: 4,
      gas: 4700000,
      gasPrice: 4000000000
    }


  }
};
  • curl -X POST --data '{"jsonrpc":"2.0","method":"eth_gasPrice","params":[],"id":73}' or web3.eth.getGasPrice() if you are using web3js. gasPrice is defined on the node level, not network level by the way. – Nico Vergauwen Oct 23 '18 at 23:54
  • Shane, thanks for editing/fixing my question to show the code. – Brenda Oct 24 '18 at 12:26
  • Nico Vergauwen-I'm not sure I get you mean. Do mean replace gasP with the curl? I've re-written a few of my scripts to use web3 and they work well. I can get the current gas price, send the transaction, and email me the link to the tx hash in etherscan. But I'm leaving to DevCon4 today and still need to re-write a few of them. Truffle.js generally lets me update the gasPrice and excecute all scripts (send tx's) with that gasPrice. So I was hoping to just do that for now until I transition eveything else to web3. I'm leaving for devcon today, that's why I wanted a dirty fix. – Brenda Oct 24 '18 at 12:38

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.