@metamask/assets-controllers 21.0.0 → 23.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (36) hide show
  1. package/CHANGELOG.md +37 -1
  2. package/dist/NftController.d.ts +74 -1
  3. package/dist/NftController.d.ts.map +1 -1
  4. package/dist/NftController.js +20 -7
  5. package/dist/NftController.js.map +1 -1
  6. package/dist/NftDetectionController.d.ts +5 -2
  7. package/dist/NftDetectionController.d.ts.map +1 -1
  8. package/dist/NftDetectionController.js +25 -13
  9. package/dist/NftDetectionController.js.map +1 -1
  10. package/dist/TokenDetectionController.d.ts +43 -56
  11. package/dist/TokenDetectionController.d.ts.map +1 -1
  12. package/dist/TokenDetectionController.js +132 -96
  13. package/dist/TokenDetectionController.js.map +1 -1
  14. package/dist/TokenListController.d.ts +4 -3
  15. package/dist/TokenListController.d.ts.map +1 -1
  16. package/dist/TokenListController.js.map +1 -1
  17. package/dist/TokenRatesController.d.ts +1 -1
  18. package/dist/TokenRatesController.d.ts.map +1 -1
  19. package/dist/TokenRatesController.js +2 -7
  20. package/dist/TokenRatesController.js.map +1 -1
  21. package/dist/assetsUtil.d.ts +44 -1
  22. package/dist/assetsUtil.d.ts.map +1 -1
  23. package/dist/assetsUtil.js +120 -1
  24. package/dist/assetsUtil.js.map +1 -1
  25. package/dist/index.d.ts +3 -2
  26. package/dist/index.d.ts.map +1 -1
  27. package/dist/index.js +4 -2
  28. package/dist/index.js.map +1 -1
  29. package/dist/token-prices-service/abstract-token-prices-service.d.ts +1 -1
  30. package/dist/token-prices-service/abstract-token-prices-service.d.ts.map +1 -1
  31. package/dist/token-prices-service/abstract-token-prices-service.js.map +1 -1
  32. package/dist/token-prices-service/codefi-v2.d.ts +6 -3
  33. package/dist/token-prices-service/codefi-v2.d.ts.map +1 -1
  34. package/dist/token-prices-service/codefi-v2.js +12 -5
  35. package/dist/token-prices-service/codefi-v2.js.map +1 -1
  36. package/package.json +13 -12
