Gas Price Issue – web3.eth.getGasPrice() always returns 1 GWei

gasgas-estimategas-priceropsten

For estimate transaction cost, I use web3.eth.getGasPrice() function. The return callback value always 1Gwei(1,000,000,000) in ROPSTEN.

web3.eth.getGasPrice(function(error, result){
    console.log(result);
});

1000000000

Does getGasPrice() work correctly in Ropsten & Mainnet?

Best Answer

web3.eth.getGasPrice() is to check the current gas price in the network and will only change if the network gas price changes. If you want to get the amount of gas the transaction will consume then you have to use this function web3.eth.estimateGas(callObject [, callback])

var result = web3.eth.estimateGas({
   to: "0xc4abd0339eb8d57087278718986382264244252f", 
   data: "0xc6888fa10000000000000000000000000000000000000000000000000000000000000003"
});

provide the address of the contract if using one and the function data which you can obtain by

var myCallData = myContractInstance.myMethod.getData(param1 [, param2, ...]);
// myCallData = '0x45ff3ff6000000000004545345345345..'