feat: Implementation of gelocation controller#8037
Open
MarioAslau wants to merge 8 commits intomainfrom
Open
Conversation
Contributor
NicolasMassart
left a comment
There was a problem hiding this comment.
A few suggestions to fix and questions, after that, will look good.
Contributor
cryptodev-2s
left a comment
There was a problem hiding this comment.
Could you also run update-readme-content to update the root readme
There was a problem hiding this comment.
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.
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.
Explanation
Problem
On mobile, four separate features (Ramp, Perps, Rewards, Card) independently call the same geolocation API endpoint (
https://on-ramp.api.cx.metamask.io/geolocation), each with its own implementation, URL constants, caching strategy, and trigger mechanism. This results in 2–4 redundant HTTP calls on every app load (~1.8–3.1 seconds each), inconsistent environment URL handling across features, and no shared cache.fetch()useEffecton mountsuccessfulFetch()checkEligibility()callsuccessfulFetch()getGeoRewardsMetadata()callfetch()getCardholder()callSolution
This PR introduces a new
@metamask/geolocation-controllerpackage that centralises geolocation fetching behind a singleGeolocationController. The controller extendsBaseControllerfrom@metamask/base-controllerand follows established controller conventions (matching the patterns inconnectivity-controller).Key design decisions:
getGeolocation()share a single in-flight promise, so multiple consumers calling simultaneously produce only one HTTP request.fetchfunction — the controller accepts afetchparameter (defaults toglobalThis.fetch), consistent with theRampsServicepattern. This makes the controller fully testable without global mocks.getGeolocationUrlcallback is provided at construction time, allowing the host application to resolve the correct API URL (prod vs. dev/UAT) without the controller having to know about environment configuration.persist: falseon all metadata fields). Geolocation is re-fetched on every app restart, which is appropriate since the data is IP-based and lightweight.What's included
New package:
packages/geolocation-controller/src/GeolocationController.ts— Controller implementation with state management, TTL cache, and promise deduplicationsrc/types.ts—GeolocationStatustype ('idle' | 'loading' | 'complete' | 'error')src/GeolocationController-method-action-types.ts— Messenger action types forgetGeolocationandrefreshGeolocationsrc/index.ts— Public exportssrc/GeolocationController.test.ts— 25 test cases covering cache behaviour, promise deduplication, fetch success/failure, refresh, messenger integration, metadata, and edge casespackage.json,tsconfig.json,tsconfig.build.json,jest.config.js,typedoc.json,README.md,CHANGELOG.md,LICENSEModified:
tsconfig.json— added project reference for the new package (alphabetical order, aftergator-permissions-controller)Public API
State:
Messenger actions:
GeolocationController:getGeolocation— Returns the cached location if fresh, otherwise fetches and returns it. Concurrent calls are deduplicated.GeolocationController:refreshGeolocation— Forces a fresh fetch, bypassing the cache.GeolocationController:getState— Returns the current controller state (provided byBaseController).Messenger events:
GeolocationController:stateChange— Fires on every state transition (provided byBaseController).References
decisions/decisions/core/0019-geolocation-controller.mddecisions/decisions/core/0001-messaging-non-controllers.mdpackages/connectivity-controller/Checklist
Note
Low Risk
Mostly additive: introduces a new package and wires it into monorepo tooling (tsconfig refs, yarn workspace, CODEOWNERS/teams, README). Risk is mainly around build/packaging integration rather than behavior changes in existing runtime code.
Overview
Introduces a new
@metamask/geolocation-controllerpackage implementingGeolocationController(aBaseController) that fetches an ISO country code via an injectedfetch/getGeolocationUrl, with TTL in-memory caching, in-flight request deduplication, and arefreshGeolocationpath that bypasses cache while preventing stale updates.Adds full package scaffolding and tests (Jest + typedoc + TS configs, changelog/license/readme), and wires the new workspace into repo metadata and tooling (
CODEOWNERS,teams.json, roottsconfigreferences,yarn.lock, and README package list/dependency graph).Written by Cursor Bugbot for commit 62051c4. This will update automatically on new commits. Configure here.