Skip to content
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

fix(multichain): use accounts{Added,Removed} to fetch/clear balances #25884

Merged
merged 19 commits into from
Jul 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
19 commits
Select commit Hold shift + click to select a range
262616b
fix(multichain): use accounts{Added,Removed} to fetch/clear balances
ccharly Jul 17, 2024
9e959df
feat(multichain): now tracks non-EVM balances to relax excessive fetches
ccharly Jul 17, 2024
17a2e96
test(multichain): fix + add more tests for BalancesController
ccharly Jul 17, 2024
00cc6be
test(multichain): add tests for BalancesTracker
ccharly Jul 17, 2024
d9c01bb
chore: lint
ccharly Jul 17, 2024
bd3dda9
refactor(multichain): use action rather than AccountsController.listM…
ccharly Jul 18, 2024
ad712af
refactor(multichain): add public BalancesController.updateBalance method
ccharly Jul 18, 2024
c8e21df
fix(multichain): only tracks non-EVM accounts when constructing Balan…
ccharly Jul 18, 2024
7008a68
test(multichain): fix metamask-controller.test.js with new BalancesCo…
ccharly Jul 18, 2024
06c946b
fix(multichain): do not subscribe to AccountsController:stateChange w…
ccharly Jul 18, 2024
502872e
feat(multichain): force fetch balance upon account creation (in Creat…
ccharly Jul 18, 2024
f973430
feat(multichain): update fetch info when fetching single account bala…
ccharly Jul 18, 2024
a3bd765
fix(multichain): fix BalancesController initial state
ccharly Jul 18, 2024
1e805b2
fix(multichain): fix btc balances tests
ccharly Jul 18, 2024
ecd1b6a
feat(multichain): add BALANCES_UPDATE_TIME (lower than average block …
ccharly Jul 19, 2024
175abe5
refactor(multichain): use Promise.allSettled when fecthing all balances
ccharly Jul 19, 2024
3dba8f7
refactor(multichain): use named function for BalancesTracker's callback
ccharly Jul 19, 2024
5be1e46
test(multichain): remove use of it.only
ccharly Jul 19, 2024
9637c8e
refactor(btc): prevent error from bubbling up when fetching BTC balan…
ccharly Jul 19, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
84 changes: 64 additions & 20 deletions app/scripts/lib/accounts/BalancesController.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import {
defaultState,
BalancesControllerMessenger,
} from './BalancesController';
import { Poller } from './Poller';
import { BalancesTracker } from './BalancesTracker';

const mockBtcAccount = createMockInternalAccount({
address: '',
Expand Down Expand Up @@ -54,8 +54,14 @@ const setupController = ({
const balancesControllerMessenger: BalancesControllerMessenger =
controllerMessenger.getRestricted({
name: 'BalancesController',
allowedActions: ['SnapController:handleRequest'],
allowedEvents: ['AccountsController:stateChange'],
allowedActions: [
'SnapController:handleRequest',
'AccountsController:listMultichainAccounts',
],
allowedEvents: [
'AccountsController:accountAdded',
'AccountsController:accountRemoved',
],
});

const mockSnapHandleRequest = jest.fn();
Expand All @@ -66,20 +72,22 @@ const setupController = ({
),
);

// TODO: remove when listMultichainAccounts action is available
const mockListMultichainAccounts = jest
.fn()
.mockReturnValue(mocks?.listMultichainAccounts ?? [mockBtcAccount]);
const mockListMultichainAccounts = jest.fn();
controllerMessenger.registerActionHandler(
'AccountsController:listMultichainAccounts',
mockListMultichainAccounts.mockReturnValue(
mocks?.listMultichainAccounts ?? [mockBtcAccount],
),
);

const controller = new BalancesController({
messenger: balancesControllerMessenger,
state,
// TODO: remove when listMultichainAccounts action is available
listMultichainAccounts: mockListMultichainAccounts,
});

return {
controller,
messenger: controllerMessenger,
mockSnapHandleRequest,
mockListMultichainAccounts,
};
Expand All @@ -91,19 +99,19 @@ describe('BalancesController', () => {
expect(controller.state).toEqual({ balances: {} });
});

it('starts polling when calling start', async () => {
const spyPoller = jest.spyOn(Poller.prototype, 'start');
it('starts tracking when calling start', async () => {
const spyTracker = jest.spyOn(BalancesTracker.prototype, 'start');
const { controller } = setupController();
await controller.start();
expect(spyPoller).toHaveBeenCalledTimes(1);
expect(spyTracker).toHaveBeenCalledTimes(1);
});

it('stops polling when calling stop', async () => {
const spyPoller = jest.spyOn(Poller.prototype, 'stop');
it('stops tracking when calling stop', async () => {
const spyTracker = jest.spyOn(BalancesTracker.prototype, 'stop');
const { controller } = setupController();
await controller.start();
await controller.stop();
expect(spyPoller).toHaveBeenCalledTimes(1);
expect(spyTracker).toHaveBeenCalledTimes(1);
});

it('update balances when calling updateBalances', async () => {
Expand All @@ -113,13 +121,49 @@ describe('BalancesController', () => {

expect(controller.state).toEqual({
balances: {
[mockBtcAccount.id]: {
'bip122:000000000933ea01ad0ee984209779ba/slip44:0': {
amount: '0.00000000',
unit: 'BTC',
},
[mockBtcAccount.id]: mockBalanceResult,
},
});
});

it('update balances when "AccountsController:accountAdded" is fired', async () => {
const { controller, messenger, mockListMultichainAccounts } =
setupController({
mocks: {
listMultichainAccounts: [],
},
});

controller.start();
mockListMultichainAccounts.mockReturnValue([mockBtcAccount]);
messenger.publish('AccountsController:accountAdded', mockBtcAccount);
await controller.updateBalances();

expect(controller.state).toEqual({
balances: {
[mockBtcAccount.id]: mockBalanceResult,
},
});
});

it('update balances when "AccountsController:accountRemoved" is fired', async () => {
const { controller, messenger, mockListMultichainAccounts } =
setupController();

controller.start();
await controller.updateBalances();
expect(controller.state).toEqual({
balances: {
[mockBtcAccount.id]: mockBalanceResult,
},
});

messenger.publish('AccountsController:accountRemoved', mockBtcAccount.id);
mockListMultichainAccounts.mockReturnValue([]);
await controller.updateBalances();

expect(controller.state).toEqual({
balances: {},
});
});
});
Loading