最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How do you estimate the current gas limit before sending a transaction? - Stack Overflow

programmeradmin2浏览0评论

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.

Share Improve this question edited Jan 16, 2023 at 19:24 TylerH 21.1k78 gold badges79 silver badges114 bronze badges asked Jun 11, 2019 at 18:39 KaneHarry729KaneHarry729 951 gold badge2 silver badges12 bronze badges 1
  • 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
Add a ment  | 

3 Answers 3

Reset to default 3

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

发布评论

评论列表(0)

  1. 暂无评论