|
| 1 | +import type { InternalAccount } from '@metamask/keyring-internal-api'; |
| 2 | + |
1 | 3 | import { PERPS_CONSTANTS } from '../constants/perpsConfig'; |
| 4 | +import type { PerpsControllerMessenger } from '../PerpsController'; |
2 | 5 | import type { AccountState } from '../types'; |
3 | 6 |
|
4 | 7 | import { |
5 | 8 | addSpotBalanceToAccountState, |
6 | 9 | aggregateAccountStates, |
7 | 10 | calculateWeightedReturnOnEquity, |
| 11 | + getSelectedEvmAccountFromMessenger, |
8 | 12 | getSpotBalance, |
9 | 13 | } from './accountUtils'; |
10 | 14 |
|
| 15 | +const SELECTED_ADDRESS = '0x1111111111111111111111111111111111111111'; |
| 16 | +const GROUP_ADDRESS = '0x2222222222222222222222222222222222222222'; |
| 17 | +const NON_EVM_ADDRESS = 'bc1qselectedaccount'; |
| 18 | + |
| 19 | +function buildAccount( |
| 20 | + address: string, |
| 21 | + id: string, |
| 22 | + type: InternalAccount['type'] = 'eip155:eoa', |
| 23 | +): InternalAccount { |
| 24 | + return { |
| 25 | + address, |
| 26 | + id, |
| 27 | + type, |
| 28 | + options: {}, |
| 29 | + methods: [], |
| 30 | + metadata: { |
| 31 | + name: id, |
| 32 | + importTime: Date.now(), |
| 33 | + keyring: { |
| 34 | + type: 'HD Key Tree', |
| 35 | + }, |
| 36 | + }, |
| 37 | + scopes: ['eip155:0'], |
| 38 | + } as InternalAccount; |
| 39 | +} |
| 40 | + |
| 41 | +function buildMessenger( |
| 42 | + call: (actionType: string) => InternalAccount | InternalAccount[], |
| 43 | +): Pick<PerpsControllerMessenger, 'call'> { |
| 44 | + return { call } as unknown as Pick<PerpsControllerMessenger, 'call'>; |
| 45 | +} |
| 46 | + |
| 47 | +describe('getSelectedEvmAccountFromMessenger', () => { |
| 48 | + it('prefers the selected account over the first evm account in the selected group', () => { |
| 49 | + const selectedAccount = buildAccount(SELECTED_ADDRESS, 'selected'); |
| 50 | + const groupedAccount = buildAccount(GROUP_ADDRESS, 'grouped'); |
| 51 | + const messenger = buildMessenger((actionType: string) => { |
| 52 | + switch (actionType) { |
| 53 | + case 'AccountsController:getSelectedAccount': |
| 54 | + return selectedAccount; |
| 55 | + case 'AccountTreeController:getAccountsFromSelectedAccountGroup': |
| 56 | + return [groupedAccount]; |
| 57 | + default: |
| 58 | + throw new Error(`Unexpected action: ${actionType}`); |
| 59 | + } |
| 60 | + }); |
| 61 | + |
| 62 | + expect(getSelectedEvmAccountFromMessenger(messenger)).toStrictEqual({ |
| 63 | + address: SELECTED_ADDRESS, |
| 64 | + }); |
| 65 | + }); |
| 66 | + |
| 67 | + it('falls back to the selected account group when selected account lookup is unavailable', () => { |
| 68 | + const groupedAccount = buildAccount(GROUP_ADDRESS, 'grouped'); |
| 69 | + const messenger = buildMessenger((actionType: string) => { |
| 70 | + switch (actionType) { |
| 71 | + case 'AccountsController:getSelectedAccount': |
| 72 | + throw new Error('Selected account unavailable'); |
| 73 | + case 'AccountTreeController:getAccountsFromSelectedAccountGroup': |
| 74 | + return [groupedAccount]; |
| 75 | + default: |
| 76 | + throw new Error(`Unexpected action: ${actionType}`); |
| 77 | + } |
| 78 | + }); |
| 79 | + |
| 80 | + expect(getSelectedEvmAccountFromMessenger(messenger)).toStrictEqual({ |
| 81 | + address: GROUP_ADDRESS, |
| 82 | + }); |
| 83 | + }); |
| 84 | + |
| 85 | + it('falls back to the selected account group when the selected account is not evm', () => { |
| 86 | + const selectedAccount = buildAccount( |
| 87 | + NON_EVM_ADDRESS, |
| 88 | + 'selected', |
| 89 | + 'bip122:p2wpkh', |
| 90 | + ); |
| 91 | + const groupedAccount = buildAccount(GROUP_ADDRESS, 'grouped'); |
| 92 | + const messenger = buildMessenger((actionType: string) => { |
| 93 | + switch (actionType) { |
| 94 | + case 'AccountsController:getSelectedAccount': |
| 95 | + return selectedAccount; |
| 96 | + case 'AccountTreeController:getAccountsFromSelectedAccountGroup': |
| 97 | + return [groupedAccount]; |
| 98 | + default: |
| 99 | + throw new Error(`Unexpected action: ${actionType}`); |
| 100 | + } |
| 101 | + }); |
| 102 | + |
| 103 | + expect(getSelectedEvmAccountFromMessenger(messenger)).toStrictEqual({ |
| 104 | + address: GROUP_ADDRESS, |
| 105 | + }); |
| 106 | + }); |
| 107 | +}); |
| 108 | + |
11 | 109 | describe('aggregateAccountStates', () => { |
12 | 110 | const fallback: AccountState = { |
13 | 111 | spendableBalance: PERPS_CONSTANTS.FallbackDataDisplay, |
|
0 commit comments