Skip to content

Commit dd11108

Browse files
authored
Merge branch 'main' into dm-add-prs-to-queue
2 parents 8ab5081 + d664234 commit dd11108

20 files changed

+810
-242
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@metamask/core-monorepo",
3-
"version": "238.0.0",
3+
"version": "239.0.0",
44
"private": true,
55
"description": "Monorepo for packages shared between MetaMask clients",
66
"repository": {

packages/assets-controllers/src/AccountTrackerController.test.ts

Lines changed: 324 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -248,6 +248,123 @@ describe('AccountTrackerController', () => {
248248
},
249249
);
250250
});
251+
252+
it('should update staked balance when includeStakedAssets is enabled', async () => {
253+
mockedQuery
254+
.mockReturnValueOnce(Promise.resolve('0x10'))
255+
.mockReturnValueOnce(Promise.resolve('0x11'));
256+
257+
await withController(
258+
{
259+
options: {
260+
includeStakedAssets: true,
261+
getStakedBalanceForChain: jest.fn().mockResolvedValue('0x1'),
262+
},
263+
isMultiAccountBalancesEnabled: false,
264+
selectedAccount: ACCOUNT_1,
265+
listAccounts: [ACCOUNT_1, ACCOUNT_2],
266+
},
267+
async ({ controller }) => {
268+
await controller.refresh();
269+
270+
expect(controller.state).toStrictEqual({
271+
accounts: {
272+
[CHECKSUM_ADDRESS_1]: { balance: '0x10', stakedBalance: '0x1' },
273+
[CHECKSUM_ADDRESS_2]: { balance: '0x0' },
274+
},
275+
accountsByChainId: {
276+
'0x1': {
277+
[CHECKSUM_ADDRESS_1]: {
278+
balance: '0x10',
279+
stakedBalance: '0x1',
280+
},
281+
[CHECKSUM_ADDRESS_2]: {
282+
balance: '0x0',
283+
},
284+
},
285+
},
286+
});
287+
},
288+
);
289+
});
290+
291+
it('should not update staked balance when includeStakedAssets is disabled', async () => {
292+
mockedQuery
293+
.mockReturnValueOnce(Promise.resolve('0x13'))
294+
.mockReturnValueOnce(Promise.resolve('0x14'));
295+
296+
await withController(
297+
{
298+
options: {
299+
includeStakedAssets: false,
300+
getStakedBalanceForChain: jest.fn().mockResolvedValue('0x1'),
301+
},
302+
isMultiAccountBalancesEnabled: false,
303+
selectedAccount: ACCOUNT_1,
304+
listAccounts: [ACCOUNT_1, ACCOUNT_2],
305+
},
306+
async ({ controller }) => {
307+
await controller.refresh();
308+
309+
expect(controller.state).toStrictEqual({
310+
accounts: {
311+
[CHECKSUM_ADDRESS_1]: { balance: '0x13' },
312+
[CHECKSUM_ADDRESS_2]: { balance: '0x0' },
313+
},
314+
accountsByChainId: {
315+
'0x1': {
316+
[CHECKSUM_ADDRESS_1]: {
317+
balance: '0x13',
318+
},
319+
[CHECKSUM_ADDRESS_2]: {
320+
balance: '0x0',
321+
},
322+
},
323+
},
324+
});
325+
},
326+
);
327+
});
328+
329+
it('should update staked balance when includeStakedAssets and multi-account is enabled', async () => {
330+
mockedQuery
331+
.mockReturnValueOnce(Promise.resolve('0x11'))
332+
.mockReturnValueOnce(Promise.resolve('0x12'));
333+
334+
await withController(
335+
{
336+
options: {
337+
includeStakedAssets: true,
338+
getStakedBalanceForChain: jest.fn().mockResolvedValue('0x1'),
339+
},
340+
isMultiAccountBalancesEnabled: true,
341+
selectedAccount: ACCOUNT_1,
342+
listAccounts: [ACCOUNT_1, ACCOUNT_2],
343+
},
344+
async ({ controller }) => {
345+
await controller.refresh();
346+
347+
expect(controller.state).toStrictEqual({
348+
accounts: {
349+
[CHECKSUM_ADDRESS_1]: { balance: '0x11', stakedBalance: '0x1' },
350+
[CHECKSUM_ADDRESS_2]: { balance: '0x12', stakedBalance: '0x1' },
351+
},
352+
accountsByChainId: {
353+
'0x1': {
354+
[CHECKSUM_ADDRESS_1]: {
355+
balance: '0x11',
356+
stakedBalance: '0x1',
357+
},
358+
[CHECKSUM_ADDRESS_2]: {
359+
balance: '0x12',
360+
stakedBalance: '0x1',
361+
},
362+
},
363+
},
364+
});
365+
},
366+
);
367+
});
251368
});
252369

