@metamask/assets-controllers 30.0.0 → 31.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 (93) hide show
  1. package/CHANGELOG.md +81 -1
  2. package/dist/AccountTrackerController.js +2 -2
  3. package/dist/AccountTrackerController.mjs +1 -1
  4. package/dist/AssetsContractController.js +2 -2
  5. package/dist/AssetsContractController.mjs +1 -1
  6. package/dist/NftController.js +2 -2
  7. package/dist/NftController.mjs +3 -3
  8. package/dist/NftDetectionController.js +2 -2
  9. package/dist/NftDetectionController.mjs +1 -1
  10. package/dist/TokenBalancesController.js +2 -2
  11. package/dist/TokenBalancesController.mjs +1 -1
  12. package/dist/TokenListController.js +3 -3
  13. package/dist/TokenListController.mjs +2 -2
  14. package/dist/TokenRatesController.js +2 -2
  15. package/dist/TokenRatesController.mjs +1 -1
  16. package/dist/TokensController.js +3 -3
  17. package/dist/TokensController.mjs +2 -2
  18. package/dist/chunk-4AC3X2U5.js +771 -0
  19. package/dist/chunk-4AC3X2U5.js.map +1 -0
  20. package/dist/chunk-543CC74T.mjs +253 -0
  21. package/dist/chunk-543CC74T.mjs.map +1 -0
  22. package/dist/{chunk-Z3OQU4XW.mjs → chunk-5W5OO2Q5.mjs} +2 -2
  23. package/dist/chunk-5W5OO2Q5.mjs.map +1 -0
  24. package/dist/chunk-62T7RKU3.mjs +1265 -0
  25. package/dist/chunk-62T7RKU3.mjs.map +1 -0
  26. package/dist/{chunk-PAJTKWEC.mjs → chunk-65PB33TE.mjs} +1 -1
  27. package/dist/chunk-65PB33TE.mjs.map +1 -0
  28. package/dist/{chunk-7MMEHAKG.mjs → chunk-73F3SN5O.mjs} +332 -265
  29. package/dist/chunk-73F3SN5O.mjs.map +1 -0
  30. package/dist/{chunk-XHMM35YT.mjs → chunk-A2DDWXMS.mjs} +2 -2
  31. package/dist/chunk-CGLUTXI7.js +1265 -0
  32. package/dist/chunk-CGLUTXI7.js.map +1 -0
  33. package/dist/{chunk-X4FFNQHE.mjs → chunk-E4ECCGJV.mjs} +1 -1
  34. package/dist/chunk-E4ECCGJV.mjs.map +1 -0
  35. package/dist/{chunk-V4ZO3F2S.js → chunk-GU53EI7A.js} +1 -1
  36. package/dist/chunk-GU53EI7A.js.map +1 -0
  37. package/dist/{chunk-MHN7CJCZ.mjs → chunk-HLCGZGPA.mjs} +1 -1
  38. package/dist/chunk-HLCGZGPA.mjs.map +1 -0
  39. package/dist/{chunk-LD4GC7OR.js → chunk-IBK6AXPP.js} +8 -9
  40. package/dist/chunk-IBK6AXPP.js.map +1 -0
  41. package/dist/{chunk-5F5EQAX5.js → chunk-JUI3XNEF.js} +3 -3
  42. package/dist/{chunk-3R43XIIX.js → chunk-PYMUBJQX.js} +2 -2
  43. package/dist/chunk-PYMUBJQX.js.map +1 -0
  44. package/dist/chunk-QFDTOEYR.js +253 -0
  45. package/dist/chunk-QFDTOEYR.js.map +1 -0
  46. package/dist/{chunk-4ODKGWYQ.js → chunk-QHWKLZUC.js} +1 -1
  47. package/dist/chunk-QHWKLZUC.js.map +1 -0
  48. package/dist/{chunk-6W5EQ3JQ.js → chunk-UEDNQBJN.js} +1 -1
  49. package/dist/chunk-UEDNQBJN.js.map +1 -0
  50. package/dist/{chunk-4LY47RPI.mjs → chunk-WCCLSUOI.mjs} +8 -9
  51. package/dist/{chunk-4LY47RPI.mjs.map → chunk-WCCLSUOI.mjs.map} +1 -1
  52. package/dist/index.js +10 -12
  53. package/dist/index.mjs +11 -13
  54. package/dist/token-service.js +2 -2
  55. package/dist/token-service.mjs +1 -1
  56. package/dist/tsconfig.build.tsbuildinfo +1 -1
  57. package/dist/types/AccountTrackerController.d.ts +1 -1
  58. package/dist/types/AccountTrackerController.d.ts.map +1 -1
  59. package/dist/types/AssetsContractController.d.ts +1 -1
  60. package/dist/types/AssetsContractController.d.ts.map +1 -1
  61. package/dist/types/NftController.d.ts +65 -184
  62. package/dist/types/NftController.d.ts.map +1 -1
  63. package/dist/types/NftDetectionController.d.ts +34 -75
  64. package/dist/types/NftDetectionController.d.ts.map +1 -1
  65. package/dist/types/TokenRatesController.d.ts +6 -6
  66. package/dist/types/TokenRatesController.d.ts.map +1 -1
  67. package/dist/types/TokensController.d.ts +27 -131
  68. package/dist/types/TokensController.d.ts.map +1 -1
  69. package/dist/types/index.d.ts +5 -3
  70. package/dist/types/index.d.ts.map +1 -1
  71. package/package.json +12 -12
  72. package/dist/chunk-3R43XIIX.js.map +0 -1
  73. package/dist/chunk-46UZDIXW.mjs +0 -1205
  74. package/dist/chunk-46UZDIXW.mjs.map +0 -1
  75. package/dist/chunk-4ODKGWYQ.js.map +0 -1
  76. package/dist/chunk-6W5EQ3JQ.js.map +0 -1
  77. package/dist/chunk-7MMEHAKG.mjs.map +0 -1
  78. package/dist/chunk-FMZML3V5.js +0 -237
  79. package/dist/chunk-FMZML3V5.js.map +0 -1
  80. package/dist/chunk-KOKB6U4Z.mjs +0 -237
  81. package/dist/chunk-KOKB6U4Z.mjs.map +0 -1
  82. package/dist/chunk-LAU6ZDZR.js +0 -1205
  83. package/dist/chunk-LAU6ZDZR.js.map +0 -1
  84. package/dist/chunk-LD4GC7OR.js.map +0 -1
  85. package/dist/chunk-MBCN3MNX.js +0 -704
  86. package/dist/chunk-MBCN3MNX.js.map +0 -1
  87. package/dist/chunk-MHN7CJCZ.mjs.map +0 -1
  88. package/dist/chunk-PAJTKWEC.mjs.map +0 -1
  89. package/dist/chunk-V4ZO3F2S.js.map +0 -1
  90. package/dist/chunk-X4FFNQHE.mjs.map +0 -1
  91. package/dist/chunk-Z3OQU4XW.mjs.map +0 -1
  92. /package/dist/{chunk-XHMM35YT.mjs.map → chunk-A2DDWXMS.mjs.map} +0 -0
  93. /package/dist/{chunk-5F5EQAX5.js.map → chunk-JUI3XNEF.js.map} +0 -0
