Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: fetch nfts on components #24547

Merged
merged 42 commits into from
Jun 19, 2024
Merged
Show file tree
Hide file tree
Changes from 40 commits
Commits
Show all changes
42 commits
Select commit Hold shift + click to select a range
e5646f7
feat: fetch nfts on components
sahar-fehri May 15, 2024
7c51be8
fix: rebase and refactor
sahar-fehri May 30, 2024
3392b08
fix: cleanup
sahar-fehri May 30, 2024
f11b98b
fix: Add NFT autodetection modal
sahar-fehri May 31, 2024
246107c
fix: lint
sahar-fehri May 31, 2024
b88ea91
fix: rm unused locales
sahar-fehri May 31, 2024
da18690
fix: fix import and rebase
sahar-fehri Jun 3, 2024
f095bf9
Merge branch 'develop' into feat/refactor-nft-experience
sahar-fehri Jun 5, 2024
a2d3e7c
fix: patch core changes
sahar-fehri Jun 6, 2024
030101a
fix: merge conflict
sahar-fehri Jun 6, 2024
45e2021
fix: fix unit test
sahar-fehri Jun 6, 2024
a21f81b
fix: fix unit test
sahar-fehri Jun 6, 2024
8ffb3a4
fix: fix unit test
sahar-fehri Jun 6, 2024
325161a
fix: fix tests
sahar-fehri Jun 6, 2024
c013257
Merge branch 'develop' into feat/refactor-nft-experience
sahar-fehri Jun 6, 2024
baa1103
fix: fix yarn
sahar-fehri Jun 6, 2024
09d2012
fix: fix unit test
sahar-fehri Jun 6, 2024
640b964
fix: fix mmi test
sahar-fehri Jun 6, 2024
0383c5a
fix: fix code fencing import
sahar-fehri Jun 6, 2024
242f332
fix: update patch
sahar-fehri Jun 7, 2024
19bef64
Revert "fix: update patch"
sahar-fehri Jun 7, 2024
4b4f212
Merge branch 'develop' into feat/refactor-nft-experience
sahar-fehri Jun 7, 2024
2093301
fix: update patch for assets-controllers
sahar-fehri Jun 7, 2024
39a7557
fix: refactor
sahar-fehri Jun 7, 2024
18c2006
fix: fix conflicts
sahar-fehri Jun 11, 2024
d7af6a3
fix: patch new core changes and refactor detectNfts
sahar-fehri Jun 11, 2024
fb4c01b
fix: fix
sahar-fehri Jun 11, 2024
44195a3
fix: show nft detection notice even when user has nfts
sahar-fehri Jun 11, 2024
a197301
fix: enable nft detection from notice banner without going to settings
sahar-fehri Jun 12, 2024
4890b91
fix: fix
sahar-fehri Jun 12, 2024
5fd89d7
fix: fix
sahar-fehri Jun 13, 2024
19cc94a
fix: lint
sahar-fehri Jun 13, 2024
8c85de7
fix: fix spinner
sahar-fehri Jun 13, 2024
92c8d4d
fix: add new toast on nft banner click
sahar-fehri Jun 13, 2024
0c2a14d
fix: enable display media onSubmit in nft modal
sahar-fehri Jun 13, 2024
b74e253
fix: fix snapshot
sahar-fehri Jun 13, 2024
875286b
fix: fix import
sahar-fehri Jun 13, 2024
52c827f
fix: code fencing
sahar-fehri Jun 13, 2024
002f88d
fix: lint
sahar-fehri Jun 13, 2024
e45ebc8
fix: merge conflicts
sahar-fehri Jun 13, 2024
7760571
fix: show nftautodetection modal only on mainnet
sahar-fehri Jun 14, 2024
666362f
fix: enable display nft media from nft banner notice
sahar-fehri Jun 14, 2024
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
251 changes: 251 additions & 0 deletions .yarn/patches/@metamask-assets-controllers-patch-a3b39b55a6.patch
Original file line number Diff line number Diff line change
@@ -0,0 +1,251 @@
diff --git a/dist/chunk-FMZML3V5.js b/dist/chunk-FMZML3V5.js
sahar-fehri marked this conversation as resolved.
Show resolved Hide resolved
index ee6155cd938366918de155e8867c7d359b8ea826..b4dfe838c463a561b0e91532bcb674806fdc52bd 100644
--- a/dist/chunk-FMZML3V5.js
+++ b/dist/chunk-FMZML3V5.js
@@ -5,6 +5,7 @@


