Skip to content

Commit

Permalink
fix: update patch for updateNftMetadata fct (#9876)
Browse files Browse the repository at this point in the history
## **Description**

Updates a a patch for assets controllers based on a small update made on
this PR MetaMask/core#4325

## **Related issues**

Related: MetaMask/core#4325
Related: #9759

## **Manual testing steps**

Same steps on this #9759

## **Screenshots/Recordings**

<!-- If applicable, add screenshots and/or recordings to visualize the
before and after of your change. -->

### **Before**

<!-- [screenshots/recordings] -->

### **After**

<!-- [screenshots/recordings] -->

## **Pre-merge author checklist**

- [ ] I’ve followed [MetaMask Coding
Standards](https://github.com/MetaMask/metamask-mobile/blob/main/.github/guidelines/CODING_GUIDELINES.md).
- [ ] I've completed the PR template to the best of my ability
- [ ] I’ve included tests if applicable
- [ ] I’ve documented my code using [JSDoc](https://jsdoc.app/) format
if applicable
- [ ] I’ve applied the right labels on the PR (see [labeling
guidelines](https://github.com/MetaMask/metamask-mobile/blob/main/.github/guidelines/LABELING_GUIDELINES.md)).
Not required for external contributors.

## **Pre-merge reviewer checklist**

- [ ] I've manually tested the PR (e.g. pull and build branch, run the
app, test code being changed).
- [ ] I confirm that this PR addresses all acceptance criteria described
in the ticket it closes and includes the necessary testing evidence such
as recordings and or screenshots.
  • Loading branch information
sahar-fehri authored Jun 6, 2024
1 parent e8816b8 commit 6a0ac51
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 39 deletions.
3 changes: 2 additions & 1 deletion app/reducers/collectibles/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import {
} from '../../selectors/nftController';
import { selectSelectedAddress } from '../../selectors/preferencesController';
import { compareTokenIds } from '../../util/tokens';
import { createDeepEqualSelector } from '../../selectors/util';

const favoritesSelector = (state) => state.collectibles.favorites;

Expand All @@ -17,7 +18,7 @@ export const collectibleContractsSelector = createSelector(
allNftContracts[address]?.[chainId] || [],
);

export const collectiblesSelector = createSelector(
export const collectiblesSelector = createDeepEqualSelector(
selectSelectedAddress,
selectChainId,
selectAllNfts,
Expand Down
56 changes: 18 additions & 38 deletions patches/@metamask+assets-controllers+26.0.0.patch
Original file line number Diff line number Diff line change
Expand Up @@ -282,15 +282,15 @@ index 45e07b8..096baf1 100644
private getCorrectChainId;
/**
diff --git a/node_modules/@metamask/assets-controllers/dist/NftController.js b/node_modules/@metamask/assets-controllers/dist/NftController.js
index 0873ccd..63fe60c 100644
index 0873ccd..9359c9a 100644
--- a/node_modules/@metamask/assets-controllers/dist/NftController.js
+++ b/node_modules/@metamask/assets-controllers/dist/NftController.js
@@ -12,7 +12,7 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
-exports.NftController = exports.getDefaultNftState = exports.OpenSeaV2ChainIds = void 0;
+exports.NftController = exports.RATE_LIMIT_NFT_INTERVAL = exports.getDefaultNftState = void 0;
+exports.NftController = exports.getDefaultNftState = void 0;
const address_1 = require("@ethersproject/address");
const base_controller_1 = require("@metamask/base-controller");
const controller_utils_1 = require("@metamask/controller-utils");
Expand All @@ -305,28 +305,18 @@ index 0873ccd..63fe60c 100644
const ALL_NFTS_STATE_KEY = 'allNfts';
const ALL_NFTS_CONTRACTS_STATE_KEY = 'allNftContracts';
/**
@@ -42,6 +38,12 @@ const getDefaultNftState = () => {
@@ -42,6 +38,10 @@ const getDefaultNftState = () => {
};
};
exports.getDefaultNftState = getDefaultNftState;
+const RATE_LIMIT_NFT_UPDATE_DELAY = 600; // 10 mins
+exports.RATE_LIMIT_NFT_INTERVAL = RATE_LIMIT_NFT_UPDATE_DELAY * 1000;
+/**
+ * Changes regarding displayNftMedia, TokenURI and error nft metadata property are not on the core repo and needed to be refactor to be removed from the patch
+ * updateNftMetadata changes will be introduced on latest versions of changes of assets controllers, v^30 or next
+ */
/**
* Controller that stores assets and exposes convenience methods
*/
@@ -69,6 +71,7 @@ class NftController extends base_controller_1.BaseControllerV1 {
constructor({ chainId: initialChainId, onPreferencesStateChange, onNetworkStateChange, getERC721AssetName, getERC721AssetSymbol, getERC721TokenURI, getERC721OwnerOf, getERC1155BalanceOf, getERC1155TokenURI, getNetworkClientById, onNftAdded, messenger, }, config, state) {
super(config, state);
this.mutex = new async_mutex_1.Mutex();
+ this.lastNftRefreshTime = 0;
/**
* EventEmitter instance used to listen to specific EIP747 events
*/
@@ -81,7 +84,7 @@ class NftController extends base_controller_1.BaseControllerV1 {
@@ -81,7 +81,7 @@ class NftController extends base_controller_1.BaseControllerV1 {
selectedAddress: '',
chainId: initialChainId,
ipfsGateway: controller_utils_1.IPFS_DEFAULT_GATEWAY_URL,
Expand All @@ -335,16 +325,12 @@ index 0873ccd..63fe60c 100644
useIPFSSubdomains: true,
isIpfsGatewayEnabled: true,
};
@@ -96,11 +99,15 @@ class NftController extends base_controller_1.BaseControllerV1 {
@@ -96,11 +96,11 @@ class NftController extends base_controller_1.BaseControllerV1 {
this.getNetworkClientById = getNetworkClientById;
this.onNftAdded = onNftAdded;
this.messagingSystem = messenger;
- onPreferencesStateChange(({ selectedAddress, ipfsGateway, openSeaEnabled, isIpfsGatewayEnabled, }) => {
+ onPreferencesStateChange(({ selectedAddress, ipfsGateway, displayNftMedia, isIpfsGatewayEnabled, }) => {
+ const { selectedAddress: previouslySelectedAddress } = this.config;
+ if (selectedAddress !== previouslySelectedAddress) {
+ this.lastNftRefreshTime = 0
+ }
this.configure({
selectedAddress,
ipfsGateway,
Expand All @@ -353,7 +339,7 @@ index 0873ccd..63fe60c 100644
isIpfsGatewayEnabled,
});
});
@@ -109,14 +116,8 @@ class NftController extends base_controller_1.BaseControllerV1 {
@@ -109,14 +109,8 @@ class NftController extends base_controller_1.BaseControllerV1 {
this.configure({ chainId });
});
}
Expand All @@ -370,7 +356,7 @@ index 0873ccd..63fe60c 100644
}
/**
* Helper method to update nested state for allNfts and allNftContracts.
@@ -137,38 +138,49 @@ class NftController extends base_controller_1.BaseControllerV1 {
@@ -137,38 +131,49 @@ class NftController extends base_controller_1.BaseControllerV1 {
});
}
/**
Expand Down Expand Up @@ -431,7 +417,7 @@ index 0873ccd..63fe60c 100644
return nftMetadata;
});
}
@@ -182,10 +194,20 @@ class NftController extends base_controller_1.BaseControllerV1 {
@@ -182,10 +187,20 @@ class NftController extends base_controller_1.BaseControllerV1 {
*/
getNftInformationFromTokenURI(contractAddress, tokenId, networkClientId) {
return __awaiter(this, void 0, void 0, function* () {
Expand All @@ -453,7 +439,7 @@ index 0873ccd..63fe60c 100644
const hasIpfsTokenURI = tokenURI.startsWith('ipfs://');
if (hasIpfsTokenURI && !isIpfsGatewayEnabled) {
return {
@@ -197,8 +219,7 @@ class NftController extends base_controller_1.BaseControllerV1 {
@@ -197,8 +212,7 @@ class NftController extends base_controller_1.BaseControllerV1 {
tokenURI: tokenURI !== null && tokenURI !== void 0 ? tokenURI : null,
};
}
Expand All @@ -463,15 +449,15 @@ index 0873ccd..63fe60c 100644
return {
image: null,
name: null,
@@ -234,6 +255,7 @@ class NftController extends base_controller_1.BaseControllerV1 {
@@ -234,6 +248,7 @@ class NftController extends base_controller_1.BaseControllerV1 {
standard: standard || null,
favorite: false,
tokenURI: tokenURI !== null && tokenURI !== void 0 ? tokenURI : null,
+ error: 'URI import error',
};
}
});
@@ -287,60 +309,29 @@ class NftController extends base_controller_1.BaseControllerV1 {
@@ -287,60 +302,29 @@ class NftController extends base_controller_1.BaseControllerV1 {
* @returns Promise resolving to the current NFT name and image.
*/
getNftInformation(contractAddress, tokenId, networkClientId) {
Expand Down Expand Up @@ -546,7 +532,7 @@ index 0873ccd..63fe60c 100644
});
}
/**
@@ -364,25 +355,20 @@ class NftController extends base_controller_1.BaseControllerV1 {
@@ -364,25 +348,20 @@ class NftController extends base_controller_1.BaseControllerV1 {
});
}
/**
Expand Down Expand Up @@ -580,15 +566,15 @@ index 0873ccd..63fe60c 100644
}
/* istanbul ignore next */
return {
@@ -449,6 +435,7 @@ class NftController extends base_controller_1.BaseControllerV1 {
@@ -449,6 +428,7 @@ class NftController extends base_controller_1.BaseControllerV1 {
tokenId: tokenId.toString(),
standard: nftMetadata.standard,
source,
+ tokenURI: nftMetadata.tokenURI,
});
}
return newNfts;
@@ -464,11 +451,12 @@ class NftController extends base_controller_1.BaseControllerV1 {
@@ -464,11 +444,12 @@ class NftController extends base_controller_1.BaseControllerV1 {
* @param options - options.
* @param options.tokenAddress - Hex address of the NFT contract.
* @param options.userAddress - The address of the account where the NFT is being added.
Expand All @@ -602,7 +588,7 @@ index 0873ccd..63fe60c 100644
var _a;
return __awaiter(this, void 0, void 0, function* () {
const releaseLock = yield this.mutex.acquire();
@@ -485,8 +473,9 @@ class NftController extends base_controller_1.BaseControllerV1 {
@@ -485,8 +466,9 @@ class NftController extends base_controller_1.BaseControllerV1 {
}
// this doesn't work currently for detection if the user switches networks while the detection is processing
// will be fixed once detection uses networkClientIds
Expand All @@ -614,7 +600,7 @@ index 0873ccd..63fe60c 100644
// If the nft is auto-detected we want some valid metadata to be present
if (source === constants_1.Source.Detected &&
'address' in contractInformation &&
@@ -501,8 +490,8 @@ class NftController extends base_controller_1.BaseControllerV1 {
@@ -501,8 +483,8 @@ class NftController extends base_controller_1.BaseControllerV1 {
return nftContracts;
}
/* istanbul ignore next */
Expand All @@ -625,7 +611,7 @@ index 0873ccd..63fe60c 100644
const newNftContracts = [...nftContracts, newEntry];
this.updateNestedNftState(newNftContracts, ALL_NFTS_CONTRACTS_STATE_KEY, {
chainId,
@@ -568,6 +557,61 @@ class NftController extends base_controller_1.BaseControllerV1 {
@@ -568,6 +550,55 @@ class NftController extends base_controller_1.BaseControllerV1 {
chainId,
});
}
Expand All @@ -642,11 +628,6 @@ index 0873ccd..63fe60c 100644
+ return __awaiter(this, void 0, void 0, function* () {
+ const releaseLock = yield this.mutex.acquire();
+ try {
+ const time = Date.now();
+ const timeSinceLastNftUpdateRequest = time - this.lastNftRefreshTime;
+ if (timeSinceLastNftUpdateRequest < exports.RATE_LIMIT_NFT_INTERVAL) {
+ return;
+ }
+ const chainId = this.getCorrectChainId({ networkClientId });
+ const nftsWithChecksumAdr = nfts.map((nft) => {
+ return Object.assign(Object.assign({}, nft), { address: (0, controller_utils_1.toChecksumHexAddress)(nft.address) });
Expand All @@ -658,7 +639,6 @@ index 0873ccd..63fe60c 100644
+ newMetadata: resMetadata,
+ };
+ })));
+ this.lastNftRefreshTime = Date.now();
+ const successfulNewFetchedNfts = nftMetadataResults.filter((result) => result.status === 'fulfilled');
+ // We want to avoid updating the state if the state and fetched nft info are the same
+ const nftsWithDifferentMetadata = [];
Expand Down Expand Up @@ -687,7 +667,7 @@ index 0873ccd..63fe60c 100644
/**
* Removes an NFT contract to the stored NFT contracts list.
*
@@ -763,15 +807,16 @@ class NftController extends base_controller_1.BaseControllerV1 {
@@ -763,15 +794,16 @@ class NftController extends base_controller_1.BaseControllerV1 {
return __awaiter(this, void 0, void 0, function* () {
tokenAddress = (0, controller_utils_1.toChecksumHexAddress)(tokenAddress);
const chainId = this.getCorrectChainId({ networkClientId });
Expand Down

0 comments on commit 6a0ac51

Please sign in to comment.