I am using rawTransactions and web3Instance.eth.sendSignedTransaction to deploy and interact with smart-contracts.

The problem is that I can't figure out hot to calculate optimal gasPrice & gasLimit, so the transactions get mined & approved asap.

Currently I have these two functions: async function getGasPrice() { return web3Instance.eth.getGasPrice() .then((gasPrice) => { return gasPrice; }) .catch((err) => { console.log('getGasPrice-error', err); }); }

async function getGasLimit() { return web3Instance.eth.getBlock('latest', false) .then((result) => { return result.gasLimit; }) .catch((err) => { console.log('getGasLimit-error', err); }); }

But when I try to deploy the contract it takes veeeeery long the transaction to get approved and mined. Then it shows Transaction was not mined within750 seconds error and gets deployed after undefined amount of time, which is not what we expect. :/

Any suggestions?

  • 1
    I don't know exactly, but I find suspicious that you take as gas limit for the transaction the gas limit of the last block (N.B. gas limit of the block != gas limit of transaction). BTW in which network are you trying to deploy your contract? (Mainnet, Ropsten, a private network etc. ...)? – Briomkez Sep 6 '18 at 9:39
  • @Briomkez That's what I've found so far. Would this be a better solution - ethereum.stackexchange.com/questions/39401/… ? But that one does not explain where 21000 gas and 68 gas are coming from. P.S. I'm deploying to Rinkeby. If I set gasPrice manually to something much larger, then it gets accepted very fast. – R-J Sep 6 '18 at 9:48
  • Found another one: ethereum.stackexchange.com/questions/49340/… – R-J Sep 6 '18 at 9:52
  • Also 21000 gas is the intrinsic cost for a single tx (i.e., each transaction costs at least 21000 gas. When you deploy a contract you may store also some byte of data (i.e. the field of the smart contract that persists on the state) each byte 0 byte costs 4 gas and each non-zero byte costs 68 gas. In addition, you have to pay for the gas to execute the constructor and eventually to store the field of the smart contract if it needs some. So, actually, you are searching for how much gas you need to perform this operation? – Briomkez Sep 6 '18 at 10:02
  • Is it correct to set the gas limit the same as estimated gas? What would happen in cases where it is not possible to calculate the gas estimate or even worse - actual gas used is more than estimated? – R-J Sep 6 '18 at 10:07

To calculate the optimal gas price You can use a third-party service.

For example: https://ethgasstation.info They have an API: https://ethgasstation.info/gasrecs.php

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