Skip to content

Commit 313552f

Browse files
refactor(tron-wallet-snap): aggressive assets cleanup with Core AssetsProvider routing
- Slim SnapAssetsAdapter to snap-owned fetch/save/read only (fetchSnapOwnedAssetsForAccount) - Move handler logic (metadata, conversions, market data, historical prices) into AssetsService - Add syncSnapOwnedAssets for cron sync; AccountsService.synchronizeAssets delegates to it - Keep PR97 fungible read routing via AssetsProvider for getAccountAssetByID/ByIDs/ByScope - Remove public saveMany, getAll, fetchAssetsAndBalancesForAccount, hasChanged from AssetsService - Drop unused state from AssetsService constructor in context.ts - Port and adapt unit tests; update eslint suppressions and changelog Co-authored-by: Ulisses Ferreira <ulisses@hey.com>
1 parent a7a3674 commit 313552f

9 files changed

Lines changed: 1454 additions & 2832 deletions

File tree

eslint-suppressions.json

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -269,6 +269,16 @@
269269
"count": 2
270270
}
271271
},
272+
"packages/tron-wallet-snap/src/services/assets/AssetsRepository.ts": {
273+
"import-x/no-extraneous-dependencies": {
274+
"count": 1
275+
}
276+
},
277+
"packages/tron-wallet-snap/src/services/assets/AssetsService.ts": {
278+
"import-x/no-extraneous-dependencies": {
279+
"count": 2
280+
}
281+
},
272282
"packages/tron-wallet-snap/src/services/assets/AssetsService.test.ts": {
273283
"@typescript-eslint/explicit-function-return-type": {
274284
"count": 3

packages/tron-wallet-snap/CHANGELOG.md

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,14 +12,15 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
1212
- Wire Core messenger endowment and resolve assets migration stage from remote feature flags on each account asset read (no routing yet).
1313
- Route fungible asset reads through the shared `AssetsProvider` from `@metamask/snap-networks-utils` using account-scoped `AssetsController:getAccountAssetByID`, `AssetsController:getAccountAssetsByIDs`, and `AssetsController:getAccountAssetsByScope` actions based on migration stage (TRX, TRC10, TRC20). Protocol assets (energy, bandwidth, staking, lock/withdrawal, rewards) remain Snap-owned. Resolution order: remote feature flags → Off default.
1414

15-
### Removed
16-
17-
- Assets migration feature-flag routing. Fungible reads (`getAccountAssetByID`, `getAccountAssetsByIDs`, `getAccountAssetsByScope`) now always use Core `AssetsController` via `AssetsProvider`; snap-owned protocol assets remain on the Snap adapter. Removed `RemoteFeatureFlagController:getState` messenger endowment.
18-
1915
### Changed
2016

17+
- Move assets handler logic (metadata, conversions, market data, historical prices) into `AssetsService`; slim `SnapAssetsAdapter` to snap-owned fetch/save/read only. Cron asset sync uses `syncSnapOwnedAssets` for protocol assets.
2118
- Update `snap.manifest.json` bundle shasum ([#82](https://github.com/MetaMask/internal-snaps/pull/82))
2219

20+
### Removed
21+
22+
- Assets migration feature-flag routing. Fungible reads (`getAccountAssetByID`, `getAccountAssetsByIDs`, `getAccountAssetsByScope`) now always use Core `AssetsController` via `AssetsProvider`; snap-owned protocol assets remain on the Snap adapter. Removed `RemoteFeatureFlagController:getState` messenger endowment.
23+
2324
## [2.0.0]
2425

2526
### Changed

packages/tron-wallet-snap/snap.manifest.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
"url": "https://github.com/MetaMask/internal-snaps.git"
88
},
99
"source": {
10-
"shasum": "asiK6MnbNlIxxdMCvfOEHEp+28D1ZKGXL7Dg3t2V0Xs=",
10+
"shasum": "ac/9ewd8ZZXTLl5K5ZMg5PSsUeITzUwYtvfHPNjKQu4=",
1111
"location": {
1212
"npm": {
1313
"filePath": "dist/bundle.js",

packages/tron-wallet-snap/src/context.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,6 @@ const securityAlertsApiClient = new SecurityAlertsApiClient(
110110
// Business Services
111111
const assetsService = new AssetsService({
112112
logger,
113-
state,
114113
assetsRepository,
115114
trongridApiClient,
116115
tronHttpClient,

packages/tron-wallet-snap/src/services/accounts/AccountsService.test.ts

Lines changed: 18 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@ import {
2121

2222
import type { SnapClient } from '../../clients/snap/SnapClient';
2323
import { Network } from '../../constants';
24-
import type { NativeAsset } from '../../entities/assets';
2524
import type { TronKeyringAccount } from '../../entities/keyring-account';
2625
import type { ILogger } from '../../utils/logger';
2726
import { mockLogger } from '../../utils/mockLogger';
@@ -119,9 +118,7 @@ type WithAccountsServiceCallback = (payload: {
119118
>;
120119
mockConfigProvider: jest.Mocked<Pick<ConfigProvider, 'get'>>;
121120
mockLogger: ILogger;
122-
mockAssetsService: jest.Mocked<
123-
Pick<AssetsService, 'fetchAssetsAndBalancesForAccount' | 'saveMany'>
124-
>;
121+
mockAssetsService: jest.Mocked<Pick<AssetsService, 'syncSnapOwnedAssets'>>;
125122
mockSnapClient: jest.Mocked<
126123
Pick<SnapClient, 'getBip32Entropy' | 'listEntropySources'>
127124
>;
@@ -268,10 +265,9 @@ async function withAccountsService(
268265
};
269266

270267
const mockAssetsService: jest.Mocked<
271-
Pick<AssetsService, 'fetchAssetsAndBalancesForAccount' | 'saveMany'>
268+
Pick<AssetsService, 'syncSnapOwnedAssets'>
272269
> = {
273-
fetchAssetsAndBalancesForAccount: jest.fn().mockResolvedValue([]),
274-
saveMany: jest.fn().mockResolvedValue(undefined),
270+
syncSnapOwnedAssets: jest.fn().mockResolvedValue(undefined),
275271
};
276272

277273
const mockTransactionsService: jest.Mocked<
@@ -1194,7 +1190,7 @@ describe('AccountsService', () => {
11941190
});
11951191

11961192
describe('synchronizeAssets', () => {
1197-
it('calls fetch for each account and scope, then saveMany', async () => {
1193+
it('calls syncSnapOwnedAssets with accounts and active networks', async () => {
11981194
const account: TronKeyringAccount = {
11991195
id: 'sync-asset-id',
12001196
address: 'TSyncAsset12345678901234567',
@@ -1206,42 +1202,22 @@ describe('AccountsService', () => {
12061202
derivationPath: "m/44'/195'/0'/0/0",
12071203
index: 0,
12081204
};
1209-
const mockAssets: NativeAsset[] = [
1210-
{
1211-
assetType: `${Network.Mainnet}/slip44:195`,
1212-
keyringAccountId: 'sync-asset-id',
1213-
network: Network.Mainnet,
1214-
symbol: 'TRX',
1215-
decimals: 6,
1216-
rawAmount: '1000000',
1217-
uiAmount: '1',
1218-
iconUrl: '',
1219-
},
1220-
];
12211205

12221206
await withAccountsService(
12231207
async ({ accountsService, mockConfigProvider, mockAssetsService }) => {
12241208
mockConfigProvider.get.mockReturnValue({
12251209
...MOCK_CONFIG,
12261210
activeNetworks: [Network.Mainnet, Network.Shasta],
12271211
});
1228-
mockAssetsService.fetchAssetsAndBalancesForAccount.mockResolvedValue(
1229-
mockAssets,
1230-
);
12311212

12321213
await accountsService.synchronizeAssets([account]);
12331214

1234-
expect(
1235-
mockAssetsService.fetchAssetsAndBalancesForAccount,
1236-
).toHaveBeenCalledTimes(2);
1237-
expect(
1238-
mockAssetsService.fetchAssetsAndBalancesForAccount,
1239-
).toHaveBeenCalledWith(Network.Mainnet, account);
1240-
expect(
1241-
mockAssetsService.fetchAssetsAndBalancesForAccount,
1242-
).toHaveBeenCalledWith(Network.Shasta, account);
1243-
expect(mockAssetsService.saveMany).toHaveBeenCalledWith(
1244-
expect.arrayContaining(mockAssets),
1215+
expect(mockAssetsService.syncSnapOwnedAssets).toHaveBeenCalledTimes(
1216+
1,
1217+
);
1218+
expect(mockAssetsService.syncSnapOwnedAssets).toHaveBeenCalledWith(
1219+
[account],
1220+
[Network.Mainnet, Network.Shasta],
12451221
);
12461222
},
12471223
);
@@ -1266,10 +1242,10 @@ describe('AccountsService', () => {
12661242

12671243
await accountsService.synchronizeAssets([account]);
12681244

1269-
expect(
1270-
mockAssetsService.fetchAssetsAndBalancesForAccount,
1271-
).not.toHaveBeenCalled();
1272-
expect(mockAssetsService.saveMany).toHaveBeenCalledWith([]);
1245+
expect(mockAssetsService.syncSnapOwnedAssets).toHaveBeenCalledWith(
1246+
[account],
1247+
[],
1248+
);
12731249
},
12741250
);
12751251
});
@@ -1358,9 +1334,10 @@ describe('AccountsService', () => {
13581334

13591335
await accountsService.synchronize([account]);
13601336

1361-
expect(
1362-
mockAssetsService.fetchAssetsAndBalancesForAccount,
1363-
).toHaveBeenCalledWith(Network.Mainnet, account);
1337+
expect(mockAssetsService.syncSnapOwnedAssets).toHaveBeenCalledWith(
1338+
[account],
1339+
[Network.Mainnet],
1340+
);
13641341
expect(
13651342
mockTransactionsService.fetchNewTransactionsForAccount,
13661343
).toHaveBeenCalledWith(Network.Mainnet, account);

packages/tron-wallet-snap/src/services/accounts/AccountsService.ts

Lines changed: 1 addition & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -522,24 +522,7 @@ export class AccountsService {
522522
*/
523523
async synchronizeAssets(accounts: TronKeyringAccount[]): Promise<void> {
524524
const scopes = this.#configProvider.get().activeNetworks;
525-
const combinations = accounts.flatMap((account) =>
526-
scopes.map((scope) => ({ account, scope })),
527-
);
528-
529-
const assetResponses = await Promise.allSettled(
530-
combinations.map(async ({ account, scope }) => {
531-
return this.#assetsService.fetchAssetsAndBalancesForAccount(
532-
scope,
533-
account,
534-
);
535-
}),
536-
);
537-
538-
const assets = assetResponses.flatMap((response) =>
539-
response.status === 'fulfilled' ? response.value : [],
540-
);
541-
542-
await this.#assetsService.saveMany(assets);
525+
await this.#assetsService.syncSnapOwnedAssets(accounts, scopes);
543526
}
544527

545528
async synchronizeTransactions(accounts: TronKeyringAccount[]): Promise<void> {

0 commit comments

Comments
 (0)