How can I get proper and optimized gas price for fast transaction in around 10sec? I want to make my transactions to be included stably and fast in blocks. Recently I using value of "fast" returned from API of ethgasstation, but i am not sure it is reliable. Please recommend me.
I always use https://ethgasstation.info.
They have an API. I have never used it but it looks very helpful. Just use a GET Request and query for their fast transaction type.
Otherwise you can craft this algorithm yourself. Here is my take on this problem:
- Look through the latest ~50 blocks.
Retrieve the gas price of all/most of the transactions from these blocks.
Now you can go into 2 directions.
Either you use an average (arithmetic mean or median) to get the gas price for a fast transaction.
Or you create a regression function in order to predict future gas prices.
Of course this program/script would have to run all the time to get up to date information as prices are always changing.
This should be roughly what ethgasstation also uses. Depending on your programming skills and creativity you might create something more accurate.
There is no way to get this 100% correct and especially not 100% optimized. The best you can get is an estimation and Ethgasstation provides their best estimate.
There are simply too many variables in play. Which miner solves the mining puzzle? How many transactions will be waiting to be mined? How big gas prices do the transactions offer? And so on.
A you can see here, the Ethereum block time currently averages around 16 seconds as of today. A transaction is confirmed when a miner adds your transaction into their block and is also able to solve the mining puzzle and this block is then part of the longest chain. Again, to be sure that this block is part of the largest chain, you might have to wait for a number of blocks mined after this block. So confirmation under 10s is not really possible.
The higher gas price you pay, the more miners will be interested in adding your transaction to their block. You can read about EGS's logic of recommendation here.