feat(fee-engine): add history tracking, testnet support, USD helper &… - #29
Open
RiH-137 wants to merge 1 commit into
Open
feat(fee-engine): add history tracking, testnet support, USD helper &…#29RiH-137 wants to merge 1 commit into
RiH-137 wants to merge 1 commit into
Conversation
There was a problem hiding this comment.
Pull request overview
Adds a new FeeEstimator helper to the btc-controller “fee-engine” layer, expanding fee estimation capabilities (testnet support, history tracking, and USD comparison utilities) and introducing a dedicated unit test suite.
Changes:
- Introduces
FeeEstimatorwith per-network fee fetching + caching, history tracking, and BTC/USD conversion helpers. - Updates helper barrel exports to include
FeeEstimatorand afeeEstimatorsingleton. - Adds a comprehensive mocha/nock unit test suite for the new estimator.
Reviewed changes
Copilot reviewed 3 out of 5 changed files in this pull request and generated 6 comments.
| File | Description |
|---|---|
btc-controller/src/helper/feeEstimator.ts |
New fee estimation helper with caching/retry, history ring buffer, and USD comparison utilities. |
btc-controller/src/helper/index.ts |
Exposes FeeEstimator and feeEstimator from the helper barrel. |
btc-controller/test/feeEstimator.js |
New unit tests covering fee fetching (mainnet/testnet), caching, history behavior, and USD calculations. |
btc-controller/coverage/tmp/coverage-980-1773166164780-0.json |
Generated coverage temp artifact added to the PR (should not be committed). |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
Comment on lines
+107
to
+110
| const cached = this.cache.get(network); | ||
| if (cached && Date.now() < cached.expiresAt) { | ||
| return cached.rates; | ||
| } |
| * Capped at 10 entries; only successful fetches are recorded. | ||
| */ | ||
| getHistory(): FeeRateEntry[] { | ||
| return [...this.history]; |
Comment on lines
+112
to
+115
| try { | ||
| const url = MEMPOOL_FEE_URL[network]; | ||
| const data = await this.fetchWithRetry(url); | ||
|
|
Comment on lines
+210
to
+222
| for (const tier of tiers) { | ||
| const satPerVByte = rates[tier]; | ||
| const totalSats = satPerVByte * vBytes; | ||
| const totalBtc = totalSats / 1e8; | ||
|
|
||
| result[tier] = { | ||
| satPerVByte, | ||
| totalSats, | ||
| totalBtc, | ||
| totalUSD: btcPrice !== null | ||
| ? parseFloat((totalBtc * btcPrice).toFixed(6)) | ||
| : null, | ||
| }; |
Comment on lines
+4
to
+7
| import { FeeEstimator, feeEstimator } from "./feeEstimator"; | ||
| import * as utils from "./utils/index"; | ||
|
|
||
| export { signTransaction, utils, getFeeAndInput, getTransactionSize, TransactionVisualizer }; | ||
| export { signTransaction, utils, getFeeAndInput, getTransactionSize, TransactionVisualizer, FeeEstimator, feeEstimator }; |
Comment on lines
+107
to
+110
| const cached = this.cache.get(network); | ||
| if (cached && Date.now() < cached.expiresAt) { | ||
| return cached.rates; | ||
| } |
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
Completes Week 7 of Phase 2 — Fee Engine. Adds four new capabilities to
the
FeeEstimatorclass and covers them with a full unit-test suite.Changes
src/helper/feeEstimator.tsfetchRates()callappends a
FeeRateEntry(rates, network, timestamp) to an internalring buffer capped at 10 entries. Exposed via
getHistory()andclearHistory().fetchRates('TESTNET')now routes tohttps://mempool.space/testnet/api/v1/fees/recommended, fullyindependent of the mainnet cache.
getBtcPriceUsd()— live BTC/USD price from mempool.space; returnsnullgracefully when the price API is unavailable.getSatVByteToUsd(satPerVByte, vBytes)— converts a fee rate +transaction size into total sats, BTC, and USD.
compareTiers(vBytes, network)— side-by-side breakdown of all fourtiers (slow / normal / fast / urgent) with sat, BTC, and USD columns.
src/helper/index.tsFeeEstimatorclass andfeeEstimatorsingleton.test/feeEstimator.js(new file)nockto mock the mempool.space API.30-second cache (per network), history overflow capping, timestamp
correctness, USD calculation accuracy,
nullprice fallback, andcompareTiersinvariants.Test results
Related planning
Days 31–34, Week 7, Phase 2 — Fee Engine (
planning_tracker.csv)