Skip to content

Conversation

@georgeweiler
Copy link
Contributor

@georgeweiler georgeweiler commented Jan 6, 2026

Explanation

Current State and Why It Needs to Change

The RampsController currently uses "geolocation" terminology (geolocation state, updateGeolocation() method), which implies a read-only, automatically-detected value. However, we plan to allow users to manually change their region in the future, so the naming should reflect that it's a user-selectable region rather than just a detected geolocation.

Additionally, consumers must manually call updateGeolocation() after creating the controller, which is error-prone and lacks a clear initialization pattern.

Solution and How It Works

This PR refactors the controller to use "userRegion" terminology and introduces proper initialization:

  1. Renamed state and methods: geolocationuserRegion, updateGeolocation()updateUserRegion()
  2. Added init() method: Encapsulates initialization logic, automatically fetches user region at startup
  3. Added setUserRegion() method: Allows manual region setting without geolocation API, preparing for future UI feature

Breaking Changes

  • geolocation state property → userRegion
  • updateGeolocation() method → updateUserRegion()
  • Removed deprecated selectGeolocation and selectGeolocationRequest selectors
  • Removed deprecated useRampsGeolocation() hook
  • Consumers should use selectUserRegion, selectUserRegionRequest, and useRampsUserRegion() instead

Why Multiple Packages Were Updated

This refactor touches both the core controller package and the mobile app because:

  • Core package contains the controller implementation and public API
  • Mobile app contains initialization, selectors, hooks, and tests that consume the controller

All changes are coordinated to ensure consistent API updates and proper migration from deprecated names.

References

Mobile PR that adopts the breaking changes: MetaMask/metamask-mobile#24280

Checklist

  • I've updated the test suite for new or updated code as appropriate
  • I've updated documentation (JSDoc, Markdown, etc.) for new or updated code as appropriate
  • I've communicated my changes to consumers by updating changelogs for packages I've changed
  • I've introduced breaking changes in this PR and have prepared draft pull requests for clients and consumer packages to resolve them

Note

BREAKING: Rename geolocation API

  • State geolocationuserRegion; method updateGeolocation()updateUserRegion() with updated request cache keys and tests

New APIs

  • Add init() to fetch initial userRegion on startup (graceful failure)
  • Add setUserRegion(region) to manually set region and fetch eligibility

Behavioral updates

  • Normalize region to lowercase; automatically fetch eligibility after updateUserRegion
  • Clear stale eligibility on failures only if region still matches; prevent race-based overwrites
  • updateEligibility(isoCode) only updates state when userRegion matches the isoCode/undefined
  • Handle null/undefined geolocation results

Misc

  • Update createRequestSelector examples and tests to reference updateUserRegion
  • Changelog updated

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

