Skip to content

Commit

Permalink
feat: add getAccount action to AccountsController (#1892)
Browse files Browse the repository at this point in the history
## Explanation

This PR adds getAccount action to the AccountsController.

## References

## Changelog

### `@metamask/package-a`

- **ADDED**: adds getAccount action to the AccountsController
## Checklist

- [x] I've updated the test suite for new or updated code as appropriate
- [x] I've updated documentation (JSDoc, Markdown, etc.) for new or
updated code as appropriate
- [x] I've highlighted breaking changes using the "BREAKING" category
above as appropriate
  • Loading branch information
montelaidev authored Jan 22, 2024
1 parent d5c5163 commit 20011c4
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 0 deletions.
26 changes: 26 additions & 0 deletions packages/accounts-controller/src/AccountsController.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1841,6 +1841,7 @@ describe('AccountsController', () => {
jest.spyOn(AccountsController.prototype, 'updateAccounts');
jest.spyOn(AccountsController.prototype, 'getAccountByAddress');
jest.spyOn(AccountsController.prototype, 'getSelectedAccount');
jest.spyOn(AccountsController.prototype, 'getAccount');
});

describe('setSelectedAccount', () => {
Expand Down Expand Up @@ -1981,5 +1982,30 @@ describe('AccountsController', () => {
expect(account).toStrictEqual(mockAccount);
});
});

describe('getAccount', () => {
it('should get account by id', async () => {
const messenger = buildMessenger();

const accountsController = setupAccountsController({
initialState: {
internalAccounts: {
accounts: { [mockAccount.id]: mockAccount },
selectedAccount: mockAccount.id,
},
},
messenger,
});

const account = messenger.call(
'AccountsController:getAccount',
mockAccount.id,
);
expect(accountsController.getAccount).toHaveBeenCalledWith(
mockAccount.id,
);
expect(account).toStrictEqual(mockAccount);
});
});
});
});
12 changes: 12 additions & 0 deletions packages/accounts-controller/src/AccountsController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,12 @@ export type AccountsControllerGetAccountByAddressAction = {
type: `${typeof controllerName}:getAccountByAddress`;
handler: AccountsController['getAccountByAddress'];
};

export type AccountsControllerGetAccountAction = {
type: `${typeof controllerName}:getAccount`;
handler: AccountsController['getAccount'];
};

export type AccountsControllerActions =
| AccountsControllerGetStateAction
| AccountsControllerSetSelectedAccountAction
Expand All @@ -79,6 +85,7 @@ export type AccountsControllerActions =
| AccountsControllerUpdateAccountsAction
| AccountsControllerGetAccountByAddressAction
| AccountsControllerGetSelectedAccountAction
| AccountsControllerGetAccountAction
| KeyringControllerGetKeyringForAccountAction
| KeyringControllerGetKeyringsByTypeAction
| KeyringControllerGetAccountsAction;
Expand Down Expand Up @@ -784,5 +791,10 @@ export class AccountsController extends BaseController<
`${controllerName}:getAccountByAddress`,
this.getAccountByAddress.bind(this),
);

this.messagingSystem.registerActionHandler(
`AccountsController:getAccount`,
this.getAccount.bind(this),
);
}
}

0 comments on commit 20011c4

Please sign in to comment.