diff --git a/packages/snap/snap.manifest.json b/packages/snap/snap.manifest.json index 098d9f22..df819e7d 100644 --- a/packages/snap/snap.manifest.json +++ b/packages/snap/snap.manifest.json @@ -7,7 +7,7 @@ "url": "https://github.com/MetaMask/snap-tron-wallet.git" }, "source": { - "shasum": "tSxhnKoJe+iTI1HJ13ENq81vPK99Q12rj7owcUCpqxQ=", + "shasum": "bTXn+pVsNZSewtiTkc7urDjf8QG92UbjkqeKn3Wz4HI=", "location": { "npm": { "filePath": "dist/bundle.js", diff --git a/packages/snap/src/services/accounts/AccountsService.ts b/packages/snap/src/services/accounts/AccountsService.ts index d5d82acf..e9c0bca0 100644 --- a/packages/snap/src/services/accounts/AccountsService.ts +++ b/packages/snap/src/services/accounts/AccountsService.ts @@ -35,6 +35,8 @@ import { createPrefixedLogger, type ILogger } from '../../utils/logger'; import { DerivationPathStruct } from '../../validation/structs'; import type { AssetsService } from '../assets/AssetsService'; import type { ConfigProvider } from '../config'; +import { MigrationStage } from '../migration/stage'; +import type { TronAssetsControllerAdapter } from '../migration/TronAssetsControllerAdapter'; import type { TransactionsService } from '../transactions/TransactionsService'; /** @@ -110,6 +112,8 @@ export class AccountsService { readonly #snapClient: SnapClient; + readonly #tronAssetsControllerAdapter: TronAssetsControllerAdapter; + constructor({ accountsRepository, configProvider, @@ -117,6 +121,7 @@ export class AccountsService { assetsService, snapClient, transactionsService, + tronAssetsControllerAdapter, }: { accountsRepository: AccountsRepository; configProvider: ConfigProvider; @@ -124,6 +129,7 @@ export class AccountsService { assetsService: AssetsService; snapClient: SnapClient; transactionsService: TransactionsService; + tronAssetsControllerAdapter: TronAssetsControllerAdapter; }) { this.#logger = createPrefixedLogger(logger, '[🔑 AccountsService]'); this.#configProvider = configProvider; @@ -131,6 +137,7 @@ export class AccountsService { this.#assetsService = assetsService; this.#transactionsService = transactionsService; this.#snapClient = snapClient; + this.#tronAssetsControllerAdapter = tronAssetsControllerAdapter; } /** @@ -509,11 +516,51 @@ export class AccountsService { }), ); - const assets = assetResponses.flatMap((response) => - response.status === 'fulfilled' ? response.value : [], + const synchronizedAssets = await Promise.all( + assetResponses.map(async (response, index) => { + if (response.status !== 'fulfilled') { + return { assets: [], shouldPersist: false, combination: undefined }; + } + + const combination = combinations[index]; + + if (!combination) { + return { assets: [], shouldPersist: false, combination: undefined }; + } + + const { scope } = combination; + const stage = + await this.#tronAssetsControllerAdapter.getMigrationStage(scope); + + if (stage >= MigrationStage.ReadAssetsControllerOnly) { + this.#logger.info( + 'Asset tracking and persistence disabled for controller-only stage', + { scope, stage }, + ); + return { + assets: response.value, + shouldPersist: false, + combination: { ...combination, stage }, + }; + } + + return { + assets: response.value, + shouldPersist: true, + combination: { ...combination, stage }, + }; + }), + ); + const assets = synchronizedAssets.flatMap( + ({ assets: responseAssets }) => responseAssets, + ); + const hasPersistableScope = synchronizedAssets.some( + ({ shouldPersist }) => shouldPersist, ); - await this.#assetsService.saveMany(assets); + if (hasPersistableScope || combinations.length === 0) { + await this.#assetsService.saveMany(assets); + } } async synchronizeTransactions(accounts: TronKeyringAccount[]): Promise {