@@ -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AE,EACtB,WAAW,IAAI,EAAE,WAAW,EAAE,EAC9B,SAAS,IAAI,EAAE,KAAK,EAAE,SAAS,EAAE,EACjC,SAAS,IAAI,EAAE,aAAa,EAAE,SAAS,EAAE,EACzC,gBAAgB,IAAI,EAAE,eAAe,EAAE,gBAAgB,EAAE,EACzD,iBAAiB,IAAI,EAAE,YAAY,EAAE,iBAAiB,EAAE,EACxD,mBAAmB,IAAI,EAAE,cAAc,EAAE,mBAAmB,EAAE,EAC9D,kBAAkB,IAAI,EAAE,aAAa,EAAE,kBAAkB,EAAE,EAC3D,aAAa,IAAI,EAAE,SAAS,EAAE,aAAa,EAAE,EAC7C,sBAAsB,IAAI;wBACxB,iBAAiB,EAAE,sBAAsB;qBAC1C,EACD,WAAW,IAAI,EAAE,QAAQ,EAAE,WAAW,EAAE,EACxC,aAAa,IAAI,EAAE,YAAY,EAAE,aAAa,EAAE,EAChD,SAAS,IAAI,EAAE,QAAQ,EAAE,SAAS,EAAE,CACrC,CAAC;oBAEF,MAAM,IAAI,CAAC,MAAM,CAAC,OAAO,EAAE,QAAQ,EAAE;wBACnC,WAAW;wBACX,WAAW;wBACX,MAAM,EAAE,kBAAM,CAAC,QAAQ;wBACvB,eAAe;qBAChB,CAAC,CAAC;iBACJ;YACH,CAAC,CAAA,CAAC,CAAC;YACH,MAAM,OAAO,CAAC,GAAG,CAAC,cAAc,CAAC,CAAC;QACpC,CAAC;KAAA;CACF;AA3RD,wDA2RC;AAED,kBAAe,sBAAsB,CAAC","sourcesContent":["import type { BaseConfig, BaseState } from '@metamask/base-controller';\nimport {\n OPENSEA_PROXY_URL,\n fetchWithErrorHandling,\n toChecksumHexAddress,\n ChainId,\n} from '@metamask/controller-utils';\nimport type {\n NetworkClientId,\n NetworkController,\n NetworkState,\n NetworkClient,\n} from '@metamask/network-controller';\nimport { StaticIntervalPollingControllerV1 } from '@metamask/polling-controller';\nimport type { PreferencesState } from '@metamask/preferences-controller';\nimport type { Hex } from '@metamask/utils';\n\nimport { Source } from './constants';\nimport type { NftController, NftState, NftMetadata } from './NftController';\n\nconst DEFAULT_INTERVAL = 180000;\n\n/**\n * @type ApiNft\n *\n * NFT object coming from OpenSea api\n * @property token_id - The NFT identifier\n * @property num_sales - Number of sales\n * @property background_color - The background color to be displayed with the item\n * @property image_url - URI of an image associated with this NFT\n * @property image_preview_url - URI of a smaller image associated with this NFT\n * @property image_thumbnail_url - URI of a thumbnail image associated with this NFT\n * @property image_original_url - URI of the original image associated with this NFT\n * @property animation_url - URI of a animation associated with this NFT\n * @property animation_original_url - URI of the original animation associated with this NFT\n * @property name - The NFT name\n * @property description - The NFT description\n * @property external_link - External link containing additional information\n * @property assetContract - The NFT contract information object\n * @property creator - The NFT owner information object\n * @property lastSale - When this item was last sold\n */\n// This interface was created before this ESLint rule was added.\n// Convert to a `type` in a future major version.\n// eslint-disable-next-line @typescript-eslint/consistent-type-definitions\nexport interface ApiNft {\n token_id: string;\n num_sales: number | null;\n background_color: string | null;\n image_url: string | null;\n image_preview_url: string | null;\n image_thumbnail_url: string | null;\n image_original_url: string | null;\n animation_url: string | null;\n animation_original_url: string | null;\n name: string | null;\n description: string | null;\n external_link: string | null;\n asset_contract: ApiNftContract;\n creator: ApiNftCreator;\n last_sale: ApiNftLastSale | null;\n}\n\n/**\n * @type ApiNftContract\n *\n * NFT contract object coming from OpenSea api\n * @property address - Address of the NFT contract\n * @property asset_contract_type - The NFT type, it could be `semi-fungible` or `non-fungible`\n * @property created_date - Creation date\n * @property collection - Object containing the contract name and URI of an image associated\n * @property schema_name - The schema followed by the contract, it could be `ERC721` or `ERC1155`\n * @property symbol - The NFT contract symbol\n * @property total_supply - Total supply of NFTs\n * @property description - The NFT contract description\n * @property external_link - External link containing additional information\n */\n// This interface was created before this ESLint rule was added.\n// Convert to a `type` in a future major version.\n// eslint-disable-next-line @typescript-eslint/consistent-type-definitions\nexport interface ApiNftContract {\n address: string;\n asset_contract_type: string | null;\n created_date: string | null;\n schema_name: string | null;\n symbol: string | null;\n total_supply: string | null;\n description: string | null;\n external_link: string | null;\n collection: {\n name: string | null;\n image_url?: string | null;\n };\n}\n\n/**\n * @type ApiNftLastSale\n *\n * NFT sale object coming from OpenSea api\n * @property event_timestamp - Object containing a `username`\n * @property total_price - URI of NFT image associated with this owner\n * @property transaction - Object containing transaction_hash and block_hash\n */\n// This interface was created before this ESLint rule was added.\n// Convert to a `type` in a future major version.\n// eslint-disable-next-line @typescript-eslint/consistent-type-definitions\nexport interface ApiNftLastSale {\n event_timestamp: string;\n total_price: string;\n transaction: { transaction_hash: string; block_hash: string };\n}\n\n/**\n * @type ApiNftCreator\n *\n * NFT creator object coming from OpenSea api\n * @property user - Object containing a `username`\n * @property profile_img_url - URI of NFT image associated with this owner\n * @property address - The owner address\n */\n// This interface was created before this ESLint rule was added.\n// Convert to a `type` in a future major version.\n// eslint-disable-next-line @typescript-eslint/consistent-type-definitions\nexport interface ApiNftCreator {\n user: { username: string };\n profile_img_url: string;\n address: string;\n}\n\n/**\n * @type NftDetectionConfig\n *\n * NftDetection configuration\n * @property interval - Polling interval used to fetch new token rates\n * @property chainId - Current chain ID\n * @property selectedAddress - Vault selected address\n */\n// This interface was created before this ESLint rule was added.\n// Convert to a `type` in a future major version.\n// eslint-disable-next-line @typescript-eslint/consistent-type-definitions\nexport interface NftDetectionConfig extends BaseConfig {\n interval: number;\n chainId: Hex;\n selectedAddress: string;\n}\n\n/**\n * Controller that passively polls on a set interval for NFT auto detection\n */\nexport class NftDetectionController extends StaticIntervalPollingControllerV1<\n NftDetectionConfig,\n BaseState\n> {\n private intervalId?: ReturnType<typeof setTimeout>;\n\n private getOwnerNftApi({\n address,\n offset,\n }: {\n address: string;\n offset: number;\n }) {\n return `${OPENSEA_PROXY_URL}/assets?owner=${address}&offset=${offset}&limit=50`;\n }\n\n private async getOwnerNfts(address: string) {\n let nftApiResponse: { assets: ApiNft[] };\n let nfts: ApiNft[] = [];\n let offset = 0;\n let pagingFinish = false;\n /* istanbul ignore if */\n do {\n nftApiResponse = await fetchWithErrorHandling({\n url: this.getOwnerNftApi({ address, offset }),\n timeout: 15000,\n });\n\n if (!nftApiResponse) {\n return nfts;\n }\n\n nftApiResponse?.assets?.length !== 0\n ? (nfts = [...nfts, ...nftApiResponse.assets])\n : (pagingFinish = true);\n offset += 50;\n } while (!pagingFinish);\n\n return nfts;\n }\n\n /**\n * Name of this controller used during composition\n */\n override name = 'NftDetectionController';\n\n private readonly getOpenSeaApiKey: () => string | undefined;\n\n private readonly addNft: NftController['addNft'];\n\n private readonly getNftState: () => NftState;\n\n private readonly getNetworkClientById: NetworkController['getNetworkClientById'];\n\n /**\n * Creates an NftDetectionController instance.\n *\n * @param options - The controller options.\n * @param options.chainId - The chain ID of the current network.\n * @param options.onNftsStateChange - Allows subscribing to assets controller state changes.\n * @param options.onPreferencesStateChange - Allows subscribing to preferences controller state changes.\n * @param options.onNetworkStateChange - Allows subscribing to network controller state changes.\n * @param options.getOpenSeaApiKey - Gets the OpenSea API key, if one is set.\n * @param options.addNft - Add an NFT.\n * @param options.getNftState - Gets the current state of the Assets controller.\n * @param options.getNetworkClientById - Gets the network client by ID, from the NetworkController.\n * @param config - Initial options used to configure this controller.\n * @param state - Initial state to set on this controller.\n */\n constructor(\n {\n chainId: initialChainId,\n getNetworkClientById,\n onPreferencesStateChange,\n onNetworkStateChange,\n getOpenSeaApiKey,\n addNft,\n getNftState,\n }: {\n chainId: Hex;\n getNetworkClientById: NetworkController['getNetworkClientById'];\n onNftsStateChange: (listener: (nftsState: NftState) => void) => void;\n onPreferencesStateChange: (\n listener: (preferencesState: PreferencesState) => void,\n ) => void;\n onNetworkStateChange: (\n listener: (networkState: NetworkState) => void,\n ) => void;\n getOpenSeaApiKey: () => string | undefined;\n addNft: NftController['addNft'];\n getNftState: () => NftState;\n },\n config?: Partial<NftDetectionConfig>,\n state?: Partial<BaseState>,\n ) {\n super(config, state);\n this.defaultConfig = {\n interval: DEFAULT_INTERVAL,\n chainId: initialChainId,\n selectedAddress: '',\n disabled: true,\n };\n this.initialize();\n this.getNftState = getNftState;\n this.getNetworkClientById = getNetworkClientById;\n onPreferencesStateChange(({ selectedAddress, useNftDetection }) => {\n const { selectedAddress: previouslySelectedAddress, disabled } =\n this.config;\n\n if (\n selectedAddress !== previouslySelectedAddress ||\n !useNftDetection !== disabled\n ) {\n this.configure({ selectedAddress, disabled: !useNftDetection });\n }\n\n if (useNftDetection !== undefined) {\n if (useNftDetection) {\n this.start();\n } else {\n this.stop();\n }\n }\n });\n\n onNetworkStateChange(({ providerConfig }) => {\n this.configure({\n chainId: providerConfig.chainId,\n });\n });\n this.getOpenSeaApiKey = getOpenSeaApiKey;\n this.addNft = addNft;\n this.setIntervalLength(this.config.interval);\n }\n\n async _executePoll(\n networkClientId: string,\n options: { address: string },\n ): Promise<void> {\n await this.detectNfts({ networkClientId, userAddress: options.address });\n }\n\n /**\n * Start polling for the currency rate.\n */\n async start() {\n if (!this.isMainnet() || this.disabled) {\n return;\n }\n\n await this.startPolling();\n }\n\n /**\n * Stop polling for the currency rate.\n */\n stop() {\n this.stopPolling();\n }\n\n private stopPolling() {\n if (this.intervalId) {\n clearInterval(this.intervalId);\n }\n }\n\n /**\n * Starts a new polling interval.\n *\n * @param interval - An interval on which to poll.\n */\n private async startPolling(interval?: number): Promise<void> {\n interval && this.configure({ interval }, false, false);\n this.stopPolling();\n await this.detectNfts();\n this.intervalId = setInterval(async () => {\n await this.detectNfts();\n }, this.config.interval);\n }\n\n /**\n * Checks whether network is mainnet or not.\n *\n * @returns Whether current network is mainnet.\n */\n isMainnet = (): boolean => this.config.chainId === ChainId.mainnet;\n\n isMainnetByNetworkClientId = (networkClient: NetworkClient): boolean => {\n return networkClient.configuration.chainId === ChainId.mainnet;\n };\n\n /**\n * Triggers asset ERC721 token auto detection on mainnet. Any newly detected NFTs are\n * added.\n *\n * @param options - Options bag.\n * @param options.networkClientId - The network client ID to detect NFTs on.\n * @param options.userAddress - The address to detect NFTs for.\n */\n async detectNfts(\n {\n networkClientId,\n userAddress,\n }: {\n networkClientId?: NetworkClientId;\n userAddress: string;\n } = { userAddress: this.config.selectedAddress },\n ) {\n /* istanbul ignore if */\n if (!this.isMainnet() || this.disabled) {\n return;\n }\n /* istanbul ignore else */\n if (!userAddress) {\n return;\n }\n\n const apiNfts = await this.getOwnerNfts(userAddress);\n const addNftPromises = apiNfts.map(async (nft: ApiNft) => {\n const {\n token_id,\n num_sales,\n background_color,\n image_url,\n image_preview_url,\n image_thumbnail_url,\n image_original_url,\n animation_url,\n animation_original_url,\n name,\n description,\n external_link,\n creator,\n asset_contract: { address, schema_name },\n last_sale,\n } = nft;\n\n let ignored;\n /* istanbul ignore else */\n const { ignoredNfts } = this.getNftState();\n if (ignoredNfts.length) {\n ignored = ignoredNfts.find((c) => {\n /* istanbul ignore next */\n return (\n c.address === toChecksumHexAddress(address) &&\n c.tokenId === token_id\n );\n });\n }\n\n /* istanbul ignore else */\n if (!ignored) {\n /* istanbul ignore next */\n const nftMetadata: NftMetadata = Object.assign(\n {},\n { name },\n creator && { creator },\n description && { description },\n image_url && { image: image_url },\n num_sales && { numberOfSales: num_sales },\n background_color && { backgroundColor: background_color },\n image_preview_url && { imagePreview: image_preview_url },\n image_thumbnail_url && { imageThumbnail: image_thumbnail_url },\n image_original_url && { imageOriginal: image_original_url },\n animation_url && { animation: animation_url },\n animation_original_url && {\n animationOriginal: animation_original_url,\n },\n schema_name && { standard: schema_name },\n external_link && { externalLink: external_link },\n last_sale && { lastSale: last_sale },\n );\n\n await this.addNft(address, token_id, {\n nftMetadata,\n userAddress,\n source: Source.Detected,\n networkClientId,\n });\n }\n });\n await Promise.all(addNftPromises);\n }\n}\n\nexport default NftDetectionController;\n"]}
1
+ {"version":3,"file":"NftDetectionController.js","sourceRoot":"","sources":["../src/NftDetectionController.ts"],"names":[],"mappings":";;;;;;;;;;;;AACA,iEAOoC;AAOpC,qEAAiF;AAIjF,6CAAmD;AACnD,2CAAqC;AAErC,mDAMyB;AAEzB,MAAM,gBAAgB,GA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AAM,EAAE,kBAAM,CAAC,QAAQ;wBACvB,eAAe;qBAChB,CAAC,CAAC;iBACJ;YACH,CAAC,CAAA,CAAC,CAAC;YACH,MAAM,OAAO,CAAC,GAAG,CAAC,cAAc,CAAC,CAAC;QACpC,CAAC;KAAA;CACF;AAxTD,wDAwTC;AAED,kBAAe,sBAAsB,CAAC","sourcesContent":["import type { BaseConfig, BaseState } from '@metamask/base-controller';\nimport {\n OPENSEA_PROXY_URL,\n fetchWithErrorHandling,\n toChecksumHexAddress,\n ChainId,\n timeoutFetch,\n safelyExecute,\n} from '@metamask/controller-utils';\nimport type {\n NetworkClientId,\n NetworkController,\n NetworkState,\n NetworkClient,\n} from '@metamask/network-controller';\nimport { StaticIntervalPollingControllerV1 } from '@metamask/polling-controller';\nimport type { PreferencesState } from '@metamask/preferences-controller';\nimport type { Hex } from '@metamask/utils';\n\nimport { mapOpenSeaNftV2ToV1 } from './assetsUtil';\nimport { Source } from './constants';\nimport type { OpenSeaV2GetNftResponse } from './NftController';\nimport {\n type NftController,\n type NftState,\n type NftMetadata,\n type OpenSeaV2ListNftsResponse,\n OpenSeaV2ChainIds,\n} from './NftController';\n\nconst DEFAULT_INTERVAL = 180000;\n\n/**\n * @type ApiNft\n *\n * NFT object coming from OpenSea api\n * @property token_id - The NFT identifier\n * @property num_sales - Number of sales\n * @property background_color - The background color to be displayed with the item\n * @property image_url - URI of an image associated with this NFT\n * @property image_preview_url - URI of a smaller image associated with this NFT\n * @property image_thumbnail_url - URI of a thumbnail image associated with this NFT\n * @property image_original_url - URI of the original image associated with this NFT\n * @property animation_url - URI of a animation associated with this NFT\n * @property animation_original_url - URI of the original animation associated with this NFT\n * @property name - The NFT name\n * @property description - The NFT description\n * @property external_link - External link containing additional information\n * @property assetContract - The NFT contract information object\n * @property creator - The NFT owner information object\n * @property lastSale - When this item was last sold\n */\n// This interface was created before this ESLint rule was added.\n// Convert to a `type` in a future major version.\n// eslint-disable-next-line @typescript-eslint/consistent-type-definitions\nexport interface ApiNft {\n token_id: string;\n num_sales: number | null;\n background_color: string | null;\n image_url: string | null;\n image_preview_url: string | null;\n image_thumbnail_url: string | null;\n image_original_url: string | null;\n animation_url: string | null;\n animation_original_url: string | null;\n name: string | null;\n description: string | null;\n external_link: string | null;\n asset_contract: ApiNftContract;\n creator: ApiNftCreator;\n last_sale: ApiNftLastSale | null;\n}\n\n/**\n * @type ApiNftContract\n *\n * NFT contract object coming from OpenSea api\n * @property address - Address of the NFT contract\n * @property asset_contract_type - The NFT type, it could be `semi-fungible` or `non-fungible`\n * @property created_date - Creation date\n * @property collection - Object containing the contract name and URI of an image associated\n * @property schema_name - The schema followed by the contract, it could be `ERC721` or `ERC1155`\n * @property symbol - The NFT contract symbol\n * @property total_supply - Total supply of NFTs\n * @property description - The NFT contract description\n * @property external_link - External link containing additional information\n */\n// This interface was created before this ESLint rule was added.\n// Convert to a `type` in a future major version.\n// eslint-disable-next-line @typescript-eslint/consistent-type-definitions\nexport interface ApiNftContract {\n address: string;\n asset_contract_type: string | null;\n created_date: string | null;\n schema_name: string | null;\n symbol: string | null;\n total_supply: string | null;\n description: string | null;\n external_link: string | null;\n collection: {\n name: string | null;\n image_url?: string | null;\n };\n}\n\n/**\n * @type ApiNftLastSale\n *\n * NFT sale object coming from OpenSea api\n * @property event_timestamp - Object containing a `username`\n * @property total_price - URI of NFT image associated with this owner\n * @property transaction - Object containing transaction_hash and block_hash\n */\n// This interface was created before this ESLint rule was added.\n// Convert to a `type` in a future major version.\n// eslint-disable-next-line @typescript-eslint/consistent-type-definitions\nexport interface ApiNftLastSale {\n event_timestamp: string;\n total_price: string;\n transaction: { transaction_hash: string; block_hash: string };\n}\n\n/**\n * @type ApiNftCreator\n *\n * NFT creator object coming from OpenSea api\n * @property user - Object containing a `username`\n * @property profile_img_url - URI of NFT image associated with this owner\n * @property address - The owner address\n */\n// This interface was created before this ESLint rule was added.\n// Convert to a `type` in a future major version.\n// eslint-disable-next-line @typescript-eslint/consistent-type-definitions\nexport interface ApiNftCreator {\n user: { username: string };\n profile_img_url: string;\n address: string;\n}\n\n/**\n * @type NftDetectionConfig\n *\n * NftDetection configuration\n * @property interval - Polling interval used to fetch new token rates\n * @property chainId - Current chain ID\n * @property selectedAddress - Vault selected address\n */\n// This interface was created before this ESLint rule was added.\n// Convert to a `type` in a future major version.\n// eslint-disable-next-line @typescript-eslint/consistent-type-definitions\nexport interface NftDetectionConfig extends BaseConfig {\n interval: number;\n chainId: Hex;\n selectedAddress: string;\n}\n\n/**\n * Controller that passively polls on a set interval for NFT auto detection\n */\nexport class NftDetectionController extends StaticIntervalPollingControllerV1<\n NftDetectionConfig,\n BaseState\n> {\n private intervalId?: ReturnType<typeof setTimeout>;\n\n private getOwnerNftApi({\n address,\n next,\n }: {\n address: string;\n next?: string;\n }) {\n return `${OPENSEA_PROXY_URL}/chain/${\n OpenSeaV2ChainIds.ethereum\n }/account/${address}/nfts?limit=200&next=${next ?? ''}`;\n }\n\n private async getOwnerNfts(address: string) {\n let nftApiResponse: OpenSeaV2ListNftsResponse;\n let nfts: ApiNft[] = [];\n let next;\n\n do {\n nftApiResponse = await fetchWithErrorHandling({\n url: this.getOwnerNftApi({ address, next }),\n timeout: 15000,\n });\n\n if (!nftApiResponse) {\n return nfts;\n }\n\n const newNfts = await Promise.all(\n nftApiResponse.nfts.map(async (nftV2) => {\n const nftV1 = mapOpenSeaNftV2ToV1(nftV2);\n\n // If the image hasn't been processed into OpenSea's CDN, the image_url will be null.\n // Try fetching the NFT individually, which returns the original image url from metadata if available.\n if (!nftV1.image_url && nftV2.metadata_url) {\n const nftDetails: OpenSeaV2GetNftResponse | undefined =\n await safelyExecute(() =>\n timeoutFetch(\n this.getNftApi({\n contractAddress: nftV2.contract,\n tokenId: nftV2.identifier,\n }),\n undefined,\n 1000,\n ).then((r) => r.json()),\n );\n\n nftV1.image_original_url = nftDetails?.nft?.image_url ?? null;\n }\n return nftV1;\n }),\n );\n\n nfts = [...nfts, ...newNfts];\n } while ((next = nftApiResponse.next));\n\n return nfts;\n }\n\n /**\n * Name of this controller used during composition\n */\n override name = 'NftDetectionController';\n\n private readonly getOpenSeaApiKey: () => string | undefined;\n\n private readonly addNft: NftController['addNft'];\n\n private readonly getNftApi: NftController['getNftApi'];\n\n private readonly getNftState: () => NftState;\n\n private readonly getNetworkClientById: NetworkController['getNetworkClientById'];\n\n /**\n * Creates an NftDetectionController instance.\n *\n * @param options - The controller options.\n * @param options.chainId - The chain ID of the current network.\n * @param options.onNftsStateChange - Allows subscribing to assets controller state changes.\n * @param options.onPreferencesStateChange - Allows subscribing to preferences controller state changes.\n * @param options.onNetworkStateChange - Allows subscribing to network controller state changes.\n * @param options.getOpenSeaApiKey - Gets the OpenSea API key, if one is set.\n * @param options.addNft - Add an NFT.\n * @param options.getNftApi - Gets the URL to fetch an NFT from OpenSea.\n * @param options.getNftState - Gets the current state of the Assets controller.\n * @param options.getNetworkClientById - Gets the network client by ID, from the NetworkController.\n * @param config - Initial options used to configure this controller.\n * @param state - Initial state to set on this controller.\n */\n constructor(\n {\n chainId: initialChainId,\n getNetworkClientById,\n onPreferencesStateChange,\n onNetworkStateChange,\n getOpenSeaApiKey,\n addNft,\n getNftApi,\n getNftState,\n }: {\n chainId: Hex;\n getNetworkClientById: NetworkController['getNetworkClientById'];\n onNftsStateChange: (listener: (nftsState: NftState) => void) => void;\n onPreferencesStateChange: (\n listener: (preferencesState: PreferencesState) => void,\n ) => void;\n onNetworkStateChange: (\n listener: (networkState: NetworkState) => void,\n ) => void;\n getOpenSeaApiKey: () => string | undefined;\n addNft: NftController['addNft'];\n getNftApi: NftController['getNftApi'];\n getNftState: () => NftState;\n },\n config?: Partial<NftDetectionConfig>,\n state?: Partial<BaseState>,\n ) {\n super(config, state);\n this.defaultConfig = {\n interval: DEFAULT_INTERVAL,\n chainId: initialChainId,\n selectedAddress: '',\n disabled: true,\n };\n this.initialize();\n this.getNftState = getNftState;\n this.getNetworkClientById = getNetworkClientById;\n onPreferencesStateChange(({ selectedAddress, useNftDetection }) => {\n const { selectedAddress: previouslySelectedAddress, disabled } =\n this.config;\n\n if (\n selectedAddress !== previouslySelectedAddress ||\n !useNftDetection !== disabled\n ) {\n this.configure({ selectedAddress, disabled: !useNftDetection });\n }\n\n if (useNftDetection !== undefined) {\n if (useNftDetection) {\n this.start();\n } else {\n this.stop();\n }\n }\n });\n\n onNetworkStateChange(({ providerConfig }) => {\n this.configure({\n chainId: providerConfig.chainId,\n });\n });\n this.getOpenSeaApiKey = getOpenSeaApiKey;\n this.addNft = addNft;\n this.getNftApi = getNftApi;\n this.setIntervalLength(this.config.interval);\n }\n\n async _executePoll(\n networkClientId: string,\n options: { address: string },\n ): Promise<void> {\n await this.detectNfts({ networkClientId, userAddress: options.address });\n }\n\n /**\n * Start polling for the currency rate.\n */\n async start() {\n if (!this.isMainnet() || this.disabled) {\n return;\n }\n\n await this.startPolling();\n }\n\n /**\n * Stop polling for the currency rate.\n */\n stop() {\n this.stopPolling();\n }\n\n private stopPolling() {\n if (this.intervalId) {\n clearInterval(this.intervalId);\n }\n }\n\n /**\n * Starts a new polling interval.\n *\n * @param interval - An interval on which to poll.\n */\n private async startPolling(interval?: number): Promise<void> {\n interval && this.configure({ interval }, false, false);\n this.stopPolling();\n await this.detectNfts();\n this.intervalId = setInterval(async () => {\n await this.detectNfts();\n }, this.config.interval);\n }\n\n /**\n * Checks whether network is mainnet or not.\n *\n * @returns Whether current network is mainnet.\n */\n isMainnet = (): boolean => this.config.chainId === ChainId.mainnet;\n\n isMainnetByNetworkClientId = (networkClient: NetworkClient): boolean => {\n return networkClient.configuration.chainId === ChainId.mainnet;\n };\n\n /**\n * Triggers asset ERC721 token auto detection on mainnet. Any newly detected NFTs are\n * added.\n *\n * @param options - Options bag.\n * @param options.networkClientId - The network client ID to detect NFTs on.\n * @param options.userAddress - The address to detect NFTs for.\n */\n async detectNfts(\n {\n networkClientId,\n userAddress,\n }: {\n networkClientId?: NetworkClientId;\n userAddress: string;\n } = { userAddress: this.config.selectedAddress },\n ) {\n /* istanbul ignore if */\n if (!this.isMainnet() || this.disabled) {\n return;\n }\n /* istanbul ignore else */\n if (!userAddress) {\n return;\n }\n\n const apiNfts = await this.getOwnerNfts(userAddress);\n const addNftPromises = apiNfts.map(async (nft: ApiNft) => {\n const {\n token_id,\n num_sales,\n background_color,\n image_url,\n image_preview_url,\n image_thumbnail_url,\n image_original_url,\n animation_url,\n animation_original_url,\n name,\n description,\n external_link,\n creator,\n asset_contract: { address, schema_name },\n last_sale,\n } = nft;\n\n let ignored;\n /* istanbul ignore else */\n const { ignoredNfts } = this.getNftState();\n if (ignoredNfts.length) {\n ignored = ignoredNfts.find((c) => {\n /* istanbul ignore next */\n return (\n c.address === toChecksumHexAddress(address) &&\n c.tokenId === token_id\n );\n });\n }\n\n /* istanbul ignore else */\n if (!ignored) {\n /* istanbul ignore next */\n const nftMetadata: NftMetadata = Object.assign(\n {},\n { name },\n creator && { creator },\n description && { description },\n image_url && { image: image_url },\n num_sales && { numberOfSales: num_sales },\n background_color && { backgroundColor: background_color },\n image_preview_url && { imagePreview: image_preview_url },\n image_thumbnail_url && { imageThumbnail: image_thumbnail_url },\n image_original_url && { imageOriginal: image_original_url },\n animation_url && { animation: animation_url },\n animation_original_url && {\n animationOriginal: animation_original_url,\n },\n schema_name && { standard: schema_name },\n external_link && { externalLink: external_link },\n last_sale && { lastSale: last_sale },\n );\n\n await this.addNft(address, token_id, {\n nftMetadata,\n userAddress,\n source: Source.Detected,\n networkClientId,\n });\n }\n });\n await Promise.all(addNftPromises);\n }\n}\n\nexport default NftDetectionController;\n"]}
@@ -1,71 +1,66 @@
1
- import type { BaseConfig, BaseState } from '@metamask/base-controller';
2
- import type { NetworkClientId, NetworkController, NetworkState } from '@metamask/network-controller';
3
- import { StaticIntervalPollingControllerV1 } from '@metamask/polling-controller';
1
+ import type { RestrictedControllerMessenger, ControllerGetStateAction, ControllerStateChangeEvent } from '@metamask/base-controller';
2
+ import type { NetworkClientId, NetworkControllerNetworkDidChangeEvent, NetworkControllerStateChangeEvent, NetworkControllerGetNetworkConfigurationByNetworkClientId } from '@metamask/network-controller';
3
+ import { StaticIntervalPollingController } from '@metamask/polling-controller';
4
4
  import type { PreferencesState } from '@metamask/preferences-controller';
