Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
5 changes: 5 additions & 0 deletions packages/tron-wallet-snap/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,11 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

## [Unreleased]

### Changed

- Extract shared asset util functions and inject `SnapAssetsAdapter` from `context` into `AssetsService` ([#143](https://github.com/MetaMask/internal-snaps/pull/143))
- Rename `getByKeyringAccountId` to `getAccountAssets` (with essential-asset synthesis) and update keyring callers ([#143](https://github.com/MetaMask/internal-snaps/pull/143))

## [3.1.0]

### Added
Expand Down
2 changes: 1 addition & 1 deletion packages/tron-wallet-snap/snap.manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
"url": "https://github.com/MetaMask/internal-snaps.git"
},
"source": {
"shasum": "e7sZ7XY8fvO4GLcznlb5N/Fh95+ANPKS/GzYoHHz2pc=",
"shasum": "NpxOo6DkB0sBh8xpisBu3o+7G6MJriDNsADcHVb/Qp8=",
"location": {
"npm": {
"filePath": "dist/bundle.js",
Expand Down
12 changes: 8 additions & 4 deletions packages/tron-wallet-snap/src/context.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import { RpcHandler } from './handlers/rpc/rpc';
import { UserInputHandler } from './handlers/user-input/userInput';
import { AccountsRepository } from './services/accounts/AccountsRepository';
import { AccountsService } from './services/accounts/AccountsService';
import { SnapAssetsAdapter } from './services/assets/adapters/SnapAssetsAdapter';
import { AssetsRepository } from './services/assets/AssetsRepository';
import { AssetsService } from './services/assets/AssetsService';
import { ConfigProvider } from './services/config';
Expand Down Expand Up @@ -113,8 +114,7 @@ const securityAlertsApiClient = new SecurityAlertsApiClient(
logger,
);

// Business Services
const assetsService = new AssetsService({
const snapAssetsAdapter = new SnapAssetsAdapter({
logger,
state,
assetsRepository,
Expand All @@ -123,8 +123,12 @@ const assetsService = new AssetsService({
priceApiClient,
tokenApiClient,
snapClient,
remoteFeatureFlagsProvider,
assetsProvider,
configProvider,
});

// Business Services
const assetsService = new AssetsService({
snapAdapter: snapAssetsAdapter,
});

const transactionsService = new TransactionsService({
Expand Down

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Simple rename, will come in handy

Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ describe('KeyringHandler', () => {
}),
} as unknown as jest.Mocked<AccountsService>;
mockAssetsService = {
getByKeyringAccountId: jest.fn().mockResolvedValue([]),
getAccountAssets: jest.fn().mockResolvedValue([]),
} as unknown as jest.Mocked<AssetsService>;
mockTransactionsService = {
checkAddressActivity: jest.fn(),
Expand Down Expand Up @@ -649,7 +649,7 @@ describe('KeyringHandler', () => {
const result = await keyringHandler.getAccountAssets(mockAccount.id);

expect(result).toStrictEqual([]);
expect(mockAssetsService.getByKeyringAccountId).toHaveBeenCalledWith(
expect(mockAssetsService.getAccountAssets).toHaveBeenCalledWith(
mockAccount.id,
);
});
Expand Down
5 changes: 2 additions & 3 deletions packages/tron-wallet-snap/src/handlers/keyring/keyring.ts

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Simple rename, will come in handy

Original file line number Diff line number Diff line change
Expand Up @@ -191,7 +191,7 @@ export class KeyringHandler implements KeyringSnapRpc {
this.#logger.info('Listing account assets', { accountId });

const assetEntities =
await this.#assetsService.getByKeyringAccountId(accountId);
await this.#assetsService.getAccountAssets(accountId);
const result = assetEntities
.filter(
(asset) =>
Expand Down Expand Up @@ -280,8 +280,7 @@ export class KeyringHandler implements KeyringSnapRpc {

await this.#getAccountOrThrow(accountId);

const assetsList =
await this.#assetsService.getByKeyringAccountId(accountId);
const assetsList = await this.#assetsService.getAccountAssets(accountId);

const assetsToUse = assetsList
.filter((asset) => assets.includes(asset.assetType))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,12 @@ jest.mock('@metamask/keyring-snap-sdk', () => ({

(global as any).snap = {};

// eslint-disable-next-line @typescript-eslint/no-require-imports
const { configProvider } = require('../../context');
// eslint-disable-next-line @typescript-eslint/no-require-imports
const { AssetsService } = require('./AssetsService');
// eslint-disable-next-line @typescript-eslint/no-require-imports
const { SnapAssetsAdapter } = require('./adapters/SnapAssetsAdapter');

const mockAccount: KeyringAccount = {
id: 'test-account-id',
Expand Down Expand Up @@ -260,7 +264,7 @@ async function withAssetsService<ReturnValue>(
trackError: jest.fn().mockResolvedValue(undefined),
};

const assetsService = new AssetsService({
const snapAdapter = new SnapAssetsAdapter({
logger: mockLogger,
assetsRepository: mockAssetsRepository,
state: mockState,
Expand All @@ -269,7 +273,9 @@ async function withAssetsService<ReturnValue>(
priceApiClient: mockPriceApiClient,
tokenApiClient: mockTokenApiClient,
snapClient: mockSnapClient,
configProvider,
});
const assetsService = new AssetsService({ snapAdapter });

return await testFunction({
assetsService,
Expand Down Expand Up @@ -2871,9 +2877,15 @@ describe('AssetsService', () => {
expect(AssetsService.hasChanged(asset, [])).toBe(true);
expect(AssetsService.hasChanged(asset, [asset])).toBe(false);

const accountAssets = await assetsService.getAccountAssets(
mockAccount.id,
);
expect(
await assetsService.getAccountAssets(mockAccount.id),
).toStrictEqual([asset]);
accountAssets.some(
(savedAsset: AssetEntity) =>
savedAsset.assetType === KnownCaip19Id.TrxMainnet,
),
).toBe(true);
expect(
await assetsService.getAccountAssetsByIDs(mockAccount.id, [
KnownCaip19Id.TrxMainnet,
Expand All @@ -2885,15 +2897,6 @@ describe('AssetsService', () => {
KnownCaip19Id.TrxMainnet,
),
).toStrictEqual(asset);
const byKeyringAccountId = await assetsService.getByKeyringAccountId(
mockAccount.id,
);
expect(
byKeyringAccountId.some(
(savedAsset: AssetEntity) =>
savedAsset.assetType === KnownCaip19Id.TrxMainnet,
),
).toBe(true);
const marketData = await assetsService.getMultipleTokensMarketData([
{
asset: KnownCaip19Id.TrxMainnet,
Expand Down
49 changes: 2 additions & 47 deletions packages/tron-wallet-snap/src/services/assets/AssetsService.ts

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Adapters should not be initialized in the service but rather passed in

Original file line number Diff line number Diff line change
@@ -1,8 +1,4 @@
import type { KeyringAccount } from '@metamask/keyring-api';
import {
AssetsProvider,
RemoteFeatureFlagsProvider,
} from '@metamask/snap-networks-utils';
import type {
AssetConversion,
AssetMetadata,
Expand All @@ -11,17 +7,9 @@ import type {
} from '@metamask/snaps-sdk';
import type { CaipAssetType } from '@metamask/utils';

import type { PriceApiClient } from '../../clients/price-api/PriceApiClient';
import type { SnapClient } from '../../clients/snap/SnapClient';
import type { TokenApiClient } from '../../clients/token-api/TokenApiClient';
import type { TronHttpClient } from '../../clients/tron-http/TronHttpClient';
import type { TrongridApiClient } from '../../clients/trongrid/TrongridApiClient';
import type { Network } from '../../constants';
import type { AssetEntity } from '../../entities/assets';
import type { ILogger } from '../../utils/logger';
import type { State, UnencryptedStateValue } from '../state/State';
import { SnapAssetsAdapter } from './adapters/SnapAssetsAdapter';
import type { AssetsRepository } from './AssetsRepository';

/**
* Assets domain facade. Currently delegates all behavior to SnapAssetsAdapter
Expand All @@ -33,37 +21,8 @@ export class AssetsService {

readonly cacheTtlsMilliseconds: SnapAssetsAdapter['cacheTtlsMilliseconds'];

constructor({
logger,
assetsRepository,
state,
trongridApiClient,
tronHttpClient,
priceApiClient,
tokenApiClient,
snapClient,
}: {
logger: ILogger;
assetsRepository: AssetsRepository;
state: State<UnencryptedStateValue>;
trongridApiClient: TrongridApiClient;
tronHttpClient: TronHttpClient;
priceApiClient: PriceApiClient;
tokenApiClient: TokenApiClient;
snapClient: SnapClient;
remoteFeatureFlagsProvider: RemoteFeatureFlagsProvider;
assetsProvider: AssetsProvider;
}) {
this.#snapAdapter = new SnapAssetsAdapter({
logger,
assetsRepository,
state,
trongridApiClient,
tronHttpClient,
priceApiClient,
tokenApiClient,
snapClient,
});
constructor({ snapAdapter }: { snapAdapter: SnapAssetsAdapter }) {
this.#snapAdapter = snapAdapter;
this.cacheTtlsMilliseconds = this.#snapAdapter.cacheTtlsMilliseconds;
}

Expand Down Expand Up @@ -114,10 +73,6 @@ export class AssetsService {
return this.#snapAdapter.getAll();
}

async getByKeyringAccountId(accountId: string): Promise<AssetEntity[]> {
return this.#snapAdapter.getByKeyringAccountId(accountId);
}

async getMultipleTokenConversions(
conversions: { from: CaipAssetType; to: CaipAssetType }[],
): Promise<
Expand Down
Loading