@metamask/assets-controllers 26.0.0 → 27.0.1

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