@metamask/assets-controllers 1.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (58) hide show
  1. package/CHANGELOG.md +22 -0
  2. package/LICENSE +20 -0
  3. package/README.md +30 -0
  4. package/dist/AccountTrackerController.d.ts +88 -0
  5. package/dist/AccountTrackerController.js +138 -0
  6. package/dist/AccountTrackerController.js.map +1 -0
  7. package/dist/AssetsContractController.d.ts +176 -0
  8. package/dist/AssetsContractController.js +314 -0
  9. package/dist/AssetsContractController.js.map +1 -0
  10. package/dist/CurrencyRateController.d.ts +98 -0
  11. package/dist/CurrencyRateController.js +193 -0
  12. package/dist/CurrencyRateController.js.map +1 -0
  13. package/dist/NftController.d.ts +409 -0
  14. package/dist/NftController.js +835 -0
  15. package/dist/NftController.js.map +1 -0
  16. package/dist/NftDetectionController.d.ts +179 -0
  17. package/dist/NftDetectionController.js +204 -0
  18. package/dist/NftDetectionController.js.map +1 -0
  19. package/dist/Standards/ERC20Standard.d.ts +42 -0
  20. package/dist/Standards/ERC20Standard.js +121 -0
  21. package/dist/Standards/ERC20Standard.js.map +1 -0
  22. package/dist/Standards/NftStandards/ERC1155/ERC1155Standard.d.ts +78 -0
  23. package/dist/Standards/NftStandards/ERC1155/ERC1155Standard.js +148 -0
  24. package/dist/Standards/NftStandards/ERC1155/ERC1155Standard.js.map +1 -0
  25. package/dist/Standards/NftStandards/ERC721/ERC721Standard.d.ts +88 -0
  26. package/dist/Standards/NftStandards/ERC721/ERC721Standard.js +182 -0
  27. package/dist/Standards/NftStandards/ERC721/ERC721Standard.js.map +1 -0
  28. package/dist/Standards/standards-types.d.ts +14 -0
  29. package/dist/Standards/standards-types.js +3 -0
  30. package/dist/Standards/standards-types.js.map +1 -0
  31. package/dist/TokenBalancesController.d.ts +69 -0
  32. package/dist/TokenBalancesController.js +94 -0
  33. package/dist/TokenBalancesController.js.map +1 -0
  34. package/dist/TokenDetectionController.d.ts +84 -0
  35. package/dist/TokenDetectionController.js +185 -0
  36. package/dist/TokenDetectionController.js.map +1 -0
  37. package/dist/TokenListController.d.ts +114 -0
  38. package/dist/TokenListController.js +256 -0
  39. package/dist/TokenListController.js.map +1 -0
  40. package/dist/TokenRatesController.d.ts +167 -0
  41. package/dist/TokenRatesController.js +284 -0
  42. package/dist/TokenRatesController.js.map +1 -0
  43. package/dist/TokensController.d.ts +238 -0
  44. package/dist/TokensController.js +530 -0
  45. package/dist/TokensController.js.map +1 -0
  46. package/dist/assetsUtil.d.ts +106 -0
  47. package/dist/assetsUtil.js +228 -0
  48. package/dist/assetsUtil.js.map +1 -0
  49. package/dist/crypto-compare.d.ts +12 -0
  50. package/dist/crypto-compare.js +67 -0
  51. package/dist/crypto-compare.js.map +1 -0
  52. package/dist/index.d.ts +11 -0
  53. package/dist/index.js +31 -0
  54. package/dist/index.js.map +1 -0
  55. package/dist/token-service.d.ts +29 -0
  56. package/dist/token-service.js +134 -0
  57. package/dist/token-service.js.map +1 -0
  58. package/package.json +75 -0