5
- import type { Hex } from '@metamask/utils';
6
5
  import type { AssetsContractController } from './AssetsContractController';
7
- import type { TokenListState } from './TokenListController';
6
+ import type { GetTokenListState, TokenListStateChange } from './TokenListController';
8
7
  import type { TokensController, TokensState } from './TokensController';
8
+ export declare const controllerName = "TokenDetectionController";
9
+ export declare type TokenDetectionState = Record<never, never>;
10
+ export declare type TokenDetectionControllerGetStateAction = ControllerGetStateAction<typeof controllerName, TokenDetectionState>;
11
+ export declare type TokenDetectionControllerActions = TokenDetectionControllerGetStateAction;
12
+ export declare type AllowedActions = NetworkControllerGetNetworkConfigurationByNetworkClientId | GetTokenListState;
13
+ export declare type TokenDetectionControllerStateChangeEvent = ControllerStateChangeEvent<typeof controllerName, TokenDetectionState>;
14
+ export declare type TokenDetectionControllerEvents = TokenDetectionControllerStateChangeEvent;
15
+ export declare type AllowedEvents = NetworkControllerStateChangeEvent | NetworkControllerNetworkDidChangeEvent | TokenListStateChange;
16
+ export declare type TokenDetectionControllerMessenger = RestrictedControllerMessenger<typeof controllerName, TokenDetectionControllerActions | AllowedActions, TokenDetectionControllerEvents | AllowedEvents, AllowedActions['type'], AllowedEvents['type']>;
9
17
  /**
10
- * @type TokenDetectionConfig
11
- *
12
- * TokenDetection configuration
13
- * @property interval - Polling interval used to fetch new token rates
14
- * @property selectedAddress - Vault selected address
18
+ * Controller that passively polls on a set interval for Tokens auto detection
19
+ * @property intervalId - Polling interval used to fetch new token rates
15
20
  * @property chainId - The chain ID of the current network
21
+ * @property selectedAddress - Vault selected address
22
+ * @property networkClientId - The network client ID of the current selected network
23
+ * @property disabled - Boolean to track if network requests are blocked
16
24
  * @property isDetectionEnabledFromPreferences - Boolean to track if detection is enabled from PreferencesController
17
25
  * @property isDetectionEnabledForNetwork - Boolean to track if detected is enabled for current network
18
26
  */
