Skip to content

Commit

Permalink
test: improve coverage
Browse files Browse the repository at this point in the history
  • Loading branch information
gantunesr committed Dec 18, 2024
1 parent 852b238 commit d71ad47
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 49 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,8 @@ import { HandlerType } from '@metamask/snaps-utils';
import type { Json, JsonRpcRequest } from '@metamask/utils';
import type { Draft } from 'immer';

import {
BalancesTracker,
BALANCE_UPDATE_INTERVALS,
NETWORK_ASSETS_MAP,
} from '.';
import { getScopeForAccount } from './utils';
import { BalancesTracker, NETWORK_ASSETS_MAP } from '.';
import { getScopeForAccount, getBlockTimeForAccount } from './utils';

const controllerName = 'MultichainBalancesController';

Expand Down Expand Up @@ -168,7 +164,7 @@ export class MultichainBalancesController extends BaseController<
// Register all non-EVM accounts into the tracker
for (const account of this.#listAccounts()) {
if (this.#isNonEvmAccount(account)) {
this.#tracker.track(account.id, this.#getBlockTimeFor(account));
this.#tracker.track(account.id, getBlockTimeForAccount(account.type));
}
}

Expand Down Expand Up @@ -197,20 +193,23 @@ export class MultichainBalancesController extends BaseController<
}

/**
* Gets the block time for a given account.
* Updates the balances of one account. This method doesn't return
* anything, but it updates the state of the controller.
*
* @param account - The account to get the block time for.
* @returns The block time for the account.
* @param accountId - The account ID.
*/
#getBlockTimeFor(account: InternalAccount): number {
if (account.type in BALANCE_UPDATE_INTERVALS) {
return BALANCE_UPDATE_INTERVALS[
account.type as keyof typeof BALANCE_UPDATE_INTERVALS
];
}
throw new Error(
`Unsupported account type for balance tracking: ${account.type}`,
);
async updateBalance(accountId: string): Promise<void> {
// NOTE: No need to track the account here, since we start tracking those when
// the "AccountsController:accountAdded" is fired.
await this.#tracker.updateBalance(accountId);
}

/**
* Updates the balances of all supported accounts. This method doesn't return
* anything, but it updates the state of the controller.
*/
async updateBalances(): Promise<void> {
await this.#tracker.updateBalances();
}

/**
Expand Down Expand Up @@ -242,17 +241,14 @@ export class MultichainBalancesController extends BaseController<
* @returns The non-EVM account.
*/
#getAccount(accountId: string): InternalAccount {
const account: InternalAccount | undefined =
this.#listMultichainAccounts().find(
(multichainAccount) => multichainAccount.id === accountId,
);
const account: InternalAccount | undefined = this.#listAccounts().find(
(multichainAccount) => multichainAccount.id === accountId,
);

if (!account) {
throw new Error(`Unknown account: ${accountId}`);
}
if (!this.#isNonEvmAccount(account)) {
throw new Error(`Account is not a non-EVM account: ${accountId}`);
}

return account;
}

Expand Down Expand Up @@ -282,26 +278,6 @@ export class MultichainBalancesController extends BaseController<
}
}

/**
* Updates the balances of one account. This method doesn't return
* anything, but it updates the state of the controller.
*
* @param accountId - The account ID.
*/
async updateBalance(accountId: string): Promise<void> {
// NOTE: No need to track the account here, since we start tracking those when
// the "AccountsController:accountAdded" is fired.
await this.#tracker.updateBalance(accountId);
}

/**
* Updates the balances of all supported accounts. This method doesn't return
* anything, but it updates the state of the controller.
*/
async updateBalances(): Promise<void> {
await this.#tracker.updateBalances();
}

/**
* Checks for non-EVM accounts.
*
Expand All @@ -327,7 +303,7 @@ export class MultichainBalancesController extends BaseController<
return;
}

this.#tracker.track(account.id, this.#getBlockTimeFor(account));
this.#tracker.track(account.id, getBlockTimeForAccount(account.type));
// NOTE: Unfortunately, we cannot update the balance right away here, because
// messenger's events are running synchronously and fetching the balance is
// asynchronous.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,12 @@ import { KeyringTypes } from '@metamask/keyring-controller';
import { validate, Network } from 'bitcoin-address-validation';
import { v4 as uuidv4 } from 'uuid';

import { MultichainNetworks } from './constants';
import { MultichainNetworks, BALANCE_UPDATE_INTERVALS } from '.';
import {
getScopeForBtcAddress,
getScopeForSolAddress,
getScopeForAccount,
getBlockTimeForAccount,
} from './utils';

const mockBtcAccount = {
Expand Down Expand Up @@ -169,3 +170,24 @@ describe('getScopeForAddress', () => {
);
});
});

describe('getBlockTimeForAccount', () => {
it('returns the block time for a supported Bitcoin account', () => {
const blockTime = getBlockTimeForAccount(BtcAccountType.P2wpkh);
expect(blockTime).toBe(BALANCE_UPDATE_INTERVALS[BtcAccountType.P2wpkh]);
});

it('returns the block time for a supported Solana account', () => {
const blockTime = getBlockTimeForAccount(SolAccountType.DataAccount);
expect(blockTime).toBe(
BALANCE_UPDATE_INTERVALS[SolAccountType.DataAccount],
);
});

it('throws an error for an unsupported account type', () => {
const unsupportedAccountType = 'unsupported-type';
expect(() => getBlockTimeForAccount(unsupportedAccountType)).toThrow(
`Unsupported account type for balance tracking: ${unsupportedAccountType}`,
);
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { BtcAccountType, SolAccountType } from '@metamask/keyring-api';
import type { InternalAccount } from '@metamask/keyring-internal-api';
import { validate, Network } from 'bitcoin-address-validation';

import { MultichainNetworks } from './constants';
import { MultichainNetworks, BALANCE_UPDATE_INTERVALS } from './constants';

/**
* Gets the scope for a specific and supported Bitcoin account.
Expand Down Expand Up @@ -58,3 +58,20 @@ export const getScopeForAccount = (account: InternalAccount): string => {
throw new Error(`Unsupported non-EVM account type: ${account.type}`);
}
};

/**
* Gets the block time for a given account.
*
* @param accountType - The account type to get the block time for.
* @returns The block time for the account.
*/
export const getBlockTimeForAccount = (accountType: string): number => {
if (accountType in BALANCE_UPDATE_INTERVALS) {
return BALANCE_UPDATE_INTERVALS[
accountType as keyof typeof BALANCE_UPDATE_INTERVALS
];
}
throw new Error(
`Unsupported account type for balance tracking: ${accountType}`,
);
};

0 comments on commit d71ad47

Please sign in to comment.