253370
describe('with networkClientId', () => {
@@ -438,6 +555,185 @@ describe('AccountTrackerController', () => {
438555
},
439556
);
440557
});
558+
559+
it('should update staked balance when includeStakedAssets is enabled', async () => {
560+
const networkClientId = 'holesky';
561+
mockedQuery
562+
.mockReturnValueOnce(Promise.resolve('0x10'))
563+
.mockReturnValueOnce(Promise.resolve('0x11'));
564+
565+
await withController(
566+
{
567+
options: {
568+
includeStakedAssets: true,
569+
getStakedBalanceForChain: jest.fn().mockResolvedValue('0x1'),
570+
},
571+
isMultiAccountBalancesEnabled: false,
572+
selectedAccount: ACCOUNT_1,
573+
listAccounts: [ACCOUNT_1, ACCOUNT_2],
574+
networkClientById: {
575+
[networkClientId]: buildCustomNetworkClientConfiguration({
576+
chainId: '0x4268',
577+
}),
578+
},
579+
},
580+
async ({ controller }) => {
581+
await controller.refresh();
582+
583+
expect(controller.state).toStrictEqual({
584+
accounts: {
585+
[CHECKSUM_ADDRESS_1]: { balance: '0x10', stakedBalance: '0x1' },
586+
[CHECKSUM_ADDRESS_2]: { balance: '0x0' },
587+
},
588+
accountsByChainId: {
589+
'0x1': {
590+
[CHECKSUM_ADDRESS_1]: {
591+
balance: '0x10',
592+
stakedBalance: '0x1',
593+
},
594+
[CHECKSUM_ADDRESS_2]: {
595+
balance: '0x0',
596+
},
597+
},
598+
},
599+
});
600+
},
601+
);
602+
});
603+
604+
it('should not update staked balance when includeStakedAssets is disabled', async () => {
605+
const networkClientId = 'holesky';
606+
mockedQuery
607+
.mockReturnValueOnce(Promise.resolve('0x13'))
608+
.mockReturnValueOnce(Promise.resolve('0x14'));
609+
610+
await withController(
611+
{
612+
options: {
613+
includeStakedAssets: false,
614+
getStakedBalanceForChain: jest.fn().mockResolvedValue('0x1'),
615+
},
616+
isMultiAccountBalancesEnabled: false,
617+
selectedAccount: ACCOUNT_1,
618+
listAccounts: [ACCOUNT_1, ACCOUNT_2],
619+
networkClientById: {
620+
[networkClientId]: buildCustomNetworkClientConfiguration({
621+
chainId: '0x4268',
622+
}),
623+
},
624+
},
625+
async ({ controller }) => {
626+
await controller.refresh();
627+
628+
expect(controller.state).toStrictEqual({
629+
accounts: {
630+
[CHECKSUM_ADDRESS_1]: { balance: '0x13' },
631+
[CHECKSUM_ADDRESS_2]: { balance: '0x0' },
632+
},
633+
accountsByChainId: {
634+
'0x1': {
635+
[CHECKSUM_ADDRESS_1]: {
636+
balance: '0x13',
637+
},
638+
[CHECKSUM_ADDRESS_2]: {
639+
balance: '0x0',
640+
},
641+
},
642+
},
643+
});
644+
},
645+
);
646+
});
647+
648+
it('should update staked balance when includeStakedAssets and multi-account is enabled', async () => {
649+
const networkClientId = 'holesky';
650+
mockedQuery
651+
.mockReturnValueOnce(Promise.resolve('0x11'))
652+
.mockReturnValueOnce(Promise.resolve('0x12'));
653+
654+
await withController(
655+
{
656+
options: {
657+
includeStakedAssets: true,
658+
getStakedBalanceForChain: jest.fn().mockResolvedValue('0x1'),
659+
},
660+
isMultiAccountBalancesEnabled: true,
661+
selectedAccount: ACCOUNT_1,
662+
listAccounts: [ACCOUNT_1, ACCOUNT_2],
663+
networkClientById: {
664+
[networkClientId]: buildCustomNetworkClientConfiguration({
665+
chainId: '0x4268',
666+
}),
667+
},
668+
},
669+
async ({ controller }) => {
670+
await controller.refresh();
671+
672+
expect(controller.state).toStrictEqual({
673+
accounts: {
674+
[CHECKSUM_ADDRESS_1]: { balance: '0x11', stakedBalance: '0x1' },
675+
[CHECKSUM_ADDRESS_2]: { balance: '0x12', stakedBalance: '0x1' },
676+
},
677+
accountsByChainId: {
678+
'0x1': {
679+
[CHECKSUM_ADDRESS_1]: {
680+
balance: '0x11',
681+
stakedBalance: '0x1',
682+
},
683+
[CHECKSUM_ADDRESS_2]: {
684+
balance: '0x12',
685+
stakedBalance: '0x1',
686+
},
687+
},
688+
},
689+
});
690+
},
691+
);
692+
});
693+
694+
it('should not update staked balance when includeStakedAssets and multi-account is enabled if network unsupported', async () => {
695+
const networkClientId = 'polygon';
696+
mockedQuery
697+
.mockReturnValueOnce(Promise.resolve('0x11'))
698+
.mockReturnValueOnce(Promise.resolve('0x12'));
699+
700+
await withController(
701+
{
702+
options: {
703+
includeStakedAssets: true,
704+
getStakedBalanceForChain: jest.fn().mockResolvedValue(undefined),
705+
},
706+
isMultiAccountBalancesEnabled: true,
707+
selectedAccount: ACCOUNT_1,
708+
listAccounts: [ACCOUNT_1, ACCOUNT_2],
709+
networkClientById: {
710+
[networkClientId]: buildCustomNetworkClientConfiguration({
711+
chainId: '0x89',
712+
}),
713+
},
714+
},
715+
async ({ controller }) => {
716+
await controller.refresh();
717+
718+
expect(controller.state).toStrictEqual({
719+
accounts: {
720+
[CHECKSUM_ADDRESS_1]: { balance: '0x11' },
721+
[CHECKSUM_ADDRESS_2]: { balance: '0x12' },
722+
},
723+
accountsByChainId: {
724+
'0x1': {
725+
[CHECKSUM_ADDRESS_1]: {
726+
balance: '0x11',
727+
},
728+
[CHECKSUM_ADDRESS_2]: {
729+
balance: '0x12',
730+
},
731+
},
732+
},
733+
});
734+
},
735+
);
736+
});
441737
});
442738
});
443739

