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?
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. ...)?21000 gas
and68 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.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?