admin管理员组文章数量:1356536
Specifically, I want my node.js to be able to send an ERC721 token to another wallet. Sometimes, the gas price is high and my transaction gets stuck there for hours. In these cases, I would like my node.js to use more gas to send the token on time.
I've tried getting the last block's gas limit, but it always seems to be far too low.
var count = web3.eth.getTransactionCount(sender);
var rawTransaction;
var block = web3.eth.getBlock("latest");
var currGas = block.gasLimit;
currGas = currGas.toString(16);
currGas = '0x' + currGas;
if(isSending){
rawTransaction = {
"from": sender,
"nonce": web3.toHex(count),
"gasPrice": currGas,
"gasLimit": "0x3A980",
"to": skyDropContract,
"value": "0x0",
"data": myContractABI.startGame.getData(index, {from: sender}),
"chainId": 0x01
};
}
I am using [email protected]
but can switch versions if I really need to. Is there a way to estimate the cost of my transaction before sending it? I might even go 1 gWei over the current estimated cost.
Specifically, I want my node.js to be able to send an ERC721 token to another wallet. Sometimes, the gas price is high and my transaction gets stuck there for hours. In these cases, I would like my node.js to use more gas to send the token on time.
I've tried getting the last block's gas limit, but it always seems to be far too low.
var count = web3.eth.getTransactionCount(sender);
var rawTransaction;
var block = web3.eth.getBlock("latest");
var currGas = block.gasLimit;
currGas = currGas.toString(16);
currGas = '0x' + currGas;
if(isSending){
rawTransaction = {
"from": sender,
"nonce": web3.toHex(count),
"gasPrice": currGas,
"gasLimit": "0x3A980",
"to": skyDropContract,
"value": "0x0",
"data": myContractABI.startGame.getData(index, {from: sender}),
"chainId": 0x01
};
}
I am using [email protected]
but can switch versions if I really need to. Is there a way to estimate the cost of my transaction before sending it? I might even go 1 gWei over the current estimated cost.
- 1 It seems that you're confusing gas limits and gas prices. You're getting the block's gas limit and trying to use that as a gas price. The block gas limit is how much gas can be used in total by all transactions in a block. The gas price is how many wei per gas unit you're willing to pay to have your transaction mined. – user94559 Commented Jun 12, 2019 at 5:32
3 Answers
Reset to default 3gasLimit refers to the maximum amount of gas that can be spent. You can get and reuse the last block total gasLimit by calling something like this:
web3.eth.getBlock("latest").gasLimit
But in practice, when you are building transactions, think of gasLimit as the maximum amount (in wei) that you are willing to pay.
This is a protection mechanism for you, as it prevents you from spending all of your ether if the cost of the execution is too high.
And it's an EVM safety feature to avoid getting stuck running, because if the execution reaches the gasLimit in costs, it will halt and not get stuck.
So for development, you can just reuse the latest block limit or use high amounts. But as you want to deploy anything, get better with your estimations and you will have a better idea of how much gas you are willingly to pay, maximum.
Try estimateGas()
with web3 1.0:
myContract.methods.startGame(index)
.estimateGas(
{
from: _from,
gasPrice: _gasPrice
}, function(error, estimatedGas) {
}
)
});
ETH has shifted to dynamic pricing, see EIP-1159. One way to estimate gas
is to pull in the previous block and find the median value of gas paid. Here's how you can do it in web3 py
def _estimate_gas(transactions) -> int:
# Returns the median of the gas in previous block transactions
return int(median(t.gas for t in transactions))
w3 = Web3(Web3.HTTPProvider("RPC"))
block = w3.eth.get_block("latest", full_transactions=True)
gas = _estimate_gas(block.transactions)
gasPrice
on the other hand is very easy, just use w3.eth.gas_price
(Cross posted my answer from Ethereum Stack Exchange here)
本文标签: javascriptHow do you estimate the current gas limit before sending a transactionStack Overflow
版权声明:本文标题:javascript - How do you estimate the current gas limit before sending a transaction? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744012183a2575769.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论