Skip to content

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

Merged
merged 6 commits into from
Apr 15, 2025
Merged

feat: gas fee estimation #4102

merged 6 commits into from
Apr 15, 2025

Conversation

piux2
Copy link
Contributor

@piux2 piux2 commented Apr 10, 2025

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"
}

  1. 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=


  1. We added telemetry metrics for any OTEL compatible tools to monitor gas price in real time.

Pasted Graphic 54
Pasted Graphic 53

@Gno2D2
Copy link
Collaborator

Gno2D2 commented Apr 10, 2025

🛠 PR Checks Summary

All Automated Checks passed. ✅

Manual Checks (for Reviewers):
  • IGNORE the bot requirements for this PR (force green CI check)
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:
  1. Fix any issues flagged by automated checks.
  2. Follow the Contributor Checklist to ensure your PR is ready for review.
    • Add new tests, or document why they are unnecessary.
    • Provide clear examples/screenshots, if necessary.
    • Update documentation, if required.
    • Ensure no breaking changes, or include BREAKING CHANGE notes.
    • Link related issues/PRs, where applicable.
☑️ Reviewer Actions:
  1. Complete manual checks for the PR, including the guidelines and additional checks if applicable.
📚 Resources:
Debug
Automated Checks
Maintainers must be able to edit this pull request (more info)

If

🟢 Condition met
└── 🟢 And
    ├── 🟢 The base branch matches this pattern: ^master$
    └── 🟢 The pull request was created from a fork (head branch repo: piux2/gno)

Then

🟢 Requirement satisfied
└── 🟢 Maintainer can modify this pull request

Manual Checks
**IGNORE** the bot requirements for this PR (force green CI check)

If

🟢 Condition met
└── 🟢 On every pull request

Can be checked by

  • Any user with comment edit permission

Copy link

codecov bot commented Apr 10, 2025

Codecov Report

Attention: Patch coverage is 35.21127% with 46 lines in your changes missing coverage. Please review.

Files with missing lines Patch % Lines
tm2/pkg/crypto/keys/client/broadcast.go 0.00% 26 Missing ⚠️
tm2/pkg/sdk/auth/keeper.go 37.50% 15 Missing ⚠️
tm2/pkg/sdk/auth/handler.go 70.58% 4 Missing and 1 partial ⚠️

📢 Thoughts on this report? Let us know!

@Kouteki Kouteki moved this from Triage to In Review in 🧙‍♂️gno.land core team Apr 11, 2025
@Kouteki Kouteki added this to the 🚀 Mainnet beta launch milestone Apr 11, 2025
@moul moul enabled auto-merge (squash) April 14, 2025 12:39
@moul moul merged commit 1873a46 into gnolang:master Apr 15, 2025
18 of 19 checks passed
@github-project-automation github-project-automation bot moved this from In Progress to Done in 💪 Bounties & Worx Apr 15, 2025
// 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())
Copy link
Contributor

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.

petar-dambovaliev pushed a commit that referenced this pull request Apr 15, 2025
## 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.

![Pasted Graphic
54](https://github.com/user-attachments/assets/d68b25a6-bf08-4375-88f3-034c2e704367)
![Pasted Graphic
53](https://github.com/user-attachments/assets/6eaf0a23-d920-4970-93f1-3d828103bc8c)

---------

Co-authored-by: Manfred Touron <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
📦 🌐 tendermint v2 Issues or PRs tm2 related 📦 ⛰️ gno.land Issues or PRs gno.land package related
Projects
Status: Done
Development

Successfully merging this pull request may close these issues.

7 participants