@@ -0,0 +1,1265 @@
1
+ "use strict";Object.defineProperty(exports, "__esModule", {value: true}); function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
2
+
3
+
4
+ var _chunkNEXY7SE2js = require('./chunk-NEXY7SE2.js');
5
+
6
+
7
+
8
+
9
+
10
+ var _chunkZ4BLTVTBjs = require('./chunk-Z4BLTVTB.js');
11
+
12
+ // src/NftController.ts
13
+ var _address = require('@ethersproject/address');
14
+
15
+
16
+ var _basecontroller = require('@metamask/base-controller');
17
+
18
+
19
+
20
+
21
+
22
+
23
+
24
+
25
+
26
+
27
+
28
+ var _controllerutils = require('@metamask/controller-utils');
29
+ var _rpcerrors = require('@metamask/rpc-errors');
30
+ var _utils = require('@metamask/utils');
31
+ var _asyncmutex = require('async-mutex');
32
+ var _bnjs = require('bn.js'); var _bnjs2 = _interopRequireDefault(_bnjs);
33
+ var _uuid = require('uuid');
34
+ var nftControllerMetadata = {
35
+ allNftContracts: { persist: true, anonymous: false },
36
+ allNfts: { persist: true, anonymous: false },
37
+ ignoredNfts: { persist: true, anonymous: false }
38
+ };
39
+ var ALL_NFTS_STATE_KEY = "allNfts";
40
+ var ALL_NFTS_CONTRACTS_STATE_KEY = "allNftContracts";
41
+ var controllerName = "NftController";
42
+ var getDefaultNftControllerState = () => ({
43
+ allNftContracts: {},
44
+ allNfts: {},
45
+ ignoredNfts: []
46
+ });
47
+ var _mutex, _selectedAddress, _chainId, _ipfsGateway, _openSeaEnabled, _useIpfsSubdomains, _isIpfsGatewayEnabled, _getERC721AssetName, _getERC721AssetSymbol, _getERC721TokenURI, _getERC721OwnerOf, _getERC1155BalanceOf, _getERC1155TokenURI, _onNftAdded, _onNetworkControllerNetworkDidChange, onNetworkControllerNetworkDidChange_fn, _onPreferencesControllerStateChange, onPreferencesControllerStateChange_fn, _updateNestedNftState, updateNestedNftState_fn, _getNftInformationFromApi, getNftInformationFromApi_fn, _getNftInformationFromTokenURI, getNftInformationFromTokenURI_fn, _getNftURIAndStandard, getNftURIAndStandard_fn, _getNftInformation, getNftInformation_fn, _getNftContractInformationFromContract, getNftContractInformationFromContract_fn, _getNftContractInformation, getNftContractInformation_fn, _addIndividualNft, addIndividualNft_fn, _addNftContract, addNftContract_fn, _removeAndIgnoreIndividualNft, removeAndIgnoreIndividualNft_fn, _removeIndividualNft, removeIndividualNft_fn, _removeNftContract, removeNftContract_fn, _validateWatchNft, validateWatchNft_fn, _getCorrectChainId, getCorrectChainId_fn;
48
+ var NftController = class extends _basecontroller.BaseController {
49
+ /**
50
+ * Creates an NftController instance.
51
+ *
52
+ * @param options - The controller options.
53
+ * @param options.chainId - The chain ID of the current network.
54
+ * @param options.selectedAddress - The currently selected address.
55
+ * @param options.ipfsGateway - The configured IPFS gateway.
56
+ * @param options.openSeaEnabled - Controls whether the OpenSea API is used.
57
+ * @param options.useIpfsSubdomains - Controls whether IPFS subdomains are used.
58
+ * @param options.isIpfsGatewayEnabled - Controls whether IPFS is enabled or not.
59
+ * @param options.getERC721AssetName - Gets the name of the asset at the given address.
60
+ * @param options.getERC721AssetSymbol - Gets the symbol of the asset at the given address.
61
+ * @param options.getERC721TokenURI - Gets the URI of the ERC721 token at the given address, with the given ID.
62
+ * @param options.getERC721OwnerOf - Get the owner of a ERC-721 NFT.
63
+ * @param options.getERC1155BalanceOf - Gets balance of a ERC-1155 NFT.
64
+ * @param options.getERC1155TokenURI - Gets the URI of the ERC1155 token at the given address, with the given ID.
65
+ * @param options.onNftAdded - Callback that is called when an NFT is added. Currently used pass data
66
+ * for tracking the NFT added event.
67
+ * @param options.messenger - The controller messenger.
68
+ * @param options.state - Initial state to set on this controller.
69
+ */
70
+ constructor({
71
+ chainId: initialChainId,
72
+ selectedAddress = "",
73
+ ipfsGateway = _controllerutils.IPFS_DEFAULT_GATEWAY_URL,
74
+ openSeaEnabled = false,
75
+ useIpfsSubdomains = true,
76
+ isIpfsGatewayEnabled = true,
77
+ getERC721AssetName,
78
+ getERC721AssetSymbol,
79
+ getERC721TokenURI,
80
+ getERC721OwnerOf,
81
+ getERC1155BalanceOf,
82
+ getERC1155TokenURI,
83
+ onNftAdded,
84
+ messenger,
85
+ state = {}
86
+ }) {
87
+ super({
88
+ name: controllerName,
89
+ metadata: nftControllerMetadata,
90
+ messenger,
91
+ state: {
92
+ ...getDefaultNftControllerState(),
93
+ ...state
94
+ }
95
+ });
96
+ /**
97
+ * Handles the network change on the network controller.
98
+ * @param networkState - The new state of the preference controller.
99
+ * @param networkState.selectedNetworkClientId - The current selected network client id.
100
+ */
101
+ _chunkZ4BLTVTBjs.__privateAdd.call(void 0, this, _onNetworkControllerNetworkDidChange);
102
+ /**
103
+ * Handles the state change of the preference controller.
104
+ * @param preferencesState - The new state of the preference controller.
105
+ * @param preferencesState.selectedAddress - The current selected address.
106
+ * @param preferencesState.ipfsGateway - The configured IPFS gateway.
107
+ * @param preferencesState.openSeaEnabled - Controls whether the OpenSea API is used.
108
+ * @param preferencesState.isIpfsGatewayEnabled - Controls whether IPFS is enabled or not.
109
+ */
110
+ _chunkZ4BLTVTBjs.__privateAdd.call(void 0, this, _onPreferencesControllerStateChange);
111
+ /**
112
+ * Helper method to update nested state for allNfts and allNftContracts.
113
+ *
114
+ * @param newCollection - the modified piece of state to update in the controller's store
115
+ * @param baseStateKey - The root key in the store to update.
116
+ * @param passedConfig - An object containing the selectedAddress and chainId that are passed through the auto-detection flow.
117
+ * @param passedConfig.userAddress - the address passed through the NFT detection flow to ensure assets are stored to the correct account
118
+ * @param passedConfig.chainId - the chainId passed through the NFT detection flow to ensure assets are stored to the correct account
119
+ */
120
+ _chunkZ4BLTVTBjs.__privateAdd.call(void 0, this, _updateNestedNftState);
121
+ /**
122
+ * Request individual NFT information from NFT API.
123
+ *
124
+ * @param contractAddress - Hex address of the NFT contract.
125
+ * @param tokenId - The NFT identifier.
126
+ * @returns Promise resolving to the current NFT name and image.
127
+ */
128
+ _chunkZ4BLTVTBjs.__privateAdd.call(void 0, this, _getNftInformationFromApi);
129
+ /**
130
+ * Request individual NFT information from contracts that follows Metadata Interface.
131
+ *
132
+ * @param contractAddress - Hex address of the NFT contract.
133
+ * @param tokenId - The NFT identifier.
134
+ * @param networkClientId - The networkClientId that can be used to identify the network client to use for this request.
135
+ * @returns Promise resolving to the current NFT name and image.
136
+ */
137
+ _chunkZ4BLTVTBjs.__privateAdd.call(void 0, this, _getNftInformationFromTokenURI);
138
+ /**
139
+ * Retrieve NFT uri with metadata. TODO Update method to use IPFS.
140
+ *
141
+ * @param contractAddress - NFT contract address.
142
+ * @param tokenId - NFT token id.
143
+ * @param networkClientId - The networkClientId that can be used to identify the network client to use for this request.
144
+ * @returns Promise resolving NFT uri and token standard.
145
+ */
146
+ _chunkZ4BLTVTBjs.__privateAdd.call(void 0, this, _getNftURIAndStandard);
147
+ /**
148
+ * Request individual NFT information (name, image url and description).
149
+ *
150
+ * @param contractAddress - Hex address of the NFT contract.
151
+ * @param tokenId - The NFT identifier.
152
+ * @param networkClientId - The networkClientId that can be used to identify the network client to use for this request.
153
+ * @returns Promise resolving to the current NFT name and image.
154
+ */
155
+ _chunkZ4BLTVTBjs.__privateAdd.call(void 0, this, _getNftInformation);
156
+ /**
157
+ * Request NFT contract information from the contract itself.
158
+ *
159
+ * @param contractAddress - Hex address of the NFT contract.
160
+ * @param networkClientId - The networkClientId that can be used to identify the network client to use for this request.
161
+ * @returns Promise resolving to the current NFT name and image.
162
+ */
163
+ _chunkZ4BLTVTBjs.__privateAdd.call(void 0, this, _getNftContractInformationFromContract);
164
+ /**
165
+ * Request NFT contract information from Blockchain and aggregate with received data from NFTMetadata.
166
+ *
167
+ * @param contractAddress - Hex address of the NFT contract.
168
+ * @param nftMetadataFromApi - Received NFT information to be aggregated with blockchain contract information.
169
+ * @param networkClientId - The networkClientId that can be used to identify the network client to use for this request.
170
+ * @returns Promise resolving to the NFT contract name, image and description.
171
+ */
172
+ _chunkZ4BLTVTBjs.__privateAdd.call(void 0, this, _getNftContractInformation);
173
+ /**
174
+ * Adds an individual NFT to the stored NFT list.
175
+ *
176
+ * @param tokenAddress - Hex address of the NFT contract.
177
+ * @param tokenId - The NFT identifier.
178
+ * @param nftMetadata - NFT optional information (name, image and description).
179
+ * @param nftContract - An object containing contract data of the NFT being added.
180
+ * @param chainId - The chainId of the network where the NFT is being added.
181
+ * @param userAddress - The address of the account where the NFT is being added.
182
+ * @param source - Whether the NFT was detected, added manually or suggested by a dapp.
183
+ * @returns A promise resolving to `undefined`.
184
+ */
185
+ _chunkZ4BLTVTBjs.__privateAdd.call(void 0, this, _addIndividualNft);
186
+ /**
187
+ * Adds an NFT contract to the stored NFT contracts list.
188
+ *
189
+ * @param options - options.
190
+ * @param options.tokenAddress - Hex address of the NFT contract.
191
+ * @param options.userAddress - The address of the account where the NFT is being added.
192
+ * @param options.nftMetadata - The retrieved NFTMetadata from API.
193
+ * @param options.networkClientId - The networkClientId that can be used to identify the network client to use for this request.
194
+ * @param options.source - Whether the NFT was detected, added manually or suggested by a dapp.
195
+ * @returns Promise resolving to the current NFT contracts list.
196
+ */
197
+ _chunkZ4BLTVTBjs.__privateAdd.call(void 0, this, _addNftContract);
198
+ /**
199
+ * Removes an individual NFT from the stored token list and saves it in ignored NFTs list.
200
+ *
201
+ * @param address - Hex address of the NFT contract.
202
+ * @param tokenId - Token identifier of the NFT.
203
+ * @param options - options.
204
+ * @param options.chainId - The chainId of the network where the NFT is being removed.
205
+ * @param options.userAddress - The address of the account where the NFT is being removed.
206
+ */
207
+ _chunkZ4BLTVTBjs.__privateAdd.call(void 0, this, _removeAndIgnoreIndividualNft);
208
+ /**
209
+ * Removes an individual NFT from the stored token list.
210
+ *
211
+ * @param address - Hex address of the NFT contract.
212
+ * @param tokenId - Token identifier of the NFT.
213
+ * @param options - options.
214
+ * @param options.chainId - The chainId of the network where the NFT is being removed.
215
+ * @param options.userAddress - The address of the account where the NFT is being removed.
216
+ */
217
+ _chunkZ4BLTVTBjs.__privateAdd.call(void 0, this, _removeIndividualNft);
218
+ /**
219
+ * Removes an NFT contract to the stored NFT contracts list.
220
+ *
221
+ * @param address - Hex address of the NFT contract.
222
+ * @param options - options.
223
+ * @param options.chainId - The chainId of the network where the NFT is being removed.
224
+ * @param options.userAddress - The address of the account where the NFT is being removed.
225
+ * @returns Promise resolving to the current NFT contracts list.
226
+ */
227
+ _chunkZ4BLTVTBjs.__privateAdd.call(void 0, this, _removeNftContract);
228
+ _chunkZ4BLTVTBjs.__privateAdd.call(void 0, this, _validateWatchNft);
229
+ // temporary method to get the correct chainId until we remove chainId from the config & the chainId arg from the detection logic
230
+ // Just a helper method to prefer the networkClient chainId first then the chainId argument and then finally the config chainId
231
+ _chunkZ4BLTVTBjs.__privateAdd.call(void 0, this, _getCorrectChainId);
232
+ _chunkZ4BLTVTBjs.__privateAdd.call(void 0, this, _mutex, new (0, _asyncmutex.Mutex)());
233
+ _chunkZ4BLTVTBjs.__privateAdd.call(void 0, this, _selectedAddress, void 0);
234
+ _chunkZ4BLTVTBjs.__privateAdd.call(void 0, this, _chainId, void 0);
235
+ _chunkZ4BLTVTBjs.__privateAdd.call(void 0, this, _ipfsGateway, void 0);
236
+ _chunkZ4BLTVTBjs.__privateAdd.call(void 0, this, _openSeaEnabled, void 0);
237
+ _chunkZ4BLTVTBjs.__privateAdd.call(void 0, this, _useIpfsSubdomains, void 0);
238
+ _chunkZ4BLTVTBjs.__privateAdd.call(void 0, this, _isIpfsGatewayEnabled, void 0);
239
+ _chunkZ4BLTVTBjs.__privateAdd.call(void 0, this, _getERC721AssetName, void 0);
240
+ _chunkZ4BLTVTBjs.__privateAdd.call(void 0, this, _getERC721AssetSymbol, void 0);
241
+ _chunkZ4BLTVTBjs.__privateAdd.call(void 0, this, _getERC721TokenURI, void 0);
242
+ _chunkZ4BLTVTBjs.__privateAdd.call(void 0, this, _getERC721OwnerOf, void 0);
243
+ _chunkZ4BLTVTBjs.__privateAdd.call(void 0, this, _getERC1155BalanceOf, void 0);
244
+ _chunkZ4BLTVTBjs.__privateAdd.call(void 0, this, _getERC1155TokenURI, void 0);
245
+ _chunkZ4BLTVTBjs.__privateAdd.call(void 0, this, _onNftAdded, void 0);
246
+ _chunkZ4BLTVTBjs.__privateSet.call(void 0, this, _selectedAddress, selectedAddress);
247
+ _chunkZ4BLTVTBjs.__privateSet.call(void 0, this, _chainId, initialChainId);
248
+ _chunkZ4BLTVTBjs.__privateSet.call(void 0, this, _ipfsGateway, ipfsGateway);
249
+ _chunkZ4BLTVTBjs.__privateSet.call(void 0, this, _openSeaEnabled, openSeaEnabled);
250
+ _chunkZ4BLTVTBjs.__privateSet.call(void 0, this, _useIpfsSubdomains, useIpfsSubdomains);
251
+ _chunkZ4BLTVTBjs.__privateSet.call(void 0, this, _isIpfsGatewayEnabled, isIpfsGatewayEnabled);
252
+ _chunkZ4BLTVTBjs.__privateSet.call(void 0, this, _getERC721AssetName, getERC721AssetName);
253
+ _chunkZ4BLTVTBjs.__privateSet.call(void 0, this, _getERC721AssetSymbol, getERC721AssetSymbol);
254
+ _chunkZ4BLTVTBjs.__privateSet.call(void 0, this, _getERC721TokenURI, getERC721TokenURI);
255
+ _chunkZ4BLTVTBjs.__privateSet.call(void 0, this, _getERC721OwnerOf, getERC721OwnerOf);
256
+ _chunkZ4BLTVTBjs.__privateSet.call(void 0, this, _getERC1155BalanceOf, getERC1155BalanceOf);
257
+ _chunkZ4BLTVTBjs.__privateSet.call(void 0, this, _getERC1155TokenURI, getERC1155TokenURI);
258
+ _chunkZ4BLTVTBjs.__privateSet.call(void 0, this, _onNftAdded, onNftAdded);
259
+ this.messagingSystem.subscribe(
260
+ "PreferencesController:stateChange",
261
+ _chunkZ4BLTVTBjs.__privateMethod.call(void 0, this, _onPreferencesControllerStateChange, onPreferencesControllerStateChange_fn).bind(this)
262
+ );
263
+ this.messagingSystem.subscribe(
264
+ "NetworkController:networkDidChange",
265
+ _chunkZ4BLTVTBjs.__privateMethod.call(void 0, this, _onNetworkControllerNetworkDidChange, onNetworkControllerNetworkDidChange_fn).bind(this)
266
+ );
267
+ }
268
+ getNftApi() {
269
+ return `${_controllerutils.NFT_API_BASE_URL}/tokens`;
270
+ }
271
+ /**
272
+ * Adds a new suggestedAsset to state. Parameters will be validated according to
273
+ * asset type being watched. A `<suggestedNftMeta.id>:pending` hub event will be emitted once added.
274
+ *
275
+ * @param asset - The asset to be watched. For now ERC721 and ERC1155 tokens are accepted.
276
+ * @param asset.address - The address of the asset contract.
277
+ * @param asset.tokenId - The ID of the asset.
278
+ * @param type - The asset type.
279
+ * @param origin - Domain origin to register the asset from.
280
+ * @param options - Options bag.
281
+ * @param options.networkClientId - The networkClientId that can be used to identify the network client to use for this request.
282
+ * @param options.userAddress - The address of the account where the NFT is being added.
283
+ * @returns Object containing a Promise resolving to the suggestedAsset address if accepted.
284
+ */
285
+ async watchNft(asset, type, origin, {
286
+ networkClientId,
287
+ userAddress = _chunkZ4BLTVTBjs.__privateGet.call(void 0, this, _selectedAddress)
288
+ } = {
289
+ userAddress: _chunkZ4BLTVTBjs.__privateGet.call(void 0, this, _selectedAddress)
290
+ }) {
291
+ await _chunkZ4BLTVTBjs.__privateMethod.call(void 0, this, _validateWatchNft, validateWatchNft_fn).call(this, asset, type, userAddress);
292
+ const nftMetadata = await _chunkZ4BLTVTBjs.__privateMethod.call(void 0, this, _getNftInformation, getNftInformation_fn).call(this, asset.address, asset.tokenId, networkClientId);
293
+ if (nftMetadata.standard && nftMetadata.standard !== type) {
294
+ throw _rpcerrors.rpcErrors.invalidInput(
295
+ `Suggested NFT of type ${nftMetadata.standard} does not match received type ${type}`
296
+ );
297
+ }
298
+ const suggestedNftMeta = {
299
+ asset: { ...asset, ...nftMetadata },
300
+ type,
301
+ id: _uuid.v4.call(void 0, ),
302
+ time: Date.now(),
303
+ interactingAddress: userAddress,
304
+ origin
305
+ };
306
+ await this._requestApproval(suggestedNftMeta);
307
+ const { address, tokenId } = asset;
308
+ const { name, standard, description, image } = nftMetadata;
309
+ await this.addNft(address, tokenId, {
310
+ nftMetadata: {
311
+ name: name ?? null,
312
+ description: description ?? null,
313
+ image: image ?? null,
314
+ standard: standard ?? null
315
+ },
316
+ userAddress,
317
+ source: "dapp" /* Dapp */,
318
+ networkClientId
319
+ });
320
+ }
321
+ /**
322
+ * Sets an OpenSea API key to retrieve NFT information.
323
+ *
324
+ * @param openSeaApiKey - OpenSea API key.
325
+ */
326
+ setApiKey(openSeaApiKey) {
327
+ this.openSeaApiKey = openSeaApiKey;
328
+ }
329
+ /**
330
+ * Checks the ownership of a ERC-721 or ERC-1155 NFT for a given address.
331
+ *
332
+ * @param ownerAddress - User public address.
333
+ * @param nftAddress - NFT contract address.
334
+ * @param tokenId - NFT token ID.
335
+ * @param options - Options bag.
336
+ * @param options.networkClientId - The networkClientId that can be used to identify the network client to use for this request.
337
+ * @returns Promise resolving the NFT ownership.
338
+ */
339
+ async isNftOwner(ownerAddress, nftAddress, tokenId, {
340
+ networkClientId
341
+ } = {}) {
342
+ try {
343
+ const owner = await _chunkZ4BLTVTBjs.__privateGet.call(void 0, this, _getERC721OwnerOf).call(this, nftAddress, tokenId, networkClientId);
344
+ return ownerAddress.toLowerCase() === owner.toLowerCase();
345
+ } catch {
346
+ }
347
+ try {
348
+ const balance = await _chunkZ4BLTVTBjs.__privateGet.call(void 0, this, _getERC1155BalanceOf).call(this, ownerAddress, nftAddress, tokenId, networkClientId);
349
+ return !balance.isZero();
350
+ } catch {
351
+ }
352
+ throw new Error(
353
+ `Unable to verify ownership. Possibly because the standard is not supported or the user's currently selected network does not match the chain of the asset in question.`
354
+ );
355
+ }
356
+ /**
357
+ * Verifies currently selected address owns entered NFT address/tokenId combo and
358
+ * adds the NFT and respective NFT contract to the stored NFT and NFT contracts lists.
359
+ *
360
+ * @param address - Hex address of the NFT contract.
361
+ * @param tokenId - The NFT identifier.
362
+ * @param options - an object of arguments
363
+ * @param options.userAddress - The address of the current user.
364
+ * @param options.networkClientId - The networkClientId that can be used to identify the network client to use for this request.
365
+ * @param options.source - Whether the NFT was detected, added manually or suggested by a dapp.
366
+ */
367
+ async addNftVerifyOwnership(address, tokenId, {
368
+ userAddress = _chunkZ4BLTVTBjs.__privateGet.call(void 0, this, _selectedAddress),
369
+ networkClientId,
370
+ source
371
+ } = {
372
+ userAddress: _chunkZ4BLTVTBjs.__privateGet.call(void 0, this, _selectedAddress)
373
+ }) {
374
+ if (!await this.isNftOwner(userAddress, address, tokenId, {
375
+ networkClientId
376
+ })) {
377
+ throw new Error("This NFT is not owned by the user");
378
+ }
379
+ await this.addNft(address, tokenId, {
380
+ networkClientId,
381
+ userAddress,
382
+ source
383
+ });
384
+ }
385
+ /**
386
+ * Adds an NFT and respective NFT contract to the stored NFT and NFT contracts lists.
387
+ *
388
+ * @param tokenAddress - Hex address of the NFT contract.
389
+ * @param tokenId - The NFT identifier.
390
+ * @param options - an object of arguments
391
+ * @param options.nftMetadata - NFT optional metadata.
392
+ * @param options.userAddress - The address of the current user.
393
+ * @param options.source - Whether the NFT was detected, added manually or suggested by a dapp.
394
+ * @param options.networkClientId - The networkClientId that can be used to identify the network client to use for this request.
395
+ * @returns Promise resolving to the current NFT list.
396
+ */
397
+ async addNft(tokenAddress, tokenId, {
398
+ nftMetadata,
399
+ userAddress = _chunkZ4BLTVTBjs.__privateGet.call(void 0, this, _selectedAddress),
400
+ source = "custom" /* Custom */,
401
+ networkClientId
402
+ } = { userAddress: _chunkZ4BLTVTBjs.__privateGet.call(void 0, this, _selectedAddress) }) {
403
+ const checksumHexAddress = _controllerutils.toChecksumHexAddress.call(void 0, tokenAddress);
404
+ const chainId = _chunkZ4BLTVTBjs.__privateMethod.call(void 0, this, _getCorrectChainId, getCorrectChainId_fn).call(this, { networkClientId });
405
+ nftMetadata = nftMetadata || await _chunkZ4BLTVTBjs.__privateMethod.call(void 0, this, _getNftInformation, getNftInformation_fn).call(this, checksumHexAddress, tokenId, networkClientId);
406
+ const newNftContracts = await _chunkZ4BLTVTBjs.__privateMethod.call(void 0, this, _addNftContract, addNftContract_fn).call(this, {
407
+ tokenAddress: checksumHexAddress,
408
+ userAddress,
409
+ networkClientId,
410
+ source,
411
+ nftMetadata
412
+ });
413
+ const nftContract = newNftContracts.find(
414
+ (contract) => contract.address.toLowerCase() === checksumHexAddress.toLowerCase()
415
+ );
416
+ if (nftContract) {
417
+ await _chunkZ4BLTVTBjs.__privateMethod.call(void 0, this, _addIndividualNft, addIndividualNft_fn).call(this, checksumHexAddress, tokenId, nftMetadata, nftContract, chainId, userAddress, source);
418
+ }
419
+ }
420
+ /**
421
+ * Refetches NFT metadata and updates the state
422
+ *
423
+ * @param options - Options for refetching NFT metadata
424
+ * @param options.nfts - nfts to update metadata for.
425
+ * @param options.userAddress - The current user address
426
+ * @param options.networkClientId - The networkClientId that can be used to identify the network client to use for this request.
427
+ */
428
+ async updateNftMetadata({
429
+ nfts,
430
+ userAddress = _chunkZ4BLTVTBjs.__privateGet.call(void 0, this, _selectedAddress),
431
+ networkClientId
432
+ }) {
433
+ const chainId = _chunkZ4BLTVTBjs.__privateMethod.call(void 0, this, _getCorrectChainId, getCorrectChainId_fn).call(this, { networkClientId });
434
+ const nftsWithChecksumAdr = nfts.map((nft) => {
435
+ return {
436
+ ...nft,
437
+ address: _controllerutils.toChecksumHexAddress.call(void 0, nft.address)
438
+ };
439
+ });
440
+ const nftMetadataResults = await Promise.all(
441
+ nftsWithChecksumAdr.map(async (nft) => {
442
+ const resMetadata = await _chunkZ4BLTVTBjs.__privateMethod.call(void 0, this, _getNftInformation, getNftInformation_fn).call(this, nft.address, nft.tokenId, networkClientId);
443
+ return {
444
+ nft,
445
+ newMetadata: resMetadata
446
+ };
447
+ })
448
+ );
449
+ const nftsWithDifferentMetadata = [];
450
+ const { allNfts } = this.state;
451
+ const stateNfts = allNfts[userAddress]?.[chainId] || [];
452
+ nftMetadataResults.forEach((singleNft) => {
453
+ const existingEntry = stateNfts.find(
454
+ (nft) => nft.address.toLowerCase() === singleNft.nft.address.toLowerCase() && nft.tokenId === singleNft.nft.tokenId
455
+ );
456
+ if (existingEntry) {
457
+ const differentMetadata = _chunkNEXY7SE2js.compareNftMetadata.call(void 0,
458
+ singleNft.newMetadata,
459
+ existingEntry
460
+ );
461
+ if (differentMetadata) {
462
+ nftsWithDifferentMetadata.push(singleNft);
463
+ }
464
+ }
465
+ });
466
+ if (nftsWithDifferentMetadata.length !== 0) {
467
+ nftsWithDifferentMetadata.forEach(
468
+ (elm) => this.updateNft(elm.nft, elm.newMetadata, userAddress, chainId)
469
+ );
470
+ }
471
+ }
472
+ /**
473
+ * Removes an NFT from the stored token list.
474
+ *
475
+ * @param address - Hex address of the NFT contract.
476
+ * @param tokenId - Token identifier of the NFT.
477
+ * @param options - an object of arguments
478
+ * @param options.networkClientId - The networkClientId that can be used to identify the network client to use for this request.
479
+ * @param options.userAddress - The address of the account where the NFT is being removed.
480
+ */
481
+ removeNft(address, tokenId, {
482
+ networkClientId,
483
+ userAddress = _chunkZ4BLTVTBjs.__privateGet.call(void 0, this, _selectedAddress)
484
+ } = {
485
+ userAddress: _chunkZ4BLTVTBjs.__privateGet.call(void 0, this, _selectedAddress)
486
+ }) {
487
+ const chainId = _chunkZ4BLTVTBjs.__privateMethod.call(void 0, this, _getCorrectChainId, getCorrectChainId_fn).call(this, { networkClientId });
488
+ const checksumHexAddress = _controllerutils.toChecksumHexAddress.call(void 0, address);
489
+ _chunkZ4BLTVTBjs.__privateMethod.call(void 0, this, _removeIndividualNft, removeIndividualNft_fn).call(this, checksumHexAddress, tokenId, {
490
+ chainId,
491
+ userAddress
492
+ });
493
+ const { allNfts } = this.state;
494
+ const nfts = allNfts[userAddress]?.[chainId] || [];
495
+ const remainingNft = nfts.find(
496
+ (nft) => nft.address.toLowerCase() === checksumHexAddress.toLowerCase()
497
+ );
498
+ if (!remainingNft) {
499
+ _chunkZ4BLTVTBjs.__privateMethod.call(void 0, this, _removeNftContract, removeNftContract_fn).call(this, checksumHexAddress, { chainId, userAddress });
500
+ }
501
+ }
502
+ /**
503
+ * Removes an NFT from the stored token list and saves it in ignored NFTs list.
504
+ *
505
+ * @param address - Hex address of the NFT contract.
506
+ * @param tokenId - Token identifier of the NFT.
507
+ * @param options - an object of arguments
508
+ * @param options.networkClientId - The networkClientId that can be used to identify the network client to use for this request.
509
+ * @param options.userAddress - The address of the account where the NFT is being removed.
510
+ */
511
+ removeAndIgnoreNft(address, tokenId, {
512
+ networkClientId,
513
+ userAddress = _chunkZ4BLTVTBjs.__privateGet.call(void 0, this, _selectedAddress)
514
+ } = {
515
+ userAddress: _chunkZ4BLTVTBjs.__privateGet.call(void 0, this, _selectedAddress)
516
+ }) {
517
+ const chainId = _chunkZ4BLTVTBjs.__privateMethod.call(void 0, this, _getCorrectChainId, getCorrectChainId_fn).call(this, { networkClientId });
518
+ const checksumHexAddress = _controllerutils.toChecksumHexAddress.call(void 0, address);
519
+ _chunkZ4BLTVTBjs.__privateMethod.call(void 0, this, _removeAndIgnoreIndividualNft, removeAndIgnoreIndividualNft_fn).call(this, checksumHexAddress, tokenId, {
520
+ chainId,
521
+ userAddress
522
+ });
523
+ const { allNfts } = this.state;
524
+ const nfts = allNfts[userAddress]?.[chainId] || [];
525
+ const remainingNft = nfts.find(
526
+ (nft) => nft.address.toLowerCase() === checksumHexAddress.toLowerCase()
527
+ );
528
+ if (!remainingNft) {
529
+ _chunkZ4BLTVTBjs.__privateMethod.call(void 0, this, _removeNftContract, removeNftContract_fn).call(this, checksumHexAddress, { chainId, userAddress });
530
+ }
531
+ }
532
+ /**
533
+ * Removes all NFTs from the ignored list.
534
+ */
535
+ clearIgnoredNfts() {
536
+ this.update((state) => {
537
+ state.ignoredNfts = [];
538
+ });
539
+ }
540
+ /**
541
+ * Checks whether input NFT is still owned by the user
542
+ * And updates the isCurrentlyOwned value on the NFT object accordingly.
543
+ *
544
+ * @param nft - The NFT object to check and update.
545
+ * @param batch - A boolean indicating whether this method is being called as part of a batch or single update.
546
+ * @param accountParams - The userAddress and chainId to check ownership against
547
+ * @param accountParams.userAddress - the address passed through the confirmed transaction flow to ensure assets are stored to the correct account
548
+ * @param accountParams.networkClientId - The networkClientId that can be used to identify the network client to use for this request.
549
+ * @returns the NFT with the updated isCurrentlyOwned value
550
+ */
551
+ async checkAndUpdateSingleNftOwnershipStatus(nft, batch, {
552
+ userAddress = _chunkZ4BLTVTBjs.__privateGet.call(void 0, this, _selectedAddress),
553
+ networkClientId
554
+ } = {
555
+ userAddress: _chunkZ4BLTVTBjs.__privateGet.call(void 0, this, _selectedAddress)
556
+ }) {
557
+ const chainId = _chunkZ4BLTVTBjs.__privateMethod.call(void 0, this, _getCorrectChainId, getCorrectChainId_fn).call(this, { networkClientId });
558
+ const { address, tokenId } = nft;
559
+ let isOwned = nft.isCurrentlyOwned;
560
+ try {
561
+ isOwned = await this.isNftOwner(userAddress, address, tokenId, {
562
+ networkClientId
563
+ });
564
+ } catch {
565
+ }
566
+ const updatedNft = {
567
+ ...nft,
568
+ isCurrentlyOwned: isOwned
569
+ };
570
+ if (batch) {
571
+ return updatedNft;
572
+ }
573
+ const { allNfts } = this.state;
574
+ const nfts = [...allNfts[userAddress]?.[chainId] || []];
575
+ const indexToUpdate = nfts.findIndex(
576
+ (item) => item.tokenId === tokenId && item.address.toLowerCase() === address.toLowerCase()
577
+ );
578
+ if (indexToUpdate !== -1) {
579
+ nfts[indexToUpdate] = updatedNft;
580
+ this.update((state) => {
581
+ state.allNfts[userAddress] = Object.assign(
582
+ {},
583
+ state.allNfts[userAddress],
584
+ {
585
+ [chainId]: nfts
586
+ }
587
+ );
588
+ });
589
+ _chunkZ4BLTVTBjs.__privateMethod.call(void 0, this, _updateNestedNftState, updateNestedNftState_fn).call(this, nfts, ALL_NFTS_STATE_KEY, {
590
+ userAddress,
591
+ chainId
592
+ });
593
+ }
594
+ return updatedNft;
595
+ }
596
+ /**
597
+ * Checks whether NFTs associated with current selectedAddress/chainId combination are still owned by the user
598
+ * And updates the isCurrentlyOwned value on each accordingly.
599
+ * @param options - an object of arguments
600
+ * @param options.networkClientId - The networkClientId that can be used to identify the network client to use for this request.
601
+ * @param options.userAddress - The address of the account where the NFT ownership status is checked/updated.
602
+ */
603
+ async checkAndUpdateAllNftsOwnershipStatus({
604
+ networkClientId,
605
+ userAddress = _chunkZ4BLTVTBjs.__privateGet.call(void 0, this, _selectedAddress)
606
+ } = {
607
+ userAddress: _chunkZ4BLTVTBjs.__privateGet.call(void 0, this, _selectedAddress)
608
+ }) {
609
+ const chainId = _chunkZ4BLTVTBjs.__privateMethod.call(void 0, this, _getCorrectChainId, getCorrectChainId_fn).call(this, { networkClientId });
610
+ const { allNfts } = this.state;
611
+ const nfts = allNfts[userAddress]?.[chainId] || [];
612
+ const updatedNfts = await Promise.all(
613
+ nfts.map(async (nft) => {
614
+ return await this.checkAndUpdateSingleNftOwnershipStatus(nft, true, {
615
+ networkClientId,
616
+ userAddress
617
+ }) ?? nft;
618
+ })
619
+ );
620
+ _chunkZ4BLTVTBjs.__privateMethod.call(void 0, this, _updateNestedNftState, updateNestedNftState_fn).call(this, updatedNfts, ALL_NFTS_STATE_KEY, {
621
+ userAddress,
622
+ chainId
623
+ });
624
+ }
625
+ /**
626
+ * Update NFT favorite status.
627
+ *
628
+ * @param address - Hex address of the NFT contract.
629
+ * @param tokenId - Hex address of the NFT contract.
630
+ * @param favorite - NFT new favorite status.
631
+ * @param options - an object of arguments
632
+ * @param options.networkClientId - The networkClientId that can be used to identify the network client to use for this request.
633
+ * @param options.userAddress - The address of the account where the NFT is being removed.
634
+ */
635
+ updateNftFavoriteStatus(address, tokenId, favorite, {
636
+ networkClientId,
637
+ userAddress = _chunkZ4BLTVTBjs.__privateGet.call(void 0, this, _selectedAddress)
638
+ } = {
639
+ userAddress: _chunkZ4BLTVTBjs.__privateGet.call(void 0, this, _selectedAddress)
640
+ }) {
641
+ const chainId = _chunkZ4BLTVTBjs.__privateMethod.call(void 0, this, _getCorrectChainId, getCorrectChainId_fn).call(this, { networkClientId });
642
+ const { allNfts } = this.state;
643
+ const nfts = [...allNfts[userAddress]?.[chainId] || []];
644
+ const index = nfts.findIndex(
645
+ (nft) => nft.address === address && nft.tokenId === tokenId
646
+ );
647
+ if (index === -1) {
648
+ return;
649
+ }
650
+ const updatedNft = {
651
+ ...nfts[index],
652
+ favorite
653
+ };
654
+ nfts[index] = updatedNft;
655
+ _chunkZ4BLTVTBjs.__privateMethod.call(void 0, this, _updateNestedNftState, updateNestedNftState_fn).call(this, nfts, ALL_NFTS_STATE_KEY, {
656
+ chainId,
657
+ userAddress
658
+ });
659
+ }
660
+ /**
661
+ * Returns an NFT by the address and token id.
662
+ *
663
+ * @param address - Hex address of the NFT contract.
664
+ * @param tokenId - Number that represents the id of the token.
665
+ * @param selectedAddress - Hex address of the user account.
666
+ * @param chainId - Id of the current network.
667
+ * @returns Object containing the NFT and its position in the array
668
+ */
669
+ findNftByAddressAndTokenId(address, tokenId, selectedAddress, chainId) {
670
+ const { allNfts } = this.state;
671
+ const nfts = allNfts[selectedAddress]?.[chainId] || [];
672
+ const index = nfts.findIndex(
673
+ (nft) => nft.address.toLowerCase() === address.toLowerCase() && nft.tokenId === tokenId
674
+ );
675
+ if (index === -1) {
676
+ return null;
677
+ }
678
+ return { nft: nfts[index], index };
679
+ }
680
+ /**
681
+ * Update NFT data.
682
+ *
683
+ * @param nft - NFT object to find the right NFT to updates.
684
+ * @param updates - NFT partial object to update properties of the NFT.
685
+ * @param selectedAddress - Hex address of the user account.
686
+ * @param chainId - Id of the current network.
687
+ */
688
+ updateNft(nft, updates, selectedAddress, chainId) {
689
+ const { allNfts } = this.state;
690
+ const nfts = allNfts[selectedAddress]?.[chainId] || [];
691
+ const nftInfo = this.findNftByAddressAndTokenId(
692
+ nft.address,
693
+ nft.tokenId,
694
+ selectedAddress,
695
+ chainId
696
+ );
697
+ if (!nftInfo) {
698
+ return;
699
+ }
700
+ const updatedNft = {
701
+ ...nft,
702
+ ...updates
703
+ };
704
+ const newNfts = [
705
+ ...nfts.slice(0, nftInfo.index),
706
+ updatedNft,
707
+ ...nfts.slice(nftInfo.index + 1)
708
+ ];
709
+ _chunkZ4BLTVTBjs.__privateMethod.call(void 0, this, _updateNestedNftState, updateNestedNftState_fn).call(this, newNfts, ALL_NFTS_STATE_KEY, {
710
+ chainId,
711
+ userAddress: selectedAddress
712
+ });
713
+ }
714
+ /**
715
+ * Resets the transaction status of an NFT.
716
+ *
717
+ * @param transactionId - NFT transaction id.
718
+ * @param selectedAddress - Hex address of the user account.
719
+ * @param chainId - Id of the current network.
720
+ * @returns a boolean indicating if the reset was well succeeded or not
721
+ */
722
+ resetNftTransactionStatusByTransactionId(transactionId, selectedAddress, chainId) {
723
+ const { allNfts } = this.state;
724
+ const nfts = allNfts[selectedAddress]?.[chainId] || [];
725
+ const index = nfts.findIndex(
726
+ (nft) => nft.transactionId === transactionId
727
+ );
728
+ if (index === -1) {
729
+ return false;
730
+ }
731
+ const updatedNft = {
732
+ ...nfts[index],
733
+ transactionId: void 0
734
+ };
735
+ const newNfts = [
736
+ ...nfts.slice(0, index),
737
+ updatedNft,
738
+ ...nfts.slice(index + 1)
739
+ ];
740
+ _chunkZ4BLTVTBjs.__privateMethod.call(void 0, this, _updateNestedNftState, updateNestedNftState_fn).call(this, newNfts, ALL_NFTS_STATE_KEY, {
741
+ chainId,
742
+ userAddress: selectedAddress
743
+ });
744
+ return true;
745
+ }
746
+ async _requestApproval(suggestedNftMeta) {
747
+ return this.messagingSystem.call(
748
+ "ApprovalController:addRequest",
749
+ {
750
+ id: suggestedNftMeta.id,
751
+ origin: suggestedNftMeta.origin,
752
+ type: _controllerutils.ApprovalType.WatchAsset,
753
+ requestData: {
754
+ id: suggestedNftMeta.id,
755
+ interactingAddress: suggestedNftMeta.interactingAddress,
756
+ asset: {
757
+ address: suggestedNftMeta.asset.address,
758
+ tokenId: suggestedNftMeta.asset.tokenId,
759
+ name: suggestedNftMeta.asset.name,
760
+ description: suggestedNftMeta.asset.description,
761
+ image: suggestedNftMeta.asset.image,
762
+ standard: suggestedNftMeta.asset.standard
763
+ }
764
+ }
765
+ },
766
+ true
767
+ );
768
+ }
769
+ };
770
+ _mutex = new WeakMap();
771
+ _selectedAddress = new WeakMap();
772
+ _chainId = new WeakMap();
773
+ _ipfsGateway = new WeakMap();
774
+ _openSeaEnabled = new WeakMap();
775
+ _useIpfsSubdomains = new WeakMap();
776
+ _isIpfsGatewayEnabled = new WeakMap();
777
+ _getERC721AssetName = new WeakMap();
778
+ _getERC721AssetSymbol = new WeakMap();
779
+ _getERC721TokenURI = new WeakMap();
780
+ _getERC721OwnerOf = new WeakMap();
781
+ _getERC1155BalanceOf = new WeakMap();
782
+ _getERC1155TokenURI = new WeakMap();
783
+ _onNftAdded = new WeakMap();
784
+ _onNetworkControllerNetworkDidChange = new WeakSet();
785
+ onNetworkControllerNetworkDidChange_fn = function({
786
+ selectedNetworkClientId
787
+ }) {
788
+ const {
789
+ configuration: { chainId }
790
+ } = this.messagingSystem.call(
791
+ "NetworkController:getNetworkClientById",
792
+ selectedNetworkClientId
793
+ );
794
+ _chunkZ4BLTVTBjs.__privateSet.call(void 0, this, _chainId, chainId);
795
+ };
796
+ _onPreferencesControllerStateChange = new WeakSet();
797
+ onPreferencesControllerStateChange_fn = async function({
798
+ selectedAddress,
799
+ ipfsGateway,
800
+ openSeaEnabled,
801
+ isIpfsGatewayEnabled
802
+ }) {
803
+ _chunkZ4BLTVTBjs.__privateSet.call(void 0, this, _selectedAddress, selectedAddress);
804
+ _chunkZ4BLTVTBjs.__privateSet.call(void 0, this, _ipfsGateway, ipfsGateway);
805
+ _chunkZ4BLTVTBjs.__privateSet.call(void 0, this, _openSeaEnabled, openSeaEnabled);
806
+ _chunkZ4BLTVTBjs.__privateSet.call(void 0, this, _isIpfsGatewayEnabled, isIpfsGatewayEnabled);
807
+ const needsUpdateNftMetadata = isIpfsGatewayEnabled && ipfsGateway !== "" || openSeaEnabled;
808
+ if (needsUpdateNftMetadata) {
809
+ const nfts = this.state.allNfts[selectedAddress]?.[_chunkZ4BLTVTBjs.__privateGet.call(void 0, this, _chainId)] ?? [];
810
+ const nftsToUpdate = nfts.filter(
811
+ (singleNft) => !singleNft.name && !singleNft.description && !singleNft.image
812
+ );
813
+ if (nftsToUpdate.length !== 0) {
814
+ await this.updateNftMetadata({
815
+ nfts: nftsToUpdate,
816
+ userAddress: selectedAddress
817
+ });
818
+ }
819
+ }
820
+ };
821
+ _updateNestedNftState = new WeakSet();
822
+ updateNestedNftState_fn = function(newCollection, baseStateKey, { userAddress, chainId }) {
823
+ this.update((state) => {
824
+ const oldState = state[baseStateKey];
825
+ const addressState = oldState[userAddress] || {};
826
+ const newAddressState = {
827
+ ...addressState,
828
+ [chainId]: newCollection
829
+ };
830
+ state[baseStateKey] = {
831
+ ...oldState,
832
+ [userAddress]: newAddressState
833
+ };
834
+ });
835
+ };
836
+ _getNftInformationFromApi = new WeakSet();
837
+ getNftInformationFromApi_fn = async function(contractAddress, tokenId) {
838
+ const urlParams = new URLSearchParams({
839
+ chainIds: "1",
840
+ tokens: `${contractAddress}:${tokenId}`,
841
+ includeTopBid: "true",
842
+ includeAttributes: "true",
843
+ includeLastSale: "true"
844
+ }).toString();
845
+ const nftInformation = await _controllerutils.fetchWithErrorHandling.call(void 0, {
846
+ url: `${this.getNftApi()}?${urlParams}`,
847
+ options: {
848
+ headers: {
849
+ Version: "1"
850
+ }
851
+ }
852
+ });
853
+ if (!nftInformation?.tokens?.[0]?.token) {
854
+ return {
855
+ name: null,
856
+ description: null,
857
+ image: null,
858
+ standard: null
859
+ };
860
+ }
861
+ const {
862
+ image,
863
+ metadata: { imageOriginal } = {},
864
+ name,
865
+ description,
866
+ collection,
867
+ kind,
868
+ rarityRank,
869
+ rarity,
870
+ attributes,
871
+ lastSale,
872
+ imageSmall
873
+ } = nftInformation.tokens[0].token;
874
+ const nftMetadata = Object.assign(
875
+ {},
876
+ { name: name || null },
877
+ { description: description || null },
878
+ { image: image || null },
879
+ collection?.creator && { creator: collection.creator },
880
+ imageOriginal && { imageOriginal },
881
+ imageSmall && { imageThumbnail: imageSmall },
882
+ kind && { standard: kind.toUpperCase() },
883
+ lastSale && { lastSale },
884
+ attributes && { attributes },
885
+ nftInformation.tokens[0].market?.topBid && {
886
+ topBid: nftInformation.tokens[0].market?.topBid
887
+ },
888
+ rarityRank && { rarityRank },
889
+ rarity && { rarity },
890
+ collection && { collection }
891
+ );
892
+ return nftMetadata;
893
+ };
894
+ _getNftInformationFromTokenURI = new WeakSet();
895
+ getNftInformationFromTokenURI_fn = async function(contractAddress, tokenId, networkClientId) {
896
+ const result = await _chunkZ4BLTVTBjs.__privateMethod.call(void 0, this, _getNftURIAndStandard, getNftURIAndStandard_fn).call(this, contractAddress, tokenId, networkClientId);
897
+ let tokenURI = result[0];
898
+ const standard = result[1];
899
+ const hasIpfsTokenURI = tokenURI.startsWith("ipfs://");
900
+ if (hasIpfsTokenURI && !_chunkZ4BLTVTBjs.__privateGet.call(void 0, this, _isIpfsGatewayEnabled)) {
901
+ return {
902
+ image: null,
903
+ name: null,
904
+ description: null,
905
+ standard: standard || null,
906
+ favorite: false,
907
+ tokenURI: tokenURI ?? null
908
+ };
909
+ }
910
+ const isDisplayNFTMediaToggleEnabled = _chunkZ4BLTVTBjs.__privateGet.call(void 0, this, _openSeaEnabled);
911
+ if (!hasIpfsTokenURI && !isDisplayNFTMediaToggleEnabled) {
912
+ return {
913
+ image: null,
914
+ name: null,
915
+ description: null,
916
+ standard: standard || null,
917
+ favorite: false,
918
+ tokenURI: tokenURI ?? null
919
+ };
920
+ }
921
+ if (hasIpfsTokenURI) {
922
+ tokenURI = _chunkNEXY7SE2js.getFormattedIpfsUrl.call(void 0,
923
+ _chunkZ4BLTVTBjs.__privateGet.call(void 0, this, _ipfsGateway),
924
+ tokenURI,
925
+ _chunkZ4BLTVTBjs.__privateGet.call(void 0, this, _useIpfsSubdomains)
926
+ );
927
+ }
928
+ try {
929
+ const object = await _controllerutils.handleFetch.call(void 0, tokenURI);
930
+ const image = Object.prototype.hasOwnProperty.call(object, "image") ? "image" : (
931
+ /* istanbul ignore next */
932
+ "image_url"
933
+ );
934
+ return {
935
+ image: object[image],
936
+ name: object.name,
937
+ description: object.description,
938
+ standard,
939
+ favorite: false,
940
+ tokenURI: tokenURI ?? null
941
+ };
942
+ } catch {
943
+ return {
944
+ image: null,
945
+ name: null,
946
+ description: null,
947
+ standard: standard || null,
948
+ favorite: false,
949
+ tokenURI: tokenURI ?? null
950
+ };
951
+ }
952
+ };
953
+ _getNftURIAndStandard = new WeakSet();
954
+ getNftURIAndStandard_fn = async function(contractAddress, tokenId, networkClientId) {
955
+ try {
956
+ const uri = await _chunkZ4BLTVTBjs.__privateGet.call(void 0, this, _getERC721TokenURI).call(this, contractAddress, tokenId, networkClientId);
957
+ return [uri, _controllerutils.ERC721];
958
+ } catch {
959
+ }
960
+ try {
961
+ const tokenURI = await _chunkZ4BLTVTBjs.__privateGet.call(void 0, this, _getERC1155TokenURI).call(this, contractAddress, tokenId, networkClientId);
962
+ if (!tokenURI.includes("{id}")) {
963
+ return [tokenURI, _controllerutils.ERC1155];
964
+ }
965
+ const hexTokenId = _utils.remove0x.call(void 0, _controllerutils.BNToHex.call(void 0, new (0, _bnjs2.default)(tokenId))).padStart(64, "0").toLowerCase();
966
+ return [tokenURI.replace("{id}", hexTokenId), _controllerutils.ERC1155];
967
+ } catch {
968
+ }
969
+ return ["", ""];
970
+ };
971
+ _getNftInformation = new WeakSet();
972
+ getNftInformation_fn = async function(contractAddress, tokenId, networkClientId) {
973
+ const chainId = _chunkZ4BLTVTBjs.__privateMethod.call(void 0, this, _getCorrectChainId, getCorrectChainId_fn).call(this, {
974
+ networkClientId
975
+ });
976
+ const [blockchainMetadata, nftApiMetadata] = await Promise.all([
977
+ _controllerutils.safelyExecute.call(void 0,
978
+ () => _chunkZ4BLTVTBjs.__privateMethod.call(void 0, this, _getNftInformationFromTokenURI, getNftInformationFromTokenURI_fn).call(this, contractAddress, tokenId, networkClientId)
979
+ ),
980
+ _chunkZ4BLTVTBjs.__privateGet.call(void 0, this, _openSeaEnabled) && chainId === "0x1" ? _controllerutils.safelyExecute.call(void 0,
981
+ () => _chunkZ4BLTVTBjs.__privateMethod.call(void 0, this, _getNftInformationFromApi, getNftInformationFromApi_fn).call(this, contractAddress, tokenId)
982
+ ) : void 0
983
+ ]);
984
+ return {
985
+ ...nftApiMetadata,
986
+ name: blockchainMetadata?.name ?? nftApiMetadata?.name ?? null,
987
+ description: blockchainMetadata?.description ?? nftApiMetadata?.description ?? null,
988
+ image: blockchainMetadata?.image ?? nftApiMetadata?.image ?? null,
989
+ standard: blockchainMetadata?.standard ?? nftApiMetadata?.standard ?? null,
990
+ tokenURI: blockchainMetadata?.tokenURI ?? null
991
+ };
992
+ };
993
+ _getNftContractInformationFromContract = new WeakSet();
994
+ getNftContractInformationFromContract_fn = async function(contractAddress, networkClientId) {
995
+ const [name, symbol] = await Promise.all([
996
+ _chunkZ4BLTVTBjs.__privateGet.call(void 0, this, _getERC721AssetName).call(this, contractAddress, networkClientId),
997
+ _chunkZ4BLTVTBjs.__privateGet.call(void 0, this, _getERC721AssetSymbol).call(this, contractAddress, networkClientId)
998
+ ]);
999
+ return {
1000
+ collection: { name },
1001
+ symbol,
1002
+ address: contractAddress
1003
+ };
1004
+ };
1005
+ _getNftContractInformation = new WeakSet();
1006
+ getNftContractInformation_fn = async function(contractAddress, nftMetadataFromApi, networkClientId) {
1007
+ const blockchainContractData = await _controllerutils.safelyExecute.call(void 0,
1008
+ () => _chunkZ4BLTVTBjs.__privateMethod.call(void 0, this, _getNftContractInformationFromContract, getNftContractInformationFromContract_fn).call(this, contractAddress, networkClientId)
1009
+ );
1010
+ if (blockchainContractData || !Object.values(nftMetadataFromApi).every((value) => value === null)) {
1011
+ return {
1012
+ address: contractAddress,
1013
+ ...blockchainContractData,
1014
+ schema_name: nftMetadataFromApi?.standard ?? null,
1015
+ collection: {
1016
+ name: null,
1017
+ image_url: nftMetadataFromApi?.collection?.image ?? nftMetadataFromApi?.collection?.imageUrl ?? null,
1018
+ tokenCount: nftMetadataFromApi?.collection?.tokenCount ?? null,
1019
+ ...nftMetadataFromApi?.collection,
1020
+ ...blockchainContractData?.collection
1021
+ }
1022
+ };
1023
+ }
1024
+ return {
1025
+ address: contractAddress,
1026
+ asset_contract_type: null,
1027
+ created_date: null,
1028
+ schema_name: null,
1029
+ symbol: null,
1030
+ total_supply: null,
1031
+ description: null,
1032
+ external_link: null,
1033
+ collection: { name: null, image_url: null }
1034
+ };
1035
+ };
1036
+ _addIndividualNft = new WeakSet();
1037
+ addIndividualNft_fn = async function(tokenAddress, tokenId, nftMetadata, nftContract, chainId, userAddress, source) {
1038
+ const releaseLock = await _chunkZ4BLTVTBjs.__privateGet.call(void 0, this, _mutex).acquire();
1039
+ try {
1040
+ const checksumHexAddress = _controllerutils.toChecksumHexAddress.call(void 0, tokenAddress);
1041
+ const { allNfts } = this.state;
1042
+ const nfts = [...allNfts[userAddress]?.[chainId] ?? []];
1043
+ const existingEntry = nfts.find(
1044
+ (nft) => nft.address.toLowerCase() === checksumHexAddress.toLowerCase() && nft.tokenId === tokenId
1045
+ );
1046
+ if (existingEntry) {
1047
+ const differentMetadata = _chunkNEXY7SE2js.compareNftMetadata.call(void 0,
1048
+ nftMetadata,
1049
+ existingEntry
1050
+ );
1051
+ if (!differentMetadata && existingEntry.isCurrentlyOwned) {
1052
+ return;
1053
+ }
1054
+ const indexToUpdate = nfts.findIndex(
1055
+ (nft) => nft.address.toLowerCase() === checksumHexAddress.toLowerCase() && nft.tokenId === tokenId
1056
+ );
1057
+ if (indexToUpdate !== -1) {
1058
+ nfts[indexToUpdate] = {
1059
+ ...existingEntry,
1060
+ ...nftMetadata
1061
+ };
1062
+ }
1063
+ } else {
1064
+ const newEntry = {
1065
+ address: checksumHexAddress,
1066
+ tokenId,
1067
+ favorite: false,
1068
+ isCurrentlyOwned: true,
1069
+ ...nftMetadata
1070
+ };
1071
+ nfts.push(newEntry);
1072
+ }
1073
+ _chunkZ4BLTVTBjs.__privateMethod.call(void 0, this, _updateNestedNftState, updateNestedNftState_fn).call(this, nfts, ALL_NFTS_STATE_KEY, {
1074
+ chainId,
1075
+ userAddress
1076
+ });
1077
+ if (_chunkZ4BLTVTBjs.__privateGet.call(void 0, this, _onNftAdded)) {
1078
+ _chunkZ4BLTVTBjs.__privateGet.call(void 0, this, _onNftAdded).call(this, {
1079
+ address: checksumHexAddress,
1080
+ symbol: nftContract.symbol,
1081
+ tokenId: tokenId.toString(),
1082
+ standard: nftMetadata.standard,
1083
+ source
1084
+ });
1085
+ }
1086
+ } finally {
1087
+ releaseLock();
1088
+ }
1089
+ };
1090
+ _addNftContract = new WeakSet();
1091
+ addNftContract_fn = async function({
1092
+ tokenAddress,
1093
+ userAddress,
1094
+ networkClientId,
1095
+ source,
1096
+ nftMetadata
1097
+ }) {
1098
+ const releaseLock = await _chunkZ4BLTVTBjs.__privateGet.call(void 0, this, _mutex).acquire();
1099
+ try {
1100
+ const checksumHexAddress = _controllerutils.toChecksumHexAddress.call(void 0, tokenAddress);
1101
+ const { allNftContracts } = this.state;
1102
+ const chainId = _chunkZ4BLTVTBjs.__privateMethod.call(void 0, this, _getCorrectChainId, getCorrectChainId_fn).call(this, {
1103
+ networkClientId
1104
+ });
1105
+ const nftContracts = allNftContracts[userAddress]?.[chainId] || [];
1106
+ const existingEntry = nftContracts.find(
1107
+ (nftContract) => nftContract.address.toLowerCase() === checksumHexAddress.toLowerCase()
1108
+ );
1109
+ if (existingEntry) {
1110
+ return nftContracts;
1111
+ }
1112
+ const contractInformation = await _chunkZ4BLTVTBjs.__privateMethod.call(void 0, this, _getNftContractInformation, getNftContractInformation_fn).call(this, checksumHexAddress, nftMetadata, networkClientId);
1113
+ const {
1114
+ asset_contract_type,
1115
+ created_date,
1116
+ symbol,
1117
+ description,
1118
+ external_link,
1119
+ schema_name,
1120
+ collection: { name, image_url, tokenCount }
1121
+ } = contractInformation;
1122
+ if (source === "detected" /* Detected */ && "address" in contractInformation && typeof contractInformation.address === "string" && "collection" in contractInformation && contractInformation.collection.name === null && "image_url" in contractInformation.collection && contractInformation.collection.image_url === null && Object.entries(contractInformation).every(([key, value]) => {
1123
+ return key === "address" || key === "collection" || !value;
1124
+ })) {
1125
+ return nftContracts;
1126
+ }
1127
+ const newEntry = Object.assign(
1128
+ {},
1129
+ { address: checksumHexAddress },
1130
+ description && { description },
1131
+ name && { name },
1132
+ image_url && { logo: image_url },
1133
+ symbol && { symbol },
1134
+ tokenCount !== null && typeof tokenCount !== "undefined" && { totalSupply: tokenCount },
1135
+ asset_contract_type && { assetContractType: asset_contract_type },
1136
+ created_date && { createdDate: created_date },
1137
+ schema_name && { schemaName: schema_name },
1138
+ external_link && { externalLink: external_link }
1139
+ );
1140
+ const newNftContracts = [...nftContracts, newEntry];
1141
+ _chunkZ4BLTVTBjs.__privateMethod.call(void 0, this, _updateNestedNftState, updateNestedNftState_fn).call(this, newNftContracts, ALL_NFTS_CONTRACTS_STATE_KEY, {
1142
+ chainId,
1143
+ userAddress
1144
+ });
1145
+ return newNftContracts;
1146
+ } finally {
1147
+ releaseLock();
1148
+ }
1149
+ };
1150
+ _removeAndIgnoreIndividualNft = new WeakSet();
1151
+ removeAndIgnoreIndividualNft_fn = function(address, tokenId, {
1152
+ chainId,
1153
+ userAddress
1154
+ }) {
1155
+ const checksumHexAddress = _controllerutils.toChecksumHexAddress.call(void 0, address);
1156
+ const { allNfts, ignoredNfts } = this.state;
1157
+ const newIgnoredNfts = [...ignoredNfts];
1158
+ const nfts = allNfts[userAddress]?.[chainId] || [];
1159
+ const newNfts = nfts.filter((nft) => {
1160
+ if (nft.address.toLowerCase() === checksumHexAddress.toLowerCase() && nft.tokenId === tokenId) {
1161
+ const alreadyIgnored = newIgnoredNfts.find(
1162
+ (c) => c.address === checksumHexAddress && c.tokenId === tokenId
1163
+ );
1164
+ !alreadyIgnored && newIgnoredNfts.push(nft);
1165
+ return false;
1166
+ }
1167
+ return true;
1168
+ });
1169
+ _chunkZ4BLTVTBjs.__privateMethod.call(void 0, this, _updateNestedNftState, updateNestedNftState_fn).call(this, newNfts, ALL_NFTS_STATE_KEY, {
1170
+ userAddress,
1171
+ chainId
1172
+ });
1173
+ this.update((state) => {
1174
+ state.ignoredNfts = newIgnoredNfts;
1175
+ });
1176
+ };
1177
+ _removeIndividualNft = new WeakSet();
1178
+ removeIndividualNft_fn = function(address, tokenId, { chainId, userAddress }) {
1179
+ const checksumHexAddress = _controllerutils.toChecksumHexAddress.call(void 0, address);
1180
+ const { allNfts } = this.state;
1181
+ const nfts = allNfts[userAddress]?.[chainId] || [];
1182
+ const newNfts = nfts.filter(
1183
+ (nft) => !(nft.address.toLowerCase() === checksumHexAddress.toLowerCase() && nft.tokenId === tokenId)
1184
+ );
1185
+ _chunkZ4BLTVTBjs.__privateMethod.call(void 0, this, _updateNestedNftState, updateNestedNftState_fn).call(this, newNfts, ALL_NFTS_STATE_KEY, {
1186
+ userAddress,
1187
+ chainId
1188
+ });
1189
+ };
1190
+ _removeNftContract = new WeakSet();
1191
+ removeNftContract_fn = function(address, { chainId, userAddress }) {
1192
+ const checksumHexAddress = _controllerutils.toChecksumHexAddress.call(void 0, address);
1193
+ const { allNftContracts } = this.state;
1194
+ const nftContracts = allNftContracts[userAddress]?.[chainId] || [];
1195
+ const newNftContracts = nftContracts.filter(
1196
+ (nftContract) => !(nftContract.address.toLowerCase() === checksumHexAddress.toLowerCase())
1197
+ );
1198
+ _chunkZ4BLTVTBjs.__privateMethod.call(void 0, this, _updateNestedNftState, updateNestedNftState_fn).call(this, newNftContracts, ALL_NFTS_CONTRACTS_STATE_KEY, {
1199
+ chainId,
1200
+ userAddress
1201
+ });
1202
+ return newNftContracts;
1203
+ };
1204
+ _validateWatchNft = new WeakSet();
1205
+ validateWatchNft_fn = async function(asset, type, userAddress, { networkClientId } = {}) {
1206
+ const { address: contractAddress, tokenId } = asset;
1207
+ if (!type) {
1208
+ throw _rpcerrors.rpcErrors.invalidParams("Asset type is required");
1209
+ }
1210
+ if (type !== _controllerutils.ERC721 && type !== _controllerutils.ERC1155) {
1211
+ throw _rpcerrors.rpcErrors.invalidParams(
1212
+ `Non NFT asset type ${type} not supported by watchNft`
1213
+ );
1214
+ }
1215
+ if (!contractAddress || !tokenId) {
1216
+ throw _rpcerrors.rpcErrors.invalidParams("Both address and tokenId are required");
1217
+ }
1218
+ if (!_address.isAddress.call(void 0, contractAddress)) {
1219
+ throw _rpcerrors.rpcErrors.invalidParams("Invalid address");
1220
+ }
1221
+ if (!/^\d+$/u.test(tokenId)) {
1222
+ throw _rpcerrors.rpcErrors.invalidParams("Invalid tokenId");
1223
+ }
1224
+ try {
1225
+ const isOwner = await this.isNftOwner(
1226
+ userAddress,
1227
+ contractAddress,
1228
+ tokenId,
1229
+ { networkClientId }
1230
+ );
1231
+ if (!isOwner) {
1232
+ throw _rpcerrors.rpcErrors.invalidInput(
1233
+ "Suggested NFT is not owned by the selected account"
1234
+ );
1235
+ }
1236
+ } catch (error) {
1237
+ if (error instanceof Error) {
1238
+ throw _rpcerrors.rpcErrors.resourceUnavailable(error.message);
1239
+ }
1240
+ throw error;
1241
+ }
1242
+ };
1243
+ _getCorrectChainId = new WeakSet();
1244
+ getCorrectChainId_fn = function({
1245
+ networkClientId
1246
+ }) {
1247
+ if (networkClientId) {
1248
+ const {
1249
+ configuration: { chainId }
1250
+ } = this.messagingSystem.call(
1251
+ "NetworkController:getNetworkClientById",
1252
+ networkClientId
1253
+ );
1254
+ return chainId;
1255
+ }
1256
+ return _chunkZ4BLTVTBjs.__privateGet.call(void 0, this, _chainId);
1257
+ };
1258
+ var NftController_default = NftController;
1259
+
1260
+
1261
+
1262
+
1263
+
1264
+ exports.getDefaultNftControllerState = getDefaultNftControllerState; exports.NftController = NftController; exports.NftController_default = NftController_default;
1265
+ //# sourceMappingURL=chunk-CGLUTXI7.js.map