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){


Does getGasPrice() work correctly in Ropsten & Mainnet?

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

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..'
| improve this answer | |
  • 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 Dec 24 '18 at 12:28
  • But, The above code is not working correct. – Mark Lee Dec 24 '18 at 12:34
  • try to use contract.methods.transfer(_receiver_address, web3.utils.toWei(amount, 'ether').encodeAbi() because internally ethereum works on wei – Hamza Ahmed Dec 24 '18 at 18:26

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.

| improve this answer | |

Your Answer

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

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