@metamask-previews/assets-controllers 20.0.0-preview.3fbb6b41 → 23.1.0-preview.353ee83b

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 (67) hide show
  1. package/CHANGELOG.md +103 -10
  2. package/dist/AccountTrackerController.d.ts +28 -7
  3. package/dist/AccountTrackerController.d.ts.map +1 -1
  4. package/dist/AccountTrackerController.js +104 -30
  5. package/dist/AccountTrackerController.js.map +1 -1
  6. package/dist/AssetsContractController.d.ts.map +1 -1
  7. package/dist/AssetsContractController.js +4 -0
  8. package/dist/AssetsContractController.js.map +1 -1
  9. package/dist/CurrencyRateController.d.ts +2 -2
  10. package/dist/CurrencyRateController.d.ts.map +1 -1
  11. package/dist/CurrencyRateController.js +1 -1
  12. package/dist/CurrencyRateController.js.map +1 -1
  13. package/dist/NftController.d.ts +75 -1
  14. package/dist/NftController.d.ts.map +1 -1
  15. package/dist/NftController.js +35 -14
  16. package/dist/NftController.js.map +1 -1
  17. package/dist/NftDetectionController.d.ts +7 -4
  18. package/dist/NftDetectionController.d.ts.map +1 -1
  19. package/dist/NftDetectionController.js +26 -14
  20. package/dist/NftDetectionController.js.map +1 -1
  21. package/dist/Standards/ERC20Standard.d.ts.map +1 -1
  22. package/dist/Standards/ERC20Standard.js +4 -0
  23. package/dist/Standards/ERC20Standard.js.map +1 -1
  24. package/dist/Standards/NftStandards/ERC1155/ERC1155Standard.d.ts +11 -10
  25. package/dist/Standards/NftStandards/ERC1155/ERC1155Standard.d.ts.map +1 -1
  26. package/dist/Standards/NftStandards/ERC1155/ERC1155Standard.js +106 -85
  27. package/dist/Standards/NftStandards/ERC1155/ERC1155Standard.js.map +1 -1
  28. package/dist/Standards/NftStandards/ERC721/ERC721Standard.d.ts.map +1 -1
  29. package/dist/Standards/NftStandards/ERC721/ERC721Standard.js +2 -0
  30. package/dist/Standards/NftStandards/ERC721/ERC721Standard.js.map +1 -1
  31. package/dist/Standards/standards-types.d.ts.map +1 -1
  32. package/dist/Standards/standards-types.js.map +1 -1
  33. package/dist/TokenDetectionController.d.ts +43 -56
  34. package/dist/TokenDetectionController.d.ts.map +1 -1
  35. package/dist/TokenDetectionController.js +132 -96
  36. package/dist/TokenDetectionController.js.map +1 -1
  37. package/dist/TokenListController.d.ts +6 -4
  38. package/dist/TokenListController.d.ts.map +1 -1
  39. package/dist/TokenListController.js +11 -10
  40. package/dist/TokenListController.js.map +1 -1
  41. package/dist/TokenRatesController.d.ts +6 -38
  42. package/dist/TokenRatesController.d.ts.map +1 -1
  43. package/dist/TokenRatesController.js +125 -93
  44. package/dist/TokenRatesController.js.map +1 -1
  45. package/dist/TokensController.d.ts +5 -6
  46. package/dist/TokensController.d.ts.map +1 -1
  47. package/dist/TokensController.js +81 -21
  48. package/dist/TokensController.js.map +1 -1
  49. package/dist/assetsUtil.d.ts +80 -9
  50. package/dist/assetsUtil.d.ts.map +1 -1
  51. package/dist/assetsUtil.js +177 -27
  52. package/dist/assetsUtil.js.map +1 -1
  53. package/dist/index.d.ts +4 -3
  54. package/dist/index.d.ts.map +1 -1
  55. package/dist/index.js +5 -3
  56. package/dist/index.js.map +1 -1
  57. package/dist/token-prices-service/abstract-token-prices-service.d.ts +15 -16
  58. package/dist/token-prices-service/abstract-token-prices-service.d.ts.map +1 -1
  59. package/dist/token-prices-service/abstract-token-prices-service.js.map +1 -1
  60. package/dist/token-prices-service/codefi-v2.d.ts +61 -3
  61. package/dist/token-prices-service/codefi-v2.d.ts.map +1 -1
  62. package/dist/token-prices-service/codefi-v2.js +86 -20
  63. package/dist/token-prices-service/codefi-v2.js.map +1 -1
  64. package/dist/token-prices-service/index.d.ts +1 -1
  65. package/dist/token-prices-service/index.js +2 -2
  66. package/dist/token-prices-service/index.js.map +1 -1
  67. package/package.json +15 -11
