Skip to content

refactor: migrate useHistoricalPricesEvm#40507

Open
n3ps wants to merge 6 commits intomainfrom
n3ps/historical-prices-query
Open

refactor: migrate useHistoricalPricesEvm#40507
n3ps wants to merge 6 commits intomainfrom
n3ps/historical-prices-query

Conversation

@n3ps
Copy link
Contributor

@n3ps n3ps commented Feb 27, 2026

Replace manual useState + useEffect + fetch pattern with useQuery

  • Built-in caching and deduplication of concurrent requests for same asset
  • No manual loading/setPrices/setMetadata state management
  • Metadata derived instead of a second useEffect

Description

Open in GitHub Codespaces

Changelog

CHANGELOG entry: refactor: migrate useHistoricalPricesEvm to useQuery

Related issues

Fixes:

Manual testing steps

  1. Go to a token detail page
  2. See chart

Screenshots/Recordings

Before

After

Pre-merge author checklist

Pre-merge reviewer checklist

  • I've manually tested the PR (e.g. pull and build branch, run the app, test code being changed).
  • I confirm that this PR addresses all acceptance criteria described in the ticket it closes and includes the necessary testing evidence such as recordings and or screenshots.

Note

Medium Risk
Touches the asset price chart data-fetching path by switching from manual fetch/state to useQuery + apiClient, which can subtly change loading/empty-state behavior and caching across assets/chains.

Overview
EVM historical price fetching is refactored from a manual useEffect + fetch + local state pattern to useQuery using apiClient.prices.getV1HistoricalPricesQueryOptions, including a select transform for prices and deriving metadata inline.

UI/tests are adjusted to reflect the new request path and states: AssetChartEmptyState now exposes data-testid="asset-chart-empty-state", asset page tests mock getBearerToken and broaden nock interception/cleanup, and snapshots/assertions update from a chart loading skeleton to the empty-state when the API returns no price data.

Written by Cursor Bugbot for commit 403be16. This will update automatically on new commits. Configure here.

Replace manual useState + useEffect + fetch pattern with
@tanstack/react-query useQuery. Benefits:
- Built-in caching (staleTime: 60s, cacheTime: 5min)
- Automatic deduplication of concurrent requests for same asset
- No manual loading/setPrices/setMetadata state management
- Metadata derived via useMemo instead of a second useEffect
- Fetch logic extracted to pure async function

Non-EVM hook (useHistoricalPricesNonEvm) is unchanged as it
uses Redux dispatch, not direct fetch.

Co-authored-by: Francis Nepomuceno <n3ps@users.noreply.github.com>
@n3ps n3ps changed the title refactor: migrate useHistoricalPricesEvm to useQuery refactor: useHistoricalPricesEvm to useQuery Feb 27, 2026
@metamaskbot metamaskbot added the team-core-extension-ux Core Extension UX team label Feb 27, 2026
@metamaskbotv2
Copy link
Contributor

metamaskbotv2 bot commented Feb 27, 2026

Builds ready [bf53a17]
⚡ Performance Benchmarks
👆 Interaction Benchmarks
BenchmarkMetricMean (ms)Min (ms)Max (ms)Std Dev (ms)P75 (ms)P95 (ms)
Load New Accountload_new_account2952922993297299
total2952922993297299
Confirm Txconfirm_tx603360306036260366036
total603360306036260366036
Bridge User Actionsbridge_load_page24018230749284307
bridge_load_asset_picker18213022037212220
bridge_search_token72169674820739748
total1171109712666912321266
🔌 Startup Benchmarks
BenchmarkMetricMean (ms)Min (ms)Max (ms)Std Dev (ms)P75 (ms)P95 (ms)
Standard HomeuiStartup14341205204512114591645
load12041010164810212351414
domContentLoaded11981005163010012321406
domInteractive3017142222581
firstPaint1597440773213272
backgroundConnect21119233418213236
firstReactRender20134872134
initialActions106124
loadScripts100381214019810321204
setupStore1463661721
numNetworkReqs312287192384
Power User HomeuiStartup2245142610778139421684500
load11881049168715712031617
domContentLoaded11751040167515511961603
domInteractive39211682938125
firstPaint191781322137259320
backgroundConnect626253703710243172647
firstReactRender25164782941
initialActions105113
loadScripts96183014571509731390
setupStore1673961826
numNetworkReqs69331552781128
🧭 User Journey Benchmarks
BenchmarkMetricMean (ms)Min (ms)Max (ms)Std Dev (ms)P75 (ms)P95 (ms)
Onboarding Import WalletimportWalletToSocialScreen2212212210221221
srpButtonToSrpForm1031001104102110
confirmSrpToPwForm24242502425
pwFormToMetricsScreen16161711717
metricsToWalletReadyScreen18171811818
doneButtonToHomeScreen994816127220412121272
openAccountMenuToAccountListLoaded73977036766222075217662
total87788628891810388648918
Onboarding New WalletcreateWalletToSocialScreen2182172181218218
srpButtonToPwForm1071071080108108
createPwToRecoveryScreen888088
skipBackupToMetricsScreen37344023840
agreeButtonToOnboardingSuccess17161701717
doneButtonToAssetList915598145332010791453
total1308981187333014641873
Asset DetailsassetClickToPriceChart623786217986
total623786217986
Solana Asset DetailsassetClickToPriceChart45444514545
total45444514545
Import Srp HomeloginToHomeScreen2052198021346120852134
openAccountMenuAfterLogin47425234752
homeAfterImportWithNewWallet25912448281213226682812
total4690464447253447254725
Send TransactionsopenSendPageFromHome36343823838
selectTokenToSendFormLoaded20182222222
reviewTransactionToConfirmationPage85784687611862876
total91190193212915932
SwapopenSwapPageFromHome1028312015114120
fetchAndDisplaySwapQuotes4581456146071745964607
total468446754690646904690
🌐 Dapp Page Load Benchmarks