… into TRAM-2923-ramps-controller-get-eligibility
@georgeweiler georgeweiler marked this pull request as ready for review January 8, 2026 23:41
@georgeweiler georgeweiler requested review from a team as code owners January 8, 2026 23:41
@georgeweiler georgeweiler changed the title Ramps controller geolocation rename refactor(ramps): ramps controller geolocation rename Jan 9, 2026
Comment on lines 509 to 515
if (state.userRegion === null) {
state.eligibility = eligibility;
} else {
const currentUserRegion = state.userRegion.toLowerCase().trim();
if (currentUserRegion === normalizedIsoCode) {
state.eligibility = eligibility;
}
Copy link
Member

Choose a reason for hiding this comment

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

small suggestion for readability: extract state.geolocation as a variable to avoid the null check and string manipulation on separate lines:

this.update((state) => {
  const userRegion = state.geolocation?.toLowerCase().trim();
  
  if (userRegion === null || userRegion === normalizedIsoCode) {
    state.eligibility = eligibility;
  }
});

@georgeweiler georgeweiler added this pull request to the merge queue Jan 12, 2026
Merged via the queue into main with commit 28f70ae Jan 12, 2026
286 checks passed
@georgeweiler georgeweiler deleted the ramps-controller-geolocation-rename branch January 12, 2026 22:12
github-merge-queue bot pushed a commit to MetaMask/metamask-mobile that referenced this pull request Jan 15, 2026
<!--
Write a short description of the changes included in this pull request,
also include relevant motivation and context. Have in mind the following
questions:
1. What is the reason for the change?
2. What is the improvement/solution?
-->

## **Description**

This PR adopts the breaking changes from `@metamask/ramps-controller`
v3.1.0, which refactors the controller to use "userRegion" terminology
instead of "geolocation" and introduces proper initialization patterns.

### **Reason for Change**

The ramps-controller package was refactored to:
1. **Improve naming clarity**: "geolocation" implies a read-only,
automatically-detected value, but we plan to allow users to manually
change their region. The new "userRegion" terminology better reflects
that it's a user-selectable region.
2. **Add proper initialization**: Previously, consumers had to manually
call `updateGeolocation()` after creating the controller, which was
error-prone. The new `init()` method encapsulates initialization logic
and automatically fetches the user region at startup.
3. **Enable manual region setting**: The new `setUserRegion()` method
allows users to manually set their region without calling the
geolocation API, preparing for future UI features.

### **Changes in This PR**

#### **Selectors** (`app/selectors/rampsController/index.ts`)
- ✅ Removed deprecated `selectGeolocation` selector
- ✅ Removed deprecated `selectGeolocationRequest` selector  
- ✅ Added `selectUserRegion` selector (replaces `selectGeolocation`)
- ✅ Added `selectUserRegionRequest` selector (replaces
`selectGeolocationRequest`)

#### **Hooks** (`app/components/UI/Ramp/hooks/`)
- ✅ Renamed `useRampsGeolocation.ts` → `useRampsUserRegion.ts`
- ✅ Updated hook to use new `updateUserRegion()` and `setUserRegion()`
methods
- ✅ Updated hook to use new `selectUserRegion` and
`selectUserRegionRequest` selectors
- ✅ Updated all references and imports throughout the codebase

#### **Controller Initialization**
(`app/core/Engine/controllers/ramps-controller/ramps-controller-init.ts`)
- ✅ Updated to call `controller.init()` at startup (non-blocking)
- ✅ Initialization automatically fetches user region via geolocation API
- ✅ Errors are handled gracefully and available via selectors

#### **Tests**
- ✅ Updated `ramps-controller-init.test.ts` to test new `init()` method
- ✅ Updated `useRampsUserRegion.test.ts` to use new API and cache keys
- ✅ Updated selector tests to use `userRegion` state property
- ✅ All tests updated to use `'updateUserRegion'` cache key instead of
`'updateGeolocation'`

#### **Yarn Patch**
- ✅ Updated ramps-controller patch to include all local changes
(userRegion state, updateUserRegion, setUserRegion, init methods)

### **Migration Guide**

**Before:**
```ts
import { selectGeolocation, selectGeolocationRequest } from './selectors/rampsController';
import { useRampsGeolocation } from './hooks/useRampsGeolocation';

// Manual initialization required
Engine.context.RampsController.updateGeolocation();
```

**After:**
```ts
import { selectUserRegion, selectUserRegionRequest } from './selectors/rampsController';
import { useRampsUserRegion } from './hooks/useRampsUserRegion';

// Automatic initialization via controller.init() in ramps-controller-init.ts
```

### **Breaking Changes Adopted**

- ❌ `geolocation` state property → ✅ `userRegion`
- ❌ `updateGeolocation()` method → ✅ `updateUserRegion()`
- ❌ `selectGeolocation` selector → ✅ `selectUserRegion`
- ❌ `selectGeolocationRequest` selector → ✅ `selectUserRegionRequest`
- ❌ `useRampsGeolocation()` hook → ✅ `useRampsUserRegion()`

### **Related PRs**

- Core package PR:
[MetaMask/core#7563](MetaMask/core#7563) -
Refactors ramps-controller to use userRegion terminology and adds init()
method



## **Changelog**

<!--
If this PR is not End-User-Facing and should not show up in the
CHANGELOG, you can choose to either:
1. Write `CHANGELOG entry: null`
2. Label with `no-changelog`

If this PR is End-User-Facing, please write a short User-Facing
description in the past tense like:
`CHANGELOG entry: Added a new tab for users to see their NFTs`
`CHANGELOG entry: Fixed a bug that was causing some NFTs to flicker`

(This helps the Release Engineer do their job more quickly and
accurately)
-->

CHANGELOG entry: Adopted new ramp controller variable names

## **Related issues**

Fixes:

## **Manual testing steps**

```gherkin
Feature: my feature name

  Scenario: user [verb for user action]
    Given [describe expected initial app state]

    When user [verb for user action]
    Then [describe expected outcome]
```

## **Screenshots/Recordings**

<!-- If applicable, add screenshots and/or recordings to visualize the
before and after of your change. -->

### **Before**

<!-- [screenshots/recordings] -->

### **After**

<!-- [screenshots/recordings] -->

## **Pre-merge author checklist**

- [ ] I’ve followed [MetaMask Contributor
Docs](https://github.com/MetaMask/contributor-docs) and [MetaMask Mobile
Coding
Standards](https://github.com/MetaMask/metamask-mobile/blob/main/.github/guidelines/CODING_GUIDELINES.md).
- [ ] I've completed the PR template to the best of my ability
- [ ] I’ve included tests if applicable
- [ ] I’ve documented my code using [JSDoc](https://jsdoc.app/) format
if applicable
- [ ] I’ve applied the right labels on the PR (see [labeling
guidelines](https://github.com/MetaMask/metamask-mobile/blob/main/.github/guidelines/LABELING_GUIDELINES.md)).
Not required for external contributors.

## **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.



<!-- CURSOR_SUMMARY -->
---

> [!NOTE]
> Adopts the new `@metamask/ramps-controller` v3 APIs and init model.
> 
> - Migrate from `geolocation` to `userRegion`: new `useRampsUserRegion`
hook (with `fetchUserRegion` and `setUserRegion`), selectors
`selectUserRegion`/`selectUserRegionRequest` (retain `selectGeolocation`
aliases); update `useRampsGeolocation` to call `updateUserRegion`
> - Initialize controller via `controller.init()` at startup
(`ramps-controller-init`), replacing `updateGeolocation` side-effect
> - Refactor ramps service init: derive environment via
`getRampsEnvironment` and platform context via `getRampsContext` (uses
`Platform.OS`)
> - Update fixtures, snapshots, and tests accordingly (new request keys
`updateUserRegion:[]`, added `eligibility`/`tokens` fields)
> - Bump dependency to `@metamask/ramps-controller@^3.0.0`
> 
> <sup>Written by [Cursor
Bugbot](https://cursor.com/dashboard?tab=bugbot) for commit
c99674b. This will update automatically
on new commits. Configure
[here](https://cursor.com/dashboard?tab=bugbot).</sup>
<!-- /CURSOR_SUMMARY -->
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants