-
-
Notifications
You must be signed in to change notification settings - Fork 293
feat(wallet): add TransactionController to wallet package #8975
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
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
fb9db90
feat(wallet): wire transaction controller
matthewwalsh0 086faae
chore: fix wallet changelog and lockfile after rebase
matthewwalsh0 5c85d14
chore: fix wallet package constraints and lockfile
matthewwalsh0 fb945ea
refactor(wallet): address PR feedback on TransactionController wiring
matthewwalsh0 2e54b30
fix(wallet): destructure disableSwaps to avoid TS2783 duplicate key e…
matthewwalsh0 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
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
144 changes: 144 additions & 0 deletions
144
...wallet/src/initialization/instances/transaction-controller/transaction-controller.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,144 @@ | ||
| import { Messenger } from '@metamask/messenger'; | ||
| import { InMemoryStorageAdapter } from '@metamask/storage-service'; | ||
| import { TransactionController } from '@metamask/transaction-controller'; | ||
|
|
||
| import type { WalletOptions } from '../../../types'; | ||
| import { Wallet } from '../../../Wallet'; | ||
| import { defaultConfigurations } from '../../defaults'; | ||
| import type { | ||
| DefaultActions, | ||
| DefaultEvents, | ||
| RootMessenger, | ||
| } from '../../defaults'; | ||
| import { AlwaysOnlineAdapter } from '../connectivity-controller/always-online-adapter'; | ||
| import { transactionController } from './transaction-controller'; | ||
|
|
||
| const controllers: TransactionController[] = []; | ||
| const wallets: Wallet[] = []; | ||
|
|
||
| const REMOTE_FEATURE_FLAG_OPTIONS = { | ||
| clientConfigApiService: { | ||
| fetchRemoteFeatureFlags: async (): Promise<{ | ||
| remoteFeatureFlags: Record<string, boolean>; | ||
| cacheTimestamp: number; | ||
| }> => ({ remoteFeatureFlags: {}, cacheTimestamp: Date.now() }), | ||
| }, | ||
| }; | ||
|
|
||
| type ActionHandler = (...args: unknown[]) => unknown; | ||
|
|
||
| type AnyMessenger = Messenger<string>; | ||
|
|
||
| describe('transactionController', () => { | ||
| afterEach(async () => { | ||
| for (const controller of controllers.splice(0)) { | ||
| controller.destroy(); | ||
| } | ||
|
|
||
| await Promise.all(wallets.splice(0).map((wallet) => wallet.destroy())); | ||
| }); | ||
|
|
||
| it('is registered as a default initialization configuration', () => { | ||
| expect(Object.values(defaultConfigurations)).toContain( | ||
| transactionController, | ||
| ); | ||
| }); | ||
|
|
||
| it('initializes a TransactionController with default state', () => { | ||
| const rootMessenger = getRootMessenger(); | ||
| const messenger = transactionController.getMessenger(rootMessenger); | ||
|
|
||
| const instance = transactionController.init({ | ||
| state: undefined, | ||
| messenger, | ||
| options: {}, | ||
| }); | ||
| controllers.push(instance); | ||
|
|
||
| expect(instance).toBeInstanceOf(TransactionController); | ||
| expect(rootMessenger.call('TransactionController:getState')).toStrictEqual({ | ||
| methodData: {}, | ||
| transactions: [], | ||
| transactionBatches: [], | ||
| lastFetchedBlockNumbers: {}, | ||
| submitHistory: [], | ||
| }); | ||
| }); | ||
|
|
||
| it('is initialized by the default Wallet configuration', () => { | ||
| const wallet = new Wallet({ | ||
| instanceOptions: getInstanceOptions(), | ||
| }); | ||
| wallets.push(wallet); | ||
|
|
||
| expect(wallet.getInstance('TransactionController')).toBeInstanceOf( | ||
| TransactionController, | ||
| ); | ||
| }); | ||
|
|
||
| it('forwards the provided state to the controller', () => { | ||
| const rootMessenger = getRootMessenger(); | ||
| const messenger = transactionController.getMessenger(rootMessenger); | ||
|
|
||
| const instance = transactionController.init({ | ||
| state: { | ||
| lastFetchedBlockNumbers: { '0x1': 123 }, | ||
| }, | ||
| messenger, | ||
| options: {}, | ||
| }); | ||
| controllers.push(instance); | ||
|
|
||
| expect(instance.state.lastFetchedBlockNumbers).toStrictEqual({ | ||
| '0x1': 123, | ||
| }); | ||
| }); | ||
| }); | ||
|
|
||
| function getRootMessenger(): RootMessenger<DefaultActions, DefaultEvents> { | ||
| const rootMessenger = new Messenger<'Root', DefaultActions, DefaultEvents>({ | ||
| namespace: 'Root', | ||
| }); | ||
|
|
||
| registerActionHandler( | ||
| rootMessenger, | ||
| 'NetworkController', | ||
| 'NetworkController:getNetworkClientRegistry', | ||
| jest.fn().mockReturnValue({}), | ||
| ); | ||
|
|
||
| return rootMessenger; | ||
| } | ||
|
|
||
| function getInstanceOptions(): WalletOptions['instanceOptions'] { | ||
| return { | ||
| connectivityController: { | ||
| connectivityAdapter: new AlwaysOnlineAdapter(), | ||
| }, | ||
| networkController: { | ||
| infuraProjectId: 'test-infura-project-id', | ||
| }, | ||
| storageService: { | ||
| storage: new InMemoryStorageAdapter(), | ||
| }, | ||
| remoteFeatureFlagController: REMOTE_FEATURE_FLAG_OPTIONS, | ||
| }; | ||
| } | ||
|
|
||
| function registerActionHandler( | ||
| parent: RootMessenger<DefaultActions, DefaultEvents>, | ||
| namespace: string, | ||
| actionType: string, | ||
| handler: ActionHandler, | ||
| ): void { | ||
| const messenger = new Messenger({ | ||
| namespace, | ||
| parent: parent as unknown as AnyMessenger, | ||
| }); | ||
|
|
||
| ( | ||
| messenger as unknown as { | ||
| registerActionHandler(type: string, handler: ActionHandler): void; | ||
| } | ||
| ).registerActionHandler(actionType, handler); | ||
| } |
57 changes: 57 additions & 0 deletions
57
...ages/wallet/src/initialization/instances/transaction-controller/transaction-controller.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,57 @@ | ||
| import { Messenger } from '@metamask/messenger'; | ||
| import type { TransactionControllerMessenger } from '@metamask/transaction-controller'; | ||
| import { TransactionController } from '@metamask/transaction-controller'; | ||
|
|
||
| import type { InitializationConfiguration } from '../../types'; | ||
|
|
||
| export type { TransactionControllerInstanceOptions } from './types'; | ||
|
|
||
| export const transactionController: InitializationConfiguration< | ||
| TransactionController, | ||
| TransactionControllerMessenger | ||
| > = { | ||
| name: 'TransactionController', | ||
| init: ({ state, messenger, options }) => { | ||
| const { disableSwaps = false, ...rest } = options; | ||
|
|
||
| return new TransactionController({ | ||
| ...rest, | ||
| disableSwaps, | ||
| messenger, | ||
| state, | ||
| }); | ||
| }, | ||
| getMessenger: (parent) => { | ||
| const messenger: TransactionControllerMessenger = new Messenger({ | ||
| namespace: 'TransactionController', | ||
| parent, | ||
| }); | ||
|
|
||
| parent.delegate({ | ||
| messenger, | ||
| actions: [ | ||
| 'AccountsController:getSelectedAccount', | ||
| 'AccountsController:getState', | ||
| 'ApprovalController:addRequest', | ||
| 'GasFeeController:fetchGasFeeEstimates', | ||
| 'KeyringController:getState', | ||
| 'KeyringController:signEip7702Authorization', | ||
| 'KeyringController:signTransaction', | ||
| 'NetworkController:findNetworkClientIdByChainId', | ||
| 'NetworkController:getEIP1559Compatibility', | ||
| 'NetworkController:getNetworkClientById', | ||
| 'NetworkController:getNetworkClientRegistry', | ||
| 'NetworkController:getState', | ||
| 'RemoteFeatureFlagController:getState', | ||
| ], | ||
| events: [ | ||
| 'AccountActivityService:transactionUpdated', | ||
| // TODO: Replace with `NetworkController:stateChanged` once TransactionController migrates. | ||
| // eslint-disable-next-line no-restricted-syntax | ||
| 'NetworkController:stateChange', | ||
| ], | ||
| }); | ||
|
|
||
| return messenger; | ||
| }, | ||
| }; |
6 changes: 6 additions & 0 deletions
6
packages/wallet/src/initialization/instances/transaction-controller/types.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,6 @@ | ||
| import type { TransactionControllerOptions } from '@metamask/transaction-controller'; | ||
|
|
||
| export type TransactionControllerInstanceOptions = Omit< | ||
| TransactionControllerOptions, | ||
| 'messenger' | 'state' | ||
| > & { disableSwaps?: boolean }; |
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
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.