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