@metamask-previews/assets-controllers 103.1.1-preview-6e596eb → 103.1.1-preview-61580fcee

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/CHANGELOG.md CHANGED
@@ -9,13 +9,6 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
9
9
 
10
10
  ### Changed
11
11
 
12
- - **BREAKING:** `NftController` no longer uses the `AssetsContractController:getERC721OwnerOf` and `AssetsContractController:getERC1155BalanceOf` messenger actions for ownership checks; these have been removed from `AllowedActions` ([#8281](https://github.com/MetaMask/core/pull/8281))
13
- - Consumers that construct the `NftController` messenger and register handlers for these two actions must remove them from their allowed actions list.
14
- - **BREAKING:** Removed the `checkAndUpdateSingleNftOwnershipStatus` method from `NftController` ([#8281](https://github.com/MetaMask/core/pull/8281))
15
- - Use `checkAndUpdateAllNftsOwnershipStatus` instead, which now batches all ownership checks via Multicall3 in a single RPC request.
16
- - `checkAndUpdateAllNftsOwnershipStatus` now removes NFTs confirmed as unowned from state instead of setting `isCurrentlyOwned: false` ([#8281](https://github.com/MetaMask/core/pull/8281))
17
- - The `isCurrentlyOwned: false` flag was originally used to power a "Previously Owned" NFTs section in MetaMask, which is no longer supported. NFTs that are confirmed as no longer owned are now removed from state immediately rather than being retained with a stale flag.
18
- - `NftController` NFT ownership checks (`isNftOwner`, `checkAndUpdateAllNftsOwnershipStatus`) now use Multicall3 to batch ERC-721 `ownerOf` and ERC-1155 `balanceOf` calls into fewer RPC requests, falling back to individual calls on unsupported chains ([#8281](https://github.com/MetaMask/core/pull/8281))
19
12
  - Bump `@metamask/accounts-controller` from `^37.1.1` to `^37.2.0` ([#8363](https://github.com/MetaMask/core/pull/8363))
20
13
  - Bump `@metamask/keyring-controller` from `^25.1.1` to `^25.2.0` ([#8363](https://github.com/MetaMask/core/pull/8363))
21
14
  - Bump `@metamask/messenger` from `^1.0.0` to `^1.1.1` ([#8364](https://github.com/MetaMask/core/pull/8364), [#8373](https://github.com/MetaMask/core/pull/8373))
@@ -17,7 +17,6 @@ var _NftController_instances, _NftController_mutex, _NftController_selectedAccou
17
17
  Object.defineProperty(exports, "__esModule", { value: true });
18
18
  exports.NftController = exports.getDefaultNftControllerState = void 0;
19
19
  const address_1 = require("@ethersproject/address");
20
- const providers_1 = require("@ethersproject/providers");
21
20
  const base_controller_1 = require("@metamask/base-controller");
22
21
  const controller_utils_1 = require("@metamask/controller-utils");
23
22
  const phishing_controller_1 = require("@metamask/phishing-controller");
@@ -28,7 +27,6 @@ const bn_js_1 = __importDefault(require("bn.js"));
28
27
  const uuid_1 = require("uuid");
29
28
  const assetsUtil_1 = require("./assetsUtil.cjs");
30
29
  const constants_1 = require("./constants.cjs");
31
- const multicall_1 = require("./multicall.cjs");
32
30
  const nftControllerMetadata = {
33
31
  allNftContracts: {
34
32
  includeInStateLogs: false,
@@ -167,27 +165,26 @@ class NftController extends base_controller_1.BaseController {
167
165
  * @param nftAddress - NFT contract address.
168
166
  * @param tokenId - NFT token ID.
169
167
  * @param networkClientId - The networkClientId that can be used to identify the network client to use for this request.
170
- * @param options - Optional parameters.
171
- * @param options.standard - The NFT standard ('ERC721' or 'ERC1155'). When provided, only the
172
- * relevant ownership check is performed, halving the number of RPC subcalls.
173
168
  * @returns Promise resolving the NFT ownership.
174
169
  */
175
- async isNftOwner(ownerAddress, nftAddress, tokenId, networkClientId, { standard } = {}) {
176
- const client = this.messenger.call('NetworkController:getNetworkClientById', networkClientId);
177
- const provider = new providers_1.Web3Provider(client.provider);
178
- const { chainId } = client.configuration;
179
- const [result] = await (0, multicall_1.getNftOwnershipForMultipleNfts)([
180
- {
181
- nftAddress,
182
- tokenId,
183
- userAddress: ownerAddress,
184
- standard: standard ?? null,
185
- },
186
- ], chainId, provider);
187
- if (result.isOwned === undefined) {
188
- throw new Error(`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.`);
170
+ async isNftOwner(ownerAddress, nftAddress, tokenId, networkClientId) {
171
+ // Checks the ownership for ERC-721.
172
+ try {
173
+ const owner = await this.messenger.call('AssetsContractController:getERC721OwnerOf', nftAddress, tokenId, networkClientId);
174
+ return ownerAddress.toLowerCase() === owner.toLowerCase();
175
+ }
176
+ catch {
177
+ // Ignore ERC-721 contract error
189
178
  }
190
- return result.isOwned;
179
+ // Checks the ownership for ERC-1155.
180
+ try {
181
+ const balance = await this.messenger.call('AssetsContractController:getERC1155BalanceOf', ownerAddress, nftAddress, tokenId, networkClientId);
182
+ return !balance.isZero();
183
+ }
184
+ catch {
185
+ // Ignore ERC-1155 contract error
186
+ }
187
+ throw new Error(`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.`);
191
188
  }
192
189
  /**
193
190
  * Verifies currently selected address owns entered NFT address/tokenId combo and
@@ -462,10 +459,59 @@ class NftController extends base_controller_1.BaseController {
462
459
  state.ignoredNfts = [];
463
460
  });
464
461
  }
462
+ /**
463
+ * Checks whether input NFT is still owned by the user
464
+ * And updates the isCurrentlyOwned value on the NFT object accordingly.
465
+ *
466
+ * @param nft - The NFT object to check and update.
467
+ * @param batch - A boolean indicating whether this method is being called as part of a batch or single update.
468
+ * @param networkClientId - The networkClientId that can be used to identify the network client to use for this request.
469
+ * @param accountParams - The userAddress and chainId to check ownership against
470
+ * @param accountParams.userAddress - the address passed through the confirmed transaction flow to ensure assets are stored to the correct account
471
+ * @returns the NFT with the updated isCurrentlyOwned value
472
+ */
473
+ async checkAndUpdateSingleNftOwnershipStatus(nft, batch, networkClientId, { userAddress } = {}) {
474
+ const addressToSearch = __classPrivateFieldGet(this, _NftController_instances, "m", _NftController_getAddressOrSelectedAddress).call(this, userAddress);
475
+ const { configuration: { chainId }, } = this.messenger.call('NetworkController:getNetworkClientById', networkClientId);
476
+ const { address, tokenId } = nft;
477
+ let isOwned = nft.isCurrentlyOwned;
478
+ try {
479
+ isOwned = await this.isNftOwner(addressToSearch, address, tokenId, networkClientId);
480
+ }
481
+ catch {
482
+ // ignore error
483
+ // this will only throw an error 'Unable to verify ownership' in which case
484
+ // we want to keep the current value of isCurrentlyOwned for this flow.
485
+ }
486
+ const updatedNft = {
487
+ ...nft,
488
+ isCurrentlyOwned: isOwned,
489
+ };
490
+ if (batch) {
491
+ return updatedNft;
492
+ }
493
+ // if this is not part of a batched update we update this one NFT in state
494
+ const { allNfts } = this.state;
495
+ const nfts = [...(allNfts[addressToSearch]?.[chainId] || [])];
496
+ const indexToUpdate = nfts.findIndex((item) => item.tokenId === tokenId &&
497
+ item.address.toLowerCase() === address.toLowerCase());
498
+ if (indexToUpdate !== -1) {
499
+ nfts[indexToUpdate] = updatedNft;
500
+ this.update((state) => {
501
+ state.allNfts[addressToSearch] = Object.assign({}, state.allNfts[addressToSearch], {
502
+ [chainId]: nfts,
503
+ });
504
+ });
505
+ __classPrivateFieldGet(this, _NftController_instances, "m", _NftController_updateNestedNftState).call(this, nfts, ALL_NFTS_STATE_KEY, {
506
+ userAddress: addressToSearch,
507
+ chainId,
508
+ });
509
+ }
510
+ return updatedNft;
511
+ }
465
512
  /**
466
513
  * Checks whether NFTs associated with current selectedAddress/chainId combination are still owned by the user
467
514
  * And updates the isCurrentlyOwned value on each accordingly.
468
- * Uses Multicall3 to batch all ownership checks into a single RPC request when available.
469
515
  *
470
516
  * @param networkClientId - The networkClientId that can be used to identify the network client to use for this request.
471
517
  * @param options - an object of arguments
@@ -473,30 +519,14 @@ class NftController extends base_controller_1.BaseController {
473
519
  */
474
520
  async checkAndUpdateAllNftsOwnershipStatus(networkClientId, { userAddress, } = {}) {
475
521
  const addressToSearch = __classPrivateFieldGet(this, _NftController_instances, "m", _NftController_getAddressOrSelectedAddress).call(this, userAddress);
476
- const client = this.messenger.call('NetworkController:getNetworkClientById', networkClientId);
477
- const { chainId } = client.configuration;
522
+ const { configuration: { chainId }, } = this.messenger.call('NetworkController:getNetworkClientById', networkClientId);
478
523
  const { allNfts } = this.state;
479
524
  const nfts = allNfts[addressToSearch]?.[chainId] || [];
480
- if (nfts.length === 0) {
481
- return;
482
- }
483
- const provider = new providers_1.Web3Provider(client.provider);
484
- let ownershipResults;
485
- try {
486
- ownershipResults = await (0, multicall_1.getNftOwnershipForMultipleNfts)(nfts.map((nft) => ({
487
- nftAddress: nft.address,
488
- tokenId: nft.tokenId,
489
- userAddress: addressToSearch,
490
- standard: nft.standard,
491
- })), chainId, provider);
492
- }
493
- catch {
494
- return;
495
- }
496
- const updatedNfts = nfts.filter((_nft, index) => {
497
- const { isOwned } = ownershipResults[index];
498
- return isOwned !== false;
499
- });
525
+ const updatedNfts = await Promise.all(nfts.map(async (nft) => {
526
+ return ((await this.checkAndUpdateSingleNftOwnershipStatus(nft, true, networkClientId, {
527
+ userAddress,
528
+ })) ?? nft);
529
+ }));
500
530
  __classPrivateFieldGet(this, _NftController_instances, "m", _NftController_updateNestedNftState).call(this, updatedNfts, ALL_NFTS_STATE_KEY, {
501
531
  userAddress: addressToSearch,
502
532
  chainId,
@@ -1131,7 +1161,7 @@ async function _NftController_addNftContracts(userAddress, contracts) {
1131
1161
  }
1132
1162
  // Check if the user owns the suggested NFT
1133
1163
  try {
1134
- const isOwner = await this.isNftOwner(userAddress, contractAddress, tokenId, networkClientId, { standard: type });
1164
+ const isOwner = await this.isNftOwner(userAddress, contractAddress, tokenId, networkClientId);
1135
1165
  if (!isOwner) {
1136
1166
  throw rpc_errors_1.rpcErrors.invalidInput('Suggested NFT is not owned by the selected account');
1137
1167
  }