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

chore: get assets list from MultichainAssetsController state #5295

Merged
merged 27 commits into from
Feb 7, 2025
Merged
Show file tree
Hide file tree
Changes from 10 commits
Commits
Show all changes
27 commits
Select commit Hold shift + click to select a range
4059b86
chore: adds call for MultichainAssetsController
zone-live Feb 6, 2025
26bd5ff
chore: clean up unused var
zone-live Feb 6, 2025
c21e1bb
Merge branch 'main' into SOL-get-assets-list
zone-live Feb 6, 2025
e1fe97e
chore: ask for assets from assets controller
zone-live Feb 6, 2025
4cbb48f
chore: clean up
zone-live Feb 6, 2025
6fb6ef5
chore: lint
zone-live Feb 6, 2025
e2cb90d
chore: update test and lint
zone-live Feb 6, 2025
d8058eb
chore: adds subscribe
zone-live Feb 6, 2025
0c8bfa3
chore: lint fix
zone-live Feb 6, 2025
1adbaff
chore: lint fix
zone-live Feb 6, 2025
3f5b7fc
chore: rely in MultichainAssetsController for the accountAdded event
zone-live Feb 7, 2025
8902ea3
Merge branch 'main' into SOL-get-assets-list
zone-live Feb 7, 2025
9de472e
chore: loop update
zone-live Feb 7, 2025
df95d31
chore: clean up constant
zone-live Feb 7, 2025
d60779e
chore: clean up constant
zone-live Feb 7, 2025
494f4fe
chore: prettier
zone-live Feb 7, 2025
6a0d9f0
chore: removing more stuff
zone-live Feb 7, 2025
250fa0e
chore: removing more stuff
zone-live Feb 7, 2025
d392f5b
refactor: re-use state coming from the event
ccharly Feb 7, 2025
1d77b6d
chore: clean up
zone-live Feb 7, 2025
aefd186
chore: lint
zone-live Feb 7, 2025
71afc94
chore: lint
zone-live Feb 7, 2025
9c2ad60
test: use exports from the index
ccharly Feb 7, 2025
7fd63ff
test: fix coverage
ccharly Feb 7, 2025
76c4a94
refactor: use void operator to avoid await
ccharly Feb 7, 2025
e51f6d7
chore: comment about error handling
ccharly Feb 7, 2025
99994b2
test: fix default state test
ccharly Feb 7, 2025
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
Original file line number Diff line number Diff line change
Expand Up @@ -140,11 +140,13 @@ const setupController = ({
allowedActions: [
'SnapController:handleRequest',
'AccountsController:listMultichainAccounts',
'MultichainAssetsController:getState',
],
allowedEvents: [
'AccountsController:accountAdded',
'AccountsController:accountRemoved',
'AccountsController:accountBalancesUpdated',
'MultichainAssetsController:stateChange',
],
});

Expand All @@ -164,6 +166,16 @@ const setupController = ({
),
);

const mockGetAssetsState = jest.fn().mockReturnValue({
accountsAssets: {
[mockBtcAccount.id]: [mockBtcNativeAsset],
},
});
messenger.registerActionHandler(
'MultichainAssetsController:getState',
mockGetAssetsState,
);

const controller = new MultichainBalancesController({
messenger: multichainBalancesMessenger,
state,
Expand All @@ -174,6 +186,7 @@ const setupController = ({
messenger,
mockSnapHandleRequest,
mockListMultichainAccounts,
mockGetAssetsState,
};
};

Expand Down Expand Up @@ -392,4 +405,41 @@ describe('BalancesController', () => {
mockBalanceResult,
);
});

it('handles an account with no assets in MultichainAssetsController state', async () => {
const { controller, mockGetAssetsState } = setupController({
mocks: {
handleRequestReturnValue: {},
},
});

mockGetAssetsState.mockReturnValue({
accountsAssets: {},
});

await controller.updateBalance(mockBtcAccount.id);

expect(controller.state.balances[mockBtcAccount.id]).toStrictEqual({});
});

it('updates balances when receiving "MultichainAssetsController:stateChange" event', async () => {
const { controller, messenger } = setupController();

messenger.publish(
'MultichainAssetsController:stateChange',
{
assetsMetadata: {},
accountsAssets: {
[mockBtcAccount.id]: [mockBtcNativeAsset],
},
},
[],
);

await waitForAllPromises();

expect(controller.state.balances[mockBtcAccount.id]).toStrictEqual(
mockBalanceResult,
);
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,11 @@ import { HandlerType } from '@metamask/snaps-utils';
import type { Json, JsonRpcRequest } from '@metamask/utils';
import type { Draft } from 'immer';

import { NETWORK_ASSETS_MAP } from '.';
import { getScopeForAccount } from './utils';
import type {
MultichainAssetsControllerGetStateAction,
MultichainAssetsControllerState,
MultichainAssetsControllerStateChangeEvent,
} from '../MultichainAssetsController';

const controllerName = 'MultichainBalancesController';

Expand Down Expand Up @@ -90,15 +93,18 @@ export type MultichainBalancesControllerEvents =
*/
type AllowedActions =
| HandleSnapRequest
| AccountsControllerListMultichainAccountsAction;
| AccountsControllerListMultichainAccountsAction
| MultichainAssetsControllerGetStateAction;

/**
* Events that this controller is allowed to subscribe.
*/
type AllowedEvents =
| AccountsControllerAccountAddedEvent
| AccountsControllerAccountRemovedEvent
| AccountsControllerAccountBalancesUpdatesEvent;
| AccountsControllerAccountBalancesUpdatesEvent
| MultichainAssetsControllerStateChangeEvent;

/**
* Messenger type for the MultichainBalancesController.
*/
Expand Down Expand Up @@ -173,6 +179,21 @@ export class MultichainBalancesController extends BaseController<
(balanceUpdate: AccountBalancesUpdatedEventPayload) =>
zone-live marked this conversation as resolved.
Show resolved Hide resolved
this.#handleOnAccountBalancesUpdated(balanceUpdate),
);
this.messagingSystem.subscribe(
'MultichainAssetsController:stateChange',
async (assetsState: MultichainAssetsControllerState) => {
for (const accountId in assetsState.accountsAssets) {
zone-live marked this conversation as resolved.
Show resolved Hide resolved
if (
Object.prototype.hasOwnProperty.call(
assetsState.accountsAssets,
accountId,
)
) {
await this.updateBalance(accountId);
}
zone-live marked this conversation as resolved.
Show resolved Hide resolved
}
},
);
}

/**
Expand All @@ -186,8 +207,11 @@ export class MultichainBalancesController extends BaseController<
const account = this.#getAccount(accountId);

if (account.metadata.snap) {
const scope = getScopeForAccount(account);
const assetTypes = NETWORK_ASSETS_MAP[scope];
const assetsState = this.messagingSystem.call(
'MultichainAssetsController:getState',
);

const assetTypes = assetsState.accountsAssets[accountId] ?? [];

zone-live marked this conversation as resolved.
Show resolved Hide resolved
const accountBalance = await this.#getBalances(
account.id,
Expand Down
Loading