-
Notifications
You must be signed in to change notification settings - Fork 5.2k
feat: integrate multichain assets rates controller to extension UI #30291
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 36 commits into
main
from
salim/integrate-mutichain-assets-rates-controller
Feb 17, 2025
Merged
Changes from all commits
Commits
Show all changes
36 commits
Select commit
Hold shift + click to select a range
50802a2
fix: bump assets-controllers to v49
sahar-fehri b1463f9
Update LavaMoat policies
metamaskbot fb857ae
fix: dedupe
sahar-fehri 4a453d1
Update LavaMoat policies
metamaskbot 54c75c9
fix: merge conflicts
sahar-fehri db67847
fix: bump assets
sahar-fehri 607ca16
fix: patch
sahar-fehri 66c944f
fix: fix yarn
sahar-fehri 98eebaa
fix: fix test
sahar-fehri 6fd1caa
Update LavaMoat policies
metamaskbot 70fabea
fix: fix patch
sahar-fehri 7d4627d
Update LavaMoat policies
metamaskbot 9d28e99
Merge branch 'main' into feat/bump-assets-controller-49.0.0
sahar-fehri 91f130a
Merge branch 'main' into feat/bump-assets-controller-49.0.0
zone-live 414eef1
Merge branch 'main' into feat/bump-assets-controller-49.0.0
sahar-fehri 550a15d
fix: lint
sahar-fehri d27585b
Merge branch 'main' into feat/bump-assets-controller-49.0.0
sahar-fehri 855ff7e
Merge branch 'main' into feat/bump-assets-controller-49.0.0
sahar-fehri 209900a
[WIP] `MultichainAssetsController` init
GuillaumeRx 51c7df5
Update multichain controllers init
GuillaumeRx d8aa775
fix: integrate multichainAssetsRatesController
salimtb 48aa482
fix: fix after rebase
salimtb ef32bf4
fix: fix linter
salimtb f174ac4
fix: fix PR comments
salimtb 3276a75
Merge branch 'main' into salim/integrate-mutichain-assets-rates-contr…
salimtb aaabc9b
fix: fix e2e tests
salimtb f05509a
fix: fix build
salimtb da24a10
fix: fix build
salimtb 04ae981
fix: fix linter
salimtb 60a2284
fix: fix PR comments
salimtb c629eb7
Merge branch 'main' into salim/integrate-mutichain-assets-rates-contr…
salimtb 942e8e9
fix: fix linter
salimtb 415aa4c
fix: add controller name
salimtb ec00454
fix: sort alphabetically the added controller
salimtb ae46e89
fix: sort initialisation
salimtb 72519bf
Merge branch 'main' into salim/integrate-mutichain-assets-rates-contr…
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
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
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 |
---|---|---|
@@ -1,7 +1,9 @@ | ||
export { getMultichainAssetsControllerMessenger } from './multichain-assets-controller-messenger'; | ||
export { getMultiChainAssetsRatesControllerMessenger } from './multichain-assets-rates-controller-messenger'; | ||
export { getMultichainBalancesControllerMessenger } from './multichain-balances-controller-messenger'; | ||
export { getMultichainTransactionsControllerMessenger } from './multichain-transactions-controller-messenger'; | ||
|
||
export type { MultichainAssetsControllerMessenger } from './multichain-assets-controller-messenger'; | ||
export type { MultiChainAssetsRatesControllerMessenger } from './multichain-assets-rates-controller-messenger'; | ||
export type { MultichainBalancesControllerMessenger } from './multichain-balances-controller-messenger'; | ||
export type { MultichainTransactionsControllerMessenger } from './multichain-transactions-controller-messenger'; |
14 changes: 14 additions & 0 deletions
14
...ontroller-init/messengers/multichain/multichain-assets-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,14 @@ | ||
import { Messenger, RestrictedMessenger } from '@metamask/base-controller'; | ||
import { getMultiChainAssetsRatesControllerMessenger } from './multichain-assets-rates-controller-messenger'; | ||
|
||
describe('getMultiChainAssetsRatesControllerMessenger', () => { | ||
it('returns a restricted messenger', () => { | ||
const messenger = new Messenger<never, never>(); | ||
const multichainAssetsRatesControllerMessenger = | ||
getMultiChainAssetsRatesControllerMessenger(messenger); | ||
|
||
expect(multichainAssetsRatesControllerMessenger).toBeInstanceOf( | ||
RestrictedMessenger, | ||
); | ||
}); | ||
}); |
61 changes: 61 additions & 0 deletions
61
...pts/controller-init/messengers/multichain/multichain-assets-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 { | ||
AccountsControllerAccountAddedEvent, | ||
AccountsControllerListMultichainAccountsAction, | ||
} from '@metamask/accounts-controller'; | ||
import { | ||
CurrencyRateStateChange, | ||
GetCurrencyRateState, | ||
MultichainAssetsControllerStateChangeEvent, | ||
MultichainAssetsControllerGetStateAction, | ||
} from '@metamask/assets-controllers'; | ||
import { | ||
KeyringControllerLockEvent, | ||
KeyringControllerUnlockEvent, | ||
} from '@metamask/keyring-controller'; | ||
import { HandleSnapRequest } from '@metamask/snaps-controllers'; | ||
|
||
type Actions = | ||
| HandleSnapRequest | ||
| AccountsControllerListMultichainAccountsAction | ||
| GetCurrencyRateState | ||
| MultichainAssetsControllerGetStateAction; | ||
|
||
type Events = | ||
| KeyringControllerLockEvent | ||
| KeyringControllerUnlockEvent | ||
| AccountsControllerAccountAddedEvent | ||
| CurrencyRateStateChange | ||
| MultichainAssetsControllerStateChangeEvent; | ||
|
||
export type MultiChainAssetsRatesControllerMessenger = ReturnType< | ||
typeof getMultiChainAssetsRatesControllerMessenger | ||
>; | ||
|
||
/** | ||
* Get a restricted messenger for the Multichain Assets Rate controller. This is scoped to the | ||
* actions and events that the multichain Assets Rate controller is allowed to handle. | ||
* | ||
* @param messenger - The controller messenger to restrict. | ||
* @returns The restricted controller messenger. | ||
*/ | ||
export function getMultiChainAssetsRatesControllerMessenger( | ||
messenger: Messenger<Actions, Events>, | ||
) { | ||
return messenger.getRestricted({ | ||
name: 'MultiChainAssetsRatesController', | ||
allowedEvents: [ | ||
'AccountsController:accountAdded', | ||
'KeyringController:lock', | ||
'KeyringController:unlock', | ||
'CurrencyRateController:stateChange', | ||
'MultichainAssetsController:stateChange', | ||
], | ||
allowedActions: [ | ||
'AccountsController:listMultichainAccounts', | ||
'SnapController:handleRequest', | ||
'CurrencyRateController:getState', | ||
'MultichainAssetsController:getState', | ||
], | ||
}); | ||
} |
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 |
---|---|---|
@@ -1,3 +1,4 @@ | ||
export { MultichainAssetsControllerInit } from './multichain-assets-controller-init'; | ||
export { MultichainBalancesControllerInit } from './multichain-balances-controller-init'; | ||
export { MultichainTransactionsControllerInit } from './multichain-transactions-controller-init'; | ||
export { MultiChainAssetsRatesControllerInit } from './multichain-rates-assets-controller-init'; |
52 changes: 52 additions & 0 deletions
52
app/scripts/controller-init/multichain/multichain-rates-assets-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,52 @@ | ||
import { MultiChainAssetsRatesController } from '@metamask/assets-controllers'; | ||
import { Messenger } from '@metamask/base-controller'; | ||
import { buildControllerInitRequestMock } from '../test/utils'; | ||
import { ControllerInitRequest } from '../types'; | ||
import { | ||
getMultiChainAssetsRatesControllerMessenger, | ||
MultiChainAssetsRatesControllerMessenger, | ||
} from '../messengers/multichain'; | ||
import { MultiChainAssetsRatesControllerInit } from './multichain-rates-assets-controller-init'; | ||
|
||
jest.mock('@metamask/assets-controllers'); | ||
|
||
function buildInitRequestMock(): jest.Mocked< | ||
ControllerInitRequest<MultiChainAssetsRatesControllerMessenger> | ||
> { | ||
const baseControllerMessenger = new Messenger(); | ||
|
||
return { | ||
...buildControllerInitRequestMock(), | ||
controllerMessenger: getMultiChainAssetsRatesControllerMessenger( | ||
baseControllerMessenger, | ||
), | ||
initMessenger: undefined, | ||
}; | ||
} | ||
|
||
describe('MultiChainAssetsRatesControllerInit', () => { | ||
const multiChainAssetsRatesControllerClassMock = jest.mocked( | ||
MultiChainAssetsRatesController, | ||
); | ||
|
||
beforeEach(() => { | ||
jest.resetAllMocks(); | ||
}); | ||
|
||
it('returns controller instance', () => { | ||
const requestMock = buildInitRequestMock(); | ||
expect( | ||
MultiChainAssetsRatesControllerInit(requestMock).controller, | ||
).toBeInstanceOf(MultiChainAssetsRatesController); | ||
}); | ||
|
||
it('initializes with correct messenger and state', () => { | ||
const requestMock = buildInitRequestMock(); | ||
MultiChainAssetsRatesControllerInit(requestMock); | ||
|
||
expect(multiChainAssetsRatesControllerClassMock).toHaveBeenCalledWith({ | ||
messenger: requestMock.controllerMessenger, | ||
state: requestMock.persistedState.MultiChainAssetsRatesController, | ||
}); | ||
}); | ||
}); |
25 changes: 25 additions & 0 deletions
25
app/scripts/controller-init/multichain/multichain-rates-assets-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,25 @@ | ||
import { MultiChainAssetsRatesController } from '@metamask/assets-controllers'; | ||
import { ControllerInitFunction } from '../types'; | ||
import { MultiChainAssetsRatesControllerMessenger } from '../messengers/multichain'; | ||
|
||
/** | ||
* Initialize the Multichain Assets Rate 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 MultiChainAssetsRatesControllerInit: ControllerInitFunction< | ||
MultiChainAssetsRatesController, | ||
MultiChainAssetsRatesControllerMessenger | ||
> = ({ controllerMessenger, persistedState }) => { | ||
const controller = new MultiChainAssetsRatesController({ | ||
messenger: controllerMessenger, | ||
state: persistedState.MultiChainAssetsRatesController, | ||
}); | ||
|
||
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
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,75 @@ | ||
import { | ||
getAssetsRates, | ||
AssetsRatesState, | ||
AssetsState, | ||
getAccountAssets, | ||
getAssetsMetadata, | ||
} from './assets'; | ||
|
||
const mockRatesState = { | ||
metamask: { | ||
conversionRates: { | ||
'token-1': { rate: 1.5, currency: 'USD' }, | ||
'token-2': { rate: 0.8, currency: 'EUR' }, | ||
}, | ||
}, | ||
}; | ||
|
||
// Mock state for testing | ||
const mockAssetsState: AssetsState = { | ||
metamask: { | ||
accountsAssets: { | ||
'5132883f-598e-482c-a02b-84eeaa352f5b': [ | ||
'solana:5eykt4UsFv8P8NJdTREpY1vzqKqZKvdp/slip44:501', | ||
], | ||
}, | ||
assetsMetadata: { | ||
'solana:5eykt4UsFv8P8NJdTREpY1vzqKqZKvdp/slip44:501': { | ||
name: 'Token 1', | ||
symbol: 'TKN1', | ||
iconUrl: 'https://example.com/token-1.png', | ||
fungible: true, | ||
units: [{ symbol: 'TKN1', name: 'Token 1', decimals: 9 }], | ||
}, | ||
}, | ||
}, | ||
}; | ||
|
||
describe('getAccountAssets', () => { | ||
it('should return the assets from the state', () => { | ||
const result = getAccountAssets(mockAssetsState); | ||
expect(result).toEqual(mockAssetsState.metamask.accountsAssets); | ||
}); | ||
}); | ||
|
||
describe('getAssetsMetadata', () => { | ||
it('should return the assets metadata from the state', () => { | ||
const result = getAssetsMetadata(mockAssetsState); | ||
expect(result).toEqual(mockAssetsState.metamask.assetsMetadata); | ||
}); | ||
|
||
it('should return undefined if state does not have metamask property', () => { | ||
const invalidState = {} as AssetsState; | ||
expect(() => getAssetsMetadata(invalidState)).toThrow(); | ||
}); | ||
}); | ||
|
||
describe('getAssetsRates', () => { | ||
it('should return the assetsRates from the state', () => { | ||
const result = getAssetsRates(mockRatesState); | ||
expect(result).toEqual(mockRatesState.metamask.conversionRates); | ||
}); | ||
|
||
it('should return an empty object if assetsRates is empty', () => { | ||
const emptyState: AssetsRatesState = { | ||
metamask: { conversionRates: {} }, | ||
}; | ||
const result = getAssetsRates(emptyState); | ||
expect(result).toEqual({}); | ||
}); | ||
|
||
it('should return undefined if state does not have metamask property', () => { | ||
const invalidState = {} as AssetsRatesState; | ||
expect(() => getAssetsRates(invalidState)).toThrow(); | ||
}); | ||
}); |
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Quick question, will this selector replace the existing getMultichainCoinRates
Asking because it's being used in the
getMultichainConversionRate
cc @salimtb @ccharly
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@zone-live The one you mentioned is limited to Native tokens, whereas the one in this PR supports both SPL tokens and Native tokens.
So, the answer is yes—if we need comprehensive data, including SPL tokens, we should use the one in this PR.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thank you @salimtb 💪🏼