-
Notifications
You must be signed in to change notification settings - Fork 5.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: integrate multichain assets rates controller to extension UI (#…
…30291) <!-- Please submit this PR as a draft initially. Do not mark it as "Ready for review" until the template has been completely filled out, and PR status checks have passed at least once. --> ## **Description** This PR aims to integrate the new multichainAssetsRatesControllers into the extension. To achieve this, an upgrade of the assets controller to version 49 is required. As a result, the target branch is set accordingly: [MetaMask PR #30250](#30250). <!-- 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? --> [](https://codespaces.new/MetaMask/metamask-extension/pull/30291?quickstart=1) ## **Related issues** Fixes: ## **Manual testing steps** 1. open a flask build and add a solana account 2. check if the polling for the rates is running ## **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 Extension Coding Standards](https://github.com/MetaMask/metamask-extension/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-extension/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. --------- Co-authored-by: sahar-fehri <[email protected]> Co-authored-by: MetaMask Bot <[email protected]> Co-authored-by: António Regadas <[email protected]> Co-authored-by: Guillaume Roux <[email protected]>
- Loading branch information
1 parent
422201c
commit 1f4afda
Showing
14 changed files
with
266 additions
and
1 deletion.
There are no files selected for viewing
This file contains 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 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 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 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 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 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 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 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 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 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 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 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 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 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