-
Notifications
You must be signed in to change notification settings - Fork 405
feat: gas fee estimation #4102
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat: gas fee estimation #4102
Conversation
🛠 PR Checks SummaryAll Automated Checks passed. ✅ Manual Checks (for Reviewers):
Read More🤖 This bot helps streamline PR reviews by verifying automated checks and providing guidance for contributors and reviewers. ✅ Automated Checks (for Contributors):🟢 Maintainers must be able to edit this pull request (more info) ☑️ Contributor Actions:
☑️ Reviewer Actions:
📚 Resources:Debug
|
Codecov ReportAttention: Patch coverage is 📢 Thoughts on this report? Let us know! |
…into feat_gasfee_estimation
// 5% fee buffer to cover the suden change of gas price | ||
feeBuffer := overflow.Mulp(fee, 5) / 100 | ||
fee = overflow.Addp(fee, feeBuffer) | ||
s := fmt.Sprintf("estimated gas usage: %d, gas fee: %d%s, current gas price: %s\n", bres.DeliverTx.GasUsed, fee, gp.Price.Denom, gp.String()) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is there a world in which perhaps the folks consuming the results from this query would get back structured data say as JSON in case they need to display the estimates in other contexts otherwise they have to manually parse out the line and the format.
## ISSUES We observed very high gas fee usage in the test network. Here is what happened. The network charges users the gas fee they specify when they submit a transaction—regardless of whether the transaction eventually fails or succeeds.A transaction may fail for several reasons, including insufficient gas, an incorrect function name, an improper parameter type, or invalid values that cause the transaction to fail. As long as the gasFee/gasWanted exceeds the gasPrice, along with passing other basic validations, the transaction will be allowed into the mempool. At that point, fees are deducted from the user’s account even though the transaction has not yet been executed on the virtual machine. If the transaction later fails during the execution, the chain still retains the fee. This design allows for basic validation upfront and defers the full execution check until block playback. However, a problem arises because users must estimate both the gas to be used and the required fee. They tend to overestimate the gas limit and gas fee to ensure that the transaction goes through, and when a transaction fails, the actual gas consumed is typically much lower than the estimation. As a result, when the third party wallet calculates GasFee/GasUsed, the resulting figure can be significantly larger than the gas price set on-chain in each block. Then the wallet suggests the gas fee by applying the formula GasFee/GasUsed multiplied by simulated gas, the resulting gas fee can be even higher. This is why we sometimes see an overestimated value. The solution below should provide a more accurate estimation of the fee required on the network ## SOLUTION 1. We provide an on-chain query to retrieve the latest gas price, instead of calculating it using the GasFee / GasUsed ratio. Third-party wallets can use the on-chain gas price multiplied by the simulated gas to estimate the GasFee for users. $ gnokey query auth/gasprice height: 0 data: { "gas": "1000", "price": "1ugnot" } 2. We provide an estimated gas fee when users use gnokey with the ‘-simulation only’ flag. There are no charges incurred during the simulation. ``` gnokey maketx call -pkgpath gno.land/r/hello -func Hello -gas-wanted 2000000 -gas-fee 1000000ugnot -broadcast -chainid tendermint_test -simulate only test1 [stdout] ("hello" string) OK! GAS WANTED: 2000000 GAS USED: 98577 HEIGHT: 0 EVENTS: [] INFO: estimated gas 98577, gas fee: 103ugnot, current gas price: 1000gas/1ugnot ``` Then users can use estimated gas fee and gas used number in the simulation to submit a transaction. ``` gnokey maketx call -pkgpath gno.land/r/hello -func Hello -gas-wanted 98577 -gas-fee 103ugnot -broadcast -chainid tendermint_test test1 [stdout] ("hello" string) OK! GAS WANTED: 98577 GAS USED: 98527 HEIGHT: 53 EVENTS: [] INFO: TX HASH: 20+++XrLH81fR53iknyujpQXJWQstDrT13nZ6iTRk/o= ``` 3. We added telemetry metrics for any OTEL compatible tools to monitor gas price in real time.   --------- Co-authored-by: Manfred Touron <[email protected]>
ISSUES
We observed very high gas fee usage in the test network. Here is what happened.
The network charges users the gas fee they specify when they submit a transaction—regardless of whether the transaction eventually fails or succeeds.A transaction may fail for several reasons, including insufficient gas, an incorrect function name, an improper parameter type, or invalid values that cause the transaction to fail. As long as the gasFee/gasWanted exceeds the gasPrice, along with passing other basic validations, the transaction will be allowed into the mempool.
At that point, fees are deducted from the user’s account even though the transaction has not yet been executed on the virtual machine. If the transaction later fails during the execution, the chain still retains the fee. This design allows for basic validation upfront and defers the full execution check until block playback.
However, a problem arises because users must estimate both the gas to be used and the required fee. They tend to overestimate the gas limit and gas fee to ensure that the transaction goes through, and when a transaction fails, the actual gas consumed is typically much lower than the estimation.
As a result, when the third party wallet calculates GasFee/GasUsed, the resulting figure can be significantly larger than the gas price set on-chain in each block. Then the wallet suggests the gas fee by applying the formula GasFee/GasUsed multiplied by simulated gas, the resulting gas fee can be even higher. This is why we sometimes see an overestimated value. The solution below should provide a more accurate estimation of the fee required on the network
SOLUTION
$ gnokey query auth/gasprice
height: 0
data: {
"gas": "1000",
"price": "1ugnot"
}
Then users can use estimated gas fee and gas used number in the simulation to submit a transaction.