-
Notifications
You must be signed in to change notification settings - Fork 5.4k
refactor: migrate tokenRates controller to modular controller init #31254
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
salimtb
merged 4 commits into
main
from
salim/mirgrate-token-rate-to-modular-architecture
Mar 31, 2025
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
d975a69
refactor: migrate tokenRates controller to modular controller init ar…
salimtb c28f96e
Merge branch 'main' into salim/mirgrate-token-rate-to-modular-archite…
salimtb ffb713a
Merge branch 'main' into salim/mirgrate-token-rate-to-modular-archite…
salimtb c416798
Merge branch 'main' into salim/mirgrate-token-rate-to-modular-archite…
salimtb File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| export { TokenRatesControllerInit } from './token-rates-controller-init'; |
47 changes: 47 additions & 0 deletions
47
app/scripts/controller-init/assets/token-rates-controller-init.test.ts
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,47 @@ | ||
| import { | ||
| TokenRatesController, | ||
| TokenRatesControllerMessenger, | ||
| } from '@metamask/assets-controllers'; | ||
| import { Messenger } from '@metamask/base-controller'; | ||
| import { buildControllerInitRequestMock } from '../test/utils'; | ||
| import { ControllerInitRequest } from '../types'; | ||
| import { getTokenRatesControllerMessenger } from '../messengers/assets'; | ||
| import { TokenRatesControllerInit } from './token-rates-controller-init'; | ||
|
|
||
| jest.mock('@metamask/assets-controllers'); | ||
|
|
||
| function buildInitRequestMock(): jest.Mocked< | ||
| ControllerInitRequest<TokenRatesControllerMessenger> | ||
| > { | ||
| const baseControllerMessenger = new Messenger(); | ||
|
|
||
| return { | ||
| ...buildControllerInitRequestMock(), | ||
| controllerMessenger: getTokenRatesControllerMessenger( | ||
| baseControllerMessenger, | ||
| ), | ||
| initMessenger: undefined, | ||
| }; | ||
| } | ||
|
|
||
| describe('TokenRatesControllerInit', () => { | ||
| const tokenRatesControllerClassMock = jest.mocked(TokenRatesController); | ||
|
|
||
| beforeEach(() => { | ||
| jest.resetAllMocks(); | ||
| }); | ||
|
|
||
| it('returns controller instance', () => { | ||
| const requestMock = buildInitRequestMock(); | ||
| expect(TokenRatesControllerInit(requestMock).controller).toBeInstanceOf( | ||
| TokenRatesController, | ||
| ); | ||
| }); | ||
|
|
||
| it('initializes with correct messenger and state', () => { | ||
| const requestMock = buildInitRequestMock(); | ||
| TokenRatesControllerInit(requestMock); | ||
|
|
||
| expect(tokenRatesControllerClassMock).toHaveBeenCalled(); | ||
| }); | ||
| }); |
30 changes: 30 additions & 0 deletions
30
app/scripts/controller-init/assets/token-rates-controller-init.ts
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,30 @@ | ||
| import { | ||
| CodefiTokenPricesServiceV2, | ||
| TokenRatesController, | ||
| } from '@metamask/assets-controllers'; | ||
| import { ControllerInitFunction } from '../types'; | ||
| import { TokenRatesControllerMessenger } from '../messengers/assets'; | ||
|
|
||
| /** | ||
| * Initialize the Token Rates controller. | ||
| * | ||
| * @param request - The request object. | ||
| * @param request.controllerMessenger - The messenger to use for the controller. | ||
| * @param request.persistedState - The persisted state of the extension. | ||
| * @returns The initialized controller. | ||
| */ | ||
| export const TokenRatesControllerInit: ControllerInitFunction< | ||
| TokenRatesController, | ||
| TokenRatesControllerMessenger | ||
| > = ({ controllerMessenger, persistedState }) => { | ||
| const controller = new TokenRatesController({ | ||
| messenger: controllerMessenger, | ||
| state: persistedState.TokenRatesController, | ||
| tokenPricesService: new CodefiTokenPricesServiceV2(), | ||
| disabled: !persistedState.PreferencesController?.useCurrencyRateCheck, | ||
| }); | ||
|
|
||
| return { | ||
| controller, | ||
| }; | ||
| }; |
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,3 @@ | ||
| export { getTokenRatesControllerMessenger } from './token-rates-controller-messenger'; | ||
|
|
||
| export type { TokenRatesControllerMessenger } from './token-rates-controller-messenger'; |
12 changes: 12 additions & 0 deletions
12
app/scripts/controller-init/messengers/assets/token-rates-controller-messenger.test.ts
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,12 @@ | ||
| import { Messenger, RestrictedMessenger } from '@metamask/base-controller'; | ||
| import { getTokenRatesControllerMessenger } from './token-rates-controller-messenger'; | ||
|
|
||
| describe('getTokenRatesControllerMessenger', () => { | ||
| it('returns a restricted messenger', () => { | ||
| const messenger = new Messenger<never, never>(); | ||
| const tokenRatesControllerMessenger = | ||
| getTokenRatesControllerMessenger(messenger); | ||
|
|
||
| expect(tokenRatesControllerMessenger).toBeInstanceOf(RestrictedMessenger); | ||
| }); | ||
| }); |
61 changes: 61 additions & 0 deletions
61
app/scripts/controller-init/messengers/assets/token-rates-controller-messenger.ts
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,61 @@ | ||
| import { Messenger } from '@metamask/base-controller'; | ||
| import { | ||
| NetworkControllerGetStateAction, | ||
| NetworkControllerGetNetworkClientByIdAction, | ||
| NetworkControllerStateChangeEvent, | ||
| } from '@metamask/network-controller'; | ||
| import { | ||
| AccountsControllerGetSelectedAccountAction, | ||
| AccountsControllerGetAccountAction, | ||
| AccountsControllerSelectedEvmAccountChangeEvent, | ||
| } from '@metamask/accounts-controller'; | ||
| import { PreferencesControllerStateChangeEvent } from '@metamask/preferences-controller'; | ||
| import { | ||
| TokensControllerGetStateAction, | ||
| TokensControllerStateChangeEvent, | ||
| } from '@metamask/assets-controllers'; | ||
|
|
||
| type Actions = | ||
| | TokensControllerGetStateAction | ||
| | NetworkControllerGetNetworkClientByIdAction | ||
| | NetworkControllerGetStateAction | ||
| | AccountsControllerGetAccountAction | ||
| | AccountsControllerGetSelectedAccountAction; | ||
|
|
||
| type Events = | ||
| | NetworkControllerStateChangeEvent | ||
| | AccountsControllerSelectedEvmAccountChangeEvent | ||
| | PreferencesControllerStateChangeEvent | ||
| | TokensControllerStateChangeEvent; | ||
|
|
||
| export type TokenRatesControllerMessenger = ReturnType< | ||
| typeof getTokenRatesControllerMessenger | ||
| >; | ||
|
|
||
| /** | ||
| * Get a restricted messenger for the Token Rates controller. This is scoped to the | ||
| * actions and events that the Token Rates controller is allowed to handle. | ||
| * | ||
| * @param messenger - The controller messenger to restrict. | ||
| * @returns The restricted controller messenger. | ||
| */ | ||
| export function getTokenRatesControllerMessenger( | ||
| messenger: Messenger<Actions, Events>, | ||
| ) { | ||
| return messenger.getRestricted({ | ||
| name: 'TokenRatesController', | ||
| allowedActions: [ | ||
| 'TokensController:getState', | ||
| 'NetworkController:getNetworkClientById', | ||
| 'NetworkController:getState', | ||
| 'AccountsController:getAccount', | ||
| 'AccountsController:getSelectedAccount', | ||
| ], | ||
| allowedEvents: [ | ||
| 'NetworkController:stateChange', | ||
| 'AccountsController:selectedEvmAccountChange', | ||
| 'PreferencesController:stateChange', | ||
| 'TokensController:stateChange', | ||
| ], | ||
| }); | ||
| } | ||
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
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
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.