@metamask-previews/assets-controllers 56.0.0-preview-bc1dd30 → 56.0.0-preview-3fadbce0
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.
- package/dist/NftController.cjs +83 -84
- package/dist/NftController.cjs.map +1 -1
- package/dist/NftController.d.cts +26 -41
- package/dist/NftController.d.cts.map +1 -1
- package/dist/NftController.d.mts +26 -41
- package/dist/NftController.d.mts.map +1 -1
- package/dist/NftController.mjs +84 -85
- package/dist/NftController.mjs.map +1 -1
- package/dist/NftDetectionController.cjs +2 -2
- package/dist/NftDetectionController.cjs.map +1 -1
- package/dist/NftDetectionController.d.cts +2 -1
- package/dist/NftDetectionController.d.cts.map +1 -1
- package/dist/NftDetectionController.d.mts +2 -1
- package/dist/NftDetectionController.d.mts.map +1 -1
- package/dist/NftDetectionController.mjs +2 -2
- package/dist/NftDetectionController.mjs.map +1 -1
- package/package.json +1 -1
package/dist/NftController.cjs
CHANGED
@@ -13,7 +13,7 @@ var __classPrivateFieldGet = (this && this.__classPrivateFieldGet) || function (
|
|
13
13
|
var __importDefault = (this && this.__importDefault) || function (mod) {
|
14
14
|
return (mod && mod.__esModule) ? mod : { "default": mod };
|
15
15
|
};
|
16
|
-
var _NftController_instances, _NftController_mutex, _NftController_selectedAccountId,
|
16
|
+
var _NftController_instances, _NftController_mutex, _NftController_selectedAccountId, _NftController_ipfsGateway, _NftController_openSeaEnabled, _NftController_useIpfsSubdomains, _NftController_isIpfsGatewayEnabled, _NftController_onNftAdded, _NftController_onPreferencesControllerStateChange, _NftController_onSelectedAccountChange, _NftController_updateNestedNftState, _NftController_getNftCollectionApi, _NftController_getNftInformationFromApi, _NftController_getNftInformationFromTokenURI, _NftController_getNftURIAndStandard, _NftController_getNftInformation, _NftController_getNftContractInformationFromContract, _NftController_getNftContractInformation, _NftController_addIndividualNft, _NftController_addNftContract, _NftController_removeAndIgnoreIndividualNft, _NftController_removeIndividualNft, _NftController_removeNftContract, _NftController_validateWatchNft, _NftController_getAddressOrSelectedAddress, _NftController_updateNftUpdateForAccount;
|
17
17
|
Object.defineProperty(exports, "__esModule", { value: true });
|
18
18
|
exports.NftController = exports.getDefaultNftControllerState = void 0;
|
19
19
|
const address_1 = require("@ethersproject/address");
|
@@ -52,7 +52,6 @@ class NftController extends base_controller_1.BaseController {
|
|
52
52
|
* Creates an NftController instance.
|
53
53
|
*
|
54
54
|
* @param options - The controller options.
|
55
|
-
* @param options.chainId - The chain ID of the current network.
|
56
55
|
* @param options.ipfsGateway - The configured IPFS gateway.
|
57
56
|
* @param options.openSeaEnabled - Controls whether the OpenSea API is used.
|
58
57
|
* @param options.useIpfsSubdomains - Controls whether IPFS subdomains are used.
|
@@ -62,7 +61,7 @@ class NftController extends base_controller_1.BaseController {
|
|
62
61
|
* @param options.messenger - The messenger.
|
63
62
|
* @param options.state - Initial state to set on this controller.
|
64
63
|
*/
|
65
|
-
constructor({
|
64
|
+
constructor({ ipfsGateway = controller_utils_1.IPFS_DEFAULT_GATEWAY_URL, openSeaEnabled = false, useIpfsSubdomains = true, isIpfsGatewayEnabled = true, onNftAdded, messenger, state = {}, }) {
|
66
65
|
super({
|
67
66
|
name: controllerName,
|
68
67
|
metadata: nftControllerMetadata,
|
@@ -75,14 +74,12 @@ class NftController extends base_controller_1.BaseController {
|
|
75
74
|
_NftController_instances.add(this);
|
76
75
|
_NftController_mutex.set(this, new async_mutex_1.Mutex());
|
77
76
|
_NftController_selectedAccountId.set(this, void 0);
|
78
|
-
_NftController_chainId.set(this, void 0);
|
79
77
|
_NftController_ipfsGateway.set(this, void 0);
|
80
78
|
_NftController_openSeaEnabled.set(this, void 0);
|
81
79
|
_NftController_useIpfsSubdomains.set(this, void 0);
|
82
80
|
_NftController_isIpfsGatewayEnabled.set(this, void 0);
|
83
81
|
_NftController_onNftAdded.set(this, void 0);
|
84
82
|
__classPrivateFieldSet(this, _NftController_selectedAccountId, this.messagingSystem.call('AccountsController:getSelectedAccount').id, "f");
|
85
|
-
__classPrivateFieldSet(this, _NftController_chainId, initialChainId, "f");
|
86
83
|
__classPrivateFieldSet(this, _NftController_ipfsGateway, ipfsGateway, "f");
|
87
84
|
__classPrivateFieldSet(this, _NftController_openSeaEnabled, openSeaEnabled, "f");
|
88
85
|
__classPrivateFieldSet(this, _NftController_useIpfsSubdomains, useIpfsSubdomains, "f");
|
@@ -92,7 +89,6 @@ class NftController extends base_controller_1.BaseController {
|
|
92
89
|
// TODO: Either fix this lint violation or explain why it's necessary to ignore.
|
93
90
|
// eslint-disable-next-line @typescript-eslint/no-misused-promises
|
94
91
|
__classPrivateFieldGet(this, _NftController_instances, "m", _NftController_onPreferencesControllerStateChange).bind(this));
|
95
|
-
this.messagingSystem.subscribe('NetworkController:networkDidChange', __classPrivateFieldGet(this, _NftController_instances, "m", _NftController_onNetworkControllerNetworkDidChange).bind(this));
|
96
92
|
this.messagingSystem.subscribe('AccountsController:selectedEvmAccountChange',
|
97
93
|
// TODO: Either fix this lint violation or explain why it's necessary to ignore.
|
98
94
|
// eslint-disable-next-line @typescript-eslint/no-misused-promises
|
@@ -112,17 +108,20 @@ class NftController extends base_controller_1.BaseController {
|
|
112
108
|
* @param asset.tokenId - The ID of the asset.
|
113
109
|
* @param type - The asset type.
|
114
110
|
* @param origin - Domain origin to register the asset from.
|
111
|
+
* @param networkClientId - The networkClientId that can be used to identify the network client to use for this request.
|
115
112
|
* @param options - Options bag.
|
116
|
-
* @param options.networkClientId - The networkClientId that can be used to identify the network client to use for this request.
|
117
113
|
* @param options.userAddress - The address of the account where the NFT is being added.
|
118
114
|
* @returns Object containing a Promise resolving to the suggestedAsset address if accepted.
|
119
115
|
*/
|
120
|
-
async watchNft(asset, type, origin,
|
116
|
+
async watchNft(asset, type, origin, networkClientId, { userAddress, } = {}) {
|
121
117
|
const addressToSearch = __classPrivateFieldGet(this, _NftController_instances, "m", _NftController_getAddressOrSelectedAddress).call(this, userAddress);
|
122
118
|
if (!addressToSearch) {
|
123
119
|
return;
|
124
120
|
}
|
125
|
-
|
121
|
+
if (!networkClientId) {
|
122
|
+
throw rpc_errors_1.rpcErrors.invalidParams('Network client id is required');
|
123
|
+
}
|
124
|
+
await __classPrivateFieldGet(this, _NftController_instances, "m", _NftController_validateWatchNft).call(this, asset, type, addressToSearch, networkClientId);
|
126
125
|
const nftMetadata = await __classPrivateFieldGet(this, _NftController_instances, "m", _NftController_getNftInformation).call(this, asset.address, asset.tokenId, networkClientId);
|
127
126
|
if (nftMetadata.standard && nftMetadata.standard !== type) {
|
128
127
|
throw rpc_errors_1.rpcErrors.invalidInput(`Suggested NFT of type ${nftMetadata.standard} does not match received type ${type}`);
|
@@ -138,7 +137,7 @@ class NftController extends base_controller_1.BaseController {
|
|
138
137
|
await this._requestApproval(suggestedNftMeta);
|
139
138
|
const { address, tokenId } = asset;
|
140
139
|
const { name, standard, description, image } = nftMetadata;
|
141
|
-
await this.addNft(address, tokenId, {
|
140
|
+
await this.addNft(address, tokenId, networkClientId, {
|
142
141
|
nftMetadata: {
|
143
142
|
name: name ?? null,
|
144
143
|
description: description ?? null,
|
@@ -147,7 +146,6 @@ class NftController extends base_controller_1.BaseController {
|
|
147
146
|
},
|
148
147
|
userAddress,
|
149
148
|
source: constants_1.Source.Dapp,
|
150
|
-
networkClientId,
|
151
149
|
});
|
152
150
|
}
|
153
151
|
/**
|
@@ -164,11 +162,10 @@ class NftController extends base_controller_1.BaseController {
|
|
164
162
|
* @param ownerAddress - User public address.
|
165
163
|
* @param nftAddress - NFT contract address.
|
166
164
|
* @param tokenId - NFT token ID.
|
167
|
-
* @param
|
168
|
-
* @param options.networkClientId - The networkClientId that can be used to identify the network client to use for this request.
|
165
|
+
* @param networkClientId - The networkClientId that can be used to identify the network client to use for this request.
|
169
166
|
* @returns Promise resolving the NFT ownership.
|
170
167
|
*/
|
171
|
-
async isNftOwner(ownerAddress, nftAddress, tokenId,
|
168
|
+
async isNftOwner(ownerAddress, nftAddress, tokenId, networkClientId) {
|
172
169
|
// Checks the ownership for ERC-721.
|
173
170
|
try {
|
174
171
|
const owner = await this.messagingSystem.call('AssetsContractController:getERC721OwnerOf', nftAddress, tokenId, networkClientId);
|
@@ -195,20 +192,17 @@ class NftController extends base_controller_1.BaseController {
|
|
195
192
|
*
|
196
193
|
* @param address - Hex address of the NFT contract.
|
197
194
|
* @param tokenId - The NFT identifier.
|
195
|
+
* @param networkClientId - The networkClientId that can be used to identify the network client to use for this request.
|
198
196
|
* @param options - an object of arguments
|
199
197
|
* @param options.userAddress - The address of the current user.
|
200
|
-
* @param options.networkClientId - The networkClientId that can be used to identify the network client to use for this request.
|
201
198
|
* @param options.source - Whether the NFT was detected, added manually or suggested by a dapp.
|
202
199
|
*/
|
203
|
-
async addNftVerifyOwnership(address, tokenId, { userAddress,
|
200
|
+
async addNftVerifyOwnership(address, tokenId, networkClientId, { userAddress, source, } = {}) {
|
204
201
|
const addressToSearch = __classPrivateFieldGet(this, _NftController_instances, "m", _NftController_getAddressOrSelectedAddress).call(this, userAddress);
|
205
|
-
if (!(await this.isNftOwner(addressToSearch, address, tokenId, {
|
206
|
-
networkClientId,
|
207
|
-
}))) {
|
202
|
+
if (!(await this.isNftOwner(addressToSearch, address, tokenId, networkClientId))) {
|
208
203
|
throw new Error('This NFT is not owned by the user');
|
209
204
|
}
|
210
|
-
await this.addNft(address, tokenId, {
|
211
|
-
networkClientId,
|
205
|
+
await this.addNft(address, tokenId, networkClientId, {
|
212
206
|
userAddress: addressToSearch,
|
213
207
|
source,
|
214
208
|
});
|
@@ -218,42 +212,39 @@ class NftController extends base_controller_1.BaseController {
|
|
218
212
|
*
|
219
213
|
* @param tokenAddress - Hex address of the NFT contract.
|
220
214
|
* @param tokenId - The NFT identifier.
|
215
|
+
* @param networkClientId - The networkClientId that can be used to identify the network client to use for this request.
|
221
216
|
* @param options - an object of arguments
|
222
217
|
* @param options.nftMetadata - NFT optional metadata.
|
223
218
|
* @param options.userAddress - The address of the current user.
|
224
219
|
* @param options.source - Whether the NFT was detected, added manually or suggested by a dapp.
|
225
|
-
* @param options.networkClientId - The networkClientId that can be used to identify the network client to use for this request.
|
226
|
-
* @param options.chainId - The chain ID to add the NFT to.
|
227
220
|
* @returns Promise resolving to the current NFT list.
|
228
221
|
*/
|
229
|
-
async addNft(tokenAddress, tokenId, { nftMetadata, userAddress, source = constants_1.Source.Custom,
|
222
|
+
async addNft(tokenAddress, tokenId, networkClientId, { nftMetadata, userAddress, source = constants_1.Source.Custom, } = {}) {
|
230
223
|
const addressToSearch = __classPrivateFieldGet(this, _NftController_instances, "m", _NftController_getAddressOrSelectedAddress).call(this, userAddress);
|
231
224
|
if (!addressToSearch) {
|
232
225
|
return;
|
233
226
|
}
|
234
227
|
const checksumHexAddress = (0, controller_utils_1.toChecksumHexAddress)(tokenAddress);
|
235
|
-
// TODO: revisit this with Solana support and instead of passing chainId, make sure chainId is read from nftMetadata
|
236
|
-
const chainIdToAddTo = chainId || __classPrivateFieldGet(this, _NftController_instances, "m", _NftController_getCorrectChainId).call(this, { networkClientId });
|
237
228
|
nftMetadata =
|
238
229
|
nftMetadata ||
|
239
230
|
(await __classPrivateFieldGet(this, _NftController_instances, "m", _NftController_getNftInformation).call(this, checksumHexAddress, tokenId, networkClientId));
|
240
|
-
const newNftContracts = await __classPrivateFieldGet(this, _NftController_instances, "m", _NftController_addNftContract).call(this, {
|
231
|
+
const newNftContracts = await __classPrivateFieldGet(this, _NftController_instances, "m", _NftController_addNftContract).call(this, networkClientId, {
|
241
232
|
tokenAddress: checksumHexAddress,
|
242
233
|
userAddress: addressToSearch,
|
243
|
-
networkClientId,
|
244
234
|
source,
|
245
235
|
nftMetadata,
|
246
|
-
chainIdHex: source === constants_1.Source.Detected ? chainIdToAddTo : undefined,
|
247
236
|
});
|
248
237
|
// If NFT contract was not added, do not add individual NFT
|
249
238
|
const nftContract = newNftContracts.find((contract) => contract.address.toLowerCase() === checksumHexAddress.toLowerCase());
|
239
|
+
const { configuration: { chainId }, } = this.messagingSystem.call('NetworkController:getNetworkClientById', networkClientId);
|
250
240
|
// This is the case when the NFT is added manually and not detected automatically
|
241
|
+
// TODO: An improvement would be to make the chainId a required field and return it when getting the NFT information
|
251
242
|
if (!nftMetadata.chainId) {
|
252
|
-
nftMetadata.chainId = (0, controller_utils_1.convertHexToDecimal)(
|
243
|
+
nftMetadata.chainId = (0, controller_utils_1.convertHexToDecimal)(chainId);
|
253
244
|
}
|
254
245
|
// If NFT contract information, add individual NFT
|
255
246
|
if (nftContract) {
|
256
|
-
await __classPrivateFieldGet(this, _NftController_instances, "m", _NftController_addIndividualNft).call(this, checksumHexAddress, tokenId, nftMetadata, nftContract,
|
247
|
+
await __classPrivateFieldGet(this, _NftController_instances, "m", _NftController_addIndividualNft).call(this, checksumHexAddress, tokenId, nftMetadata, nftContract, chainId, addressToSearch, source);
|
257
248
|
}
|
258
249
|
}
|
259
250
|
/**
|
@@ -262,13 +253,11 @@ class NftController extends base_controller_1.BaseController {
|
|
262
253
|
* @param options - Options for refetching NFT metadata
|
263
254
|
* @param options.nfts - nfts to update metadata for.
|
264
255
|
* @param options.userAddress - The current user address
|
265
|
-
* @param options.networkClientId - The networkClientId that can be used to identify the network client to use for this request.
|
266
256
|
*/
|
267
|
-
async updateNftMetadata({ nfts, userAddress,
|
257
|
+
async updateNftMetadata({ nfts, userAddress, }) {
|
268
258
|
const addressToSearch = __classPrivateFieldGet(this, _NftController_instances, "m", _NftController_getAddressOrSelectedAddress).call(this, userAddress);
|
269
259
|
const releaseLock = await __classPrivateFieldGet(this, _NftController_mutex, "f").acquire();
|
270
260
|
try {
|
271
|
-
const chainId = __classPrivateFieldGet(this, _NftController_instances, "m", _NftController_getCorrectChainId).call(this, { networkClientId });
|
272
261
|
const nftsWithChecksumAdr = nfts.map((nft) => {
|
273
262
|
return {
|
274
263
|
...nft,
|
@@ -276,7 +265,11 @@ class NftController extends base_controller_1.BaseController {
|
|
276
265
|
};
|
277
266
|
});
|
278
267
|
const nftMetadataResults = await Promise.all(nftsWithChecksumAdr.map(async (nft) => {
|
279
|
-
|
268
|
+
// Each NFT should have a chainId; convert nft.chainId to networkClientId
|
269
|
+
const networkClientId = this.messagingSystem.call('NetworkController:getNetworkClientIdByChainId', (0, controller_utils_1.toHex)(nft.chainId));
|
270
|
+
const resMetadata = networkClientId
|
271
|
+
? await __classPrivateFieldGet(this, _NftController_instances, "m", _NftController_getNftInformation).call(this, nft.address, nft.tokenId, networkClientId)
|
272
|
+
: undefined;
|
280
273
|
return {
|
281
274
|
nft,
|
282
275
|
newMetadata: resMetadata,
|
@@ -285,19 +278,27 @@ class NftController extends base_controller_1.BaseController {
|
|
285
278
|
// We want to avoid updating the state if the state and fetched nft info are the same
|
286
279
|
const nftsWithDifferentMetadata = [];
|
287
280
|
const { allNfts } = this.state;
|
288
|
-
|
281
|
+
// get from state allNfts that match nftsWithChecksumAdr
|
282
|
+
const stateNfts = nftsWithChecksumAdr.map((nft) => {
|
283
|
+
return allNfts[addressToSearch]?.[(0, controller_utils_1.toHex)(nft.chainId)]?.find((nftElement) => nftElement.address.toLowerCase() === nft.address.toLowerCase() &&
|
284
|
+
nftElement.tokenId === nft.tokenId);
|
285
|
+
});
|
289
286
|
nftMetadataResults.forEach((singleNft) => {
|
290
|
-
const existingEntry = stateNfts.find((nft) => nft
|
291
|
-
|
292
|
-
|
287
|
+
const existingEntry = stateNfts.find((nft) => nft?.address.toLowerCase() ===
|
288
|
+
singleNft.nft.address.toLowerCase() &&
|
289
|
+
nft?.tokenId === singleNft.nft.tokenId);
|
290
|
+
if (existingEntry && singleNft.newMetadata) {
|
293
291
|
const differentMetadata = (0, assetsUtil_1.compareNftMetadata)(singleNft.newMetadata, existingEntry);
|
294
292
|
if (differentMetadata) {
|
295
|
-
nftsWithDifferentMetadata.push(
|
293
|
+
nftsWithDifferentMetadata.push({
|
294
|
+
nft: singleNft.nft,
|
295
|
+
newMetadata: singleNft.newMetadata,
|
296
|
+
});
|
296
297
|
}
|
297
298
|
}
|
298
299
|
});
|
299
300
|
if (nftsWithDifferentMetadata.length !== 0) {
|
300
|
-
nftsWithDifferentMetadata.forEach((elm) => this.updateNft(elm.nft, elm.newMetadata, addressToSearch, chainId));
|
301
|
+
nftsWithDifferentMetadata.forEach((elm) => this.updateNft(elm.nft, elm.newMetadata, addressToSearch, (0, controller_utils_1.toHex)(elm.nft.chainId)));
|
301
302
|
}
|
302
303
|
}
|
303
304
|
finally {
|
@@ -309,13 +310,13 @@ class NftController extends base_controller_1.BaseController {
|
|
309
310
|
*
|
310
311
|
* @param address - Hex address of the NFT contract.
|
311
312
|
* @param tokenId - Token identifier of the NFT.
|
313
|
+
* @param networkClientId - The networkClientId that can be used to identify the network client to use for this request.
|
312
314
|
* @param options - an object of arguments
|
313
|
-
* @param options.networkClientId - The networkClientId that can be used to identify the network client to use for this request.
|
314
315
|
* @param options.userAddress - The address of the account where the NFT is being removed.
|
315
316
|
*/
|
316
|
-
removeNft(address, tokenId,
|
317
|
+
removeNft(address, tokenId, networkClientId, { userAddress } = {}) {
|
317
318
|
const addressToSearch = __classPrivateFieldGet(this, _NftController_instances, "m", _NftController_getAddressOrSelectedAddress).call(this, userAddress);
|
318
|
-
const
|
319
|
+
const { configuration: { chainId }, } = this.messagingSystem.call('NetworkController:getNetworkClientById', networkClientId);
|
319
320
|
const checksumHexAddress = (0, controller_utils_1.toChecksumHexAddress)(address);
|
320
321
|
__classPrivateFieldGet(this, _NftController_instances, "m", _NftController_removeIndividualNft).call(this, checksumHexAddress, tokenId, {
|
321
322
|
chainId,
|
@@ -336,13 +337,13 @@ class NftController extends base_controller_1.BaseController {
|
|
336
337
|
*
|
337
338
|
* @param address - Hex address of the NFT contract.
|
338
339
|
* @param tokenId - Token identifier of the NFT.
|
340
|
+
* @param networkClientId - The networkClientId that can be used to identify the network client to use for this request.
|
339
341
|
* @param options - an object of arguments
|
340
|
-
* @param options.networkClientId - The networkClientId that can be used to identify the network client to use for this request.
|
341
342
|
* @param options.userAddress - The address of the account where the NFT is being removed.
|
342
343
|
*/
|
343
|
-
removeAndIgnoreNft(address, tokenId,
|
344
|
+
removeAndIgnoreNft(address, tokenId, networkClientId, { userAddress } = {}) {
|
344
345
|
const addressToSearch = __classPrivateFieldGet(this, _NftController_instances, "m", _NftController_getAddressOrSelectedAddress).call(this, userAddress);
|
345
|
-
const
|
346
|
+
const { configuration: { chainId }, } = this.messagingSystem.call('NetworkController:getNetworkClientById', networkClientId);
|
346
347
|
const checksumHexAddress = (0, controller_utils_1.toChecksumHexAddress)(address);
|
347
348
|
__classPrivateFieldGet(this, _NftController_instances, "m", _NftController_removeAndIgnoreIndividualNft).call(this, checksumHexAddress, tokenId, {
|
348
349
|
chainId,
|
@@ -372,20 +373,18 @@ class NftController extends base_controller_1.BaseController {
|
|
372
373
|
*
|
373
374
|
* @param nft - The NFT object to check and update.
|
374
375
|
* @param batch - A boolean indicating whether this method is being called as part of a batch or single update.
|
376
|
+
* @param networkClientId - The networkClientId that can be used to identify the network client to use for this request.
|
375
377
|
* @param accountParams - The userAddress and chainId to check ownership against
|
376
378
|
* @param accountParams.userAddress - the address passed through the confirmed transaction flow to ensure assets are stored to the correct account
|
377
|
-
* @param accountParams.networkClientId - The networkClientId that can be used to identify the network client to use for this request.
|
378
379
|
* @returns the NFT with the updated isCurrentlyOwned value
|
379
380
|
*/
|
380
|
-
async checkAndUpdateSingleNftOwnershipStatus(nft, batch, { userAddress
|
381
|
+
async checkAndUpdateSingleNftOwnershipStatus(nft, batch, networkClientId, { userAddress } = {}) {
|
381
382
|
const addressToSearch = __classPrivateFieldGet(this, _NftController_instances, "m", _NftController_getAddressOrSelectedAddress).call(this, userAddress);
|
382
|
-
const
|
383
|
+
const { configuration: { chainId }, } = this.messagingSystem.call('NetworkController:getNetworkClientById', networkClientId);
|
383
384
|
const { address, tokenId } = nft;
|
384
385
|
let isOwned = nft.isCurrentlyOwned;
|
385
386
|
try {
|
386
|
-
isOwned = await this.isNftOwner(addressToSearch, address, tokenId,
|
387
|
-
networkClientId,
|
388
|
-
});
|
387
|
+
isOwned = await this.isNftOwner(addressToSearch, address, tokenId, networkClientId);
|
389
388
|
}
|
390
389
|
catch {
|
391
390
|
// ignore error
|
@@ -421,18 +420,18 @@ class NftController extends base_controller_1.BaseController {
|
|
421
420
|
/**
|
422
421
|
* Checks whether NFTs associated with current selectedAddress/chainId combination are still owned by the user
|
423
422
|
* And updates the isCurrentlyOwned value on each accordingly.
|
423
|
+
*
|
424
|
+
* @param networkClientId - The networkClientId that can be used to identify the network client to use for this request.
|
424
425
|
* @param options - an object of arguments
|
425
|
-
* @param options.networkClientId - The networkClientId that can be used to identify the network client to use for this request.
|
426
426
|
* @param options.userAddress - The address of the account where the NFT ownership status is checked/updated.
|
427
427
|
*/
|
428
|
-
async checkAndUpdateAllNftsOwnershipStatus(
|
428
|
+
async checkAndUpdateAllNftsOwnershipStatus(networkClientId, { userAddress, } = {}) {
|
429
429
|
const addressToSearch = __classPrivateFieldGet(this, _NftController_instances, "m", _NftController_getAddressOrSelectedAddress).call(this, userAddress);
|
430
|
-
const
|
430
|
+
const { configuration: { chainId }, } = this.messagingSystem.call('NetworkController:getNetworkClientById', networkClientId);
|
431
431
|
const { allNfts } = this.state;
|
432
432
|
const nfts = allNfts[addressToSearch]?.[chainId] || [];
|
433
433
|
const updatedNfts = await Promise.all(nfts.map(async (nft) => {
|
434
|
-
return ((await this.checkAndUpdateSingleNftOwnershipStatus(nft, true, {
|
435
|
-
networkClientId,
|
434
|
+
return ((await this.checkAndUpdateSingleNftOwnershipStatus(nft, true, networkClientId, {
|
436
435
|
userAddress,
|
437
436
|
})) ?? nft);
|
438
437
|
}));
|
@@ -447,13 +446,13 @@ class NftController extends base_controller_1.BaseController {
|
|
447
446
|
* @param address - Hex address of the NFT contract.
|
448
447
|
* @param tokenId - Hex address of the NFT contract.
|
449
448
|
* @param favorite - NFT new favorite status.
|
449
|
+
* @param networkClientId - The networkClientId that can be used to identify the network client to use for this request.
|
450
450
|
* @param options - an object of arguments
|
451
|
-
* @param options.networkClientId - The networkClientId that can be used to identify the network client to use for this request.
|
452
451
|
* @param options.userAddress - The address of the account where the NFT is being removed.
|
453
452
|
*/
|
454
|
-
updateNftFavoriteStatus(address, tokenId, favorite,
|
453
|
+
updateNftFavoriteStatus(address, tokenId, favorite, networkClientId, { userAddress, } = {}) {
|
455
454
|
const addressToSearch = __classPrivateFieldGet(this, _NftController_instances, "m", _NftController_getAddressOrSelectedAddress).call(this, userAddress);
|
456
|
-
const
|
455
|
+
const { configuration: { chainId }, } = this.messagingSystem.call('NetworkController:getNetworkClientById', networkClientId);
|
457
456
|
const { allNfts } = this.state;
|
458
457
|
const nfts = [...(allNfts[addressToSearch]?.[chainId] || [])];
|
459
458
|
const index = nfts.findIndex((nft) => nft.address === address && nft.tokenId === tokenId);
|
@@ -597,12 +596,10 @@ class NftController extends base_controller_1.BaseController {
|
|
597
596
|
}
|
598
597
|
}
|
599
598
|
exports.NftController = NftController;
|
600
|
-
_NftController_mutex = new WeakMap(), _NftController_selectedAccountId = new WeakMap(),
|
601
|
-
const { configuration: { chainId }, } = this.messagingSystem.call('NetworkController:getNetworkClientById', selectedNetworkClientId);
|
602
|
-
__classPrivateFieldSet(this, _NftController_chainId, chainId, "f");
|
603
|
-
}, _NftController_onPreferencesControllerStateChange =
|
599
|
+
_NftController_mutex = new WeakMap(), _NftController_selectedAccountId = new WeakMap(), _NftController_ipfsGateway = new WeakMap(), _NftController_openSeaEnabled = new WeakMap(), _NftController_useIpfsSubdomains = new WeakMap(), _NftController_isIpfsGatewayEnabled = new WeakMap(), _NftController_onNftAdded = new WeakMap(), _NftController_instances = new WeakSet(), _NftController_onPreferencesControllerStateChange =
|
604
600
|
/**
|
605
601
|
* Handles the state change of the preference controller.
|
602
|
+
*
|
606
603
|
* @param preferencesState - The new state of the preference controller.
|
607
604
|
* @param preferencesState.ipfsGateway - The configured IPFS gateway.
|
608
605
|
* @param preferencesState.openSeaEnabled - Controls whether the OpenSea API is used.
|
@@ -626,6 +623,7 @@ async function _NftController_onPreferencesControllerStateChange({ ipfsGateway,
|
|
626
623
|
}, _NftController_onSelectedAccountChange =
|
627
624
|
/**
|
628
625
|
* Handles the selected account change on the accounts controller.
|
626
|
+
*
|
629
627
|
* @param internalAccount - The new selected account.
|
630
628
|
*/
|
631
629
|
async function _NftController_onSelectedAccountChange(internalAccount) {
|
@@ -848,9 +846,7 @@ async function _NftController_getNftURIAndStandard(contractAddress, tokenId, net
|
|
848
846
|
* @returns Promise resolving to the current NFT name and image.
|
849
847
|
*/
|
850
848
|
async function _NftController_getNftInformation(contractAddress, tokenId, networkClientId) {
|
851
|
-
const
|
852
|
-
networkClientId,
|
853
|
-
});
|
849
|
+
const { configuration: { chainId }, } = this.messagingSystem.call('NetworkController:getNetworkClientById', networkClientId);
|
854
850
|
const [blockchainMetadata, nftApiMetadata] = await Promise.all([
|
855
851
|
(0, controller_utils_1.safelyExecute)(() => __classPrivateFieldGet(this, _NftController_instances, "m", _NftController_getNftInformationFromTokenURI).call(this, contractAddress, tokenId, networkClientId)),
|
856
852
|
__classPrivateFieldGet(this, _NftController_openSeaEnabled, "f") && chainId === '0x1'
|
@@ -873,7 +869,9 @@ async function _NftController_getNftInformation(contractAddress, tokenId, networ
|
|
873
869
|
* @param networkClientId - The networkClientId that can be used to identify the network client to use for this request.
|
874
870
|
* @returns Promise resolving to the current NFT name and image.
|
875
871
|
*/
|
876
|
-
async function _NftController_getNftContractInformationFromContract(
|
872
|
+
async function _NftController_getNftContractInformationFromContract(
|
873
|
+
// TODO for calls to blockchain we need to explicitly pass the currentNetworkClientId since its relying on the provider
|
874
|
+
contractAddress, networkClientId) {
|
877
875
|
const [name, symbol] = await Promise.all([
|
878
876
|
this.messagingSystem.call('AssetsContractController:getERC721AssetName', contractAddress, networkClientId),
|
879
877
|
this.messagingSystem.call('AssetsContractController:getERC721AssetSymbol', contractAddress, networkClientId),
|
@@ -1008,22 +1006,20 @@ async function _NftController_addIndividualNft(tokenAddress, tokenId, nftMetadat
|
|
1008
1006
|
/**
|
1009
1007
|
* Adds an NFT contract to the stored NFT contracts list.
|
1010
1008
|
*
|
1009
|
+
* @param networkClientId - The networkClientId that can be used to identify the network client to use for this request.
|
1011
1010
|
* @param options - options.
|
1012
1011
|
* @param options.tokenAddress - Hex address of the NFT contract.
|
1013
1012
|
* @param options.userAddress - The address of the account where the NFT is being added.
|
1014
1013
|
* @param options.nftMetadata - The retrieved NFTMetadata from API.
|
1015
|
-
* @param options.networkClientId - The networkClientId that can be used to identify the network client to use for this request.
|
1016
1014
|
* @param options.source - Whether the NFT was detected, added manually or suggested by a dapp.
|
1017
|
-
* @param options.chainIdHex - The chainId to add the NFT contract to.
|
1018
1015
|
* @returns Promise resolving to the current NFT contracts list.
|
1019
1016
|
*/
|
1020
|
-
async function _NftController_addNftContract({ tokenAddress, userAddress,
|
1017
|
+
async function _NftController_addNftContract(networkClientId, { tokenAddress, userAddress, source, nftMetadata, }) {
|
1021
1018
|
const releaseLock = await __classPrivateFieldGet(this, _NftController_mutex, "f").acquire();
|
1022
1019
|
try {
|
1023
1020
|
const checksumHexAddress = (0, controller_utils_1.toChecksumHexAddress)(tokenAddress);
|
1024
1021
|
const { allNftContracts } = this.state;
|
1025
|
-
|
1026
|
-
const chainId = chainIdHex || __classPrivateFieldGet(this, _NftController_instances, "m", _NftController_getCorrectChainId).call(this, { networkClientId });
|
1022
|
+
const { configuration: { chainId }, } = this.messagingSystem.call('NetworkController:getNetworkClientById', networkClientId);
|
1027
1023
|
const nftContracts = allNftContracts[userAddress]?.[chainId] || [];
|
1028
1024
|
const existingEntry = nftContracts.find((nftContract) => nftContract.address.toLowerCase() ===
|
1029
1025
|
checksumHexAddress.toLowerCase());
|
@@ -1117,7 +1113,7 @@ async function _NftController_addNftContract({ tokenAddress, userAddress, networ
|
|
1117
1113
|
userAddress,
|
1118
1114
|
});
|
1119
1115
|
return newNftContracts;
|
1120
|
-
}, _NftController_validateWatchNft = async function _NftController_validateWatchNft(asset, type, userAddress,
|
1116
|
+
}, _NftController_validateWatchNft = async function _NftController_validateWatchNft(asset, type, userAddress, networkClientId) {
|
1121
1117
|
const { address: contractAddress, tokenId } = asset;
|
1122
1118
|
// Validate parameters
|
1123
1119
|
if (!type) {
|
@@ -1140,7 +1136,7 @@ async function _NftController_addNftContract({ tokenAddress, userAddress, networ
|
|
1140
1136
|
}
|
1141
1137
|
// Check if the user owns the suggested NFT
|
1142
1138
|
try {
|
1143
|
-
const isOwner = await this.isNftOwner(userAddress, contractAddress, tokenId,
|
1139
|
+
const isOwner = await this.isNftOwner(userAddress, contractAddress, tokenId, networkClientId);
|
1144
1140
|
if (!isOwner) {
|
1145
1141
|
throw rpc_errors_1.rpcErrors.invalidInput('Suggested NFT is not owned by the selected account');
|
1146
1142
|
}
|
@@ -1152,12 +1148,6 @@ async function _NftController_addNftContract({ tokenAddress, userAddress, networ
|
|
1152
1148
|
}
|
1153
1149
|
throw error;
|
1154
1150
|
}
|
1155
|
-
}, _NftController_getCorrectChainId = function _NftController_getCorrectChainId({ networkClientId, }) {
|
1156
|
-
if (networkClientId) {
|
1157
|
-
const { configuration: { chainId }, } = this.messagingSystem.call('NetworkController:getNetworkClientById', networkClientId);
|
1158
|
-
return chainId;
|
1159
|
-
}
|
1160
|
-
return __classPrivateFieldGet(this, _NftController_chainId, "f");
|
1161
1151
|
}, _NftController_getAddressOrSelectedAddress = function _NftController_getAddressOrSelectedAddress(address) {
|
1162
1152
|
if (address) {
|
1163
1153
|
return address;
|
@@ -1165,12 +1155,21 @@ async function _NftController_addNftContract({ tokenAddress, userAddress, networ
|
|
1165
1155
|
// If the address is not defined (or empty), we fallback to the currently selected account's address
|
1166
1156
|
const selectedAccount = this.messagingSystem.call('AccountsController:getAccount', __classPrivateFieldGet(this, _NftController_selectedAccountId, "f"));
|
1167
1157
|
return selectedAccount?.address || '';
|
1168
|
-
}, _NftController_updateNftUpdateForAccount =
|
1169
|
-
|
1158
|
+
}, _NftController_updateNftUpdateForAccount =
|
1159
|
+
/**
|
1160
|
+
* Updates the all nfts in state for the account.
|
1161
|
+
* Nfts will be updated if they don't have a name, description or image.
|
1162
|
+
*
|
1163
|
+
* @param account - The account to update the NFT metadata for.
|
1164
|
+
*/
|
1165
|
+
async function _NftController_updateNftUpdateForAccount(account) {
|
1166
|
+
// get all nfts for the account for all chains
|
1167
|
+
const nfts = Object.values(this.state.allNfts[account.address] || {}).flat();
|
1170
1168
|
// Filter only nfts
|
1171
1169
|
const nftsToUpdate = nfts.filter((singleNft) => !singleNft.name && !singleNft.description && !singleNft.image);
|
1172
1170
|
if (nftsToUpdate.length !== 0 &&
|
1173
1171
|
nftsToUpdate.length < NFT_UPDATE_THRESHOLD) {
|
1172
|
+
// TODO: get the chainId for the NFT
|
1174
1173
|
await this.updateNftMetadata({
|
1175
1174
|
nfts: nftsToUpdate,
|
1176
1175
|
userAddress: account.address,
|