feat(transaction): add useGasEstimator hook#2638
Open
lau90eth wants to merge 1 commit into
Open
Conversation
Adds real-time gas estimation with spike detection and safety indicators. - useGasEstimator hook: estimates gas fees, detects spikes, suggests wait time - estimateGasFee utility: reads onchain gas price, calculates median, detects spikes - Auto-refresh every 15s (configurable) - Shows: safe ✅, spike 🔴 with estimated wait time - 4 tests covering: empty state, normal estimate, spike detection, API error Refs: coinbase#2572
|
@lau90eth is attempting to deploy a commit to the Coinbase Team on Vercel. A member of the Team first needs to authorize it. |
🟡 Heimdall Review Status
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Every user on Base has overpaid gas at least once. Gas spikes are invisible
until after signing — wallets show "0.00 ETH" and then charge $50.
OnchainKit has zero gas visibility before the user confirms.
This PR adds
useGasEstimator, a hook that shows real-time gas fees withspike detection — before the user clicks "Confirm".
What changed
useGasEstimatorhook — estimates real-time gas fees for the currentTransactioncalls. Detects spikes, flags safe windows, and suggestswait time when gas is elevated. Auto-refreshes every 15s (configurable).
estimateGasFeeutility — reads onchain gas price via viemgetGasPrice+estimateGas, maintains a 20-block median historyfor spike detection, calculates worst-case max fee with 150% buffer.
GasEstimatetype — standardized shape for gas data includingspike/safe flags and wait time estimate.
src/transaction/index.tsUsage
API
Notes to reviewers
getGasPriceandestimateGas. Works on any chain configured inOnchainKitProvider.per chain.
isSpike= current gas > 2x median.isSafe= currentgas < 1.2x median.
waitTimeMinutes— heuristic based on historical gas volatilityin the cached 20-block window. Not guaranteed — presented as an
estimate to guide user decisions.
maxAcceptableFee— optional. When set,isSafealso requiresestimated fee to be below this threshold.
refreshIntervalms. Clears on unmount.callsfromuseTransactionContextautomatically.Testing
useGasEstimator.test.ts— 4 testsestimateGas.test.ts— utility unit testsTest cases covered:
isSpike: truewhen gas > 2x medianRisk
Low. Read-only hook. Uses
getGasPriceandestimateGas—both are standard viem read operations with zero side effects.
Returns
nullwhen no calls are present.Does not modify
TransactionProvider,TransactionButton,or any existing transaction logic.