Current Commit: bf53a17 | Date: 2/27/2026

📄 Localhost MetaMask Test Dapp

Samples: 100

Summary

  • pageLoadTime-> current mean value: 1.04s (±46ms) 🟡 | historical mean value: 1.05s ⬇️ (historical data)
  • domContentLoaded-> current mean value: 727ms (±42ms) 🟢 | historical mean value: 740ms ⬇️ (historical data)
  • firstContentfulPaint-> current mean value: 79ms (±11ms) 🟢 | historical mean value: 83ms ⬇️ (historical data)

📈 Detailed Results

Metric Mean Std Dev Min Max P95 P99
pageLoadTime 1.04s 46ms 1.01s 1.35s 1.05s 1.35s
domContentLoaded 727ms 42ms 706ms 1.02s 742ms 1.02s
firstPaint 79ms 11ms 64ms 172ms 88ms 172ms
firstContentfulPaint 79ms 11ms 64ms 172ms 88ms 172ms
largestContentfulPaint 0ms 0ms 0ms 0ms 0ms 0ms
Bundle size diffs [🚀 Bundle size reduced!]
  • background: 105.97 KiB (2.01%)
  • ui: -24.87 KiB (-0.3%)
  • common: -143.66 KiB (-1.27%)

@github-actions
Copy link
Contributor

CLA Signature Action: All authors have signed the CLA. You may need to manually re-run the blocking PR check if it doesn't pass in a few minutes.

@n3ps n3ps force-pushed the n3ps/historical-prices-query branch from 60d608e to e4a39bb Compare February 28, 2026 01:54
@n3ps n3ps force-pushed the n3ps/historical-prices-query branch from e4a39bb to 793b25d Compare February 28, 2026 02:05
@n3ps n3ps force-pushed the n3ps/historical-prices-query branch from 793b25d to b9dbbb8 Compare February 28, 2026 02:21
setLoading(true);
const timePeriod = fromIso8601DurationToPriceApiTimePeriod(timeRange);
fetch(
`https://price.api.cx.metamask.io/v1/chains/${chainId}/historical-prices/${address}?vsCurrency=${currency}&timePeriod=${timePeriod}`,
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  1. Replace manual fetch

);

// Fetch the prices, and set them locally as a result of the fetch
useEffect(() => {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  1. And manual useEffect

@@ -118,83 +124,34 @@ const useHistoricalPricesEvm = ({
getShouldShowFiat(state, hexChainId),
);

const [loading, setLoading] = useState<boolean>(false);
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  1. And manual loading and data states

});
}, [isEvm, chainId, address, currency, timeRange, showFiat]);
// Fetch the prices, and set them locally as a result of the fetch
const { data: prices = [], isFetching } = useQuery({
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  1. With a useQuery

@n3ps n3ps changed the title refactor: useHistoricalPricesEvm to useQuery refactor: migrate useHistoricalPricesEvm Feb 28, 2026
@n3ps n3ps marked this pull request as ready for review February 28, 2026 02:54
@n3ps n3ps enabled auto-merge February 28, 2026 02:54
Copy link

@cursor cursor bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Cursor Bugbot has reviewed your changes and found 1 potential issue.

Bugbot Autofix is OFF. To automatically fix reported issues with cloud agents, have a team admin enable autofix in the Cursor dashboard.

@sonarqubecloud
Copy link

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

size-M team-core-extension-ux Core Extension UX team

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants