5

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?

2
  • I checked. It is returning same to me too. I think there is something wrong with this. For mainnet,it seems working fine
    – Aniket
    Commented Dec 24, 2018 at 5:33
  • 1
    @Aniket see my answer below, it is returning the median gas price of the test net which rarely changes. Commented Sep 7, 2019 at 19:34

5 Answers 5

4

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..'
3
  • Thank you. I hope to calculate ether Token to another wallet. but the sender's wallet only has tokens. So, I hope to send minimum ether. For calculating, I try to get gas_price and estimate_gas as below. ' contract = new web3.eth.Contract(ABI, _my_contract_address); var cont_data = contract.methods.transfer(_receiver_address, web3.utils.toHex(amount.toString())).encodeABI(); web3.eth.estimateGas({to: _my_contract_address, data: cont_data}, function (error, gasAmount) { //set gasPrice and gasLimiit = gasAmount} ); ' Is it right?
    – Mark Lee
    Commented Dec 24, 2018 at 12:28
  • But, The above code is not working correct.
    – Mark Lee
    Commented Dec 24, 2018 at 12:34
  • try to use contract.methods.transfer(_receiver_address, web3.utils.toWei(amount, 'ether').encodeAbi() because internally ethereum works on wei Commented Dec 24, 2018 at 18:26
0

Most (if not all) testnets will return a fixed price. Since gas price doesn't affect priority of request mining, almost all transactions provide 1 Gwei. When you check gas price with web3, it sends the eth_gasPrice RPC method call to Geth/Parity, which returns the median gas price for transactions from the last 10 blocks. Remember, this is median, not mean.

0

Check if your blocks are well synced. web3.eth.getGasPrice()looks at the median gas price of last blocks. It falls back to 1000000000 if they don't exist.

0

This may be due to the fact that you are testing on Ropsten. I am also testign on Rinkeby and it will always return a fixed price. You must understand that gas price is a kind of auction that the users pay for in order to get their transactions finalized as quick as possible. Since for test networks there is no incentive to pay for fast transactions, the gas price is constant. Not sure about the incentives part though.

0

I believe that every test network has a pre-set gas price. I don't know if this behavior is defined somewhere, but it would be interesting to know. Note that the page rinkeby.etherscan.io/gatracker (or ropsten), which could report the price of gas on this testnet, doesn't even exist.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.