@@ -0,0 +1 @@
1
+ {"version":3,"file":"CurrencyRateController.js","sourceRoot":"","sources":["../src/CurrencyRateController.ts"],"names":[],"mappings":";;;;;;;;;;;;AAAA,6CAAoC;AAEpC,+DAGmC;AACnC,iEAIoC;AACpC,qDAAiF;AAsBjF,MAAM,IAAI,GAAG,wBAAwB,CAAC;AAoBtC,MAAM,QAAQ,GAAG;IACf,cAAc,EAAE,EAAE,OAAO,EAAE,IAAI,EAAE,SAAS,EAAE,IAAI,EAAE;IAClD,cAAc,EAAE,EAAE,OAAO,EAAE,IAAI,EAAE,SAAS,EAAE,IAAI,EAAE;IAClD,eAAe,EAAE,EAAE,OAAO,EAAE,IAAI,EAAE,SAAS,EAAE,IAAI,EAAE;IACnD,cAAc,EAAE,EAAE,OAAO,EAAE,IAAI,EAAE,SAAS,EAAE,IAAI,EAAE;IAClD,sBAAsB,EAAE,EAAE,OAAO,EAAE,KAAK,EAAE,SAAS,EAAE,IAAI,EA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F;AA9LD,wDA8LC;AAED,kBAAe,sBAAsB,CAAC","sourcesContent":["import { Mutex } from 'async-mutex';\nimport type { Patch } from 'immer';\nimport {\n BaseControllerV2,\n RestrictedControllerMessenger,\n} from '@metamask/base-controller';\nimport {\n TESTNET_TICKER_SYMBOLS,\n FALL_BACK_VS_CURRENCY,\n safelyExecute,\n} from '@metamask/controller-utils';\nimport { fetchExchangeRate as defaultFetchExchangeRate } from './crypto-compare';\n\n/**\n * @type CurrencyRateState\n * @property conversionDate - Timestamp of conversion rate expressed in ms since UNIX epoch\n * @property conversionRate - Conversion rate from current base asset to the current currency\n * @property currentCurrency - Currently-active ISO 4217 currency code\n * @property nativeCurrency - Symbol for the base asset used for conversion\n * @property pendingCurrentCurrency - The currency being switched to\n * @property pendingNativeCurrency - The base asset currency being switched to\n * @property usdConversionRate - Conversion rate from usd to the current currency\n */\nexport type CurrencyRateState = {\n conversionDate: number | null;\n conversionRate: number | null;\n currentCurrency: string;\n nativeCurrency: string;\n pendingCurrentCurrency: string | null;\n pendingNativeCurrency: string | null;\n usdConversionRate: number | null;\n};\n\nconst name = 'CurrencyRateController';\n\nexport type CurrencyRateStateChange = {\n type: `${typeof name}:stateChange`;\n payload: [CurrencyRateState, Patch[]];\n};\n\nexport type GetCurrencyRateState = {\n type: `${typeof name}:getState`;\n handler: () => CurrencyRateState;\n};\n\ntype CurrencyRateMessenger = RestrictedControllerMessenger<\n typeof name,\n GetCurrencyRateState,\n CurrencyRateStateChange,\n never,\n never\n>;\n\nconst metadata = {\n conversionDate: { persist: true, anonymous: true },\n conversionRate: { persist: true, anonymous: true },\n currentCurrency: { persist: true, anonymous: true },\n nativeCurrency: { persist: true, anonymous: true },\n pendingCurrentCurrency: { persist: false, anonymous: true },\n pendingNativeCurrency: { persist: false, anonymous: true },\n usdConversionRate: { persist: true, anonymous: true },\n};\n\nconst defaultState = {\n conversionDate: 0,\n conversionRate: 0,\n currentCurrency: 'usd',\n nativeCurrency: 'ETH',\n pendingCurrentCurrency: null,\n pendingNativeCurrency: null,\n usdConversionRate: null,\n};\n\n/**\n * Controller that passively polls on a set interval for an exchange rate from the current network\n * asset to the user's preferred currency.\n */\nexport class CurrencyRateController extends BaseControllerV2<\n typeof name,\n CurrencyRateState,\n CurrencyRateMessenger\n> {\n private mutex = new Mutex();\n\n private intervalId?: ReturnType<typeof setTimeout>;\n\n private intervalDelay;\n\n private fetchExchangeRate;\n\n private includeUsdRate;\n\n /**\n * Creates a CurrencyRateController instance.\n *\n * @param options - Constructor options.\n * @param options.includeUsdRate - Keep track of the USD rate in addition to the current currency rate.\n * @param options.interval - The polling interval, in milliseconds.\n * @param options.messenger - A reference to the messaging system.\n * @param options.state - Initial state to set on this controller.\n * @param options.fetchExchangeRate - Fetches the exchange rate from an external API. This option is primarily meant for use in unit tests.\n */\n constructor({\n includeUsdRate = false,\n interval = 180000,\n messenger,\n state,\n fetchExchangeRate = defaultFetchExchangeRate,\n }: {\n includeUsdRate?: boolean;\n interval?: number;\n messenger: CurrencyRateMessenger;\n state?: Partial<CurrencyRateState>;\n fetchExchangeRate?: typeof defaultFetchExchangeRate;\n }) {\n super({\n name,\n metadata,\n messenger,\n state: { ...defaultState, ...state },\n });\n this.includeUsdRate = includeUsdRate;\n this.intervalDelay = interval;\n this.fetchExchangeRate = fetchExchangeRate;\n }\n\n /**\n * Start polling for the currency rate.\n */\n async start() {\n await this.startPolling();\n }\n\n /**\n * Stop polling for the currency rate.\n */\n stop() {\n this.stopPolling();\n }\n\n /**\n * Prepare to discard this controller.\n *\n * This stops any active polling.\n */\n override destroy() {\n super.destroy();\n this.stopPolling();\n }\n\n /**\n * Sets a currency to track.\n *\n * @param currentCurrency - ISO 4217 currency code.\n */\n async setCurrentCurrency(currentCurrency: string) {\n this.update((state) => {\n state.pendingCurrentCurrency = currentCurrency;\n });\n await this.updateExchangeRate();\n }\n\n /**\n * Sets a new native currency.\n *\n * @param symbol - Symbol for the base asset.\n */\n async setNativeCurrency(symbol: string) {\n this.update((state) => {\n state.pendingNativeCurrency = symbol;\n });\n await this.updateExchangeRate();\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 private async startPolling(): Promise<void> {\n this.stopPolling();\n // TODO: Expose polling currency rate update errors\n await safelyExecute(() => this.updateExchangeRate());\n this.intervalId = setInterval(async () => {\n await safelyExecute(() => this.updateExchangeRate());\n }, this.intervalDelay);\n }\n\n /**\n * Updates exchange rate for the current currency.\n *\n * @returns The controller state.\n */\n async updateExchangeRate(): Promise<CurrencyRateState | void> {\n const releaseLock = await this.mutex.acquire();\n const {\n currentCurrency: stateCurrentCurrency,\n nativeCurrency: stateNativeCurrency,\n pendingCurrentCurrency,\n pendingNativeCurrency,\n } = this.state;\n\n let conversionDate: number | null = null;\n let conversionRate: number | null = null;\n let usdConversionRate: number | null = null;\n const currentCurrency = pendingCurrentCurrency ?? stateCurrentCurrency;\n const nativeCurrency = pendingNativeCurrency ?? stateNativeCurrency;\n\n // For preloaded testnets (Rinkeby, Ropsten, Goerli, Kovan) we want to fetch exchange rate for real ETH.\n const nativeCurrencyForExchangeRate = Object.values(\n TESTNET_TICKER_SYMBOLS,\n ).includes(nativeCurrency)\n ? FALL_BACK_VS_CURRENCY // ETH\n : nativeCurrency;\n\n try {\n if (\n currentCurrency &&\n nativeCurrency &&\n // if either currency is an empty string we can skip the comparison\n // because it will result in an error from the api and ultimately\n // a null conversionRate either way.\n currentCurrency !== '' &&\n nativeCurrency !== ''\n ) {\n ({ conversionRate, usdConversionRate } = await this.fetchExchangeRate(\n currentCurrency,\n nativeCurrencyForExchangeRate,\n this.includeUsdRate,\n ));\n conversionDate = Date.now() / 1000;\n }\n } catch (error) {\n if (\n !(\n error instanceof Error &&\n error.message.includes('market does not exist for this coin pair')\n )\n ) {\n throw error;\n }\n } finally {\n try {\n this.update(() => {\n return {\n conversionDate,\n conversionRate,\n // we currently allow and handle an empty string as a valid nativeCurrency\n // in cases where a user has not entered a native ticker symbol for a custom network\n // currentCurrency is not from user input but this protects us from unexpected changes.\n nativeCurrency,\n currentCurrency,\n pendingCurrentCurrency: null,\n pendingNativeCurrency: null,\n usdConversionRate,\n };\n });\n } finally {\n releaseLock();\n }\n }\n return this.state;\n }\n}\n\nexport default CurrencyRateController;\n"]}
@@ -0,0 +1,409 @@
1
+ /// <reference types="node" />
2
+ import { EventEmitter } from 'events';
3
+ import { BaseController, BaseConfig, BaseState } from '@metamask/base-controller';
4
+ import type { PreferencesState } from '@metamask/preferences-controller';
5
+ import type { NetworkState } from '@metamask/network-controller';
6
+ import { NetworkType } from '@metamask/controller-utils';
7
+ import type { ApiNftCreator, ApiNftLastSale } from './NftDetectionController';
8
+ import type { AssetsContractController } from './AssetsContractController';
9
+ /**
10
+ * @type Nft
11
+ *
12
+ * NFT representation
13
+ * @property address - Hex address of a ERC721 contract
14
+ * @property description - The NFT description
15
+ * @property image - URI of custom NFT image associated with this tokenId
16
+ * @property name - Name associated with this tokenId and contract address
17
+ * @property tokenId - The NFT identifier
18
+ * @property numberOfSales - Number of sales
19
+ * @property backgroundColor - The background color to be displayed with the item
20
+ * @property imagePreview - URI of a smaller image associated with this NFT
21
+ * @property imageThumbnail - URI of a thumbnail image associated with this NFT
22
+ * @property imageOriginal - URI of the original image associated with this NFT
23
+ * @property animation - URI of a animation associated with this NFT
24
+ * @property animationOriginal - URI of the original animation associated with this NFT
25
+ * @property externalLink - External link containing additional information
26
+ * @property creator - The NFT owner information object
27
+ * @property isCurrentlyOwned - Boolean indicating whether the address/chainId combination where it's currently stored currently owns this NFT
28
+ * @property transactionId - Transaction Id associated with the NFT
29
+ */
30
+ export interface Nft extends NftMetadata {
31
+ tokenId: string;
32
+ address: string;
33
+ isCurrentlyOwned?: boolean;
34
+ }
35
+ /**
36
+ * @type NftContract
37
+ *
38
+ * NFT contract information representation
39
+ * @property name - Contract name
40
+ * @property logo - Contract logo
41
+ * @property address - Contract address
42
+ * @property symbol - Contract symbol
43
+ * @property description - Contract description
44
+ * @property totalSupply - Total supply of NFTs
45
+ * @property assetContractType - The NFT type, it could be `semi-fungible` or `non-fungible`
46
+ * @property createdDate - Creation date
47
+ * @property schemaName - The schema followed by the contract, it could be `ERC721` or `ERC1155`
48
+ * @property externalLink - External link containing additional information
49
+ */
50
+ export interface NftContract {
51
+ name?: string;
52
+ logo?: string;
53
+ address: string;
54
+ symbol?: string;
55
+ description?: string;
56
+ totalSupply?: string;
57
+ assetContractType?: string;
58
+ createdDate?: string;
59
+ schemaName?: string;
60
+ externalLink?: string;
61
+ }
62
+ /**
63
+ * @type NftMetadata
64
+ *
65
+ * NFT custom information
66
+ * @property name - NFT custom name
67
+ * @property description - The NFT description
68
+ * @property numberOfSales - Number of sales
69
+ * @property backgroundColor - The background color to be displayed with the item
70
+ * @property image - Image custom image URI
71
+ * @property imagePreview - URI of a smaller image associated with this NFT
72
+ * @property imageThumbnail - URI of a thumbnail image associated with this NFT
73
+ * @property imageOriginal - URI of the original image associated with this NFT
74
+ * @property animation - URI of a animation associated with this NFT
75
+ * @property animationOriginal - URI of the original animation associated with this NFT
76
+ * @property externalLink - External link containing additional information
77
+ * @property creator - The NFT owner information object
78
+ * @property standard - NFT standard name for the NFT, e.g., ERC-721 or ERC-1155
79
+ */
80
+ export interface NftMetadata {
81
+ name: string | null;
82
+ description: string | null;
83
+ image: string | null;
84
+ standard: string | null;
85
+ favorite?: boolean;
86
+ numberOfSales?: number;
87
+ backgroundColor?: string;
88
+ imagePreview?: string;
89
+ imageThumbnail?: string;
90
+ imageOriginal?: string;
91
+ animation?: string;
92
+ animationOriginal?: string;
93
+ externalLink?: string;
94
+ creator?: ApiNftCreator;
95
+ lastSale?: ApiNftLastSale;
96
+ transactionId?: string;
97
+ }
98
+ interface AccountParams {
99
+ userAddress: string;
100
+ chainId: string;
101
+ }
102
+ /**
103
+ * @type NftConfig
104
+ *
105
+ * NFT controller configuration
106
+ * @property networkType - Network ID as per net_version
107
+ * @property selectedAddress - Vault selected address
108
+ */
109
+ export interface NftConfig extends BaseConfig {
110
+ networkType: NetworkType;
111
+ selectedAddress: string;
112
+ chainId: string;
113
+ ipfsGateway: string;
114
+ openSeaEnabled: boolean;
115
+ useIPFSSubdomains: boolean;
116
+ }
117
+ /**
118
+ * @type NftState
119
+ *
120
+ * NFT controller state
121
+ * @property allNftContracts - Object containing NFT contract information
122
+ * @property allNfts - Object containing NFTs per account and network
123
+ * @property ignoredNfts - List of NFTs that should be ignored
124
+ */
125
+ export interface NftState extends BaseState {
126
+ allNftContracts: {
127
+ [key: string]: {
128
+ [key: string]: NftContract[];
129
+ };
130
+ };
131
+ allNfts: {
132
+ [key: string]: {
133
+ [key: string]: Nft[];
134
+ };
135
+ };
136
+ ignoredNfts: Nft[];
137
+ }
138
+ /**
139
+ * Controller that stores assets and exposes convenience methods
140
+ */
141
+ export declare class NftController extends BaseController<NftConfig, NftState> {
142
+ private mutex;
143
+ private getNftApi;
144
+ private getNftContractInformationApi;
145
+ /**
146
+ * Helper method to update nested state for allNfts and allNftContracts.
147
+ *
148
+ * @param newCollection - the modified piece of state to update in the controller's store
149
+ * @param baseStateKey - The root key in the store to update.
150
+ * @param passedConfig - An object containing the selectedAddress and chainId that are passed through the auto-detection flow.
151
+ * @param passedConfig.userAddress - the address passed through the NFT detection flow to ensure detected assets are stored to the correct account
152
+ * @param passedConfig.chainId - the chainId passed through the NFT detection flow to ensure detected assets are stored to the correct account
153
+ */
154
+ private updateNestedNftState;
155
+ /**
156
+ * Request individual NFT information from OpenSea API.
157
+ *
158
+ * @param contractAddress - Hex address of the NFT contract.
159
+ * @param tokenId - The NFT identifier.
160
+ * @returns Promise resolving to the current NFT name and image.
161
+ */
162
+ private getNftInformationFromApi;
163
+ /**
164
+ * Request individual NFT information from contracts that follows Metadata Interface.
165
+ *
166
+ * @param contractAddress - Hex address of the NFT contract.
167
+ * @param tokenId - The NFT identifier.
168
+ * @returns Promise resolving to the current NFT name and image.
169
+ */
170
+ private getNftInformationFromTokenURI;
171
+ /**
172
+ * Retrieve NFT uri with metadata. TODO Update method to use IPFS.
173
+ *
174
+ * @param contractAddress - NFT contract address.
175
+ * @param tokenId - NFT token id.
176
+ * @returns Promise resolving NFT uri and token standard.
177
+ */
178
+ private getNftURIAndStandard;
179
+ /**
180
+ * Request individual NFT information (name, image url and description).
181
+ *
182
+ * @param contractAddress - Hex address of the NFT contract.
183
+ * @param tokenId - The NFT identifier.
184
+ * @returns Promise resolving to the current NFT name and image.
185
+ */
186
+ private getNftInformation;
187
+ /**
188
+ * Request NFT contract information from OpenSea API.
189
+ *
190
+ * @param contractAddress - Hex address of the NFT contract.
191
+ * @returns Promise resolving to the current NFT name and image.
192
+ */
193
+ private getNftContractInformationFromApi;
194
+ /**
195
+ * Request NFT contract information from the contract itself.
196
+ *
197
+ * @param contractAddress - Hex address of the NFT contract.
198
+ * @returns Promise resolving to the current NFT name and image.
199
+ */
200
+ private getNftContractInformationFromContract;
201
+ /**
202
+ * Request NFT contract information from OpenSea API.
203
+ *
204
+ * @param contractAddress - Hex address of the NFT contract.
205
+ * @returns Promise resolving to the NFT contract name, image and description.
206
+ */
207
+ private getNftContractInformation;
208
+ /**
209
+ * Adds an individual NFT to the stored NFT list.
210
+ *
211
+ * @param address - Hex address of the NFT contract.
212
+ * @param tokenId - The NFT identifier.
213
+ * @param nftMetadata - NFT optional information (name, image and description).
214
+ * @param nftContract - An object containing contract data of the NFT being added.
215
+ * @param detection - The chain ID and address of the currently selected network and account at the moment the NFT was detected.
216
+ * @returns Promise resolving to the current NFT list.
217
+ */
218
+ private addIndividualNft;
219
+ /**
220
+ * Adds an NFT contract to the stored NFT contracts list.
221
+ *
222
+ * @param address - Hex address of the NFT contract.
223
+ * @param detection - The chain ID and address of the currently selected network and account at the moment the NFT was detected.
224
+ * @returns Promise resolving to the current NFT contracts list.
225
+ */
226
+ private addNftContract;
227
+ /**
228
+ * Removes an individual NFT from the stored token list and saves it in ignored NFTs list.
229
+ *
230
+ * @param address - Hex address of the NFT contract.
231
+ * @param tokenId - Token identifier of the NFT.
232
+ */
233
+ private removeAndIgnoreIndividualNft;
234
+ /**
235
+ * Removes an individual NFT from the stored token list.
236
+ *
237
+ * @param address - Hex address of the NFT contract.
238
+ * @param tokenId - Token identifier of the NFT.
239
+ */
240
+ private removeIndividualNft;
241
+ /**
242
+ * Removes an NFT contract to the stored NFT contracts list.
243
+ *
244
+ * @param address - Hex address of the NFT contract.
245
+ * @returns Promise resolving to the current NFT contracts list.
246
+ */
247
+ private removeNftContract;
248
+ /**
249
+ * EventEmitter instance used to listen to specific EIP747 events
250
+ */
251
+ hub: EventEmitter;
252
+ /**
253
+ * Optional API key to use with opensea
254
+ */
255
+ openSeaApiKey?: string;
256
+ /**
257
+ * Name of this controller used during composition
258
+ */
259
+ name: string;
260
+ private getERC721AssetName;
261
+ private getERC721AssetSymbol;
262
+ private getERC721TokenURI;
263
+ private getERC721OwnerOf;
264
+ private getERC1155BalanceOf;
265
+ private getERC1155TokenURI;
266
+ private onNftAdded?;
267
+ /**
268
+ * Creates an NftController instance.
269
+ *
270
+ * @param options - The controller options.
271
+ * @param options.onPreferencesStateChange - Allows subscribing to preference controller state changes.
272
+ * @param options.onNetworkStateChange - Allows subscribing to network controller state changes.
273
+ * @param options.getERC721AssetName - Gets the name of the asset at the given address.
274
+ * @param options.getERC721AssetSymbol - Gets the symbol of the asset at the given address.
275
+ * @param options.getERC721TokenURI - Gets the URI of the ERC721 token at the given address, with the given ID.
276
+ * @param options.getERC721OwnerOf - Get the owner of a ERC-721 NFT.
277
+ * @param options.getERC1155BalanceOf - Gets balance of a ERC-1155 NFT.
278
+ * @param options.getERC1155TokenURI - Gets the URI of the ERC1155 token at the given address, with the given ID.
279
+ * @param options.onNftAdded - Callback that is called when an NFT is added. Currently used pass data
280
+ * for tracking the NFT added event.
281
+ * @param config - Initial options used to configure this controller.
282
+ * @param state - Initial state to set on this controller.
283
+ */
284
+ constructor({ onPreferencesStateChange, onNetworkStateChange, getERC721AssetName, getERC721AssetSymbol, getERC721TokenURI, getERC721OwnerOf, getERC1155BalanceOf, getERC1155TokenURI, onNftAdded, }: {
285
+ onPreferencesStateChange: (listener: (preferencesState: PreferencesState) => void) => void;
286
+ onNetworkStateChange: (listener: (networkState: NetworkState) => void) => void;
287
+ getERC721AssetName: AssetsContractController['getERC721AssetName'];
288
+ getERC721AssetSymbol: AssetsContractController['getERC721AssetSymbol'];
289
+ getERC721TokenURI: AssetsContractController['getERC721TokenURI'];
290
+ getERC721OwnerOf: AssetsContractController['getERC721OwnerOf'];
291
+ getERC1155BalanceOf: AssetsContractController['getERC1155BalanceOf'];
292
+ getERC1155TokenURI: AssetsContractController['getERC1155TokenURI'];
293
+ onNftAdded?: (data: {
294
+ address: string;
295
+ symbol: string | undefined;
296
+ tokenId: string;
297
+ standard: string | null;
298
+ source: string;
299
+ }) => void;
300
+ }, config?: Partial<BaseConfig>, state?: Partial<NftState>);
301
+ /**
302
+ * Sets an OpenSea API key to retrieve NFT information.
303
+ *
304
+ * @param openSeaApiKey - OpenSea API key.
305
+ */
306
+ setApiKey(openSeaApiKey: string): void;
307
+ /**
308
+ * Checks the ownership of a ERC-721 or ERC-1155 NFT for a given address.
309
+ *
310
+ * @param ownerAddress - User public address.
311
+ * @param nftAddress - NFT contract address.
312
+ * @param nftId - NFT token ID.
313
+ * @returns Promise resolving the NFT ownership.
314
+ */
315
+ isNftOwner(ownerAddress: string, nftAddress: string, nftId: string): Promise<boolean>;
316
+ /**
317
+ * Verifies currently selected address owns entered NFT address/tokenId combo and
318
+ * adds the NFT and respective NFT contract to the stored NFT and NFT contracts lists.
319
+ *
320
+ * @param address - Hex address of the NFT contract.
321
+ * @param tokenId - The NFT identifier.
322
+ */
323
+ addNftVerifyOwnership(address: string, tokenId: string): Promise<void>;
324
+ /**
325
+ * Adds an NFT and respective NFT contract to the stored NFT and NFT contracts lists.
326
+ *
327
+ * @param address - Hex address of the NFT contract.
328
+ * @param tokenId - The NFT identifier.
329
+ * @param nftMetadata - NFT optional metadata.
330
+ * @param detection - The chain ID and address of the currently selected network and account at the moment the NFT was detected.
331
+ * @returns Promise resolving to the current NFT list.
332
+ */
333
+ addNft(address: string, tokenId: string, nftMetadata?: NftMetadata, detection?: AccountParams): Promise<void>;
334
+ /**
335
+ * Removes an NFT from the stored token list.
336
+ *
337
+ * @param address - Hex address of the NFT contract.
338
+ * @param tokenId - Token identifier of the NFT.
339
+ */
340
+ removeNft(address: string, tokenId: string): void;
341
+ /**
342
+ * Removes an NFT from the stored token list and saves it in ignored NFTs list.
343
+ *
344
+ * @param address - Hex address of the NFT contract.
345
+ * @param tokenId - Token identifier of the NFT.
346
+ */
347
+ removeAndIgnoreNft(address: string, tokenId: string): void;
348
+ /**
349
+ * Removes all NFTs from the ignored list.
350
+ */
351
+ clearIgnoredNfts(): void;
352
+ /**
353
+ * Checks whether input NFT is still owned by the user
354
+ * And updates the isCurrentlyOwned value on the NFT object accordingly.
355
+ *
356
+ * @param nft - The NFT object to check and update.
357
+ * @param batch - A boolean indicating whether this method is being called as part of a batch or single update.
358
+ * @param accountParams - The userAddress and chainId to check ownership against
359
+ * @param accountParams.userAddress - the address passed through the confirmed transaction flow to ensure detected assets are stored to the correct account
360
+ * @param accountParams.chainId - the chainId passed through the confirmed transaction flow to ensure detected assets are stored to the correct account
361
+ * @returns the NFT with the updated isCurrentlyOwned value
362
+ */
363
+ checkAndUpdateSingleNftOwnershipStatus(nft: Nft, batch: boolean, { userAddress, chainId }?: AccountParams | undefined): Promise<Nft>;
364
+ /**
365
+ * Checks whether NFTs associated with current selectedAddress/chainId combination are still owned by the user
366
+ * And updates the isCurrentlyOwned value on each accordingly.
367
+ */
368
+ checkAndUpdateAllNftsOwnershipStatus(): Promise<void>;
369
+ /**
370
+ * Update NFT favorite status.
371
+ *
372
+ * @param address - Hex address of the NFT contract.
373
+ * @param tokenId - Hex address of the NFT contract.
374
+ * @param favorite - NFT new favorite status.
375
+ */
376
+ updateNftFavoriteStatus(address: string, tokenId: string, favorite: boolean): void;
377
+ /**
378
+ * Returns an NFT by the address and token id.
379
+ *
380
+ * @param address - Hex address of the NFT contract.
381
+ * @param tokenId - Number that represents the id of the token.
382
+ * @param selectedAddress - Hex address of the user account.
383
+ * @param chainId - Id of the current network.
384
+ * @returns Object containing the NFT and its position in the array
385
+ */
386
+ findNftByAddressAndTokenId(address: string, tokenId: string, selectedAddress: string, chainId: string): {
387
+ nft: Nft;
388
+ index: number;
389
+ } | null;
390
+ /**
391
+ * Update NFT data.
392
+ *
393
+ * @param nft - NFT object to find the right NFT to updates.
394
+ * @param updates - NFT partial object to update properties of the NFT.
395
+ * @param selectedAddress - Hex address of the user account.
396
+ * @param chainId - Id of the current network.
397
+ */
398
+ updateNft(nft: Nft, updates: Partial<Nft>, selectedAddress: string, chainId: string): void;
399
+ /**
400
+ * Resets the transaction status of an NFT.
401
+ *
402
+ * @param transactionId - NFT transaction id.
403
+ * @param selectedAddress - Hex address of the user account.
404
+ * @param chainId - Id of the current network.
405
+ * @returns a boolean indicating if the reset was well succeded or not
406
+ */
407
+ resetNftTransactionStatusByTransactionId(transactionId: string, selectedAddress: string, chainId: string): boolean;
408
+ }
409
+ export default NftController;