19
- export interface TokenDetectionConfig extends BaseConfig {
20
- interval: number;
21
- selectedAddress: string;
22
- chainId: Hex;
23
- isDetectionEnabledFromPreferences: boolean;
24
- isDetectionEnabledForNetwork: boolean;
25
- }
26
- /**
27
- * Controller that passively polls on a set interval for Tokens auto detection
28
- */
29
- export declare class TokenDetectionController extends StaticIntervalPollingControllerV1<TokenDetectionConfig, BaseState> {
30
- private intervalId?;
31
- /**
32
- * Name of this controller used during composition
33
- */
34
- name: string;
35
- private readonly getBalancesInSingleCall;
36
- private readonly addDetectedTokens;
37
- private readonly getTokensState;
38
- private readonly getTokenListState;
39
- private readonly getNetworkClientById;
27
+ export declare class TokenDetectionController extends StaticIntervalPollingController<typeof controllerName, TokenDetectionState, TokenDetectionControllerMessenger> {
28
+ #private;
40
29
  /**
41
30
  * Creates a TokenDetectionController instance.
42
31
  *
43
32
  * @param options - The controller options.
33
+ * @param options.messenger - The controller messaging system.
34
+ * @param options.disabled - If set to true, all network requests are blocked.
35
+ * @param options.interval - Polling interval used to fetch new token rates
36
+ * @param options.networkClientId - The selected network client ID of the current network
37
+ * @param options.selectedAddress - Vault selected address
44
38
  * @param options.onPreferencesStateChange - Allows subscribing to preferences controller state changes.
45
- * @param options.onNetworkStateChange - Allows subscribing to network controller state changes.
46
- * @param options.onTokenListStateChange - Allows subscribing to token list controller state changes.
47
- * @param options.getBalancesInSingleCall - Gets the balances of a list of tokens for the given address.
48
39
  * @param options.addDetectedTokens - Add a list of detected tokens.
49
- * @param options.getTokenListState - Gets the current state of the TokenList controller.
40
+ * @param options.getBalancesInSingleCall - Gets the balances of a list of tokens for the given address.
50
41
  * @param options.getTokensState - Gets the current state of the Tokens controller.
51
- * @param options.getNetworkState - Gets the state of the network controller.
52
42
  * @param options.getPreferencesState - Gets the state of the preferences controller.
53
- * @param options.getNetworkClientById - Gets the network client by ID.
54
- * @param config - Initial options used to configure this controller.
55
- * @param state - Initial state to set on this controller.
56
43
  */
57
- constructor({ onPreferencesStateChange, onNetworkStateChange, onTokenListStateChange, getBalancesInSingleCall, addDetectedTokens, getTokenListState, getTokensState, getNetworkState, getPreferencesState, getNetworkClientById, }: {
44
+ constructor({ networkClientId, selectedAddress, interval, disabled, onPreferencesStateChange, getBalancesInSingleCall, addDetectedTokens, getPreferencesState, getTokensState, messenger, }: {
45
+ networkClientId: NetworkClientId;
46
+ selectedAddress?: string;
47
+ interval?: number;
48
+ disabled?: boolean;
58
49
  onPreferencesStateChange: (listener: (preferencesState: PreferencesState) => void) => void;
59
- onNetworkStateChange: (listener: (networkState: NetworkState) => void) => void;
60
- onTokenListStateChange: (listener: (tokenListState: TokenListState) => void) => void;
61
- getBalancesInSingleCall: AssetsContractController['getBalancesInSingleCall'];
62
50
  addDetectedTokens: TokensController['addDetectedTokens'];
63
- getTokenListState: () => TokenListState;
51
+ getBalancesInSingleCall: AssetsContractController['getBalancesInSingleCall'];
64
52
  getTokensState: () => TokensState;
65
- getNetworkState: () => NetworkState;
66
53
  getPreferencesState: () => PreferencesState;
67
- getNetworkClientById: NetworkController['getNetworkClientById'];
68
- }, config?: Partial<TokenDetectionConfig>, state?: Partial<BaseState>);
54
+ messenger: TokenDetectionControllerMessenger;
55
+ });
56
+ /**
57
+ * Allows controller to make active and passive polling requests
58
+ */
59
+ enable(): void;
60
+ /**
61
+ * Blocks controller from making network calls
62
+ */
63
+ disable(): void;
69
64
  /**
70
65
  * Start polling for detected tokens.
71
66
  */
@@ -74,14 +69,6 @@ export declare class TokenDetectionController extends StaticIntervalPollingContr
74
69
  * Stop polling for detected tokens.
75
70
  */
76
71
  stop(): void;
77
- private stopPolling;
78
- /**
79
- * Starts a new polling interval.
80
- *
81
- * @param interval - An interval on which to poll.
82
- */
83
- private startPolling;
84
- private getCorrectChainId;
85
72
  _executePoll(networkClientId: string, options: {
86
73
  address: string;
87
74
  }): Promise<void>;
@@ -92,7 +79,7 @@ export declare class TokenDetectionController extends StaticIntervalPollingContr
92
79
  * @param options.networkClientId - The ID of the network client to use.
93
80
  * @param options.accountAddress - The account address to use.
94
81
  */
95
- detectTokens(options?: {
82
+ detectTokens({ networkClientId, accountAddress, }?: {
96
83
  networkClientId?: NetworkClientId;
97
84
  accountAddress?: string;
98
85
  }): Promise<void>;
@@ -1 +1 @@
1
- {"version":3,"file":"TokenDetectionController.d.ts","sourceRoot":"","sources":["../src/TokenDetectionController.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,UAAU,EAAE,SAAS,EAAE,MAAM,2BAA2B,CAAC;AAKvE,OAAO,KAAK,EACV,eAAe,EACf,iBAAiB,EACjB,YAAY,EACb,MAAM,8BAA8B,CAAC;AACtC,OAAO,EAAE,iCAAiC,EAAE,MAAM,8BAA8B,CAAC;AACjF,OAAO,KAAK,EAAE,gBAAgB,EAAE,MAAM,kCAAkC,CAAC;AACzE,OAAO,KAAK,EAAE,GAAG,EAAE,MAAM,iBAAiB,CAAC;AAE3C,OAAO,KAAK,EAAE,wBAAwB,EAAE,MAAM,4BAA4B,CAAC;AAE3E,OAAO,KAAK,EAAE,cAAc,EAAE,MAAM,uBAAuB,CAAC;AAE5D,OAAO,KAAK,EAAE,gBAAgB,EAAE,WAAW,EAAE,MAAM,oBAAoB,CAAC;AAIxE;;;;;;;;;GASG;AAIH,MAAM,WAAW,oBAAqB,SAAQ,UAAU;IACtD,QAAQ,EAAE,MAAM,CAAC;IACjB,eAAe,EAAE,MAAM,CAAC;IACxB,OAAO,EAAE,GAAG,CAAC;IACb,iCAAiC,EAAE,OAAO,CAAC;IAC3C,4BAA4B,EAAE,OAAO,CAAC;CACvC;AAED;;GAEG;AACH,qBAAa,wBAAyB,SAAQ,iCAAiC,CAC7E,oBAAoB,EACpB,SAAS,CACV;IACC,OAAO,CAAC,UAAU,CAAC,CAAgC;IAEnD;;OAEG;IACM,IAAI,SAA8B;IAE3C,OAAO,CAAC,QAAQ,CAAC,uBAAuB,CAAsD;IAE9F,OAAO,CAAC,QAAQ,CAAC,iBAAiB,CAAwC;IAE1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;OAMG;IACG,YAAY,CAAC,EACjB,eAAe,EACf,cAAc,GACf,GAAE;QACD,eAAe,CAAC,EAAE,eAAe,CAAC;QAClC,cAAc,CAAC,EAAE,MAAM,CAAC;KACpB,GAAG,OAAO,CAAC,IAAI,CAAC;CAsFvB;AAED,eAAe,wBAAwB,CAAC"}
@@ -8,127 +8,144 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
8
8
  step((generator = generator.apply(thisArg, _arguments || [])).next());
9
9
  });
10
10
  };
11
+ var __classPrivateFieldSet = (this && this.__classPrivateFieldSet) || function (receiver, state, value, kind, f) {
12
+ if (kind === "m") throw new TypeError("Private method is not writable");
13
+ if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a setter");
14
+ if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot write private member to an object whose class did not declare it");
15
+ return (kind === "a" ? f.call(receiver, value) : f ? f.value = value : state.set(receiver, value)), value;
16
+ };
17
+ var __classPrivateFieldGet = (this && this.__classPrivateFieldGet) || function (receiver, state, kind, f) {
18
+ if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a getter");
19
+ if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot read private member from an object whose class did not declare it");
20
+ return kind === "m" ? f : kind === "a" ? f.call(receiver) : f ? f.value : state.get(receiver);
21
+ };
22
+ var _TokenDetectionController_instances, _TokenDetectionController_intervalId, _TokenDetectionController_chainId, _TokenDetectionController_selectedAddress, _TokenDetectionController_networkClientId, _TokenDetectionController_disabled, _TokenDetectionController_isDetectionEnabledFromPreferences, _TokenDetectionController_isDetectionEnabledForNetwork, _TokenDetectionController_addDetectedTokens, _TokenDetectionController_getBalancesInSingleCall, _TokenDetectionController_getTokensState, _TokenDetectionController_stopPolling, _TokenDetectionController_startPolling, _TokenDetectionController_getCorrectChainId;
11
23
  Object.defineProperty(exports, "__esModule", { value: true });
12
- exports.TokenDetectionController = void 0;
24
+ exports.TokenDetectionController = exports.controllerName = void 0;
13
25
  const controller_utils_1 = require("@metamask/controller-utils");
14
26
  const polling_controller_1 = require("@metamask/polling-controller");
15
27
  const assetsUtil_1 = require("./assetsUtil");
16
28
  const DEFAULT_INTERVAL = 180000;
29
+ exports.controllerName = 'TokenDetectionController';
17
30
  /**
18
31
  * Controller that passively polls on a set interval for Tokens auto detection
32
+ * @property intervalId - Polling interval used to fetch new token rates
33
+ * @property chainId - The chain ID of the current network
34
+ * @property selectedAddress - Vault selected address
35
+ * @property networkClientId - The network client ID of the current selected network
36
+ * @property disabled - Boolean to track if network requests are blocked
37
+ * @property isDetectionEnabledFromPreferences - Boolean to track if detection is enabled from PreferencesController
38
+ * @property isDetectionEnabledForNetwork - Boolean to track if detected is enabled for current network
19
39
  */
20
- class TokenDetectionController extends polling_controller_1.StaticIntervalPollingControllerV1 {
40
+ class TokenDetectionController extends polling_controller_1.StaticIntervalPollingController {
21
41
  /**
22
42
  * Creates a TokenDetectionController instance.
23
43
  *
24
44
  * @param options - The controller options.
45
+ * @param options.messenger - The controller messaging system.
46
+ * @param options.disabled - If set to true, all network requests are blocked.
47
+ * @param options.interval - Polling interval used to fetch new token rates
48
+ * @param options.networkClientId - The selected network client ID of the current network
49
+ * @param options.selectedAddress - Vault selected address
25
50
  * @param options.onPreferencesStateChange - Allows subscribing to preferences controller state changes.
26
- * @param options.onNetworkStateChange - Allows subscribing to network controller state changes.
27
- * @param options.onTokenListStateChange - Allows subscribing to token list controller state changes.
28
- * @param options.getBalancesInSingleCall - Gets the balances of a list of tokens for the given address.
29
51
  * @param options.addDetectedTokens - Add a list of detected tokens.
30
- * @param options.getTokenListState - Gets the current state of the TokenList controller.
52
+ * @param options.getBalancesInSingleCall - Gets the balances of a list of tokens for the given address.
31
53
  * @param options.getTokensState - Gets the current state of the Tokens controller.
32
- * @param options.getNetworkState - Gets the state of the network controller.
33
54
  * @param options.getPreferencesState - Gets the state of the preferences controller.
34
- * @param options.getNetworkClientById - Gets the network client by ID.
35
- * @param config - Initial options used to configure this controller.
36
- * @param state - Initial state to set on this controller.
37
55
  */
38
- constructor({ onPreferencesStateChange, onNetworkStateChange, onTokenListStateChange, getBalancesInSingleCall, addDetectedTokens, getTokenListState, getTokensState, getNetworkState, getPreferencesState, getNetworkClientById, }, config, state) {
39
- const { providerConfig: { chainId: defaultChainId }, } = getNetworkState();
56
+ constructor({ networkClientId, selectedAddress = '', interval = DEFAULT_INTERVAL, disabled = true, onPreferencesStateChange, getBalancesInSingleCall, addDetectedTokens, getPreferencesState, getTokensState, messenger, }) {
40
57
  const { useTokenDetection: defaultUseTokenDetection } = getPreferencesState();
41
- super(config, state);
42
- /**
43
- * Name of this controller used during composition
44
- */
45
- this.name = 'TokenDetectionController';
46
- this.defaultConfig = Object.assign({ interval: DEFAULT_INTERVAL, selectedAddress: '', disabled: true, chainId: defaultChainId, isDetectionEnabledFromPreferences: defaultUseTokenDetection, isDetectionEnabledForNetwork: (0, assetsUtil_1.isTokenDetectionSupportedForNetwork)(defaultChainId) }, config);
47
- this.initialize();
48
- this.setIntervalLength(this.config.interval);
49
- this.getTokensState = getTokensState;
50
- this.getTokenListState = getTokenListState;
51
- this.addDetectedTokens = addDetectedTokens;
52
- this.getBalancesInSingleCall = getBalancesInSingleCall;
53
- this.getNetworkClientById = getNetworkClientById;
54
- onTokenListStateChange(({ tokenList }) => {
58
+ super({
59
+ name: exports.controllerName,
60
+ messenger,
61
+ state: {},
62
+ metadata: {},
63
+ });
64
+ _TokenDetectionController_instances.add(this);
65
+ _TokenDetectionController_intervalId.set(this, void 0);
66
+ _TokenDetectionController_chainId.set(this, void 0);
67
+ _TokenDetectionController_selectedAddress.set(this, void 0);
68
+ _TokenDetectionController_networkClientId.set(this, void 0);
69
+ _TokenDetectionController_disabled.set(this, void 0);
70
+ _TokenDetectionController_isDetectionEnabledFromPreferences.set(this, void 0);
71
+ _TokenDetectionController_isDetectionEnabledForNetwork.set(this, void 0);
72
+ _TokenDetectionController_addDetectedTokens.set(this, void 0);
73
+ _TokenDetectionController_getBalancesInSingleCall.set(this, void 0);
74
+ _TokenDetectionController_getTokensState.set(this, void 0);
75
+ __classPrivateFieldSet(this, _TokenDetectionController_disabled, disabled, "f");
76
+ this.setIntervalLength(interval);
77
+ __classPrivateFieldSet(this, _TokenDetectionController_networkClientId, networkClientId, "f");
78
+ __classPrivateFieldSet(this, _TokenDetectionController_selectedAddress, selectedAddress, "f");
79
+ __classPrivateFieldSet(this, _TokenDetectionController_chainId, __classPrivateFieldGet(this, _TokenDetectionController_instances, "m", _TokenDetectionController_getCorrectChainId).call(this, networkClientId), "f");
80
+ __classPrivateFieldSet(this, _TokenDetectionController_isDetectionEnabledFromPreferences, defaultUseTokenDetection, "f");
81
+ __classPrivateFieldSet(this, _TokenDetectionController_isDetectionEnabledForNetwork, (0, assetsUtil_1.isTokenDetectionSupportedForNetwork)(__classPrivateFieldGet(this, _TokenDetectionController_chainId, "f")), "f");
82
+ __classPrivateFieldSet(this, _TokenDetectionController_addDetectedTokens, addDetectedTokens, "f");
83
+ __classPrivateFieldSet(this, _TokenDetectionController_getBalancesInSingleCall, getBalancesInSingleCall, "f");
84
+ __classPrivateFieldSet(this, _TokenDetectionController_getTokensState, getTokensState, "f");
85
+ this.messagingSystem.subscribe('TokenListController:stateChange', ({ tokenList }) => __awaiter(this, void 0, void 0, function* () {
55
86
  const hasTokens = Object.keys(tokenList).length;
56
87
  if (hasTokens) {
57
- this.detectTokens();
88
+ yield this.detectTokens();
58
89
  }
59
- });
60
- onPreferencesStateChange(({ selectedAddress, useTokenDetection }) => {
61
- const { selectedAddress: currentSelectedAddress, isDetectionEnabledFromPreferences, } = this.config;
62
- const isSelectedAddressChanged = selectedAddress !== currentSelectedAddress;
63
- const isDetectionChangedFromPreferences = isDetectionEnabledFromPreferences !== useTokenDetection;
64
- this.configure({
65
- isDetectionEnabledFromPreferences: useTokenDetection,
66
- selectedAddress,
67
- });
90
+ }));
91
+ onPreferencesStateChange(({ selectedAddress: newSelectedAddress, useTokenDetection }) => __awaiter(this, void 0, void 0, function* () {
92
+ const isSelectedAddressChanged = __classPrivateFieldGet(this, _TokenDetectionController_selectedAddress, "f") !== newSelectedAddress;
93
+ const isDetectionChangedFromPreferences = __classPrivateFieldGet(this, _TokenDetectionController_isDetectionEnabledFromPreferences, "f") !== useTokenDetection;
94
+ __classPrivateFieldSet(this, _TokenDetectionController_selectedAddress, newSelectedAddress, "f");
95
+ __classPrivateFieldSet(this, _TokenDetectionController_isDetectionEnabledFromPreferences, useTokenDetection, "f");
68
96
  if (useTokenDetection &&
69
97
  (isSelectedAddressChanged || isDetectionChangedFromPreferences)) {
70
- this.detectTokens();
98
+ yield this.detectTokens();
71
99
  }
72
- });
73
- onNetworkStateChange(({ providerConfig: { chainId } }) => {
74
- const { chainId: currentChainId } = this.config;
75
- const isDetectionEnabledForNetwork = (0, assetsUtil_1.isTokenDetectionSupportedForNetwork)(chainId);
76
- const isChainIdChanged = currentChainId !== chainId;
77
- this.configure({
78
- chainId,
79
- isDetectionEnabledForNetwork,
80
- });
81
- if (isDetectionEnabledForNetwork && isChainIdChanged) {
82
- this.detectTokens();
100
+ }));
101
+ this.messagingSystem.subscribe('NetworkController:networkDidChange', ({ selectedNetworkClientId }) => __awaiter(this, void 0, void 0, function* () {
102
+ __classPrivateFieldSet(this, _TokenDetectionController_networkClientId, selectedNetworkClientId, "f");
103
+ const newChainId = __classPrivateFieldGet(this, _TokenDetectionController_instances, "m", _TokenDetectionController_getCorrectChainId).call(this, selectedNetworkClientId);
104
+ const isChainIdChanged = __classPrivateFieldGet(this, _TokenDetectionController_chainId, "f") !== newChainId;
105
+ __classPrivateFieldSet(this, _TokenDetectionController_chainId, newChainId, "f");
106
+ __classPrivateFieldSet(this, _TokenDetectionController_isDetectionEnabledForNetwork, (0, assetsUtil_1.isTokenDetectionSupportedForNetwork)(newChainId), "f");
107
+ if (__classPrivateFieldGet(this, _TokenDetectionController_isDetectionEnabledForNetwork, "f") && isChainIdChanged) {
108
+ yield this.detectTokens();
83
109
  }
84
- });
110
+ }));
85
111
  }
86
112
  /**
87
- * Start polling for detected tokens.
113
+ * Allows controller to make active and passive polling requests
88
114
  */
89
- start() {
90
- return __awaiter(this, void 0, void 0, function* () {
91
- this.configure({ disabled: false });
92
- yield this.startPolling();
93
- });
115
+ enable() {
116
+ __classPrivateFieldSet(this, _TokenDetectionController_disabled, false, "f");
94
117
  }
95
118
  /**
96
- * Stop polling for detected tokens.
119
+ * Blocks controller from making network calls
97
120
  */
98
- stop() {
99
- this.configure({ disabled: true });
100
- this.stopPolling();
101
- }
102
- stopPolling() {
103
- if (this.intervalId) {
104
- clearInterval(this.intervalId);
105
- }
121
+ disable() {
122
+ __classPrivateFieldSet(this, _TokenDetectionController_disabled, true, "f");
106
123
  }
107
124
  /**
108
- * Starts a new polling interval.
109
- *
110
- * @param interval - An interval on which to poll.
125
+ * Start polling for detected tokens.
111
126
  */
112
- startPolling(interval) {
127
+ start() {
113
128
  return __awaiter(this, void 0, void 0, function* () {
114
- interval && this.configure({ interval }, false, false);
115
- this.stopPolling();
116
- yield this.detectTokens();
117
- this.intervalId = setInterval(() => __awaiter(this, void 0, void 0, function* () {
118
- yield this.detectTokens();
119
- }), this.config.interval);
129
+ this.enable();
130
+ yield __classPrivateFieldGet(this, _TokenDetectionController_instances, "m", _TokenDetectionController_startPolling).call(this);
120
131
  });
121
132
  }
122
- getCorrectChainId(networkClientId) {
123
- if (networkClientId) {
124
- return this.getNetworkClientById(networkClientId).configuration.chainId;
125
- }
126
- return this.config.chainId;
133
+ /**
134
+ * Stop polling for detected tokens.
135
+ */
136
+ stop() {
137
+ this.disable();
138
+ __classPrivateFieldGet(this, _TokenDetectionController_instances, "m", _TokenDetectionController_stopPolling).call(this);
127
139
  }
128
140
  _executePoll(networkClientId, options) {
129
- return this.detectTokens({
130
- networkClientId,
131
- accountAddress: options.address,
141
+ return __awaiter(this, void 0, void 0, function* () {
142
+ if (__classPrivateFieldGet(this, _TokenDetectionController_disabled, "f")) {
143
+ return;
144
+ }
145
+ yield this.detectTokens({
146
+ networkClientId,
147
+ accountAddress: options.address,
148
+ });
132
149
  });
133
150
  }
134
151
  /**
@@ -138,21 +155,19 @@ class TokenDetectionController extends polling_controller_1.StaticIntervalPollin
138
155
  * @param options.networkClientId - The ID of the network client to use.
139
156
  * @param options.accountAddress - The account address to use.
140
157
  */
141
- detectTokens(options) {
158
+ detectTokens({ networkClientId, accountAddress, } = {}) {
142
159
  return __awaiter(this, void 0, void 0, function* () {
143
- const { networkClientId, accountAddress } = options || {};
144
- const { disabled, isDetectionEnabledForNetwork, isDetectionEnabledFromPreferences, } = this.config;
145
- if (disabled ||
146
- !isDetectionEnabledForNetwork ||
147
- !isDetectionEnabledFromPreferences) {
160
+ if (__classPrivateFieldGet(this, _TokenDetectionController_disabled, "f") ||
161
+ !__classPrivateFieldGet(this, _TokenDetectionController_isDetectionEnabledForNetwork, "f") ||
162
+ !__classPrivateFieldGet(this, _TokenDetectionController_isDetectionEnabledFromPreferences, "f")) {
148
163
  return;
149
164
  }
150
- const { tokens } = this.getTokensState();
151
- const selectedAddress = accountAddress || this.config.selectedAddress;
152
- const chainId = this.getCorrectChainId(networkClientId);
165
+ const { tokens } = __classPrivateFieldGet(this, _TokenDetectionController_getTokensState, "f").call(this);
166
+ const selectedAddress = accountAddress || __classPrivateFieldGet(this, _TokenDetectionController_selectedAddress, "f");
167
+ const chainId = __classPrivateFieldGet(this, _TokenDetectionController_instances, "m", _TokenDetectionController_getCorrectChainId).call(this, networkClientId);
153
168
  const tokensAddresses = tokens.map(
154
169
  /* istanbul ignore next*/ (token) => token.address.toLowerCase());
155
- const { tokenList } = this.getTokenListState();
170
+ const { tokenList } = this.messagingSystem.call('TokenListController:getState');
156
171
  const tokensToDetect = [];
157
172
  for (const address of Object.keys(tokenList)) {
158
173
  if (!tokensAddresses.includes(address)) {
@@ -171,16 +186,17 @@ class TokenDetectionController extends polling_controller_1.StaticIntervalPollin
171
186
  break;
172
187
  }
173
188
  yield (0, controller_utils_1.safelyExecute)(() => __awaiter(this, void 0, void 0, function* () {
174
- const balances = yield this.getBalancesInSingleCall(selectedAddress, tokensSlice);
189
+ var _a;
190
+ const balances = yield __classPrivateFieldGet(this, _TokenDetectionController_getBalancesInSingleCall, "f").call(this, selectedAddress, tokensSlice);
175
191
  const tokensToAdd = [];
176
192
  for (const tokenAddress of Object.keys(balances)) {
177
193
  let ignored;
178
194
  /* istanbul ignore else */
179
- const { ignoredTokens } = this.getTokensState();
195
+ const { ignoredTokens } = __classPrivateFieldGet(this, _TokenDetectionController_getTokensState, "f").call(this);
180
196
  if (ignoredTokens.length) {
181
197
  ignored = ignoredTokens.find((ignoredTokenAddress) => ignoredTokenAddress === (0, controller_utils_1.toChecksumHexAddress)(tokenAddress));
182
198
  }
183
- const caseInsensitiveTokenKey = Object.keys(tokenList).find((i) => i.toLowerCase() === tokenAddress.toLowerCase()) || '';
199
+ const caseInsensitiveTokenKey = (_a = Object.keys(tokenList).find((i) => i.toLowerCase() === tokenAddress.toLowerCase())) !== null && _a !== void 0 ? _a : '';
184
200
  if (ignored === undefined) {
185
201
  const { decimals, symbol, aggregators, iconUrl, name } = tokenList[caseInsensitiveTokenKey];
186
202
  tokensToAdd.push({
@@ -195,7 +211,7 @@ class TokenDetectionController extends polling_controller_1.StaticIntervalPollin
195
211
  }
196
212
  }
197
213
  if (tokensToAdd.length) {
198
- yield this.addDetectedTokens(tokensToAdd, {
214
+ yield __classPrivateFieldGet(this, _TokenDetectionController_addDetectedTokens, "f").call(this, tokensToAdd, {
199
215
  selectedAddress,
200
216
  chainId,
201
217
  });
@@ -206,5 +222,25 @@ class TokenDetectionController extends polling_controller_1.StaticIntervalPollin
206
222
  }
207
223
  }
208
224
  exports.TokenDetectionController = TokenDetectionController;
225
+ _TokenDetectionController_intervalId = new WeakMap(), _TokenDetectionController_chainId = new WeakMap(), _TokenDetectionController_selectedAddress = new WeakMap(), _TokenDetectionController_networkClientId = new WeakMap(), _TokenDetectionController_disabled = new WeakMap(), _TokenDetectionController_isDetectionEnabledFromPreferences = new WeakMap(), _TokenDetectionController_isDetectionEnabledForNetwork = new WeakMap(), _TokenDetectionController_addDetectedTokens = new WeakMap(), _TokenDetectionController_getBalancesInSingleCall = new WeakMap(), _TokenDetectionController_getTokensState = new WeakMap(), _TokenDetectionController_instances = new WeakSet(), _TokenDetectionController_stopPolling = function _TokenDetectionController_stopPolling() {
226
+ if (__classPrivateFieldGet(this, _TokenDetectionController_intervalId, "f")) {
227
+ clearInterval(__classPrivateFieldGet(this, _TokenDetectionController_intervalId, "f"));
228
+ }
229
+ }, _TokenDetectionController_startPolling = function _TokenDetectionController_startPolling() {
230
+ return __awaiter(this, void 0, void 0, function* () {
231
+ if (__classPrivateFieldGet(this, _TokenDetectionController_disabled, "f")) {
232
+ return;
233
+ }
234
+ __classPrivateFieldGet(this, _TokenDetectionController_instances, "m", _TokenDetectionController_stopPolling).call(this);
235
+ yield this.detectTokens();
236
+ __classPrivateFieldSet(this, _TokenDetectionController_intervalId, setInterval(() => __awaiter(this, void 0, void 0, function* () {
237
+ yield this.detectTokens();
238
+ }), this.getIntervalLength()), "f");
239
+ });
240
+ }, _TokenDetectionController_getCorrectChainId = function _TokenDetectionController_getCorrectChainId(networkClientId) {
241
+ var _a;
242
+ const { chainId } = (_a = this.messagingSystem.call('NetworkController:getNetworkConfigurationByNetworkClientId', networkClientId !== null && networkClientId !== void 0 ? networkClientId : __classPrivateFieldGet(this, _TokenDetectionController_networkClientId, "f"))) !== null && _a !== void 0 ? _a : {};
243
+ return chainId !== null && chainId !== void 0 ? chainId : __classPrivateFieldGet(this, _TokenDetectionController_chainId, "f");
244
+ };
209
245
  exports.default = TokenDetectionController;
210
246
  //# sourceMappingURL=TokenDetectionController.js.map