0

Description of the issue: I do not have sufficient funds in my wallet to transfer a token from my wallet to an external wallet. So the transaction will fail and shows us an error message. Instead of that error message I would like to show the value of estimated gas price. For that I am looking for API template. Please do suggest if you have any alternative solutions for the same.

  • What do you mean by template? Each contract is different gas costs can have large variation even among similar contracts. Look at the Etherchain table https://www.etherchain.org/tools/gasnow. – Ismael Jan 08 '22 at 05:40
  • I need an api to give estimated gas price for that specific transaction. The code of the API may looks like below . I am asking , Since I am not sure how exactly it's look like https://api.etherscan.io/api ?module=gastracker &action=gasestimate &gasprice=2000000000 &apikey=YourApiKeyToken – Ashok Sandhyala Jan 10 '22 at 07:30
  • You don't need an API you could use web3.eth.estimateGas, see here for an example https://ethereum.stackexchange.com/questions/21510/estimating-gas-cost-of-a-transaction-function-with-web3. – Ismael Jan 11 '22 at 00:12

2 Answers2

0

tx.gasprice should do the trick. Although would recommend not relying on this in long run since it can be manipulated by miners. Alternative is block.baseFee which doesn't give all gas spent, but rather the baseFee spent (this can't be manipulated like the other).

Medici
  • 97
  • 2
0

I don't know what you mean by API template, but you can estimate your costs knowing two things:

  • Gas price (price)
  • Gas used by the transaction (gas)

Money paid for a transaction is cost = price * gas;

The amount of gas used will be calculated when the block is mined. You can only set the gas limit: the maximum amount of gas you are willing to pay.

Different kinds of transactions cost different amounts of gas to be executed, which is related to the complexity of the method called on the smart contract.

But for gas price, it is a matter of how congested the network is. Set up a too-small value, and your transaction will get stuck, or a too-high value, you will overpay.

There are several services dedicated to calculating the optimal values of the gas price you should input in your transactions that will make you pay the lowest value possible while still getting your transaction mined reasonably fast.

One of those services is Owlracle (DISCLAIMER: I am the founder of this service). As a user, you can quickly open the website and get a glimpse of the values or install the browser Owlracle extension. Or, as a dev, you can make requests to the API and get all the desired information to use on your apps/server.