@metamask/assets-controllers 79.0.0 → 80.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.
- package/CHANGELOG.md +41 -1
- package/README.md +1 -1
- package/dist/NftDetectionController.cjs +1 -72
- package/dist/NftDetectionController.cjs.map +1 -1
- package/dist/NftDetectionController.d.cts +0 -1
- package/dist/NftDetectionController.d.cts.map +1 -1
- package/dist/NftDetectionController.d.mts +0 -1
- package/dist/NftDetectionController.d.mts.map +1 -1
- package/dist/NftDetectionController.mjs +1 -72
- package/dist/NftDetectionController.mjs.map +1 -1
- package/dist/TokenBalancesController.cjs +228 -9
- package/dist/TokenBalancesController.cjs.map +1 -1
- package/dist/TokenBalancesController.d.cts +27 -3
- package/dist/TokenBalancesController.d.cts.map +1 -1
- package/dist/TokenBalancesController.d.mts +27 -3
- package/dist/TokenBalancesController.d.mts.map +1 -1
- package/dist/TokenBalancesController.mjs +226 -9
- package/dist/TokenBalancesController.mjs.map +1 -1
- package/dist/TokenDetectionController.cjs +73 -44
- package/dist/TokenDetectionController.cjs.map +1 -1
- package/dist/TokenDetectionController.d.cts +37 -9
- package/dist/TokenDetectionController.d.cts.map +1 -1
- package/dist/TokenDetectionController.d.mts +37 -9
- package/dist/TokenDetectionController.d.mts.map +1 -1
- package/dist/TokenDetectionController.mjs +73 -44
- package/dist/TokenDetectionController.mjs.map +1 -1
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +1 -1
- package/dist/index.d.cts.map +1 -1
- package/dist/index.d.mts +1 -1
- package/dist/index.d.mts.map +1 -1
- package/dist/index.mjs.map +1 -1
- package/dist/multi-chain-accounts-service/api-balance-fetcher.cjs +6 -2
- package/dist/multi-chain-accounts-service/api-balance-fetcher.cjs.map +1 -1
- package/dist/multi-chain-accounts-service/api-balance-fetcher.d.cts +1 -1
- package/dist/multi-chain-accounts-service/api-balance-fetcher.d.cts.map +1 -1
- package/dist/multi-chain-accounts-service/api-balance-fetcher.d.mts +1 -1
- package/dist/multi-chain-accounts-service/api-balance-fetcher.d.mts.map +1 -1
- package/dist/multi-chain-accounts-service/api-balance-fetcher.mjs +6 -2
- package/dist/multi-chain-accounts-service/api-balance-fetcher.mjs.map +1 -1
- package/package.json +16 -14
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"NftDetectionController.mjs","sourceRoot":"","sources":["../src/NftDetectionController.ts"],"names":[],"mappings":";;;;;;;;;;;;AAGA,OAAO,EAAE,cAAc,EAAE,kCAAkC;AAC3D,OAAO,EACL,oBAAoB,EACpB,OAAO,EACP,gBAAgB,EAChB,eAAe,EACf,mBAAmB,EACnB,WAAW,EACX,sBAAsB,EACtB,eAAe,EACf,KAAK,EACN,mCAAmC;AAYpC,OAAO,EAAE,qBAAqB,EAAY,wBAAwB;AAElE,OAAO,EAAE,uBAAuB,EAAE,yBAAqB;AACvD,OAAO,EAAE,MAAM,EAAE,wBAAoB;AAQrC,MAAM,cAAc,GAAG,wBAAwB,CAAC;AAwBhD;;GAEG;AACH,MAAM,6BAA6B,GAAa,IAAI,GAAG,CAAC;IACtD,4FAA4F;IAC5F,qEAAqE;IACrE,KAAK;IACL,QAAQ;IACR,OAAO,EAAE,MAAM;CAChB,CAAC,CAAC;AA0JH,MAAM,CAAN,IAAY,kBAKX;AALD,WAAY,kBAAkB;IAC5B,uCAAiB,CAAA;IACjB,mCAAa,CAAA;IACb,yCAAmB,CAAA;IACnB,6CAAuB,CAAA;AACzB,CAAC,EALW,kBAAkB,KAAlB,kBAAkB,QAK7B;AAkOD,MAAM,CAAC,MAAM,6BAA6B,GAAG,EAAE,CAAC;AAEhD;;GAEG;AACH,MAAM,OAAO,sBAAuB,SAAQ,cAI3C;IASC;;;;;;;;OAQG;IACH,YAAY,EACV,SAAS,EACT,QAAQ,GAAG,KAAK,EAChB,MAAM,EACN,WAAW,GAMZ;QACC,KAAK,CAAC;YACJ,IAAI,EAAE,cAAc;YACpB,SAAS;YACT,QAAQ,EAAE,EA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Ae,EAAoB;IACvE,IAAI,CAAC,eAAe,KAAK,uBAAA,IAAI,wCAAU,EAAE;QACvC,uBAAA,IAAI,oCAAa,CAAC,eAAe,MAAA,CAAC;KACnC;AACH,CAAC,2FAEe,EACd,QAAQ,EACR,OAAO,EACP,IAAI,GAKL;IACC,4FAA4F;IAC5F,MAAM,cAAc,GAAG,QAAQ,CAAC,IAAI,CAAC,YAAY,CAAC,CAAC;IACnD,OAAO,GACL,gBACF,UAAU,OAAO,oBAAoB,cAAc,6CAA6C,IAAI,IAAI,EAAE,EAAE,CAAC;AAC/G,CAAC,yCAED,KAAK,+CACH,OAAe,EACf,QAAe,EACf,MAA0B;IAE1B,gCAAgC;IAChC,MAAM,iBAAiB,GAAG,QAAQ,CAAC,GAAG,CAAC,CAAC,OAAO,EAAE,EAAE,CACjD,mBAAmB,CAAC,OAAO,CAAC,CAAC,QAAQ,EAAE,CACxC,CAAC;IACF,MAAM,GAAG,GAAG,uBAAA,IAAI,iFAAgB,MAApB,IAAI,EAAiB;QAC/B,QAAQ,EAAE,iBAAiB;QAC3B,OAAO;QACP,IAAI,EAAE,MAAM;KACb,CAAC,CAAC;IACH,MAAM,cAAc,GAAsB,MAAM,WAAW,CAAC,GAAG,EAAE;QAC/D,OAAO,EAAE;YACP,OAAO,EAAE,eAAe;SACzB;KACF,CAAC,CAAC;IACH,OAAO,cAAc,CAAC;AACxB,CAAC;AAiPH,eAAe,sBAAsB,CAAC","sourcesContent":["import type { AccountsControllerGetSelectedAccountAction } from '@metamask/accounts-controller';\nimport type { AddApprovalRequest } from '@metamask/approval-controller';\nimport type { RestrictedMessenger } from '@metamask/base-controller';\nimport { BaseController } from '@metamask/base-controller';\nimport {\n toChecksumHexAddress,\n ChainId,\n NFT_API_BASE_URL,\n NFT_API_VERSION,\n convertHexToDecimal,\n handleFetch,\n fetchWithErrorHandling,\n NFT_API_TIMEOUT,\n toHex,\n} from '@metamask/controller-utils';\nimport type {\n NetworkClient,\n NetworkControllerGetNetworkClientByIdAction,\n NetworkControllerStateChangeEvent,\n NetworkControllerGetStateAction,\n} from '@metamask/network-controller';\nimport type {\n PreferencesControllerGetStateAction,\n PreferencesControllerStateChangeEvent,\n PreferencesState,\n} from '@metamask/preferences-controller';\nimport { createDeferredPromise, type Hex } from '@metamask/utils';\n\nimport { reduceInBatchesSerially } from './assetsUtil';\nimport { Source } from './constants';\nimport {\n type NftController,\n type NftControllerState,\n type NftMetadata,\n} from './NftController';\nimport type { NetworkControllerFindNetworkClientIdByChainIdAction } from '../../network-controller/src/NetworkController';\n\nconst controllerName = 'NftDetectionController';\n\nexport type NFTDetectionControllerState = Record<never, never>;\n\nexport type AllowedActions =\n | AddApprovalRequest\n | NetworkControllerGetStateAction\n | NetworkControllerGetNetworkClientByIdAction\n | PreferencesControllerGetStateAction\n | AccountsControllerGetSelectedAccountAction\n | NetworkControllerFindNetworkClientIdByChainIdAction;\n\nexport type AllowedEvents =\n | PreferencesControllerStateChangeEvent\n | NetworkControllerStateChangeEvent;\n\nexport type NftDetectionControllerMessenger = RestrictedMessenger<\n typeof controllerName,\n AllowedActions,\n AllowedEvents,\n AllowedActions['type'],\n AllowedEvents['type']\n>;\n\n/**\n * A set of supported networks for NFT detection.\n */\nconst supportedNftDetectionNetworks: Set<Hex> = new Set([\n // TODO: We should consider passing this constant from the NftDetectionController contructor\n // to reduce the complexity to add further network into this constant\n '0x1', // Mainnet\n '0xe708', // Linea Mainnet\n '0x531', // Sei\n]);\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 */\nexport type ApiNft = {\n // TODO: Either fix this lint violation or explain why it's necessary to ignore.\n // eslint-disable-next-line @typescript-eslint/naming-convention\n token_id: string;\n // TODO: Either fix this lint violation or explain why it's necessary to ignore.\n // eslint-disable-next-line @typescript-eslint/naming-convention\n num_sales: number | null;\n // TODO: Either fix this lint violation or explain why it's necessary to ignore.\n // eslint-disable-next-line @typescript-eslint/naming-convention\n background_color: string | null;\n // TODO: Either fix this lint violation or explain why it's necessary to ignore.\n // eslint-disable-next-line @typescript-eslint/naming-convention\n image_url: string | null;\n // TODO: Either fix this lint violation or explain why it's necessary to ignore.\n // eslint-disable-next-line @typescript-eslint/naming-convention\n image_preview_url: string | null;\n // TODO: Either fix this lint violation or explain why it's necessary to ignore.\n // eslint-disable-next-line @typescript-eslint/naming-convention\n image_thumbnail_url: string | null;\n // TODO: Either fix this lint violation or explain why it's necessary to ignore.\n // eslint-disable-next-line @typescript-eslint/naming-convention\n image_original_url: string | null;\n // TODO: Either fix this lint violation or explain why it's necessary to ignore.\n // eslint-disable-next-line @typescript-eslint/naming-convention\n animation_url: string | null;\n // TODO: Either fix this lint violation or explain why it's necessary to ignore.\n // eslint-disable-next-line @typescript-eslint/naming-convention\n animation_original_url: string | null;\n name: string | null;\n description: string | null;\n // TODO: Either fix this lint violation or explain why it's necessary to ignore.\n // eslint-disable-next-line @typescript-eslint/naming-convention\n external_link: string | null;\n // TODO: Either fix this lint violation or explain why it's necessary to ignore.\n // eslint-disable-next-line @typescript-eslint/naming-convention\n asset_contract: ApiNftContract;\n creator: ApiNftCreator;\n // TODO: Either fix this lint violation or explain why it's necessary to ignore.\n // eslint-disable-next-line @typescript-eslint/naming-convention\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 */\nexport type ApiNftContract = {\n address: string;\n // TODO: Either fix this lint violation or explain why it's necessary to ignore.\n // eslint-disable-next-line @typescript-eslint/naming-convention\n asset_contract_type: string | null;\n // TODO: Either fix this lint violation or explain why it's necessary to ignore.\n // eslint-disable-next-line @typescript-eslint/naming-convention\n created_date: string | null;\n // TODO: Either fix this lint violation or explain why it's necessary to ignore.\n // eslint-disable-next-line @typescript-eslint/naming-convention\n schema_name: string | null;\n symbol: string | null;\n // TODO: Either fix this lint violation or explain why it's necessary to ignore.\n // eslint-disable-next-line @typescript-eslint/naming-convention\n total_supply: string | null;\n description: string | null;\n // TODO: Either fix this lint violation or explain why it's necessary to ignore.\n // eslint-disable-next-line @typescript-eslint/naming-convention\n external_link: string | null;\n collection: {\n name: string | null;\n // TODO: Either fix this lint violation or explain why it's necessary to ignore.\n // eslint-disable-next-line @typescript-eslint/naming-convention\n image_url?: string | null;\n tokenCount?: 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 */\nexport type ApiNftLastSale = {\n // TODO: Either fix this lint violation or explain why it's necessary to ignore.\n // eslint-disable-next-line @typescript-eslint/naming-convention\n event_timestamp: string;\n // TODO: Either fix this lint violation or explain why it's necessary to ignore.\n // eslint-disable-next-line @typescript-eslint/naming-convention\n total_price: string;\n // TODO: Either fix this lint violation or explain why it's necessary to ignore.\n // eslint-disable-next-line @typescript-eslint/naming-convention\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 */\nexport type ApiNftCreator = {\n user: { username: string };\n // TODO: Either fix this lint violation or explain why it's necessary to ignore.\n // eslint-disable-next-line @typescript-eslint/naming-convention\n profile_img_url: string;\n address: string;\n};\n\nexport type ReservoirResponse = {\n tokens: TokensResponse[];\n continuation?: string;\n};\n\nexport type TokensResponse = {\n token: TokenResponse;\n ownership: Ownership;\n market?: Market;\n blockaidResult?: Blockaid;\n};\n\nexport enum BlockaidResultType {\n Benign = 'Benign',\n Spam = 'Spam',\n Warning = 'Warning',\n Malicious = 'Malicious',\n}\n\nexport type Blockaid = {\n contract: string;\n chainId: number;\n // TODO: Either fix this lint violation or explain why it's necessary to ignore.\n // eslint-disable-next-line @typescript-eslint/naming-convention\n result_type: BlockaidResultType;\n // TODO: Either fix this lint violation or explain why it's necessary to ignore.\n // eslint-disable-next-line @typescript-eslint/naming-convention\n malicious_score: string;\n // TODO: Either fix this lint violation or explain why it's necessary to ignore.\n // eslint-disable-next-line @typescript-eslint/naming-convention\n attack_types: object;\n};\n\nexport type Market = {\n floorAsk?: FloorAsk;\n topBid?: TopBid;\n};\n\nexport type TokenResponse = {\n chainId: number;\n contract: string;\n tokenId: string;\n kind?: string;\n name?: string;\n image?: string;\n imageSmall?: string;\n imageLarge?: string;\n metadata?: Metadata;\n description?: string;\n supply?: number;\n remainingSupply?: number;\n rarityScore?: number;\n rarity?: number;\n rarityRank?: number;\n media?: string;\n isFlagged?: boolean;\n isSpam?: boolean;\n isNsfw?: boolean;\n metadataDisabled?: boolean;\n lastFlagUpdate?: string;\n lastFlagChange?: string;\n collection?: Collection;\n lastSale?: LastSale;\n topBid?: TopBid;\n lastAppraisalValue?: number;\n attributes?: Attributes[];\n};\n\nexport type TopBid = {\n id?: string;\n price?: Price;\n source?: {\n id?: string;\n domain?: string;\n name?: string;\n icon?: string;\n url?: string;\n };\n};\n\nexport type LastSale = {\n saleId?: string;\n token?: {\n contract?: string;\n tokenId?: string;\n name?: string;\n image?: string;\n collection?: {\n id?: string;\n name?: string;\n };\n };\n orderSource?: string;\n orderSide?: 'ask' | 'bid';\n orderKind?: string;\n orderId?: string;\n from?: string;\n to?: string;\n amount?: string;\n fillSource?: string;\n block?: number;\n txHash?: string;\n logIndex?: number;\n batchIndex?: number;\n timestamp?: number;\n price?: Price;\n washTradingScore?: number;\n royaltyFeeBps?: number;\n marketplaceFeeBps?: number;\n paidFullRoyalty?: boolean;\n feeBreakdown?: FeeBreakdown[];\n isDeleted?: boolean;\n createdAt?: string;\n updatedAt?: string;\n};\n\nexport type FeeBreakdown = {\n kind?: string;\n bps?: number;\n recipient?: string;\n source?: string;\n rawAmount?: string;\n};\n\nexport type Attributes = {\n key?: string;\n kind?: string;\n value: string;\n tokenCount?: number;\n onSaleCount?: number;\n floorAskPrice?: Price | null;\n topBidValue?: number | null;\n createdAt?: string;\n};\n\nexport type GetCollectionsResponse = {\n collections: CollectionResponse[];\n};\n\nexport type CollectionResponse = {\n id?: string;\n chainId?: number;\n openseaVerificationStatus?: string;\n contractDeployedAt?: string;\n creator?: string;\n ownerCount?: string;\n topBid?: TopBid & {\n sourceDomain?: string;\n };\n};\n\nexport type FloorAskCollection = {\n id?: string;\n price?: Price;\n maker?: string;\n kind?: string;\n validFrom?: number;\n validUntil?: number;\n source?: SourceCollection;\n rawData?: Metadata;\n isNativeOffChainCancellable?: boolean;\n};\n\nexport type SourceCollection = {\n id: string;\n domain: string;\n name: string;\n icon: string;\n url: string;\n};\n\nexport type TokenCollection = {\n id?: string;\n name?: string;\n slug?: string;\n symbol?: string;\n imageUrl?: string;\n image?: string;\n isSpam?: boolean;\n isNsfw?: boolean;\n creator?: string;\n tokenCount?: string;\n metadataDisabled?: boolean;\n openseaVerificationStatus?: string;\n floorAskPrice?: Price;\n royaltiesBps?: number;\n royalties?: Royalties[];\n floorAsk?: FloorAskCollection;\n};\n\nexport type Collection = TokenCollection & CollectionResponse;\n\nexport type Royalties = {\n bps?: number;\n recipient?: string;\n};\n\nexport type Ownership = {\n tokenCount?: string;\n onSaleCount?: string;\n floorAsk?: FloorAsk;\n acquiredAt?: string;\n};\n\nexport type FloorAsk = {\n id?: string;\n price?: Price;\n maker?: string;\n kind?: string;\n validFrom?: number;\n validUntil?: number;\n source?: Source;\n rawData?: Metadata;\n isNativeOffChainCancellable?: boolean;\n};\n\nexport type Price = {\n currency?: {\n contract?: string;\n name?: string;\n symbol?: string;\n decimals?: number;\n chainId?: number;\n };\n amount?: {\n raw?: string;\n decimal?: number;\n usd?: number;\n native?: number;\n };\n netAmount?: {\n raw?: string;\n decimal?: number;\n usd?: number;\n native?: number;\n };\n};\n\nexport type Metadata = {\n imageOriginal?: string;\n tokenURI?: string;\n};\n\nexport const MAX_GET_COLLECTION_BATCH_SIZE = 20;\n\n/**\n * Controller that passively detects nfts for a user address\n */\nexport class NftDetectionController extends BaseController<\n typeof controllerName,\n NFTDetectionControllerState,\n NftDetectionControllerMessenger\n> {\n #disabled: boolean;\n\n readonly #addNft: NftController['addNft'];\n\n readonly #getNftState: () => NftControllerState;\n\n #inProcessNftFetchingUpdates: Record<`${string}:${string}`, Promise<void>>;\n\n /**\n * The controller options\n *\n * @param options - The controller options.\n * @param options.messenger - A reference to the messaging system.\n * @param options.disabled - Represents previous value of useNftDetection. Used to detect changes of useNftDetection. Default value is true.\n * @param options.addNft - Add an NFT.\n * @param options.getNftState - Gets the current state of the Assets controller.\n */\n constructor({\n messenger,\n disabled = false,\n addNft,\n getNftState,\n }: {\n messenger: NftDetectionControllerMessenger;\n disabled: boolean;\n addNft: NftController['addNft'];\n getNftState: () => NftControllerState;\n }) {\n super({\n name: controllerName,\n messenger,\n metadata: {},\n state: {},\n });\n this.#disabled = disabled;\n this.#inProcessNftFetchingUpdates = {};\n\n this.#getNftState = getNftState;\n this.#addNft = addNft;\n\n this.messagingSystem.subscribe(\n 'PreferencesController:stateChange',\n this.#onPreferencesControllerStateChange.bind(this),\n );\n }\n\n /**\n * Checks whether network is mainnet or not.\n *\n * @returns Whether current network is mainnet.\n */\n isMainnet(): boolean {\n const { selectedNetworkClientId } = this.messagingSystem.call(\n 'NetworkController:getState',\n );\n const {\n configuration: { chainId },\n } = this.messagingSystem.call(\n 'NetworkController:getNetworkClientById',\n selectedNetworkClientId,\n );\n return chainId === ChainId.mainnet;\n }\n\n isMainnetByNetworkClientId(networkClient: NetworkClient): boolean {\n return networkClient.configuration.chainId === ChainId.mainnet;\n }\n\n /**\n * Handles the state change of the preference controller.\n * @param preferencesState - The new state of the preference controller.\n * @param preferencesState.useNftDetection - Boolean indicating user preference on NFT detection.\n */\n #onPreferencesControllerStateChange({ useNftDetection }: PreferencesState) {\n if (!useNftDetection !== this.#disabled) {\n this.#disabled = !useNftDetection;\n }\n }\n\n #getOwnerNftApi({\n chainIds,\n address,\n next,\n }: {\n chainIds: string[];\n address: string;\n next?: string;\n }) {\n // from chainIds construct a string of chainIds that can be used like chainIds=1&chainIds=56\n const chainIdsString = chainIds.join('&chainIds=');\n return `${\n NFT_API_BASE_URL as string\n }/users/${address}/tokens?chainIds=${chainIdsString}&limit=50&includeTopBid=true&continuation=${next ?? ''}`;\n }\n\n async #getOwnerNfts(\n address: string,\n chainIds: Hex[],\n cursor: string | undefined,\n ) {\n // Convert hex chainId to number\n const convertedChainIds = chainIds.map((chainId) =>\n convertHexToDecimal(chainId).toString(),\n );\n const url = this.#getOwnerNftApi({\n chainIds: convertedChainIds,\n address,\n next: cursor,\n });\n const nftApiResponse: ReservoirResponse = await handleFetch(url, {\n headers: {\n Version: NFT_API_VERSION,\n },\n });\n return nftApiResponse;\n }\n\n /**\n * Triggers asset ERC721 token auto detection on mainnet. Any newly detected NFTs are\n * added.\n *\n * @param chainIds - The chain IDs to detect NFTs on.\n * @param options - Options bag.\n * @param options.userAddress - The address to detect NFTs for.\n */\n async detectNfts(chainIds: Hex[], options?: { userAddress?: string }) {\n const userAddress =\n options?.userAddress ??\n this.messagingSystem.call('AccountsController:getSelectedAccount')\n .address;\n\n // filter out unsupported chainIds\n const supportedChainIds = chainIds.filter((chainId) =>\n supportedNftDetectionNetworks.has(chainId),\n );\n /* istanbul ignore if */\n if (supportedChainIds.length === 0 || this.#disabled) {\n return;\n }\n /* istanbul ignore else */\n if (!userAddress) {\n return;\n }\n // create a string of all chainIds\n const chainIdsString = chainIds.join(',');\n\n const updateKey: `${string}:${string}` = `${chainIdsString}:${userAddress}`;\n if (updateKey in this.#inProcessNftFetchingUpdates) {\n // This prevents redundant updates\n // This promise is resolved after the in-progress update has finished,\n // and state has been updated.\n await this.#inProcessNftFetchingUpdates[updateKey];\n return;\n }\n\n const {\n promise: inProgressUpdate,\n resolve: updateSucceeded,\n reject: updateFailed,\n } = createDeferredPromise({ suppressUnhandledRejection: true });\n this.#inProcessNftFetchingUpdates[updateKey] = inProgressUpdate;\n\n let next;\n let apiNfts: TokensResponse[] = [];\n let resultNftApi: ReservoirResponse;\n try {\n do {\n resultNftApi = await this.#getOwnerNfts(\n userAddress,\n supportedChainIds,\n next,\n );\n apiNfts = resultNftApi.tokens.filter(\n (elm) =>\n elm.token.isSpam === false &&\n (elm.blockaidResult?.result_type\n ? elm.blockaidResult?.result_type === BlockaidResultType.Benign\n : true),\n );\n // Retrieve collections from apiNfts\n // contract and collection.id are equal for simple contract addresses; this is to exclude cases for shared contracts\n const collections = apiNfts.reduce<Record<string, string[]>>(\n (acc, currValue) => {\n if (\n !acc[currValue.token.chainId]?.includes(\n currValue.token.contract,\n ) &&\n currValue.token.contract === currValue?.token?.collection?.id\n ) {\n if (!acc[currValue.token.chainId]) {\n acc[currValue.token.chainId] = [];\n }\n acc[currValue.token.chainId].push(currValue.token.contract);\n }\n return acc;\n },\n {} as Record<string, string[]>,\n );\n\n if (\n Object.values(collections).some((contracts) => contracts.length > 0)\n ) {\n // Call API to retrieve collections infos\n // The api accept a max of 20 contracts\n const collectionsResponses = await Promise.all(\n Object.entries(collections).map(([chainId, contracts]) =>\n reduceInBatchesSerially({\n values: contracts,\n batchSize: MAX_GET_COLLECTION_BATCH_SIZE,\n eachBatch: async (allResponses, batch) => {\n const params = new URLSearchParams(\n batch.map((s) => ['contract', s]),\n );\n params.append('chainId', chainId);\n const collectionResponseForBatch =\n await fetchWithErrorHandling({\n url: `${\n NFT_API_BASE_URL as string\n }/collections?${params.toString()}`,\n options: {\n headers: {\n Version: NFT_API_VERSION,\n },\n },\n timeout: NFT_API_TIMEOUT,\n });\n\n return {\n ...allResponses,\n ...collectionResponseForBatch,\n };\n },\n initialResult: {},\n }),\n ),\n );\n // create a new collectionsResponse that is of type GetCollectionsResponse and merges the results of collectionsResponses\n const collectionResponse: GetCollectionsResponse = {\n collections: [],\n };\n\n collectionsResponses.forEach((singleCollectionResponse) => {\n if (\n (singleCollectionResponse as GetCollectionsResponse)?.collections\n ) {\n collectionResponse?.collections.push(\n ...(singleCollectionResponse as GetCollectionsResponse)\n .collections,\n );\n }\n });\n\n // Add collections response fields to newnfts\n if (collectionResponse.collections?.length) {\n apiNfts.forEach((singleNFT) => {\n const found = collectionResponse.collections.find(\n (elm) =>\n elm.id?.toLowerCase() ===\n singleNFT.token.contract.toLowerCase() &&\n singleNFT.token.chainId === elm.chainId,\n );\n if (found) {\n singleNFT.token = {\n ...singleNFT.token,\n collection: {\n ...(singleNFT.token.collection ?? {}),\n openseaVerificationStatus: found?.openseaVerificationStatus,\n contractDeployedAt: found.contractDeployedAt,\n creator: found?.creator,\n ownerCount: found.ownerCount,\n topBid: found.topBid,\n },\n };\n }\n });\n }\n }\n\n // Proceed to add NFTs\n const addNftPromises = apiNfts.map(async (nft) => {\n const {\n tokenId,\n contract,\n kind,\n image: imageUrl,\n imageSmall: imageThumbnailUrl,\n metadata,\n name,\n description,\n attributes,\n topBid,\n lastSale,\n rarityRank,\n rarityScore,\n collection,\n chainId,\n } = nft.token;\n\n // Use a fallback if metadata is null\n const { imageOriginal: imageOriginalUrl } = metadata || {};\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(contract) &&\n c.tokenId === tokenId\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 description && { description },\n imageUrl && { image: imageUrl },\n imageThumbnailUrl && { imageThumbnail: imageThumbnailUrl },\n imageOriginalUrl && { imageOriginal: imageOriginalUrl },\n kind && { standard: kind.toUpperCase() },\n lastSale && { lastSale },\n attributes && { attributes },\n topBid && { topBid },\n rarityRank && { rarityRank },\n rarityScore && { rarityScore },\n collection && { collection },\n chainId && { chainId },\n );\n const networkClientId = this.messagingSystem.call(\n 'NetworkController:findNetworkClientIdByChainId',\n toHex(chainId),\n );\n await this.#addNft(contract, tokenId, networkClientId, {\n nftMetadata,\n userAddress,\n source: Source.Detected,\n });\n }\n });\n await Promise.all(addNftPromises);\n } while ((next = resultNftApi.continuation));\n updateSucceeded();\n } catch (error) {\n updateFailed(error);\n throw error;\n } finally {\n delete this.#inProcessNftFetchingUpdates[updateKey];\n }\n }\n}\n\nexport default NftDetectionController;\n"]}
|
|
1
|
+
{"version":3,"file":"NftDetectionController.mjs","sourceRoot":"","sources":["../src/NftDetectionController.ts"],"names":[],"mappings":";;;;;;;;;;;;AAGA,OAAO,EAAE,cAAc,EAAE,kCAAkC;AAC3D,OAAO,EACL,oBAAoB,EACpB,OAAO,EACP,gBAAgB,EAChB,eAAe,EACf,mBAAmB,EACnB,WAAW,EACX,KAAK,EACN,mCAAmC;AAYpC,OAAO,EAAE,qBAAqB,EAAY,wBAAwB;AAElE,OAAO,EAAE,MAAM,EAAE,wBAAoB;AAQrC,MAAM,cAAc,GAAG,wBAAwB,CAAC;AAwBhD;;GAEG;AACH,MAAM,6BAA6B,GAAa,IAAI,GAAG,CAAC;IACtD,4FAA4F;IAC5F,qEAAqE;IACrE,KAAK;IACL,QAAQ;IACR,OAAO,EAAE,MAAM;CA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C,SAAS,CAAC,CAAC;SACrD;IACH,CAAC;CACF;gZAvLqC,EAAE,eAAe,EAAoB;IACvE,IAAI,CAAC,eAAe,KAAK,uBAAA,IAAI,wCAAU,EAAE;QACvC,uBAAA,IAAI,oCAAa,CAAC,eAAe,MAAA,CAAC;KACnC;AACH,CAAC,2FAEe,EACd,QAAQ,EACR,OAAO,EACP,IAAI,GAKL;IACC,4FAA4F;IAC5F,MAAM,cAAc,GAAG,QAAQ,CAAC,IAAI,CAAC,YAAY,CAAC,CAAC;IACnD,OAAO,GACL,gBACF,UAAU,OAAO,oBAAoB,cAAc,6CAA6C,IAAI,IAAI,EAAE,EAAE,CAAC;AAC/G,CAAC,yCAED,KAAK,+CACH,OAAe,EACf,QAAe,EACf,MAA0B;IAE1B,gCAAgC;IAChC,MAAM,iBAAiB,GAAG,QAAQ,CAAC,GAAG,CAAC,CAAC,OAAO,EAAE,EAAE,CACjD,mBAAmB,CAAC,OAAO,CAAC,CAAC,QAAQ,EAAE,CACxC,CAAC;IACF,MAAM,GAAG,GAAG,uBAAA,IAAI,iFAAgB,MAApB,IAAI,EAAiB;QAC/B,QAAQ,EAAE,iBAAiB;QAC3B,OAAO;QACP,IAAI,EAAE,MAAM;KACb,CAAC,CAAC;IACH,MAAM,cAAc,GAAsB,MAAM,WAAW,CAAC,GAAG,EAAE;QAC/D,OAAO,EAAE;YACP,OAAO,EAAE,eAAe;SACzB;KACF,CAAC,CAAC;IACH,OAAO,cAAc,CAAC;AACxB,CAAC;AA+IH,eAAe,sBAAsB,CAAC","sourcesContent":["import type { AccountsControllerGetSelectedAccountAction } from '@metamask/accounts-controller';\nimport type { AddApprovalRequest } from '@metamask/approval-controller';\nimport type { RestrictedMessenger } from '@metamask/base-controller';\nimport { BaseController } from '@metamask/base-controller';\nimport {\n toChecksumHexAddress,\n ChainId,\n NFT_API_BASE_URL,\n NFT_API_VERSION,\n convertHexToDecimal,\n handleFetch,\n toHex,\n} from '@metamask/controller-utils';\nimport type {\n NetworkClient,\n NetworkControllerGetNetworkClientByIdAction,\n NetworkControllerStateChangeEvent,\n NetworkControllerGetStateAction,\n} from '@metamask/network-controller';\nimport type {\n PreferencesControllerGetStateAction,\n PreferencesControllerStateChangeEvent,\n PreferencesState,\n} from '@metamask/preferences-controller';\nimport { createDeferredPromise, type Hex } from '@metamask/utils';\n\nimport { Source } from './constants';\nimport {\n type NftController,\n type NftControllerState,\n type NftMetadata,\n} from './NftController';\nimport type { NetworkControllerFindNetworkClientIdByChainIdAction } from '../../network-controller/src/NetworkController';\n\nconst controllerName = 'NftDetectionController';\n\nexport type NFTDetectionControllerState = Record<never, never>;\n\nexport type AllowedActions =\n | AddApprovalRequest\n | NetworkControllerGetStateAction\n | NetworkControllerGetNetworkClientByIdAction\n | PreferencesControllerGetStateAction\n | AccountsControllerGetSelectedAccountAction\n | NetworkControllerFindNetworkClientIdByChainIdAction;\n\nexport type AllowedEvents =\n | PreferencesControllerStateChangeEvent\n | NetworkControllerStateChangeEvent;\n\nexport type NftDetectionControllerMessenger = RestrictedMessenger<\n typeof controllerName,\n AllowedActions,\n AllowedEvents,\n AllowedActions['type'],\n AllowedEvents['type']\n>;\n\n/**\n * A set of supported networks for NFT detection.\n */\nconst supportedNftDetectionNetworks: Set<Hex> = new Set([\n // TODO: We should consider passing this constant from the NftDetectionController contructor\n // to reduce the complexity to add further network into this constant\n '0x1', // Mainnet\n '0xe708', // Linea Mainnet\n '0x531', // Sei\n]);\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 */\nexport type ApiNft = {\n // TODO: Either fix this lint violation or explain why it's necessary to ignore.\n // eslint-disable-next-line @typescript-eslint/naming-convention\n token_id: string;\n // TODO: Either fix this lint violation or explain why it's necessary to ignore.\n // eslint-disable-next-line @typescript-eslint/naming-convention\n num_sales: number | null;\n // TODO: Either fix this lint violation or explain why it's necessary to ignore.\n // eslint-disable-next-line @typescript-eslint/naming-convention\n background_color: string | null;\n // TODO: Either fix this lint violation or explain why it's necessary to ignore.\n // eslint-disable-next-line @typescript-eslint/naming-convention\n image_url: string | null;\n // TODO: Either fix this lint violation or explain why it's necessary to ignore.\n // eslint-disable-next-line @typescript-eslint/naming-convention\n image_preview_url: string | null;\n // TODO: Either fix this lint violation or explain why it's necessary to ignore.\n // eslint-disable-next-line @typescript-eslint/naming-convention\n image_thumbnail_url: string | null;\n // TODO: Either fix this lint violation or explain why it's necessary to ignore.\n // eslint-disable-next-line @typescript-eslint/naming-convention\n image_original_url: string | null;\n // TODO: Either fix this lint violation or explain why it's necessary to ignore.\n // eslint-disable-next-line @typescript-eslint/naming-convention\n animation_url: string | null;\n // TODO: Either fix this lint violation or explain why it's necessary to ignore.\n // eslint-disable-next-line @typescript-eslint/naming-convention\n animation_original_url: string | null;\n name: string | null;\n description: string | null;\n // TODO: Either fix this lint violation or explain why it's necessary to ignore.\n // eslint-disable-next-line @typescript-eslint/naming-convention\n external_link: string | null;\n // TODO: Either fix this lint violation or explain why it's necessary to ignore.\n // eslint-disable-next-line @typescript-eslint/naming-convention\n asset_contract: ApiNftContract;\n creator: ApiNftCreator;\n // TODO: Either fix this lint violation or explain why it's necessary to ignore.\n // eslint-disable-next-line @typescript-eslint/naming-convention\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 */\nexport type ApiNftContract = {\n address: string;\n // TODO: Either fix this lint violation or explain why it's necessary to ignore.\n // eslint-disable-next-line @typescript-eslint/naming-convention\n asset_contract_type: string | null;\n // TODO: Either fix this lint violation or explain why it's necessary to ignore.\n // eslint-disable-next-line @typescript-eslint/naming-convention\n created_date: string | null;\n // TODO: Either fix this lint violation or explain why it's necessary to ignore.\n // eslint-disable-next-line @typescript-eslint/naming-convention\n schema_name: string | null;\n symbol: string | null;\n // TODO: Either fix this lint violation or explain why it's necessary to ignore.\n // eslint-disable-next-line @typescript-eslint/naming-convention\n total_supply: string | null;\n description: string | null;\n // TODO: Either fix this lint violation or explain why it's necessary to ignore.\n // eslint-disable-next-line @typescript-eslint/naming-convention\n external_link: string | null;\n collection: {\n name: string | null;\n // TODO: Either fix this lint violation or explain why it's necessary to ignore.\n // eslint-disable-next-line @typescript-eslint/naming-convention\n image_url?: string | null;\n tokenCount?: 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 */\nexport type ApiNftLastSale = {\n // TODO: Either fix this lint violation or explain why it's necessary to ignore.\n // eslint-disable-next-line @typescript-eslint/naming-convention\n event_timestamp: string;\n // TODO: Either fix this lint violation or explain why it's necessary to ignore.\n // eslint-disable-next-line @typescript-eslint/naming-convention\n total_price: string;\n // TODO: Either fix this lint violation or explain why it's necessary to ignore.\n // eslint-disable-next-line @typescript-eslint/naming-convention\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 */\nexport type ApiNftCreator = {\n user: { username: string };\n // TODO: Either fix this lint violation or explain why it's necessary to ignore.\n // eslint-disable-next-line @typescript-eslint/naming-convention\n profile_img_url: string;\n address: string;\n};\n\nexport type ReservoirResponse = {\n tokens: TokensResponse[];\n continuation?: string;\n};\n\nexport type TokensResponse = {\n token: TokenResponse;\n ownership: Ownership;\n market?: Market;\n blockaidResult?: Blockaid;\n};\n\nexport enum BlockaidResultType {\n Benign = 'Benign',\n Spam = 'Spam',\n Warning = 'Warning',\n Malicious = 'Malicious',\n}\n\nexport type Blockaid = {\n contract: string;\n chainId: number;\n // TODO: Either fix this lint violation or explain why it's necessary to ignore.\n // eslint-disable-next-line @typescript-eslint/naming-convention\n result_type: BlockaidResultType;\n // TODO: Either fix this lint violation or explain why it's necessary to ignore.\n // eslint-disable-next-line @typescript-eslint/naming-convention\n malicious_score: string;\n // TODO: Either fix this lint violation or explain why it's necessary to ignore.\n // eslint-disable-next-line @typescript-eslint/naming-convention\n attack_types: object;\n};\n\nexport type Market = {\n floorAsk?: FloorAsk;\n topBid?: TopBid;\n};\n\nexport type TokenResponse = {\n chainId: number;\n contract: string;\n tokenId: string;\n kind?: string;\n name?: string;\n image?: string;\n imageSmall?: string;\n imageLarge?: string;\n metadata?: Metadata;\n description?: string;\n supply?: number;\n remainingSupply?: number;\n rarityScore?: number;\n rarity?: number;\n rarityRank?: number;\n media?: string;\n isFlagged?: boolean;\n isSpam?: boolean;\n isNsfw?: boolean;\n metadataDisabled?: boolean;\n lastFlagUpdate?: string;\n lastFlagChange?: string;\n collection?: Collection;\n lastSale?: LastSale;\n topBid?: TopBid;\n lastAppraisalValue?: number;\n attributes?: Attributes[];\n};\n\nexport type TopBid = {\n id?: string;\n price?: Price;\n source?: {\n id?: string;\n domain?: string;\n name?: string;\n icon?: string;\n url?: string;\n };\n};\n\nexport type LastSale = {\n saleId?: string;\n token?: {\n contract?: string;\n tokenId?: string;\n name?: string;\n image?: string;\n collection?: {\n id?: string;\n name?: string;\n };\n };\n orderSource?: string;\n orderSide?: 'ask' | 'bid';\n orderKind?: string;\n orderId?: string;\n from?: string;\n to?: string;\n amount?: string;\n fillSource?: string;\n block?: number;\n txHash?: string;\n logIndex?: number;\n batchIndex?: number;\n timestamp?: number;\n price?: Price;\n washTradingScore?: number;\n royaltyFeeBps?: number;\n marketplaceFeeBps?: number;\n paidFullRoyalty?: boolean;\n feeBreakdown?: FeeBreakdown[];\n isDeleted?: boolean;\n createdAt?: string;\n updatedAt?: string;\n};\n\nexport type FeeBreakdown = {\n kind?: string;\n bps?: number;\n recipient?: string;\n source?: string;\n rawAmount?: string;\n};\n\nexport type Attributes = {\n key?: string;\n kind?: string;\n value: string;\n tokenCount?: number;\n onSaleCount?: number;\n floorAskPrice?: Price | null;\n topBidValue?: number | null;\n createdAt?: string;\n};\n\nexport type GetCollectionsResponse = {\n collections: CollectionResponse[];\n};\n\nexport type CollectionResponse = {\n id?: string;\n chainId?: number;\n openseaVerificationStatus?: string;\n contractDeployedAt?: string;\n creator?: string;\n ownerCount?: string;\n topBid?: TopBid & {\n sourceDomain?: string;\n };\n};\n\nexport type FloorAskCollection = {\n id?: string;\n price?: Price;\n maker?: string;\n kind?: string;\n validFrom?: number;\n validUntil?: number;\n source?: SourceCollection;\n rawData?: Metadata;\n isNativeOffChainCancellable?: boolean;\n};\n\nexport type SourceCollection = {\n id: string;\n domain: string;\n name: string;\n icon: string;\n url: string;\n};\n\nexport type TokenCollection = {\n id?: string;\n name?: string;\n slug?: string;\n symbol?: string;\n imageUrl?: string;\n image?: string;\n isSpam?: boolean;\n isNsfw?: boolean;\n creator?: string;\n tokenCount?: string;\n metadataDisabled?: boolean;\n openseaVerificationStatus?: string;\n floorAskPrice?: Price;\n royaltiesBps?: number;\n royalties?: Royalties[];\n floorAsk?: FloorAskCollection;\n};\n\nexport type Collection = TokenCollection & CollectionResponse;\n\nexport type Royalties = {\n bps?: number;\n recipient?: string;\n};\n\nexport type Ownership = {\n tokenCount?: string;\n onSaleCount?: string;\n floorAsk?: FloorAsk;\n acquiredAt?: string;\n};\n\nexport type FloorAsk = {\n id?: string;\n price?: Price;\n maker?: string;\n kind?: string;\n validFrom?: number;\n validUntil?: number;\n source?: Source;\n rawData?: Metadata;\n isNativeOffChainCancellable?: boolean;\n};\n\nexport type Price = {\n currency?: {\n contract?: string;\n name?: string;\n symbol?: string;\n decimals?: number;\n chainId?: number;\n };\n amount?: {\n raw?: string;\n decimal?: number;\n usd?: number;\n native?: number;\n };\n netAmount?: {\n raw?: string;\n decimal?: number;\n usd?: number;\n native?: number;\n };\n};\n\nexport type Metadata = {\n imageOriginal?: string;\n tokenURI?: string;\n};\n\n/**\n * Controller that passively detects nfts for a user address\n */\nexport class NftDetectionController extends BaseController<\n typeof controllerName,\n NFTDetectionControllerState,\n NftDetectionControllerMessenger\n> {\n #disabled: boolean;\n\n readonly #addNft: NftController['addNft'];\n\n readonly #getNftState: () => NftControllerState;\n\n #inProcessNftFetchingUpdates: Record<`${string}:${string}`, Promise<void>>;\n\n /**\n * The controller options\n *\n * @param options - The controller options.\n * @param options.messenger - A reference to the messaging system.\n * @param options.disabled - Represents previous value of useNftDetection. Used to detect changes of useNftDetection. Default value is true.\n * @param options.addNft - Add an NFT.\n * @param options.getNftState - Gets the current state of the Assets controller.\n */\n constructor({\n messenger,\n disabled = false,\n addNft,\n getNftState,\n }: {\n messenger: NftDetectionControllerMessenger;\n disabled: boolean;\n addNft: NftController['addNft'];\n getNftState: () => NftControllerState;\n }) {\n super({\n name: controllerName,\n messenger,\n metadata: {},\n state: {},\n });\n this.#disabled = disabled;\n this.#inProcessNftFetchingUpdates = {};\n\n this.#getNftState = getNftState;\n this.#addNft = addNft;\n\n this.messagingSystem.subscribe(\n 'PreferencesController:stateChange',\n this.#onPreferencesControllerStateChange.bind(this),\n );\n }\n\n /**\n * Checks whether network is mainnet or not.\n *\n * @returns Whether current network is mainnet.\n */\n isMainnet(): boolean {\n const { selectedNetworkClientId } = this.messagingSystem.call(\n 'NetworkController:getState',\n );\n const {\n configuration: { chainId },\n } = this.messagingSystem.call(\n 'NetworkController:getNetworkClientById',\n selectedNetworkClientId,\n );\n return chainId === ChainId.mainnet;\n }\n\n isMainnetByNetworkClientId(networkClient: NetworkClient): boolean {\n return networkClient.configuration.chainId === ChainId.mainnet;\n }\n\n /**\n * Handles the state change of the preference controller.\n * @param preferencesState - The new state of the preference controller.\n * @param preferencesState.useNftDetection - Boolean indicating user preference on NFT detection.\n */\n #onPreferencesControllerStateChange({ useNftDetection }: PreferencesState) {\n if (!useNftDetection !== this.#disabled) {\n this.#disabled = !useNftDetection;\n }\n }\n\n #getOwnerNftApi({\n chainIds,\n address,\n next,\n }: {\n chainIds: string[];\n address: string;\n next?: string;\n }) {\n // from chainIds construct a string of chainIds that can be used like chainIds=1&chainIds=56\n const chainIdsString = chainIds.join('&chainIds=');\n return `${\n NFT_API_BASE_URL as string\n }/users/${address}/tokens?chainIds=${chainIdsString}&limit=50&includeTopBid=true&continuation=${next ?? ''}`;\n }\n\n async #getOwnerNfts(\n address: string,\n chainIds: Hex[],\n cursor: string | undefined,\n ) {\n // Convert hex chainId to number\n const convertedChainIds = chainIds.map((chainId) =>\n convertHexToDecimal(chainId).toString(),\n );\n const url = this.#getOwnerNftApi({\n chainIds: convertedChainIds,\n address,\n next: cursor,\n });\n const nftApiResponse: ReservoirResponse = await handleFetch(url, {\n headers: {\n Version: NFT_API_VERSION,\n },\n });\n return nftApiResponse;\n }\n\n /**\n * Triggers asset ERC721 token auto detection on mainnet. Any newly detected NFTs are\n * added.\n *\n * @param chainIds - The chain IDs to detect NFTs on.\n * @param options - Options bag.\n * @param options.userAddress - The address to detect NFTs for.\n */\n async detectNfts(chainIds: Hex[], options?: { userAddress?: string }) {\n const userAddress =\n options?.userAddress ??\n this.messagingSystem.call('AccountsController:getSelectedAccount')\n .address;\n\n // filter out unsupported chainIds\n const supportedChainIds = chainIds.filter((chainId) =>\n supportedNftDetectionNetworks.has(chainId),\n );\n /* istanbul ignore if */\n if (supportedChainIds.length === 0 || this.#disabled) {\n return;\n }\n /* istanbul ignore else */\n if (!userAddress) {\n return;\n }\n // create a string of all chainIds\n const chainIdsString = chainIds.join(',');\n\n const updateKey: `${string}:${string}` = `${chainIdsString}:${userAddress}`;\n if (updateKey in this.#inProcessNftFetchingUpdates) {\n // This prevents redundant updates\n // This promise is resolved after the in-progress update has finished,\n // and state has been updated.\n await this.#inProcessNftFetchingUpdates[updateKey];\n return;\n }\n\n const {\n promise: inProgressUpdate,\n resolve: updateSucceeded,\n reject: updateFailed,\n } = createDeferredPromise({ suppressUnhandledRejection: true });\n this.#inProcessNftFetchingUpdates[updateKey] = inProgressUpdate;\n\n let next;\n let apiNfts: TokensResponse[] = [];\n let resultNftApi: ReservoirResponse;\n try {\n do {\n resultNftApi = await this.#getOwnerNfts(\n userAddress,\n supportedChainIds,\n next,\n );\n apiNfts = resultNftApi.tokens.filter(\n (elm) =>\n elm.token.isSpam === false &&\n (elm.blockaidResult?.result_type\n ? elm.blockaidResult?.result_type === BlockaidResultType.Benign\n : true),\n );\n\n // Proceed to add NFTs\n const addNftPromises = apiNfts.map(async (nft) => {\n const {\n tokenId,\n contract,\n kind,\n image: imageUrl,\n imageSmall: imageThumbnailUrl,\n metadata,\n name,\n description,\n attributes,\n topBid,\n lastSale,\n rarityRank,\n rarityScore,\n collection,\n chainId,\n } = nft.token;\n\n // Use a fallback if metadata is null\n const { imageOriginal: imageOriginalUrl } = metadata || {};\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(contract) &&\n c.tokenId === tokenId\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 description && { description },\n imageUrl && { image: imageUrl },\n imageThumbnailUrl && { imageThumbnail: imageThumbnailUrl },\n imageOriginalUrl && { imageOriginal: imageOriginalUrl },\n kind && { standard: kind.toUpperCase() },\n lastSale && { lastSale },\n attributes && { attributes },\n topBid && { topBid },\n rarityRank && { rarityRank },\n rarityScore && { rarityScore },\n collection && { collection },\n chainId && { chainId },\n );\n const networkClientId = this.messagingSystem.call(\n 'NetworkController:findNetworkClientIdByChainId',\n toHex(chainId),\n );\n await this.#addNft(contract, tokenId, networkClientId, {\n nftMetadata,\n userAddress,\n source: Source.Detected,\n });\n }\n });\n await Promise.all(addNftPromises);\n } while ((next = resultNftApi.continuation));\n updateSucceeded();\n } catch (error) {\n updateFailed(error);\n throw error;\n } finally {\n delete this.#inProcessNftFetchingUpdates[updateKey];\n }\n }\n}\n\nexport default NftDetectionController;\n"]}
|
|
@@ -10,9 +10,9 @@ var __classPrivateFieldGet = (this && this.__classPrivateFieldGet) || function (
|
|
|
10
10
|
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");
|
|
11
11
|
return kind === "m" ? f : kind === "a" ? f.call(receiver) : f ? f.value : state.get(receiver);
|
|
12
12
|
};
|
|
13
|
-
var _TokenBalancesController_instances, _TokenBalancesController_platform, _TokenBalancesController_queryAllAccounts, _TokenBalancesController_accountsApiChainIds, _TokenBalancesController_balanceFetchers, _TokenBalancesController_allTokens, _TokenBalancesController_detectedTokens, _TokenBalancesController_defaultInterval, _TokenBalancesController_chainPollingConfig, _TokenBalancesController_intervalPollingTimers, _TokenBalancesController_isControllerPollingActive, _TokenBalancesController_requestedChainIds, _TokenBalancesController_chainIdsWithTokens, _TokenBalancesController_getProvider, _TokenBalancesController_getNetworkClient, _TokenBalancesController_createAccountsApiFetcher, _TokenBalancesController_startIntervalGroupPolling, _TokenBalancesController_startPollingForInterval, _TokenBalancesController_setPollingTimer, _TokenBalancesController_onTokensChanged, _TokenBalancesController_onNetworkChanged, _TokenBalancesController_onAccountRemoved;
|
|
13
|
+
var _TokenBalancesController_instances, _TokenBalancesController_platform, _TokenBalancesController_queryAllAccounts, _TokenBalancesController_accountsApiChainIds, _TokenBalancesController_balanceFetchers, _TokenBalancesController_allTokens, _TokenBalancesController_detectedTokens, _TokenBalancesController_allIgnoredTokens, _TokenBalancesController_defaultInterval, _TokenBalancesController_websocketActivePollingInterval, _TokenBalancesController_chainPollingConfig, _TokenBalancesController_intervalPollingTimers, _TokenBalancesController_isControllerPollingActive, _TokenBalancesController_requestedChainIds, _TokenBalancesController_statusChangeDebouncer, _TokenBalancesController_chainIdsWithTokens, _TokenBalancesController_getProvider, _TokenBalancesController_getNetworkClient, _TokenBalancesController_createAccountsApiFetcher, _TokenBalancesController_startIntervalGroupPolling, _TokenBalancesController_startPollingForInterval, _TokenBalancesController_setPollingTimer, _TokenBalancesController_isTokenTracked, _TokenBalancesController_onTokensChanged, _TokenBalancesController_onNetworkChanged, _TokenBalancesController_onAccountRemoved, _TokenBalancesController_prepareBalanceUpdates, _TokenBalancesController_onAccountActivityBalanceUpdate, _TokenBalancesController_onAccountActivityStatusChanged, _TokenBalancesController_processAccumulatedStatusChanges;
|
|
14
14
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
15
|
-
exports.TokenBalancesController = void 0;
|
|
15
|
+
exports.TokenBalancesController = exports.parseAssetType = exports.caipChainIdToHex = void 0;
|
|
16
16
|
const providers_1 = require("@ethersproject/providers");
|
|
17
17
|
const controller_utils_1 = require("@metamask/controller-utils");
|
|
18
18
|
const polling_controller_1 = require("@metamask/polling-controller");
|
|
@@ -23,7 +23,8 @@ const AssetsContractController_1 = require("./AssetsContractController.cjs");
|
|
|
23
23
|
const api_balance_fetcher_1 = require("./multi-chain-accounts-service/api-balance-fetcher.cjs");
|
|
24
24
|
const rpc_balance_fetcher_1 = require("./rpc-service/rpc-balance-fetcher.cjs");
|
|
25
25
|
const CONTROLLER = 'TokenBalancesController';
|
|
26
|
-
const DEFAULT_INTERVAL_MS =
|
|
26
|
+
const DEFAULT_INTERVAL_MS = 30000; // 30 seconds
|
|
27
|
+
const DEFAULT_WEBSOCKET_ACTIVE_POLLING_INTERVAL_MS = 300000; // 5 minutes
|
|
27
28
|
const metadata = {
|
|
28
29
|
tokenBalances: {
|
|
29
30
|
includeInStateLogs: false,
|
|
@@ -38,11 +39,52 @@ const metadata = {
|
|
|
38
39
|
const draft = (base, fn) => (0, immer_1.produce)(base, fn);
|
|
39
40
|
const ZERO_ADDRESS = '0x0000000000000000000000000000000000000000';
|
|
40
41
|
const checksum = (addr) => (0, controller_utils_1.toChecksumHexAddress)(addr);
|
|
42
|
+
/**
|
|
43
|
+
* Convert CAIP chain ID or hex chain ID to hex chain ID
|
|
44
|
+
* Handles both CAIP-2 format (e.g., "eip155:1") and hex format (e.g., "0x1")
|
|
45
|
+
*
|
|
46
|
+
* @param chainId - CAIP chain ID (e.g., "eip155:1") or hex chain ID (e.g., "0x1")
|
|
47
|
+
* @returns Hex chain ID (e.g., "0x1")
|
|
48
|
+
* @throws {Error} If chainId is neither a valid CAIP-2 chain ID nor a hex string
|
|
49
|
+
*/
|
|
50
|
+
const caipChainIdToHex = (chainId) => {
|
|
51
|
+
if ((0, utils_1.isStrictHexString)(chainId)) {
|
|
52
|
+
return chainId;
|
|
53
|
+
}
|
|
54
|
+
if ((0, utils_1.isCaipChainId)(chainId)) {
|
|
55
|
+
return (0, controller_utils_1.toHex)((0, utils_1.parseCaipChainId)(chainId).reference);
|
|
56
|
+
}
|
|
57
|
+
throw new Error('caipChainIdToHex - Failed to provide CAIP-2 or Hex chainId');
|
|
58
|
+
};
|
|
59
|
+
exports.caipChainIdToHex = caipChainIdToHex;
|
|
60
|
+
/**
|
|
61
|
+
* Extract token address from asset type
|
|
62
|
+
* Returns tuple of [tokenAddress, isNativeToken] or null if invalid
|
|
63
|
+
*
|
|
64
|
+
* @param assetType - Asset type string (e.g., 'eip155:1/erc20:0x...' or 'eip155:1/slip44:60')
|
|
65
|
+
* @returns Tuple of [tokenAddress, isNativeToken] or null if invalid
|
|
66
|
+
*/
|
|
67
|
+
const parseAssetType = (assetType) => {
|
|
68
|
+
if (!(0, utils_1.isCaipAssetType)(assetType)) {
|
|
69
|
+
return null;
|
|
70
|
+
}
|
|
71
|
+
const parsed = (0, utils_1.parseCaipAssetType)(assetType);
|
|
72
|
+
// ERC20 token (e.g., "eip155:1/erc20:0x...")
|
|
73
|
+
if (parsed.assetNamespace === 'erc20') {
|
|
74
|
+
return [parsed.assetReference, false];
|
|
75
|
+
}
|
|
76
|
+
// Native token (e.g., "eip155:1/slip44:60")
|
|
77
|
+
if (parsed.assetNamespace === 'slip44') {
|
|
78
|
+
return [ZERO_ADDRESS, true];
|
|
79
|
+
}
|
|
80
|
+
return null;
|
|
81
|
+
};
|
|
82
|
+
exports.parseAssetType = parseAssetType;
|
|
41
83
|
// endregion
|
|
42
84
|
// ────────────────────────────────────────────────────────────────────────────
|
|
43
85
|
// region: Main controller
|
|
44
86
|
class TokenBalancesController extends (0, polling_controller_1.StaticIntervalPollingController)() {
|
|
45
|
-
constructor({ messenger, interval = DEFAULT_INTERVAL_MS, chainPollingIntervals = {}, state = {}, queryMultipleAccounts = true, accountsApiChainIds = () => [], allowExternalServices = () => true, platform, }) {
|
|
87
|
+
constructor({ messenger, interval = DEFAULT_INTERVAL_MS, websocketActivePollingInterval = DEFAULT_WEBSOCKET_ACTIVE_POLLING_INTERVAL_MS, chainPollingIntervals = {}, state = {}, queryMultipleAccounts = true, accountsApiChainIds = () => [], allowExternalServices = () => true, platform, }) {
|
|
46
88
|
super({
|
|
47
89
|
name: CONTROLLER,
|
|
48
90
|
messenger,
|
|
@@ -56,8 +98,11 @@ class TokenBalancesController extends (0, polling_controller_1.StaticIntervalPol
|
|
|
56
98
|
_TokenBalancesController_balanceFetchers.set(this, void 0);
|
|
57
99
|
_TokenBalancesController_allTokens.set(this, {});
|
|
58
100
|
_TokenBalancesController_detectedTokens.set(this, {});
|
|
101
|
+
_TokenBalancesController_allIgnoredTokens.set(this, {});
|
|
59
102
|
/** Default polling interval for chains without specific configuration */
|
|
60
103
|
_TokenBalancesController_defaultInterval.set(this, void 0);
|
|
104
|
+
/** Polling interval when WebSocket is active and providing real-time updates */
|
|
105
|
+
_TokenBalancesController_websocketActivePollingInterval.set(this, void 0);
|
|
61
106
|
/** Per-chain polling configuration */
|
|
62
107
|
_TokenBalancesController_chainPollingConfig.set(this, void 0);
|
|
63
108
|
/** Active polling timers grouped by interval */
|
|
@@ -66,6 +111,11 @@ class TokenBalancesController extends (0, polling_controller_1.StaticIntervalPol
|
|
|
66
111
|
_TokenBalancesController_isControllerPollingActive.set(this, false);
|
|
67
112
|
/** Store original chainIds from startPolling to preserve intent */
|
|
68
113
|
_TokenBalancesController_requestedChainIds.set(this, []);
|
|
114
|
+
/** Debouncing for rapid status changes to prevent excessive HTTP calls */
|
|
115
|
+
_TokenBalancesController_statusChangeDebouncer.set(this, {
|
|
116
|
+
timer: null,
|
|
117
|
+
pendingChanges: new Map(),
|
|
118
|
+
});
|
|
69
119
|
_TokenBalancesController_getProvider.set(this, (chainId) => {
|
|
70
120
|
const { networkConfigurationsByChainId } = this.messagingSystem.call('NetworkController:getState');
|
|
71
121
|
const cfg = networkConfigurationsByChainId[chainId];
|
|
@@ -167,6 +217,7 @@ class TokenBalancesController extends (0, polling_controller_1.StaticIntervalPol
|
|
|
167
217
|
});
|
|
168
218
|
__classPrivateFieldSet(this, _TokenBalancesController_allTokens, state.allTokens, "f");
|
|
169
219
|
__classPrivateFieldSet(this, _TokenBalancesController_detectedTokens, state.allDetectedTokens, "f");
|
|
220
|
+
__classPrivateFieldSet(this, _TokenBalancesController_allIgnoredTokens, state.allIgnoredTokens, "f");
|
|
170
221
|
// Only update balances for chains that still have tokens (and only if we haven't already updated state)
|
|
171
222
|
if (changed.length && !hasChanges) {
|
|
172
223
|
this.updateBalances({ chainIds: changed }).catch((error) => {
|
|
@@ -210,10 +261,85 @@ class TokenBalancesController extends (0, polling_controller_1.StaticIntervalPol
|
|
|
210
261
|
delete s.tokenBalances[addr];
|
|
211
262
|
});
|
|
212
263
|
});
|
|
264
|
+
// ────────────────────────────────────────────────────────────────────────────
|
|
265
|
+
// AccountActivityService event handlers
|
|
266
|
+
/**
|
|
267
|
+
* Handle real-time balance updates from AccountActivityService
|
|
268
|
+
* Processes balance updates and updates the token balance state
|
|
269
|
+
* If any balance update has an error, triggers fallback polling for the chain
|
|
270
|
+
*
|
|
271
|
+
* @param options0 - Balance update parameters
|
|
272
|
+
* @param options0.address - Account address
|
|
273
|
+
* @param options0.chain - CAIP chain identifier
|
|
274
|
+
* @param options0.updates - Array of balance updates for the account
|
|
275
|
+
*/
|
|
276
|
+
_TokenBalancesController_onAccountActivityBalanceUpdate.set(this, async ({ address, chain, updates, }) => {
|
|
277
|
+
const chainId = (0, exports.caipChainIdToHex)(chain);
|
|
278
|
+
const account = checksum(address);
|
|
279
|
+
try {
|
|
280
|
+
// Process all balance updates at once
|
|
281
|
+
const { tokenBalances, newTokens, nativeBalanceUpdates } = __classPrivateFieldGet(this, _TokenBalancesController_instances, "m", _TokenBalancesController_prepareBalanceUpdates).call(this, updates, account, chainId);
|
|
282
|
+
// Update state once with all token balances
|
|
283
|
+
if (tokenBalances.length > 0) {
|
|
284
|
+
this.update((state) => {
|
|
285
|
+
var _a, _b;
|
|
286
|
+
// Initialize account and chain structure
|
|
287
|
+
(_a = state.tokenBalances)[account] ?? (_a[account] = {});
|
|
288
|
+
(_b = state.tokenBalances[account])[chainId] ?? (_b[chainId] = {});
|
|
289
|
+
// Apply all token balance updates
|
|
290
|
+
for (const { tokenAddress, balance } of tokenBalances) {
|
|
291
|
+
state.tokenBalances[account][chainId][tokenAddress] = balance;
|
|
292
|
+
}
|
|
293
|
+
});
|
|
294
|
+
}
|
|
295
|
+
// Update native balances in AccountTrackerController
|
|
296
|
+
if (nativeBalanceUpdates.length > 0) {
|
|
297
|
+
this.messagingSystem.call('AccountTrackerController:updateNativeBalances', nativeBalanceUpdates);
|
|
298
|
+
}
|
|
299
|
+
// Import any new tokens that were discovered (balance already updated from websocket)
|
|
300
|
+
if (newTokens.length > 0) {
|
|
301
|
+
await this.messagingSystem.call('TokenDetectionController:addDetectedTokensViaWs', {
|
|
302
|
+
tokensSlice: newTokens,
|
|
303
|
+
chainId: chainId,
|
|
304
|
+
});
|
|
305
|
+
}
|
|
306
|
+
}
|
|
307
|
+
catch (error) {
|
|
308
|
+
console.warn(`Error updating balances from AccountActivityService for chain ${chain}, account ${address}:`, error);
|
|
309
|
+
console.warn('Balance update data:', JSON.stringify(updates, null, 2));
|
|
310
|
+
// On error, trigger fallback polling
|
|
311
|
+
await this.updateBalances({ chainIds: [chainId] }).catch(() => {
|
|
312
|
+
// Silently handle polling errors
|
|
313
|
+
});
|
|
314
|
+
}
|
|
315
|
+
});
|
|
316
|
+
/**
|
|
317
|
+
* Handle status changes from AccountActivityService
|
|
318
|
+
* Uses aggressive debouncing to prevent excessive HTTP calls from rapid up/down changes
|
|
319
|
+
*
|
|
320
|
+
* @param options0 - Status change event data
|
|
321
|
+
* @param options0.chainIds - Array of chain identifiers
|
|
322
|
+
* @param options0.status - Connection status ('up' for connected, 'down' for disconnected)
|
|
323
|
+
*/
|
|
324
|
+
_TokenBalancesController_onAccountActivityStatusChanged.set(this, ({ chainIds, status, }) => {
|
|
325
|
+
// Update pending changes (latest status wins for each chain)
|
|
326
|
+
for (const chainId of chainIds) {
|
|
327
|
+
__classPrivateFieldGet(this, _TokenBalancesController_statusChangeDebouncer, "f").pendingChanges.set(chainId, status);
|
|
328
|
+
}
|
|
329
|
+
// Clear existing timer to extend debounce window
|
|
330
|
+
if (__classPrivateFieldGet(this, _TokenBalancesController_statusChangeDebouncer, "f").timer) {
|
|
331
|
+
clearTimeout(__classPrivateFieldGet(this, _TokenBalancesController_statusChangeDebouncer, "f").timer);
|
|
332
|
+
}
|
|
333
|
+
// Set new timer - only process changes after activity settles
|
|
334
|
+
__classPrivateFieldGet(this, _TokenBalancesController_statusChangeDebouncer, "f").timer = setTimeout(() => {
|
|
335
|
+
__classPrivateFieldGet(this, _TokenBalancesController_instances, "m", _TokenBalancesController_processAccumulatedStatusChanges).call(this);
|
|
336
|
+
}, 5000); // 5-second debounce window
|
|
337
|
+
});
|
|
213
338
|
__classPrivateFieldSet(this, _TokenBalancesController_platform, platform ?? 'extension', "f");
|
|
214
339
|
__classPrivateFieldSet(this, _TokenBalancesController_queryAllAccounts, queryMultipleAccounts, "f");
|
|
215
340
|
__classPrivateFieldSet(this, _TokenBalancesController_accountsApiChainIds, accountsApiChainIds, "f");
|
|
216
341
|
__classPrivateFieldSet(this, _TokenBalancesController_defaultInterval, interval, "f");
|
|
342
|
+
__classPrivateFieldSet(this, _TokenBalancesController_websocketActivePollingInterval, websocketActivePollingInterval, "f");
|
|
217
343
|
__classPrivateFieldSet(this, _TokenBalancesController_chainPollingConfig, { ...chainPollingIntervals }, "f");
|
|
218
344
|
// Strategy order: API first, then RPC fallback
|
|
219
345
|
__classPrivateFieldSet(this, _TokenBalancesController_balanceFetchers, [
|
|
@@ -227,9 +353,10 @@ class TokenBalancesController extends (0, polling_controller_1.StaticIntervalPol
|
|
|
227
353
|
], "f");
|
|
228
354
|
this.setIntervalLength(interval);
|
|
229
355
|
// initial token state & subscriptions
|
|
230
|
-
const { allTokens, allDetectedTokens } = this.messagingSystem.call('TokensController:getState');
|
|
356
|
+
const { allTokens, allDetectedTokens, allIgnoredTokens } = this.messagingSystem.call('TokensController:getState');
|
|
231
357
|
__classPrivateFieldSet(this, _TokenBalancesController_allTokens, allTokens, "f");
|
|
232
358
|
__classPrivateFieldSet(this, _TokenBalancesController_detectedTokens, allDetectedTokens, "f");
|
|
359
|
+
__classPrivateFieldSet(this, _TokenBalancesController_allIgnoredTokens, allIgnoredTokens, "f");
|
|
233
360
|
this.messagingSystem.subscribe('TokensController:stateChange', (tokensState) => {
|
|
234
361
|
__classPrivateFieldGet(this, _TokenBalancesController_onTokensChanged, "f").call(this, tokensState).catch((error) => {
|
|
235
362
|
console.warn('Error handling token state change:', error);
|
|
@@ -240,6 +367,10 @@ class TokenBalancesController extends (0, polling_controller_1.StaticIntervalPol
|
|
|
240
367
|
// Register action handlers for polling interval control
|
|
241
368
|
this.messagingSystem.registerActionHandler(`TokenBalancesController:updateChainPollingConfigs`, this.updateChainPollingConfigs.bind(this));
|
|
242
369
|
this.messagingSystem.registerActionHandler(`TokenBalancesController:getChainPollingConfig`, this.getChainPollingConfig.bind(this));
|
|
370
|
+
// Subscribe to AccountActivityService balance updates for real-time updates
|
|
371
|
+
this.messagingSystem.subscribe('AccountActivityService:balanceUpdated', __classPrivateFieldGet(this, _TokenBalancesController_onAccountActivityBalanceUpdate, "f").bind(this));
|
|
372
|
+
// Subscribe to AccountActivityService status changes for dynamic polling management
|
|
373
|
+
this.messagingSystem.subscribe('AccountActivityService:statusChanged', __classPrivateFieldGet(this, _TokenBalancesController_onAccountActivityStatusChanged, "f").bind(this));
|
|
243
374
|
}
|
|
244
375
|
/**
|
|
245
376
|
* Override to support per-chain polling intervals by grouping chains by interval
|
|
@@ -394,15 +525,14 @@ class TokenBalancesController extends (0, polling_controller_1.StaticIntervalPol
|
|
|
394
525
|
}
|
|
395
526
|
// Update with actual fetched balances only if the value has changed
|
|
396
527
|
aggregated.forEach(({ success, value, account, token, chainId }) => {
|
|
397
|
-
var _a, _b;
|
|
528
|
+
var _a, _b, _c;
|
|
398
529
|
if (success && value !== undefined) {
|
|
399
530
|
const newBalance = (0, controller_utils_1.toHex)(value);
|
|
400
531
|
const tokenAddress = checksum(token);
|
|
401
532
|
const currentBalance = d.tokenBalances[account]?.[chainId]?.[tokenAddress];
|
|
402
533
|
// Only update if the balance has actually changed
|
|
403
534
|
if (currentBalance !== newBalance) {
|
|
404
|
-
((
|
|
405
|
-
newBalance;
|
|
535
|
+
((_c = ((_a = d.tokenBalances)[_b = account] ?? (_a[_b] = {})))[chainId] ?? (_c[chainId] = {}))[tokenAddress] = newBalance;
|
|
406
536
|
}
|
|
407
537
|
}
|
|
408
538
|
});
|
|
@@ -467,6 +597,11 @@ class TokenBalancesController extends (0, polling_controller_1.StaticIntervalPol
|
|
|
467
597
|
__classPrivateFieldSet(this, _TokenBalancesController_isControllerPollingActive, false, "f");
|
|
468
598
|
__classPrivateFieldGet(this, _TokenBalancesController_intervalPollingTimers, "f").forEach((timer) => clearInterval(timer));
|
|
469
599
|
__classPrivateFieldGet(this, _TokenBalancesController_intervalPollingTimers, "f").clear();
|
|
600
|
+
// Clean up debouncing timer
|
|
601
|
+
if (__classPrivateFieldGet(this, _TokenBalancesController_statusChangeDebouncer, "f").timer) {
|
|
602
|
+
clearTimeout(__classPrivateFieldGet(this, _TokenBalancesController_statusChangeDebouncer, "f").timer);
|
|
603
|
+
__classPrivateFieldGet(this, _TokenBalancesController_statusChangeDebouncer, "f").timer = null;
|
|
604
|
+
}
|
|
470
605
|
// Unregister action handlers
|
|
471
606
|
this.messagingSystem.unregisterActionHandler(`TokenBalancesController:updateChainPollingConfigs`);
|
|
472
607
|
this.messagingSystem.unregisterActionHandler(`TokenBalancesController:getChainPollingConfig`);
|
|
@@ -474,7 +609,7 @@ class TokenBalancesController extends (0, polling_controller_1.StaticIntervalPol
|
|
|
474
609
|
}
|
|
475
610
|
}
|
|
476
611
|
exports.TokenBalancesController = TokenBalancesController;
|
|
477
|
-
_TokenBalancesController_platform = new WeakMap(), _TokenBalancesController_queryAllAccounts = new WeakMap(), _TokenBalancesController_accountsApiChainIds = new WeakMap(), _TokenBalancesController_balanceFetchers = new WeakMap(), _TokenBalancesController_allTokens = new WeakMap(), _TokenBalancesController_detectedTokens = new WeakMap(), _TokenBalancesController_defaultInterval = new WeakMap(), _TokenBalancesController_chainPollingConfig = new WeakMap(), _TokenBalancesController_intervalPollingTimers = new WeakMap(), _TokenBalancesController_isControllerPollingActive = new WeakMap(), _TokenBalancesController_requestedChainIds = new WeakMap(), _TokenBalancesController_getProvider = new WeakMap(), _TokenBalancesController_getNetworkClient = new WeakMap(), _TokenBalancesController_createAccountsApiFetcher = new WeakMap(), _TokenBalancesController_onTokensChanged = new WeakMap(), _TokenBalancesController_onNetworkChanged = new WeakMap(), _TokenBalancesController_onAccountRemoved = new WeakMap(), _TokenBalancesController_instances = new WeakSet(), _TokenBalancesController_chainIdsWithTokens = function _TokenBalancesController_chainIdsWithTokens() {
|
|
612
|
+
_TokenBalancesController_platform = new WeakMap(), _TokenBalancesController_queryAllAccounts = new WeakMap(), _TokenBalancesController_accountsApiChainIds = new WeakMap(), _TokenBalancesController_balanceFetchers = new WeakMap(), _TokenBalancesController_allTokens = new WeakMap(), _TokenBalancesController_detectedTokens = new WeakMap(), _TokenBalancesController_allIgnoredTokens = new WeakMap(), _TokenBalancesController_defaultInterval = new WeakMap(), _TokenBalancesController_websocketActivePollingInterval = new WeakMap(), _TokenBalancesController_chainPollingConfig = new WeakMap(), _TokenBalancesController_intervalPollingTimers = new WeakMap(), _TokenBalancesController_isControllerPollingActive = new WeakMap(), _TokenBalancesController_requestedChainIds = new WeakMap(), _TokenBalancesController_statusChangeDebouncer = new WeakMap(), _TokenBalancesController_getProvider = new WeakMap(), _TokenBalancesController_getNetworkClient = new WeakMap(), _TokenBalancesController_createAccountsApiFetcher = new WeakMap(), _TokenBalancesController_onTokensChanged = new WeakMap(), _TokenBalancesController_onNetworkChanged = new WeakMap(), _TokenBalancesController_onAccountRemoved = new WeakMap(), _TokenBalancesController_onAccountActivityBalanceUpdate = new WeakMap(), _TokenBalancesController_onAccountActivityStatusChanged = new WeakMap(), _TokenBalancesController_instances = new WeakSet(), _TokenBalancesController_chainIdsWithTokens = function _TokenBalancesController_chainIdsWithTokens() {
|
|
478
613
|
return [
|
|
479
614
|
...new Set([
|
|
480
615
|
...Object.keys(__classPrivateFieldGet(this, _TokenBalancesController_allTokens, "f")),
|
|
@@ -529,6 +664,90 @@ _TokenBalancesController_platform = new WeakMap(), _TokenBalancesController_quer
|
|
|
529
664
|
});
|
|
530
665
|
}, interval);
|
|
531
666
|
__classPrivateFieldGet(this, _TokenBalancesController_intervalPollingTimers, "f").set(interval, timer);
|
|
667
|
+
}, _TokenBalancesController_isTokenTracked = function _TokenBalancesController_isTokenTracked(tokenAddress, account, chainId) {
|
|
668
|
+
const normalizedAddress = tokenAddress.toLowerCase();
|
|
669
|
+
// Check if token exists in allTokens
|
|
670
|
+
if (__classPrivateFieldGet(this, _TokenBalancesController_allTokens, "f")?.[chainId]?.[account.toLowerCase()]?.some((token) => token.address.toLowerCase() === normalizedAddress)) {
|
|
671
|
+
return true;
|
|
672
|
+
}
|
|
673
|
+
// Check if token exists in allIgnoredTokens
|
|
674
|
+
if (__classPrivateFieldGet(this, _TokenBalancesController_allIgnoredTokens, "f")?.[chainId]?.[account.toLowerCase()]?.some((addr) => addr.toLowerCase() === normalizedAddress)) {
|
|
675
|
+
return true;
|
|
676
|
+
}
|
|
677
|
+
return false;
|
|
678
|
+
}, _TokenBalancesController_prepareBalanceUpdates = function _TokenBalancesController_prepareBalanceUpdates(updates, account, chainId) {
|
|
679
|
+
const tokenBalances = [];
|
|
680
|
+
const newTokens = [];
|
|
681
|
+
const nativeBalanceUpdates = [];
|
|
682
|
+
for (const update of updates) {
|
|
683
|
+
const { asset, postBalance } = update;
|
|
684
|
+
// Throw if balance update has an error
|
|
685
|
+
if (postBalance.error) {
|
|
686
|
+
throw new Error('Balance update has error');
|
|
687
|
+
}
|
|
688
|
+
// Parse token address from asset type
|
|
689
|
+
const parsed = (0, exports.parseAssetType)(asset.type);
|
|
690
|
+
if (!parsed) {
|
|
691
|
+
throw new Error('Failed to parse asset type');
|
|
692
|
+
}
|
|
693
|
+
const [tokenAddress, isNativeToken] = parsed;
|
|
694
|
+
// Validate token address
|
|
695
|
+
if (!(0, utils_1.isStrictHexString)(tokenAddress) ||
|
|
696
|
+
!(0, controller_utils_1.isValidHexAddress)(tokenAddress)) {
|
|
697
|
+
throw new Error('Invalid token address');
|
|
698
|
+
}
|
|
699
|
+
const checksumTokenAddress = checksum(tokenAddress);
|
|
700
|
+
const isTracked = __classPrivateFieldGet(this, _TokenBalancesController_instances, "m", _TokenBalancesController_isTokenTracked).call(this, checksumTokenAddress, account, chainId);
|
|
701
|
+
// postBalance.amount is in hex format (raw units)
|
|
702
|
+
const balanceHex = postBalance.amount;
|
|
703
|
+
// Add token balance (tracked tokens, ignored tokens, and native tokens all get balance updates)
|
|
704
|
+
tokenBalances.push({
|
|
705
|
+
tokenAddress: checksumTokenAddress,
|
|
706
|
+
balance: balanceHex,
|
|
707
|
+
});
|
|
708
|
+
// Add native balance update if this is a native token
|
|
709
|
+
if (isNativeToken) {
|
|
710
|
+
nativeBalanceUpdates.push({
|
|
711
|
+
address: account,
|
|
712
|
+
chainId,
|
|
713
|
+
balance: balanceHex,
|
|
714
|
+
});
|
|
715
|
+
}
|
|
716
|
+
// Handle untracked ERC20 tokens - queue for import
|
|
717
|
+
if (!isNativeToken && !isTracked) {
|
|
718
|
+
newTokens.push(checksumTokenAddress);
|
|
719
|
+
}
|
|
720
|
+
}
|
|
721
|
+
return { tokenBalances, newTokens, nativeBalanceUpdates };
|
|
722
|
+
}, _TokenBalancesController_processAccumulatedStatusChanges = function _TokenBalancesController_processAccumulatedStatusChanges() {
|
|
723
|
+
const changes = Array.from(__classPrivateFieldGet(this, _TokenBalancesController_statusChangeDebouncer, "f").pendingChanges.entries());
|
|
724
|
+
__classPrivateFieldGet(this, _TokenBalancesController_statusChangeDebouncer, "f").pendingChanges.clear();
|
|
725
|
+
__classPrivateFieldGet(this, _TokenBalancesController_statusChangeDebouncer, "f").timer = null;
|
|
726
|
+
if (changes.length === 0) {
|
|
727
|
+
return;
|
|
728
|
+
}
|
|
729
|
+
// Calculate final polling configurations
|
|
730
|
+
const chainConfigs = {};
|
|
731
|
+
for (const [chainId, status] of changes) {
|
|
732
|
+
// Convert CAIP format (eip155:1) to hex format (0x1)
|
|
733
|
+
// chainId is always in CAIP format from AccountActivityService
|
|
734
|
+
const hexChainId = (0, exports.caipChainIdToHex)(chainId);
|
|
735
|
+
if (status === 'down') {
|
|
736
|
+
// Chain is down - use default polling since no real-time updates available
|
|
737
|
+
chainConfigs[hexChainId] = { interval: __classPrivateFieldGet(this, _TokenBalancesController_defaultInterval, "f") };
|
|
738
|
+
}
|
|
739
|
+
else {
|
|
740
|
+
// Chain is up - use longer intervals since WebSocket provides real-time updates
|
|
741
|
+
chainConfigs[hexChainId] = {
|
|
742
|
+
interval: __classPrivateFieldGet(this, _TokenBalancesController_websocketActivePollingInterval, "f"),
|
|
743
|
+
};
|
|
744
|
+
}
|
|
745
|
+
}
|
|
746
|
+
// Add jitter to prevent synchronized requests across instances
|
|
747
|
+
const jitterDelay = Math.random() * __classPrivateFieldGet(this, _TokenBalancesController_defaultInterval, "f"); // 0 to default interval
|
|
748
|
+
setTimeout(() => {
|
|
749
|
+
this.updateChainPollingConfigs(chainConfigs, { immediateUpdate: true });
|
|
750
|
+
}, jitterDelay);
|
|
532
751
|
};
|
|
533
752
|
exports.default = TokenBalancesController;
|
|
534
753
|
//# sourceMappingURL=TokenBalancesController.cjs.map
|