@@ -1 +1 @@
1
- {"version":3,"file":"NftDetectionController.js","sourceRoot":"","sources":["../src/NftDetectionController.ts"],"names":[],"mappings":";;;;;;;;;;;;AACA,iEAKoC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E,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 { PollingControllerV1 } 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 PollingControllerV1<\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,GAAG,MAAM,CAAC;AA8HhC;;GAEG;AACH,MAAa,sBAAuB,SAAQ,sDAG3C;IA4EC;;;;;;;;;;;;;;;OAeG;IACH,YACE,EACE,OAAO,EAAE,cAAc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,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 +1 @@
1
- {"version":3,"file":"ERC20Standard.d.ts","sourceRoot":"","sources":["../../src/Standards/ERC20Standard.ts"],"names":[],"mappings":";AACA,OAAO,KAAK,EAAE,YAAY,EAAE,MAAM,0BAA0B,CAAC;AAM7D,OAAO,KAAK,EAAE,EAAE,EAAE,MAAM,iBAAiB,CAAC;AAI1C,qBAAa,aAAa;IACxB,OAAO,CAAC,QAAQ,CAAC,QAAQ,CAAe;gBAE5B,QAAQ,EAAE,YAAY;IAIlC;;;;;;OAMG;IACG,YAAY,CAAC,OAAO,EAAE,MAAM,EAAE,eAAe,EAAE,MAAM,GAAG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IACG,UAAU,CACd,OAAO,EAAE,MAAM,EACf,WAAW,CAAC,EAAE,MAAM,GACnB,OAAO,CAAC;QACT,QAAQ,EAAE,MAAM,CAAC;QACjB,MAAM,EAAE,MAAM,GAAG,SAAS,CAAC;QAC3B,QAAQ,EAAE,MAAM,GAAG,SAAS,CAAC;QAC7B,OAAO,EAAE,EAAE,GAAG,SAAS,CAAC;KACzB,CAAC;CAaH"}
@@ -47,6 +47,8 @@ class ERC20Standard {
47
47
  try {
48
48
  const decimals = yield contract.decimals();
49
49
  return decimals.toString();
50
+ // TODO: Replace `any` with type
51
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
50
52
  }
51
53
  catch (err) {
52
54
  // Mirror previous implementation
@@ -69,6 +71,8 @@ class ERC20Standard {
69
71
  try {
70
72
  const name = yield contract.name();
71
73
  return name.toString();
74
+ // TODO: Replace `any` with type
75
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
72
76
  }
73
77
  catch (err) {
74
78
  // Mirror previous implementation
@@ -1 +1 @@
1
- {"version":3,"file":"ERC20Standard.js","sourceRoot":"","sources":["../../src/Standards/ERC20Standard.ts"],"names":[],"mappings":";;;;;;;;;;;;AAAA,wDAAoD;AAEpD,mDAAmD;AACnD,iEAAmD;AACnD,mEAAuD;AACvD,2CAA0D;AAC1D,qDAAyC;AAGzC,8CAAoD;AAEpD,MAAa,aAAa;IAGxB,YAAY,QAAsB;QAChC,IAAI,CAAC,QAAQ,GAAG,QAAQ,CAAC;IAC3B,CAAC;IAED;;;;;;OAMG;IACG,YAAY,CAAC,OAAe,EAAE,eAAuB;;YACzD,MAAM,QAAQ,GAAG,IAAI,oBAAQ,CAAC,OAAO,EAAE,4BAAQ,EAAE,IAAI,CAAC,QAAQ,CAAC,CAAC;YAChE,MAAM,OAAO,GAAG,MAAM,QAAQ,CAAC,SAAS,CAAC,eAAe,CAAC,CAAC;YAC1D,OAAO,IAAA,gCAAmB,EAAC,OAAO,CAAC,CAAC;QACtC,CAAC;KAAA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M,CAAC,CAAC;YAChC,kDAAkD;YAClD,IAAI;gBACF,MAAM,OAAO,GAAG,IAAA,wBAAY,EAAC,QAAQ,EAAE,MAAM,CAAC,CAAC;gBAC/C,IAAI,CAAA,OAAO,aAAP,OAAO,uBAAP,OAAO,CAAE,MAAM,IAAG,CAAC,EAAE;oBACvB,OAAO,OAAO,CAAC;iBAChB;aACF;YAAC,WAAM;gBACN,eAAe;aAChB;YAED,iDAAiD;YACjD,IAAI;gBACF,MAAM,IAAI,GAAG,IAAA,wBAAM,EAAC,MAAM,CAAC,CAAC;gBAC5B,IAAI,IAAI,CAAC,MAAM,GAAG,CAAC,EAAE;oBACnB,OAAO,IAAI,CAAC;iBACb;aACF;YAAC,WAAM;gBACN,eAAe;aAChB;YAED,MAAM,IAAI,KAAK,CAAC,8BAA8B,CAAC,CAAC;QAClD,CAAC;KAAA;IAED;;;;;;OAMG;IACG,UAAU,CACd,OAAe,EACf,WAAoB;;YAOpB,MAAM,CAAC,QAAQ,EAAE,MAAM,EAAE,OAAO,CAAC,GAAG,MAAM,OAAO,CAAC,GAAG,CAAC;gBACpD,IAAI,CAAC,gBAAgB,CAAC,OAAO,CAAC;gBAC9B,IAAI,CAAC,cAAc,CAAC,OAAO,CAAC;gBAC5B,WAAW,CAAC,CAAC,CAAC,IAAI,CAAC,YAAY,CAAC,OAAO,EAAE,WAAW,CAAC,CAAC,CAAC,CAAC,SAAS;aAClE,CAAC,CAAC;YACH,OAAO;gBACL,QAAQ;gBACR,MAAM;gBACN,OAAO;gBACP,QAAQ,EAAE,wBAAK;aAChB,CAAC;QACJ,CAAC;KAAA;CACF;AA1HD,sCA0HC","sourcesContent":["import { Contract } from '@ethersproject/contracts';\nimport type { Web3Provider } from '@ethersproject/providers';\nimport { decodeSingle } from '@metamask/abi-utils';\nimport { ERC20 } from '@metamask/controller-utils';\nimport { abiERC20 } from '@metamask/metamask-eth-abis';\nimport { assertIsStrictHexString } from '@metamask/utils';\nimport { toUtf8 } from 'ethereumjs-util';\nimport type { BN } from 'ethereumjs-util';\n\nimport { ethersBigNumberToBN } from '../assetsUtil';\n\nexport class ERC20Standard {\n private readonly provider: Web3Provider;\n\n constructor(provider: Web3Provider) {\n this.provider = provider;\n }\n\n /**\n * Get balance or count for current account on specific asset contract.\n *\n * @param address - Asset ERC20 contract address.\n * @param selectedAddress - Current account public address.\n * @returns Promise resolving to BN object containing balance for current account on specific asset contract.\n */\n async getBalanceOf(address: string, selectedAddress: string): Promise<BN> {\n const contract = new Contract(address, abiERC20, this.provider);\n const balance = await contract.balanceOf(selectedAddress);\n return ethersBigNumberToBN(balance);\n }\n\n /**\n * Query for the decimals for a given ERC20 asset.\n *\n * @param address - ERC20 asset contract string.\n * @returns Promise resolving to the 'decimals'.\n */\n async getTokenDecimals(address: string): Promise<string> {\n const contract = new Contract(address, abiERC20, this.provider);\n try {\n const decimals = await contract.decimals();\n return decimals.toString();\n } catch (err: any) {\n // Mirror previous implementation\n if (err.message.includes('call revert exception')) {\n throw new Error('Failed to parse token decimals');\n }\n throw err;\n }\n }\n\n /**\n * Query for the name for a given ERC20 asset.\n *\n * @param address - ERC20 asset contract string.\n * @returns Promise resolving to the 'name'.\n */\n async getTokenName(address: string): Promise<string> {\n const contract = new Contract(address, abiERC20, this.provider);\n try {\n const name = await contract.name();\n return name.toString();\n } catch (err: any) {\n // Mirror previous implementation\n if (err.message.includes('call revert exception')) {\n throw new Error('Failed to parse token name');\n }\n throw err;\n }\n }\n\n /**\n * Query for symbol for a given ERC20 asset.\n *\n * @param address - ERC20 asset contract address.\n * @returns Promise resolving to the 'symbol'.\n */\n async getTokenSymbol(address: string): Promise<string> {\n // Signature for calling `symbol()`\n const payload = { to: address, data: '0x95d89b41' };\n const result = await this.provider.call(payload);\n assertIsStrictHexString(result);\n // Parse as string - treat empty string as failure\n try {\n const decoded = decodeSingle('string', result);\n if (decoded?.length > 0) {\n return decoded;\n }\n } catch {\n // Ignore error\n }\n\n // Parse as bytes - treat empty string as failure\n try {\n const utf8 = toUtf8(result);\n if (utf8.length > 0) {\n return utf8;\n }\n } catch {\n // Ignore error\n }\n\n throw new Error('Failed to parse token symbol');\n }\n\n /**\n * Query if a contract implements an interface.\n *\n * @param address - Asset contract address.\n * @param userAddress - The public address for the currently active user's account.\n * @returns Promise resolving an object containing the standard, decimals, symbol and balance of the given contract/userAddress pair.\n */\n async getDetails(\n address: string,\n userAddress?: string,\n ): Promise<{\n standard: string;\n symbol: string | undefined;\n decimals: string | undefined;\n balance: BN | undefined;\n }> {\n const [decimals, symbol, balance] = await Promise.all([\n this.getTokenDecimals(address),\n this.getTokenSymbol(address),\n userAddress ? this.getBalanceOf(address, userAddress) : undefined,\n ]);\n return {\n decimals,\n symbol,\n balance,\n standard: ERC20,\n };\n }\n}\n"]}
1
+ {"version":3,"file":"ERC20Standard.js","sourceRoot":"","sources":["../../src/Standards/ERC20Standard.ts"],"names":[],"mappings":";;;;;;;;;;;;AAAA,wDAAoD;AAEpD,mDAAmD;AACnD,iEAAmD;AACnD,mEAAuD;AACvD,2CAA0D;AAC1D,qDAAyC;AAGzC,8CAAoD;AAEpD,MAAa,aAAa;IAGxB,YAAY,QAAsB;QAChC,IAAI,CAAC,QAAQ,GAAG,QAAQ,CAAC;IAC3B,CAAC;IAED;;;;;;OAMG;IACG,YAAY,CAAC,OAAe,EAAE,eAAuB;;YACzD,MAAM,QAAQ,GAAG,IAAI,oBAAQ,CAAC,OAAO,EAAE,4BAAQ,EAAE,IAAI,CAAC,QAAQ,CAAC,CAAC;YAChE,MAAM,OAAO,GAAG,MAAM,QAAQ,CAAC,SAAS,CAAC,eAAe,CAAC,CAAC;YAC1D,OAAO,IAAA,gCAAmB,EAAC,OAAO,CAAC,CAAC;QACtC,CAAC;KAAA;IAED;;;;;OAKG;IACG,gBAAgB,CAAC,OAAe;;YACpC,MAAM,QAAQ,GAAG,IAAI,oBAAQ,CAAC,OAAO,EAAE,4BAAQ,EAAE,IAAI,CAAC,QAAQ,CAAC,CAAC;YAChE,IAAI;g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E,MAAM,CAAC,CAAC;gBAC/C,IAAI,CAAA,OAAO,aAAP,OAAO,uBAAP,OAAO,CAAE,MAAM,IAAG,CAAC,EAAE;oBACvB,OAAO,OAAO,CAAC;iBAChB;aACF;YAAC,WAAM;gBACN,eAAe;aAChB;YAED,iDAAiD;YACjD,IAAI;gBACF,MAAM,IAAI,GAAG,IAAA,wBAAM,EAAC,MAAM,CAAC,CAAC;gBAC5B,IAAI,IAAI,CAAC,MAAM,GAAG,CAAC,EAAE;oBACnB,OAAO,IAAI,CAAC;iBACb;aACF;YAAC,WAAM;gBACN,eAAe;aAChB;YAED,MAAM,IAAI,KAAK,CAAC,8BAA8B,CAAC,CAAC;QAClD,CAAC;KAAA;IAED;;;;;;OAMG;IACG,UAAU,CACd,OAAe,EACf,WAAoB;;YAOpB,MAAM,CAAC,QAAQ,EAAE,MAAM,EAAE,OAAO,CAAC,GAAG,MAAM,OAAO,CAAC,GAAG,CAAC;gBACpD,IAAI,CAAC,gBAAgB,CAAC,OAAO,CAAC;gBAC9B,IAAI,CAAC,cAAc,CAAC,OAAO,CAAC;gBAC5B,WAAW,CAAC,CAAC,CAAC,IAAI,CAAC,YAAY,CAAC,OAAO,EAAE,WAAW,CAAC,CAAC,CAAC,CAAC,SAAS;aAClE,CAAC,CAAC;YACH,OAAO;gBACL,QAAQ;gBACR,MAAM;gBACN,OAAO;gBACP,QAAQ,EAAE,wBAAK;aAChB,CAAC;QACJ,CAAC;KAAA;CACF;AA9HD,sCA8HC","sourcesContent":["import { Contract } from '@ethersproject/contracts';\nimport type { Web3Provider } from '@ethersproject/providers';\nimport { decodeSingle } from '@metamask/abi-utils';\nimport { ERC20 } from '@metamask/controller-utils';\nimport { abiERC20 } from '@metamask/metamask-eth-abis';\nimport { assertIsStrictHexString } from '@metamask/utils';\nimport { toUtf8 } from 'ethereumjs-util';\nimport type { BN } from 'ethereumjs-util';\n\nimport { ethersBigNumberToBN } from '../assetsUtil';\n\nexport class ERC20Standard {\n private readonly provider: Web3Provider;\n\n constructor(provider: Web3Provider) {\n this.provider = provider;\n }\n\n /**\n * Get balance or count for current account on specific asset contract.\n *\n * @param address - Asset ERC20 contract address.\n * @param selectedAddress - Current account public address.\n * @returns Promise resolving to BN object containing balance for current account on specific asset contract.\n */\n async getBalanceOf(address: string, selectedAddress: string): Promise<BN> {\n const contract = new Contract(address, abiERC20, this.provider);\n const balance = await contract.balanceOf(selectedAddress);\n return ethersBigNumberToBN(balance);\n }\n\n /**\n * Query for the decimals for a given ERC20 asset.\n *\n * @param address - ERC20 asset contract string.\n * @returns Promise resolving to the 'decimals'.\n */\n async getTokenDecimals(address: string): Promise<string> {\n const contract = new Contract(address, abiERC20, this.provider);\n try {\n const decimals = await contract.decimals();\n return decimals.toString();\n // TODO: Replace `any` with type\n // eslint-disable-next-line @typescript-eslint/no-explicit-any\n } catch (err: any) {\n // Mirror previous implementation\n if (err.message.includes('call revert exception')) {\n throw new Error('Failed to parse token decimals');\n }\n throw err;\n }\n }\n\n /**\n * Query for the name for a given ERC20 asset.\n *\n * @param address - ERC20 asset contract string.\n * @returns Promise resolving to the 'name'.\n */\n async getTokenName(address: string): Promise<string> {\n const contract = new Contract(address, abiERC20, this.provider);\n try {\n const name = await contract.name();\n return name.toString();\n // TODO: Replace `any` with type\n // eslint-disable-next-line @typescript-eslint/no-explicit-any\n } catch (err: any) {\n // Mirror previous implementation\n if (err.message.includes('call revert exception')) {\n throw new Error('Failed to parse token name');\n }\n throw err;\n }\n }\n\n /**\n * Query for symbol for a given ERC20 asset.\n *\n * @param address - ERC20 asset contract address.\n * @returns Promise resolving to the 'symbol'.\n */\n async getTokenSymbol(address: string): Promise<string> {\n // Signature for calling `symbol()`\n const payload = { to: address, data: '0x95d89b41' };\n const result = await this.provider.call(payload);\n assertIsStrictHexString(result);\n // Parse as string - treat empty string as failure\n try {\n const decoded = decodeSingle('string', result);\n if (decoded?.length > 0) {\n return decoded;\n }\n } catch {\n // Ignore error\n }\n\n // Parse as bytes - treat empty string as failure\n try {\n const utf8 = toUtf8(result);\n if (utf8.length > 0) {\n return utf8;\n }\n } catch {\n // Ignore error\n }\n\n throw new Error('Failed to parse token symbol');\n }\n\n /**\n * Query if a contract implements an interface.\n *\n * @param address - Asset contract address.\n * @param userAddress - The public address for the currently active user's account.\n * @returns Promise resolving an object containing the standard, decimals, symbol and balance of the given contract/userAddress pair.\n */\n async getDetails(\n address: string,\n userAddress?: string,\n ): Promise<{\n standard: string;\n symbol: string | undefined;\n decimals: string | undefined;\n balance: BN | undefined;\n }> {\n const [decimals, symbol, balance] = await Promise.all([\n this.getTokenDecimals(address),\n this.getTokenSymbol(address),\n userAddress ? this.getBalanceOf(address, userAddress) : undefined,\n ]);\n return {\n decimals,\n symbol,\n balance,\n standard: ERC20,\n };\n }\n}\n"]}
@@ -1,3 +1,4 @@
1
+ /// <reference types="bn.js" />
1
2
  import type { Web3Provider } from '@ethersproject/providers';
2
3
  import type { BN } from 'ethereumjs-util';
3
4
  export declare class ERC1155Standard {
@@ -9,21 +10,21 @@ export declare class ERC1155Standard {
9
10
  * @param address - ERC1155 asset contract address.
10
11
  * @returns Promise resolving to whether the contract implements ERC1155 URI Metadata interface.
11
12
  */
12
- contractSupportsURIMetadataInterface: (address: string) => Promise<boolean>;
13
+ contractSupportsURIMetadataInterface(address: string): Promise<boolean>;
13
14
  /**
14
15
  * Query if contract implements ERC1155 Token Receiver interface.
15
16
  *
16
17
  * @param address - ERC1155 asset contract address.
17
18
  * @returns Promise resolving to whether the contract implements ERC1155 Token Receiver interface.
18
19
  */
19
- contractSupportsTokenReceiverInterface: (address: string) => Promise<boolean>;
20
+ contractSupportsTokenReceiverInterface(address: string): Promise<boolean>;
20
21
  /**
21
22
  * Query if contract implements ERC1155 interface.
22
23
  *
23
24
  * @param address - ERC1155 asset contract address.
24
25
  * @returns Promise resolving to whether the contract implements the base ERC1155 interface.
25
26
  */
26
- contractSupportsBase1155Interface: (address: string) => Promise<boolean>;
27
+ contractSupportsBase1155Interface(address: string): Promise<boolean>;
27
28
  /**
28
29
  * Query for tokenURI for a given asset.
29
30
  *
@@ -31,7 +32,7 @@ export declare class ERC1155Standard {
31
32
  * @param tokenId - ERC1155 asset identifier.
32
33
  * @returns Promise resolving to the 'tokenURI'.
33
34
  */
34
- getTokenURI: (address: string, tokenId: string) => Promise<string>;
35
+ getTokenURI(address: string, tokenId: string): Promise<string>;
35
36
  /**
36
37
  * Query for balance of a given ERC1155 token.
37
38
  *
@@ -40,7 +41,7 @@ export declare class ERC1155Standard {
40
41
  * @param tokenId - ERC1155 asset identifier.
41
42
  * @returns Promise resolving to the 'balanceOf'.
42
43
  */
43
- getBalanceOf: (contractAddress: string, address: string, tokenId: string) => Promise<BN>;
44
+ getBalanceOf(contractAddress: string, address: string, tokenId: string): Promise<BN>;
44
45
  /**
45
46
  * Transfer single ERC1155 token.
46
47
  * When minting/creating tokens, the from arg MUST be set to 0x0 (i.e. zero address).
@@ -53,21 +54,21 @@ export declare class ERC1155Standard {
53
54
  * @param value - Number of tokens to be sent.
54
55
  * @returns Promise resolving to the 'transferSingle'.
55
56
  */
56
- transferSingle: (operator: string, from: string, to: string, id: string, value: string) => Promise<void>;
57
+ transferSingle(operator: string, from: string, to: string, id: string, value: string): Promise<void>;
57
58
  /**
58
59
  * Query for symbol for a given asset.
59
60
  *
60
61
  * @param address - ERC1155 asset contract address.
61
62
  * @returns Promise resolving to the 'symbol'.
62
63
  */
63
- getAssetSymbol: (address: string) => Promise<string>;
64
+ getAssetSymbol(address: string): Promise<string>;
64
65
  /**
65
66
  * Query for name for a given asset.
66
67
  *
67
68
  * @param address - ERC1155 asset contract address.
68
69
  * @returns Promise resolving to the 'name'.
69
70
  */
70
- getAssetName: (address: string) => Promise<string>;
71
+ getAssetName(address: string): Promise<string>;
71
72
  /**
72
73
  * Query if a contract implements an interface.
73
74
  *
@@ -75,7 +76,7 @@ export declare class ERC1155Standard {
75
76
  * @param interfaceId - Interface identifier.
76
77
  * @returns Promise resolving to whether the contract implements `interfaceID`.
77
78
  */
78
- private readonly contractSupportsInterface;
79
+ private contractSupportsInterface;
79
80
  /**
80
81
  * Query if a contract implements an interface.
81
82
  *
@@ -84,7 +85,7 @@ export declare class ERC1155Standard {
84
85
  * @param tokenId - tokenId of a given token in the contract.
85
86
  * @returns Promise resolving an object containing the standard, tokenURI, symbol and name of the given contract/tokenId pair.
86
87
  */
87
- getDetails: (address: string, ipfsGateway: string, tokenId?: string) => Promise<{
88
+ getDetails(address: string, ipfsGateway: string, tokenId?: string): Promise<{
88
89
  standard: string;
89
90
  tokenURI: string | undefined;
90
91
  image: string | undefined;
@@ -1 +1 @@
1
- {"version":3,"file":"ERC1155Standard.d.ts","sourceRoot":"","sources":["../../../../src/Standards/NftStandards/ERC1155/ERC1155Standard.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAE,YAAY,EAAE,MAAM,0BAA0B,CAAC;AAU7D,OAAO,KAAK,EAAE,EAAE,EAAE,MAAM,iBAAiB,CAAC;AAI1C,qBAAa,eAAe;IAC1B,OAAO,CAAC,QAAQ,CAAC,QAAQ,CAAe;gBAE5B,QAAQ,EAAE,YAAY;IAIlC;;;;;OAKG;IACH,oCAAoC,YACzB,MAAM,KACd,QAAQ,OAAO,CAAC,CAKjB;IAEF;;;;;OAKG;IACH,sCAAsC,YAC3B,MAAM,KACd,QAAQ,OAAO,CAAC,CAKjB;IAEF;;;;;OAKG;IACH,iCAAiC,YACtB,MAAM,KACd,QAAQ,OAAO,CAAC,CAEjB;IAEF;;;;;;OAMG;IACH,WAAW,YAAmB,MAAM,WAAW,MAAM,KAAG,QAAQ,MAAM,CAAC,CAGrE;IAEF;;;;;;;OAOG;IACH,YAAY,oBACO,MAAM,WACd,MAAM,WACN,MAAM,KACd,QAAQ,EAAE,CAAC,CAIZ;IAEF;;;;;;;;;;;OAWG;IACH,cAAc,aACF,MAAM,QACV,MAAM,MACR,MAAM,MACN,MAAM,SACH,MAAM,KACZ,QAAQ,IAAI,CAAC,CAmBd;IAEF;;;;;OAKG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,QAAQ,EAAE,MAAM,EAChB,IAAI,EAAE,MAAM,EACZ,EAAE,EAAE,MAAM,EACV,EAAE,EAAE,MAAM,EACV,KAAK,EAAE,MAAM,GACZ,OAAO,CAAC,IAAI,CAAC;IAqBhB;;;;;OAKG;IACG,cAAc,CAAC,OAAO,EAAE,MAAM,GAAG,OAAO,CAAC,MAAM,CAAC;IAmBtD;;;;;OAKG;IACG,YAAY,CAAC,OAAO,EAAE,MAAM,GAAG,OAAO,CAAC,MAAM,CAAC;IAmBpD;;;;;;OAMG;YACW,yBAAyB;IAQvC;;;;;;;OAOG;IACG,UAAU,CACd,OAAO,EAAE,MAAM,EACf,WAAW,EAAE,MAAM,EACnB,OAAO,CAAC,EAAE,MAAM,GACf,OAAO,CAAC;QACT,QAAQ,EAAE,MAAM,CAAC;QACjB,QAAQ,EAAE,MAAM,GAAG,SAAS,CAAC;QAC7B,KAAK,EAAE,MAAM,GAAG,SAAS,CAAC;QAC1B,IAAI,EAAE,MAAM,GAAG,SAAS,CAAC;QACzB,MAAM,EAAE,MAAM,GAAG,SAAS,CAAC;KAC5B,CAAC;CA8CH"}
@@ -16,70 +16,83 @@ const metamask_eth_abis_1 = require("@metamask/metamask-eth-abis");
16
16
  const assetsUtil_1 = require("../../../assetsUtil");
17
17
  class ERC1155Standard {
18
18
  constructor(provider) {
19
- /**
20
- * Query if contract implements ERC1155 URI Metadata interface.
21
- *
22
- * @param address - ERC1155 asset contract address.
23
- * @returns Promise resolving to whether the contract implements ERC1155 URI Metadata interface.
24
- */
25
- this.contractSupportsURIMetadataInterface = (address) => __awaiter(this, void 0, void 0, function* () {
19
+ this.provider = provider;
20
+ }
21
+ /**
22
+ * Query if contract implements ERC1155 URI Metadata interface.
23
+ *
24
+ * @param address - ERC1155 asset contract address.
25
+ * @returns Promise resolving to whether the contract implements ERC1155 URI Metadata interface.
26
+ */
27
+ contractSupportsURIMetadataInterface(address) {
28
+ return __awaiter(this, void 0, void 0, function* () {
26
29
  return this.contractSupportsInterface(address, controller_utils_1.ERC1155_METADATA_URI_INTERFACE_ID);
27
30
  });
28
- /**
29
- * Query if contract implements ERC1155 Token Receiver interface.
30
- *
31
- * @param address - ERC1155 asset contract address.
32
- * @returns Promise resolving to whether the contract implements ERC1155 Token Receiver interface.
33
- */
34
- this.contractSupportsTokenReceiverInterface = (address) => __awaiter(this, void 0, void 0, function* () {
31
+ }
32
+ /**
33
+ * Query if contract implements ERC1155 Token Receiver interface.
34
+ *
35
+ * @param address - ERC1155 asset contract address.
36
+ * @returns Promise resolving to whether the contract implements ERC1155 Token Receiver interface.
37
+ */
38
+ contractSupportsTokenReceiverInterface(address) {
39
+ return __awaiter(this, void 0, void 0, function* () {
35
40
  return this.contractSupportsInterface(address, controller_utils_1.ERC1155_TOKEN_RECEIVER_INTERFACE_ID);
36
41
  });
37
- /**
38
- * Query if contract implements ERC1155 interface.
39
- *
40
- * @param address - ERC1155 asset contract address.
41
- * @returns Promise resolving to whether the contract implements the base ERC1155 interface.
42
- */
43
- this.contractSupportsBase1155Interface = (address) => __awaiter(this, void 0, void 0, function* () {
42
+ }
43
+ /**
44
+ * Query if contract implements ERC1155 interface.
45
+ *
46
+ * @param address - ERC1155 asset contract address.
47
+ * @returns Promise resolving to whether the contract implements the base ERC1155 interface.
48
+ */
49
+ contractSupportsBase1155Interface(address) {
50
+ return __awaiter(this, void 0, void 0, function* () {
44
51
  return this.contractSupportsInterface(address, controller_utils_1.ERC1155_INTERFACE_ID);
45
52
  });
46
- /**
47
- * Query for tokenURI for a given asset.
48
- *
49
- * @param address - ERC1155 asset contract address.
50
- * @param tokenId - ERC1155 asset identifier.
51
- * @returns Promise resolving to the 'tokenURI'.
52
- */
53
- this.getTokenURI = (address, tokenId) => __awaiter(this, void 0, void 0, function* () {
53
+ }
54
+ /**
55
+ * Query for tokenURI for a given asset.
56
+ *
57
+ * @param address - ERC1155 asset contract address.
58
+ * @param tokenId - ERC1155 asset identifier.
59
+ * @returns Promise resolving to the 'tokenURI'.
60
+ */
61
+ getTokenURI(address, tokenId) {
62
+ return __awaiter(this, void 0, void 0, function* () {
54
63
  const contract = new contracts_1.Contract(address, metamask_eth_abis_1.abiERC1155, this.provider);
55
64
  return contract.uri(tokenId);
56
65
  });
57
- /**
58
- * Query for balance of a given ERC1155 token.
59
- *
60
- * @param contractAddress - ERC1155 asset contract address.
61
- * @param address - Wallet public address.
62
- * @param tokenId - ERC1155 asset identifier.
63
- * @returns Promise resolving to the 'balanceOf'.
64
- */
65
- this.getBalanceOf = (contractAddress, address, tokenId) => __awaiter(this, void 0, void 0, function* () {
66
+ }
67
+ /**
68
+ * Query for balance of a given ERC1155 token.
69
+ *
70
+ * @param contractAddress - ERC1155 asset contract address.
71
+ * @param address - Wallet public address.
72
+ * @param tokenId - ERC1155 asset identifier.
73
+ * @returns Promise resolving to the 'balanceOf'.
74
+ */
75
+ getBalanceOf(contractAddress, address, tokenId) {
76
+ return __awaiter(this, void 0, void 0, function* () {
66
77
  const contract = new contracts_1.Contract(contractAddress, metamask_eth_abis_1.abiERC1155, this.provider);
67
78
  const balance = yield contract.balanceOf(address, tokenId);
68
79
  return (0, assetsUtil_1.ethersBigNumberToBN)(balance);
69
80
  });
70
- /**
71
- * Transfer single ERC1155 token.
72
- * When minting/creating tokens, the from arg MUST be set to 0x0 (i.e. zero address).
73
- * When burning/destroying tokens, the to arg MUST be set to 0x0 (i.e. zero address).
74
- *
75
- * @param operator - ERC1155 token address.
76
- * @param from - ERC1155 token holder.
77
- * @param to - ERC1155 token recipient.
78
- * @param id - ERC1155 token id.
79
- * @param value - Number of tokens to be sent.
80
- * @returns Promise resolving to the 'transferSingle'.
81
- */
82
- this.transferSingle = (operator, from, to, id, value) => __awaiter(this, void 0, void 0, function* () {
81
+ }
82
+ /**
83
+ * Transfer single ERC1155 token.
84
+ * When minting/creating tokens, the from arg MUST be set to 0x0 (i.e. zero address).
85
+ * When burning/destroying tokens, the to arg MUST be set to 0x0 (i.e. zero address).
86
+ *
87
+ * @param operator - ERC1155 token address.
88
+ * @param from - ERC1155 token holder.
89
+ * @param to - ERC1155 token recipient.
90
+ * @param id - ERC1155 token id.
91
+ * @param value - Number of tokens to be sent.
92
+ * @returns Promise resolving to the 'transferSingle'.
93
+ */
94
+ transferSingle(operator, from, to, id, value) {
95
+ return __awaiter(this, void 0, void 0, function* () {
83
96
  const contract = new contracts_1.Contract(operator, metamask_eth_abis_1.abiERC1155, this.provider);
84
97
  return new Promise((resolve, reject) => {
85
98
  contract.transferSingle(operator, from, to, id, value, (error, result) => {
@@ -92,13 +105,15 @@ class ERC1155Standard {
92
105
  });
93
106
  });
94
107
  });
95
- /**
96
- * Query for symbol for a given asset.
97
- *
98
- * @param address - ERC1155 asset contract address.
99
- * @returns Promise resolving to the 'symbol'.
100
- */
101
- this.getAssetSymbol = (address) => __awaiter(this, void 0, void 0, function* () {
108
+ }
109
+ /**
110
+ * Query for symbol for a given asset.
111
+ *
112
+ * @param address - ERC1155 asset contract address.
113
+ * @returns Promise resolving to the 'symbol'.
114
+ */
115
+ getAssetSymbol(address) {
116
+ return __awaiter(this, void 0, void 0, function* () {
102
117
  const contract = new contracts_1.Contract(address,
103
118
  // Contract ABI fragment containing only the symbol method to fetch the symbol of the contract.
104
119
  [
@@ -113,13 +128,15 @@ class ERC1155Standard {
113
128
  ], this.provider);
114
129
  return contract.symbol();
115
130
  });
116
- /**
117
- * Query for name for a given asset.
118
- *
119
- * @param address - ERC1155 asset contract address.
120
- * @returns Promise resolving to the 'name'.
121
- */
122
- this.getAssetName = (address) => __awaiter(this, void 0, void 0, function* () {
131
+ }
132
+ /**
133
+ * Query for name for a given asset.
134
+ *
135
+ * @param address - ERC1155 asset contract address.
136
+ * @returns Promise resolving to the 'name'.
137
+ */
138
+ getAssetName(address) {
139
+ return __awaiter(this, void 0, void 0, function* () {
123
140
  const contract = new contracts_1.Contract(address,
124
141
  // Contract ABI fragment containing only the name method to fetch the name of the contract.
125
142
  [
@@ -134,26 +151,30 @@ class ERC1155Standard {
134
151
  ], this.provider);
135
152
  return contract.name();
136
153
  });
137
- /**
138
- * Query if a contract implements an interface.
139
- *
140
- * @param address - ERC1155 asset contract address.
141
- * @param interfaceId - Interface identifier.
142
- * @returns Promise resolving to whether the contract implements `interfaceID`.
143
- */
144
- this.contractSupportsInterface = (address, interfaceId) => __awaiter(this, void 0, void 0, function* () {
154
+ }
155
+ /**
156
+ * Query if a contract implements an interface.
157
+ *
158
+ * @param address - ERC1155 asset contract address.
159
+ * @param interfaceId - Interface identifier.
160
+ * @returns Promise resolving to whether the contract implements `interfaceID`.
161
+ */
162
+ contractSupportsInterface(address, interfaceId) {
163
+ return __awaiter(this, void 0, void 0, function* () {
145
164
  const contract = new contracts_1.Contract(address, metamask_eth_abis_1.abiERC1155, this.provider);
146
165
  return contract.supportsInterface(interfaceId);
147
166
  });
148
- /**
149
- * Query if a contract implements an interface.
150
- *
151
- * @param address - Asset contract address.
152
- * @param ipfsGateway - The user's preferred IPFS gateway.
153
- * @param tokenId - tokenId of a given token in the contract.
154
- * @returns Promise resolving an object containing the standard, tokenURI, symbol and name of the given contract/tokenId pair.
155
- */
156
- this.getDetails = (address, ipfsGateway, tokenId) => __awaiter(this, void 0, void 0, function* () {
167
+ }
168
+ /**
169
+ * Query if a contract implements an interface.
170
+ *
171
+ * @param address - Asset contract address.
172
+ * @param ipfsGateway - The user's preferred IPFS gateway.
173
+ * @param tokenId - tokenId of a given token in the contract.
174
+ * @returns Promise resolving an object containing the standard, tokenURI, symbol and name of the given contract/tokenId pair.
175
+ */
176
+ getDetails(address, ipfsGateway, tokenId) {
177
+ return __awaiter(this, void 0, void 0, function* () {
157
178
  const isERC1155 = yield this.contractSupportsBase1155Interface(address);
158
179
  if (!isERC1155) {
159
180
  throw new Error("This isn't a valid ERC1155 contract");
@@ -168,7 +189,7 @@ class ERC1155Standard {
168
189
  : uri))
169
190
  : undefined,
170
191
  ]);
171
- if (tokenId) {
192
+ if (tokenURI) {
172
193
  try {
173
194
  const response = yield (0, controller_utils_1.timeoutFetch)(tokenURI);
174
195
  const object = yield response.json();
@@ -178,7 +199,8 @@ class ERC1155Standard {
178
199
  }
179
200
  }
180
201
  catch (_a) {
181
- // ignore
202
+ // Catch block should be kept empty to ignore exceptions, and
203
+ // pass as much information as possible to the return statement
182
204
  }
183
205
  }
184
206
  // TODO consider querying to the metadata to get name.
@@ -190,7 +212,6 @@ class ERC1155Standard {
190
212
  name,
191
213
  };
192
214
  });
193
- this.provider = provider;
194
215
  }
195
216
  }
196
217
  exports.ERC1155Standard = ERC1155Standard;
@@ -1 +1 @@
1
- {"version":3,"file":"ERC1155Standard.js","sourceRoot":"","sources":["../../../../src/Standards/NftStandards/ERC1155/ERC1155Standard.ts"],"names":[],"mappings":";;;;;;;;;;;;AAAA,wDAAoD;AAEpD,iEAOoC;AACpC,mEAAyD;AAGzD,oDAA+E;AAE/E,MAAa,eAAe;IAG1B,YAAY,QAAsB;QAIlC;;;;;WAKG;QACH,yCAAoC,GAAG,CACrC,OAAe,EACG,EAAE;YACpB,OAAO,IAAI,CAAC,yBAAyB,CACnC,OAAO,EACP,oDAAiC,CAClC,CAAC;QACJ,CAAC,CAAA,CAAC;QAEF;;;;;WAKG;QACH,2CAAsC,GAAG,CACvC,OAAe,EACG,EAAE;YACpB,OAAO,IAAI,CAAC,yBAAyB,CACnC,OAAO,EACP,sDAAmC,CACpC,CAAC;QACJ,CAAC,CAAA,CAAC;QAEF;;;;;WAKG;QACH,sCAAiC,GAAG,CAClC,OAAe,EACG,EAAE;YACpB,OAAO,IAAI,CAAC,yBAAyB,CAAC,OAAO,EAAE,uCAAoB,CAAC,CAAC;QACvE,CAAC,CAAA,CAAC;QAEF;;;;;;WAMG;QACH,gBAAW,GAAG,CAAO,OAAe,EAAE,OAAe,EAAmB,EAAE;YACxE,MAAM,QAAQ,GAAG,IAAI,oBAAQ,CAAC,OAAO,EAAE,8BAAU,EAAE,IAAI,CAAC,QAAQ,CAAC,CAAC;YAClE,OAAO,QAAQ,CAAC,GAAG,CAAC,OAA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,CAAC;IAC3B,CAAC;CAiPF;AAtPD,0CAsPC","sourcesContent":["import { Contract } from '@ethersproject/contracts';\nimport type { Web3Provider } from '@ethersproject/providers';\nimport {\n ERC1155,\n ERC1155_INTERFACE_ID,\n ERC1155_METADATA_URI_INTERFACE_ID,\n ERC1155_TOKEN_RECEIVER_INTERFACE_ID,\n safelyExecute,\n timeoutFetch,\n} from '@metamask/controller-utils';\nimport { abiERC1155 } from '@metamask/metamask-eth-abis';\nimport type { BN } from 'ethereumjs-util';\n\nimport { getFormattedIpfsUrl, ethersBigNumberToBN } from '../../../assetsUtil';\n\nexport class ERC1155Standard {\n private readonly provider: Web3Provider;\n\n constructor(provider: Web3Provider) {\n this.provider = provider;\n }\n\n /**\n * Query if contract implements ERC1155 URI Metadata interface.\n *\n * @param address - ERC1155 asset contract address.\n * @returns Promise resolving to whether the contract implements ERC1155 URI Metadata interface.\n */\n contractSupportsURIMetadataInterface = async (\n address: string,\n ): Promise<boolean> => {\n return this.contractSupportsInterface(\n address,\n ERC1155_METADATA_URI_INTERFACE_ID,\n );\n };\n\n /**\n * Query if contract implements ERC1155 Token Receiver interface.\n *\n * @param address - ERC1155 asset contract address.\n * @returns Promise resolving to whether the contract implements ERC1155 Token Receiver interface.\n */\n contractSupportsTokenReceiverInterface = async (\n address: string,\n ): Promise<boolean> => {\n return this.contractSupportsInterface(\n address,\n ERC1155_TOKEN_RECEIVER_INTERFACE_ID,\n );\n };\n\n /**\n * Query if contract implements ERC1155 interface.\n *\n * @param address - ERC1155 asset contract address.\n * @returns Promise resolving to whether the contract implements the base ERC1155 interface.\n */\n contractSupportsBase1155Interface = async (\n address: string,\n ): Promise<boolean> => {\n return this.contractSupportsInterface(address, ERC1155_INTERFACE_ID);\n };\n\n /**\n * Query for tokenURI for a given asset.\n *\n * @param address - ERC1155 asset contract address.\n * @param tokenId - ERC1155 asset identifier.\n * @returns Promise resolving to the 'tokenURI'.\n */\n getTokenURI = async (address: string, tokenId: string): Promise<string> => {\n const contract = new Contract(address, abiERC1155, this.provider);\n return contract.uri(tokenId);\n };\n\n /**\n * Query for balance of a given ERC1155 token.\n *\n * @param contractAddress - ERC1155 asset contract address.\n * @param address - Wallet public address.\n * @param tokenId - ERC1155 asset identifier.\n * @returns Promise resolving to the 'balanceOf'.\n */\n getBalanceOf = async (\n contractAddress: string,\n address: string,\n tokenId: string,\n ): Promise<BN> => {\n const contract = new Contract(contractAddress, abiERC1155, this.provider);\n const balance = await contract.balanceOf(address, tokenId);\n return ethersBigNumberToBN(balance);\n };\n\n /**\n * Transfer single ERC1155 token.\n * When minting/creating tokens, the from arg MUST be set to 0x0 (i.e. zero address).\n * When burning/destroying tokens, the to arg MUST be set to 0x0 (i.e. zero address).\n *\n * @param operator - ERC1155 token address.\n * @param from - ERC1155 token holder.\n * @param to - ERC1155 token recipient.\n * @param id - ERC1155 token id.\n * @param value - Number of tokens to be sent.\n * @returns Promise resolving to the 'transferSingle'.\n */\n transferSingle = async (\n operator: string,\n from: string,\n to: string,\n id: string,\n value: string,\n ): Promise<void> => {\n const contract = new Contract(operator, abiERC1155, this.provider);\n return new Promise<void>((resolve, reject) => {\n contract.transferSingle(\n operator,\n from,\n to,\n id,\n value,\n (error: Error, result: void) => {\n /* istanbul ignore if */\n if (error) {\n reject(error);\n return;\n }\n resolve(result);\n },\n );\n });\n };\n\n /**\n * Query for symbol for a given asset.\n *\n * @param address - ERC1155 asset contract address.\n * @returns Promise resolving to the 'symbol'.\n */\n getAssetSymbol = async (address: string): Promise<string> => {\n const contract = new Contract(\n address,\n // Contract ABI fragment containing only the symbol method to fetch the symbol of the contract.\n [\n {\n inputs: [],\n name: 'symbol',\n outputs: [{ name: '_symbol', type: 'string' }],\n stateMutability: 'view',\n type: 'function',\n payable: false,\n },\n ],\n this.provider,\n );\n return contract.symbol();\n };\n\n /**\n * Query for name for a given asset.\n *\n * @param address - ERC1155 asset contract address.\n * @returns Promise resolving to the 'name'.\n */\n getAssetName = async (address: string): Promise<string> => {\n const contract = new Contract(\n address,\n // Contract ABI fragment containing only the name method to fetch the name of the contract.\n [\n {\n inputs: [],\n name: 'name',\n outputs: [{ name: '_name', type: 'string' }],\n stateMutability: 'view',\n type: 'function',\n payable: false,\n },\n ],\n this.provider,\n );\n return contract.name();\n };\n\n /**\n * Query if a contract implements an interface.\n *\n * @param address - ERC1155 asset contract address.\n * @param interfaceId - Interface identifier.\n * @returns Promise resolving to whether the contract implements `interfaceID`.\n */\n private readonly contractSupportsInterface = async (\n address: string,\n interfaceId: string,\n ): Promise<boolean> => {\n const contract = new Contract(address, abiERC1155, this.provider);\n return contract.supportsInterface(interfaceId);\n };\n\n /**\n * Query if a contract implements an interface.\n *\n * @param address - Asset contract address.\n * @param ipfsGateway - The user's preferred IPFS gateway.\n * @param tokenId - tokenId of a given token in the contract.\n * @returns Promise resolving an object containing the standard, tokenURI, symbol and name of the given contract/tokenId pair.\n */\n getDetails = async (\n address: string,\n ipfsGateway: string,\n tokenId?: string,\n ): Promise<{\n standard: string;\n tokenURI: string | undefined;\n image: string | undefined;\n name: string | undefined;\n symbol: string | undefined;\n }> => {\n const isERC1155 = await this.contractSupportsBase1155Interface(address);\n\n if (!isERC1155) {\n throw new Error(\"This isn't a valid ERC1155 contract\");\n }\n\n let image;\n\n const [symbol, name, tokenURI] = await Promise.all([\n safelyExecute(() => this.getAssetSymbol(address)),\n safelyExecute(() => this.getAssetName(address)),\n tokenId\n ? safelyExecute(() =>\n this.getTokenURI(address, tokenId).then((uri) =>\n uri.startsWith('ipfs://')\n ? getFormattedIpfsUrl(ipfsGateway, uri, true)\n : uri,\n ),\n )\n : undefined,\n ]);\n\n if (tokenId) {\n try {\n const response = await timeoutFetch(tokenURI);\n const object = await response.json();\n image = object?.image;\n if (image?.startsWith('ipfs://')) {\n image = getFormattedIpfsUrl(ipfsGateway, image, true);\n }\n } catch {\n // ignore\n }\n }\n\n // TODO consider querying to the metadata to get name.\n return {\n standard: ERC1155,\n tokenURI,\n image,\n symbol,\n name,\n };\n };\n}\n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,EAAE,IAAI,CAAC;wBAC7C,CAAC,CAAC,GAAG,CACR,CACF;oBACH,CAAC,CAAC,SAAS;aACd,CAAC,CAAC;YAEH,IAAI,QAAQ,EAAE;gBACZ,IAAI;oBACF,MAAM,QAAQ,GAAG,MAAM,IAAA,+BAAY,EAAC,QAAQ,CAAC,CAAC;oBAC9C,MAAM,MAAM,GAAG,MAAM,QAAQ,CAAC,IAAI,EAAE,CAAC;oBACrC,KAAK,GAAG,MAAM,aAAN,MAAM,uBAAN,MAAM,CAAE,KAAK,CAAC;oBACtB,IAAI,KAAK,aAAL,KAAK,uBAAL,KAAK,CAAE,UAAU,CAAC,SAAS,CAAC,EAAE;wBAChC,KAAK,GAAG,IAAA,gCAAmB,EAAC,WAAW,EAAE,KAAK,EAAE,IAAI,CAAC,CAAC;qBACvD;iBACF;gBAAC,WAAM;oBACN,6DAA6D;oBAC7D,+DAA+D;iBAChE;aACF;YAED,sDAAsD;YACtD,OAAO;gBACL,QAAQ,EAAE,0BAAO;gBACjB,QAAQ;gBACR,KAAK;gBACL,MAAM;gBACN,IAAI;aACL,CAAC;QACJ,CAAC;KAAA;CACF;AArPD,0CAqPC","sourcesContent":["import { Contract } from '@ethersproject/contracts';\nimport type { Web3Provider } from '@ethersproject/providers';\nimport {\n ERC1155,\n ERC1155_INTERFACE_ID,\n ERC1155_METADATA_URI_INTERFACE_ID,\n ERC1155_TOKEN_RECEIVER_INTERFACE_ID,\n safelyExecute,\n timeoutFetch,\n} from '@metamask/controller-utils';\nimport { abiERC1155 } from '@metamask/metamask-eth-abis';\nimport type { BN } from 'ethereumjs-util';\n\nimport { getFormattedIpfsUrl, ethersBigNumberToBN } from '../../../assetsUtil';\n\nexport class ERC1155Standard {\n private readonly provider: Web3Provider;\n\n constructor(provider: Web3Provider) {\n this.provider = provider;\n }\n\n /**\n * Query if contract implements ERC1155 URI Metadata interface.\n *\n * @param address - ERC1155 asset contract address.\n * @returns Promise resolving to whether the contract implements ERC1155 URI Metadata interface.\n */\n async contractSupportsURIMetadataInterface(\n address: string,\n ): Promise<boolean> {\n return this.contractSupportsInterface(\n address,\n ERC1155_METADATA_URI_INTERFACE_ID,\n );\n }\n\n /**\n * Query if contract implements ERC1155 Token Receiver interface.\n *\n * @param address - ERC1155 asset contract address.\n * @returns Promise resolving to whether the contract implements ERC1155 Token Receiver interface.\n */\n async contractSupportsTokenReceiverInterface(\n address: string,\n ): Promise<boolean> {\n return this.contractSupportsInterface(\n address,\n ERC1155_TOKEN_RECEIVER_INTERFACE_ID,\n );\n }\n\n /**\n * Query if contract implements ERC1155 interface.\n *\n * @param address - ERC1155 asset contract address.\n * @returns Promise resolving to whether the contract implements the base ERC1155 interface.\n */\n async contractSupportsBase1155Interface(address: string): Promise<boolean> {\n return this.contractSupportsInterface(address, ERC1155_INTERFACE_ID);\n }\n\n /**\n * Query for tokenURI for a given asset.\n *\n * @param address - ERC1155 asset contract address.\n * @param tokenId - ERC1155 asset identifier.\n * @returns Promise resolving to the 'tokenURI'.\n */\n async getTokenURI(address: string, tokenId: string): Promise<string> {\n const contract = new Contract(address, abiERC1155, this.provider);\n return contract.uri(tokenId);\n }\n\n /**\n * Query for balance of a given ERC1155 token.\n *\n * @param contractAddress - ERC1155 asset contract address.\n * @param address - Wallet public address.\n * @param tokenId - ERC1155 asset identifier.\n * @returns Promise resolving to the 'balanceOf'.\n */\n async getBalanceOf(\n contractAddress: string,\n address: string,\n tokenId: string,\n ): Promise<BN> {\n const contract = new Contract(contractAddress, abiERC1155, this.provider);\n const balance = await contract.balanceOf(address, tokenId);\n return ethersBigNumberToBN(balance);\n }\n\n /**\n * Transfer single ERC1155 token.\n * When minting/creating tokens, the from arg MUST be set to 0x0 (i.e. zero address).\n * When burning/destroying tokens, the to arg MUST be set to 0x0 (i.e. zero address).\n *\n * @param operator - ERC1155 token address.\n * @param from - ERC1155 token holder.\n * @param to - ERC1155 token recipient.\n * @param id - ERC1155 token id.\n * @param value - Number of tokens to be sent.\n * @returns Promise resolving to the 'transferSingle'.\n */\n async transferSingle(\n operator: string,\n from: string,\n to: string,\n id: string,\n value: string,\n ): Promise<void> {\n const contract = new Contract(operator, abiERC1155, this.provider);\n return new Promise<void>((resolve, reject) => {\n contract.transferSingle(\n operator,\n from,\n to,\n id,\n value,\n (error: Error, result: void) => {\n /* istanbul ignore if */\n if (error) {\n reject(error);\n return;\n }\n resolve(result);\n },\n );\n });\n }\n\n /**\n * Query for symbol for a given asset.\n *\n * @param address - ERC1155 asset contract address.\n * @returns Promise resolving to the 'symbol'.\n */\n async getAssetSymbol(address: string): Promise<string> {\n const contract = new Contract(\n address,\n // Contract ABI fragment containing only the symbol method to fetch the symbol of the contract.\n [\n {\n inputs: [],\n name: 'symbol',\n outputs: [{ name: '_symbol', type: 'string' }],\n stateMutability: 'view',\n type: 'function',\n payable: false,\n },\n ],\n this.provider,\n );\n return contract.symbol();\n }\n\n /**\n * Query for name for a given asset.\n *\n * @param address - ERC1155 asset contract address.\n * @returns Promise resolving to the 'name'.\n */\n async getAssetName(address: string): Promise<string> {\n const contract = new Contract(\n address,\n // Contract ABI fragment containing only the name method to fetch the name of the contract.\n [\n {\n inputs: [],\n name: 'name',\n outputs: [{ name: '_name', type: 'string' }],\n stateMutability: 'view',\n type: 'function',\n payable: false,\n },\n ],\n this.provider,\n );\n return contract.name();\n }\n\n /**\n * Query if a contract implements an interface.\n *\n * @param address - ERC1155 asset contract address.\n * @param interfaceId - Interface identifier.\n * @returns Promise resolving to whether the contract implements `interfaceID`.\n */\n private async contractSupportsInterface(\n address: string,\n interfaceId: string,\n ): Promise<boolean> {\n const contract = new Contract(address, abiERC1155, this.provider);\n return contract.supportsInterface(interfaceId);\n }\n\n /**\n * Query if a contract implements an interface.\n *\n * @param address - Asset contract address.\n * @param ipfsGateway - The user's preferred IPFS gateway.\n * @param tokenId - tokenId of a given token in the contract.\n * @returns Promise resolving an object containing the standard, tokenURI, symbol and name of the given contract/tokenId pair.\n */\n async getDetails(\n address: string,\n ipfsGateway: string,\n tokenId?: string,\n ): Promise<{\n standard: string;\n tokenURI: string | undefined;\n image: string | undefined;\n name: string | undefined;\n symbol: string | undefined;\n }> {\n const isERC1155 = await this.contractSupportsBase1155Interface(address);\n\n if (!isERC1155) {\n throw new Error(\"This isn't a valid ERC1155 contract\");\n }\n\n let image;\n\n const [symbol, name, tokenURI] = await Promise.all([\n safelyExecute(() => this.getAssetSymbol(address)),\n safelyExecute(() => this.getAssetName(address)),\n tokenId\n ? safelyExecute(() =>\n this.getTokenURI(address, tokenId).then((uri) =>\n uri.startsWith('ipfs://')\n ? getFormattedIpfsUrl(ipfsGateway, uri, true)\n : uri,\n ),\n )\n : undefined,\n ]);\n\n if (tokenURI) {\n try {\n const response = await timeoutFetch(tokenURI);\n const object = await response.json();\n image = object?.image;\n if (image?.startsWith('ipfs://')) {\n image = getFormattedIpfsUrl(ipfsGateway, image, true);\n }\n } catch {\n // Catch block should be kept empty to ignore exceptions, and\n // pass as much information as possible to the return statement\n }\n }\n\n // TODO consider querying to the metadata to get name.\n return {\n standard: ERC1155,\n tokenURI,\n image,\n symbol,\n name,\n };\n }\n}\n"]}
@@ -1 +1 @@
1
- {"version":3,"file":"ERC721Standard.d.ts","sourceRoot":"","sources":["../../../../src/Standards/NftStandards/ERC721/ERC721Standard.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAE,YAAY,EAAE,MAAM,0BAA0B,CAAC;AAa7D,qBAAa,cAAc;IACzB,OAAO,CAAC,QAAQ,CAAC,QAAQ,CAAe;gBAE5B,QAAQ,EAAE,YAAY;IAIlC;;;;;OAKG;IACH,iCAAiC,YACtB,MAAM,KACd,QAAQ,OAAO,CAAC,CAKjB;IAEF;;;;;OAKG;IACH,mCAAmC,YACxB,MAAM,KACd,QAAQ,OAAO,CAAC,CAKjB;IAEF;;;;;OAKG;IACH,gCAAgC,YACrB,MAAM,KACd,QAAQ,OAAO,CAAC,CAEjB;IAEF;;;;;;;OAOG;IACH,aAAa,YACF,MAAM,mBACE,MAAM,SAChB,MA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,MAAM,WAAW,MAAM,KAAG,QAAQ,MAAM,CAAC,CASrE;IAEF;;;;;OAKG;IACH,YAAY,YAAmB,MAAM,KAAG,QAAQ,MAAM,CAAC,CAGrD;IAEF;;;;;OAKG;IACH,cAAc,YAAmB,MAAM,KAAG,QAAQ,MAAM,CAAC,CAGvD;IAEF;;;;;;OAMG;IACG,UAAU,CAAC,OAAO,EAAE,MAAM,EAAE,OAAO,EAAE,MAAM,GAAG,OAAO,CAAC,MAAM,CAAC;IAKnE;;;;;;OAMG;IACH,OAAO,CAAC,QAAQ,CAAC,yBAAyB,CAgBxC;IAEF;;;;;;;OAOG;IACH,UAAU,YACC,MAAM,eACF,MAAM,YACT,MAAM;kBAEN,MAAM;kBACN,MAAM,GAAG,SAAS;gBACpB,MAAM,GAAG,SAAS;cACpB,MAAM,GAAG,SAAS;eACjB,MAAM,GAAG,SAAS;OA0CzB;CACH"}
@@ -101,6 +101,8 @@ class ERC721Standard {
101
101
  const contract = new contracts_1.Contract(address, metamask_eth_abis_1.abiERC721, this.provider);
102
102
  try {
103
103
  return yield contract.supportsInterface(interfaceId);
104
+ // TODO: Replace `any` with type
105
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
104
106
  }
105
107
  catch (err) {
106
108
  // Mirror previous implementation