@@ -462,6 +758,33 @@ describe('AccountTrackerController', () => {
462758
},
463759
);
464760
});
761+
762+
it('should sync staked balance with addresses', async () => {
763+
await withController(
764+
{
765+
options: {
766+
includeStakedAssets: true,
767+
getStakedBalanceForChain: jest.fn().mockResolvedValue('0x1'),
768+
},
769+
isMultiAccountBalancesEnabled: true,
770+
selectedAccount: ACCOUNT_1,
771+
listAccounts: [],
772+
},
773+
async ({ controller }) => {
774+
mockedQuery
775+
.mockReturnValueOnce(Promise.resolve('0x10'))
776+
.mockReturnValueOnce(Promise.resolve('0x20'));
777+
const result = await controller.syncBalanceWithAddresses([
778+
ADDRESS_1,
779+
ADDRESS_2,
780+
]);
781+
expect(result[ADDRESS_1].balance).toBe('0x10');
782+
expect(result[ADDRESS_2].balance).toBe('0x20');
783+
expect(result[ADDRESS_1].stakedBalance).toBe('0x1');
784+
expect(result[ADDRESS_2].stakedBalance).toBe('0x1');
785+
},
786+
);
787+
});
465788
});
466789

467790
it('should call refresh every interval on legacy polling', async () => {
@@ -647,6 +970,7 @@ async function withController<ReturnValue>(
647970

648971
const controller = new AccountTrackerController({
649972
messenger: accountTrackerMessenger,
973+
getStakedBalanceForChain: jest.fn(),
650974
...options,
651975
});
652976

0 commit comments

Comments
 (0)