var _controllerutils = require('@metamask/controller-utils');
+var _utils = require('@metamask/utils');
var _pollingcontroller = require('@metamask/polling-controller');
var DEFAULT_INTERVAL = 18e4;
var BlockaidResultType = /* @__PURE__ */ ((BlockaidResultType2) => {
@@ -14,6 +15,8 @@ var BlockaidResultType = /* @__PURE__ */ ((BlockaidResultType2) => {
BlockaidResultType2["Malicious"] = "Malicious";
return BlockaidResultType2;
})(BlockaidResultType || {});
+const supportedNftDetectionNetworks= [_controllerutils.ChainId.mainnet];
+var inProcessNftFetchingUpdates;
var NftDetectionController = class extends _pollingcontroller.StaticIntervalPollingControllerV1 {
/**
* Creates an NftDetectionController instance.
@@ -50,6 +53,7 @@ var NftDetectionController = class extends _pollingcontroller.StaticIntervalPoll
* Name of this controller used during composition
*/
this.name = "NftDetectionController";
+ this.inProcessNftFetchingUpdates= {};
/**
* Checks whether network is mainnet or not.
*
@@ -72,11 +76,6 @@ var NftDetectionController = class extends _pollingcontroller.StaticIntervalPoll
const { selectedAddress: previouslySelectedAddress, disabled } = this.config;
if (selectedAddress !== previouslySelectedAddress || !useNftDetection !== disabled) {
this.configure({ selectedAddress, disabled: !useNftDetection });
- if (useNftDetection) {
- this.start();
- } else {
- this.stop();
- }
}
});
onNetworkStateChange(({ selectedNetworkClientId }) => {
@@ -92,34 +91,33 @@ var NftDetectionController = class extends _pollingcontroller.StaticIntervalPoll
this.setIntervalLength(this.config.interval);
}
getOwnerNftApi({
+ chainId,
address,
next
}) {
- return `${_controllerutils.NFT_API_BASE_URL}/users/${address}/tokens?chainIds=1&limit=50&includeTopBid=true&continuation=${next ?? ""}`;
+ return `${_controllerutils.NFT_API_BASE_URL}/users/${address}/tokens?chainIds=${chainId}&limit=50&includeTopBid=true&continuation=${next ?? ""}`;
}
- async getOwnerNfts(address) {
- let nftApiResponse;
- let nfts = [];
- let next;
- do {
- nftApiResponse = await _controllerutils.fetchWithErrorHandling.call(void 0, {
- url: this.getOwnerNftApi({ address, next }),
- options: {
- headers: {
- Version: "1"
- }
+ async getOwnerNfts(
+ address,
+ chainId,
+ cursor,
+ ) {
+ // Convert hex chainId to number
+ const convertedChainId = (0, _controllerutils.convertHexToDecimal)(chainId).toString();
+ const url = this.getOwnerNftApi({
+ chainId: convertedChainId,
+ address,
+ next: cursor,
+ });
+
+ const nftApiResponse = await _controllerutils.handleFetch.call(void 0, url,
+ {
+ headers: {
+ Version: "1"
},
- timeout: 15e3
- });
- if (!nftApiResponse) {
- return nfts;
}
- const newNfts = nftApiResponse.tokens.filter(
- (elm) => elm.token.isSpam === false && (elm.blockaidResult?.result_type ? elm.blockaidResult?.result_type === "Benign" /* Benign */ : true)
- );
- nfts = [...nfts, ...newNfts];
- } while (next = nftApiResponse.continuation);
- return nfts;
+ );
+ return nftApiResponse;
}
async _executePoll(networkClientId, options) {
await this.detectNfts({ networkClientId, userAddress: options.address });
@@ -169,62 +167,103 @@ var NftDetectionController = class extends _pollingcontroller.StaticIntervalPoll
networkClientId,
userAddress
} = { userAddress: this.config.selectedAddress }) {
- if (!this.isMainnet() || this.disabled) {
+ const { chainId } = this.config;
+ if (!supportedNftDetectionNetworks.includes(chainId) || this.disabled) {
return;
}
if (!userAddress) {
return;
}
- const apiNfts = await this.getOwnerNfts(userAddress);
- const addNftPromises = apiNfts.map(async (nft) => {
- const {
- tokenId: token_id,
- contract,
- kind,
- image: image_url,
- imageSmall: image_thumbnail_url,
- metadata: { imageOriginal: image_original_url } = {},
- name,
- description,
- attributes,
- topBid,
- lastSale,
- rarityRank,
- rarityScore,
- collection
- } = nft.token;
- let ignored;
- const { ignoredNfts } = this.getNftState();
- if (ignoredNfts.length) {
- ignored = ignoredNfts.find((c) => {
- return c.address === _controllerutils.toChecksumHexAddress.call(void 0, contract) && c.tokenId === token_id;
- });
- }
- if (!ignored) {
- const nftMetadata = Object.assign(
- {},
- { name },
- description && { description },
- image_url && { image: image_url },
- image_thumbnail_url && { imageThumbnail: image_thumbnail_url },
- image_original_url && { imageOriginal: image_original_url },
- kind && { standard: kind.toUpperCase() },
- lastSale && { lastSale },
- attributes && { attributes },
- topBid && { topBid },
- rarityRank && { rarityRank },
- rarityScore && { rarityScore },
- collection && { collection }
- );
- await this.addNft(contract, token_id, {
- nftMetadata,
- userAddress,
- source: "detected" /* Detected */,
- networkClientId
+
+ const updateKey = `${chainId}:${userAddress}`;
+ if (updateKey in this.inProcessNftFetchingUpdates) {
+ // This prevents redundant updates
+ // This promise is resolved after the in-progress update has finished,
+ // and state has been updated.
+ await this.inProcessNftFetchingUpdates[updateKey];
+ return;
+ }
+ const {
+ promise: inProgressUpdate,
+ resolve: updateSucceeded,
+ reject: updateFailed
+ } = _utils.createDeferredPromise.call(void 0, { suppressUnhandledRejection: true });
+ this.inProcessNftFetchingUpdates[updateKey] = inProgressUpdate;
+
+ let next;
+ let apiNfts= [];
+ let resultNftApi;
+
+ try{
+ do {
+ resultNftApi = await this.getOwnerNfts(userAddress, chainId, next)
+ apiNfts = resultNftApi.tokens.filter(
+ (elm) =>
+ elm.token.isSpam === false &&
+ (elm.blockaidResult?.result_type
+ ? elm.blockaidResult?.result_type === BlockaidResultType.Benign
+ : true),
+ );
+ const addNftPromises = apiNfts.map(async (nft) => {
+ const {
+ tokenId: token_id,
+ contract,
+ kind,
+ image: image_url,
+ imageSmall: image_thumbnail_url,
+ metadata: { imageOriginal: image_original_url } = {},
+ name,
+ description,
+ attributes,
+ topBid,
+ lastSale,
+ rarityRank,
+ rarityScore,
+ collection,
+ } = nft.token;
+
+ let ignored;
+ /* istanbul ignore else */
+ const { ignoredNfts } = this.getNftState();
+ if (ignoredNfts.length) {
+ ignored = ignoredNfts.find((c) => {
+ return c.address === _controllerutils.toChecksumHexAddress.call(void 0, contract) && c.tokenId === token_id;
+ });
+ }
+ /* istanbul ignore else */
+ if (!ignored) {
+ const nftMetadata = Object.assign(
+ {},
+ { name },
+ description && { description },
+ image_url && { image: image_url },
+ image_thumbnail_url && { imageThumbnail: image_thumbnail_url },
+ image_original_url && { imageOriginal: image_original_url },
+ kind && { standard: kind.toUpperCase() },
+ lastSale && { lastSale },
+ attributes && { attributes },
+ topBid && { topBid },
+ rarityRank && { rarityRank },
+ rarityScore && { rarityScore },
+ collection && { collection }
+ );
+ await this.addNft(contract, token_id, {
+ nftMetadata,
+ userAddress,
+ source: "detected" /* Detected */,
+ networkClientId
+ });
+ }
});
- }
- });
- await Promise.all(addNftPromises);
+ await Promise.all(addNftPromises);
+ } while ((next = resultNftApi.continuation));
+ updateSucceeded();
+ } catch (error){
+ updateFailed(error);
+ throw error;
+ } finally {
+ delete this.inProcessNftFetchingUpdates[updateKey];
+ }
}
};
var NftDetectionController_default = NftDetectionController;
3 changes: 0 additions & 3 deletions app/_locales/de/messages.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 0 additions & 3 deletions app/_locales/el/messages.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

20 changes: 16 additions & 4 deletions app/_locales/en/messages.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 0 additions & 3 deletions app/_locales/es/messages.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 0 additions & 3 deletions app/_locales/fr/messages.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 0 additions & 3 deletions app/_locales/hi/messages.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 0 additions & 3 deletions app/_locales/id/messages.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 0 additions & 3 deletions app/_locales/ja/messages.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading