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

fix: disable polling when MM closed #24162

Merged

Conversation

sahar-fehri
Copy link
Contributor

Description

Once nftDetection is enabled by the user, the nftDetection controller will start polling data in the background every 3 min even if the user closes the metamask popup or the tab.

We want to stop unnecessary polling when MM is closed in an attempt to reduce traffic.

Open in GitHub Codespaces

Related issues

Related to: MetaMask/core#4178

Manual testing steps

  1. Go to this page...

Screenshots/Recordings

Before

After

Pre-merge author checklist

  • I’ve followed MetaMask Coding Standards.
  • 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 format if applicable
  • I’ve applied the right labels on the PR (see labeling guidelines). 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.

@sahar-fehri sahar-fehri requested a review from a team as a code owner April 22, 2024 11:09
Copy link
Contributor

CLA Signature Action: All authors have signed the CLA. You may need to manually re-run the blocking PR check if it doesn't pass in a few minutes.

@sahar-fehri sahar-fehri added the needs-assets-ux-review A shared label between the Assets and UX team to flag PRs ready for consolidated team review. label Apr 22, 2024
@darkwing
Copy link
Contributor

E2E failures look legit

@metamaskbot
Copy link
Collaborator

Builds ready [78f47bc]
Page Load Metrics (1198 ± 600 ms)
PlatformPageMetricMin (ms)Max (ms)Average (ms)StandardDeviation (ms)MarginOfError (ms)
ChromeHomefirstPaint632291234622
domContentLoaded11100282010
load51291111981249600
domInteractive11100282010
Bundle size diffs [🚨 Warning! Bundle size has increased!]
  • background: 386 Bytes (0.01%)
  • ui: 0 Bytes (0.00%)
  • common: 27 Bytes (0.00%)

@gauthierpetetin gauthierpetetin added the needs-dev-review PR needs reviews from other engineers (in order to receive required approvals) label Apr 23, 2024
salimtb
salimtb previously approved these changes Apr 23, 2024
NidhiKJha
NidhiKJha previously approved these changes Apr 23, 2024
@sahar-fehri sahar-fehri dismissed stale reviews from NidhiKJha and salimtb via 172dffc April 23, 2024 12:58
Copy link

codecov bot commented Apr 23, 2024

Codecov Report

Attention: Patch coverage is 66.66667% with 3 lines in your changes are missing coverage. Please review.

Project coverage is 67.44%. Comparing base (b3c8d32) to head (f49cd66).
Report is 18 commits behind head on develop.

Files Patch % Lines
app/scripts/metamask-controller.js 66.67% 3 Missing ⚠️
Additional details and impacted files
@@             Coverage Diff             @@
##           develop   #24162      +/-   ##
===========================================
- Coverage    67.47%   67.44%   -0.02%     
===========================================
  Files         1257     1257              
  Lines        49232    49242      +10     
  Branches     12822    12816       -6     
===========================================
- Hits         33216    33211       -5     
- Misses       16016    16031      +15     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@metamaskbot
Copy link
Collaborator

Builds ready [f49cd66]
Page Load Metrics (858 ± 582 ms)
PlatformPageMetricMin (ms)Max (ms)Average (ms)StandardDeviation (ms)MarginOfError (ms)
ChromeHomefirstPaint592871125325
domContentLoaded978282010
load4732808581213582
domInteractive978282010
Bundle size diffs [🚨 Warning! Bundle size has increased!]
  • background: 311 Bytes (0.01%)
  • ui: 0 Bytes (0.00%)
  • common: 27 Bytes (0.00%)

sahar-fehri added a commit to MetaMask/core that referenced this pull request Apr 23, 2024
## Explanation

This PR goes together with this extension PR
MetaMask/metamask-extension#24162

We want to reduce third party calls for nft detection when a user has
closed the MM popup/window.
To do that we are leveraging `stopNetworkRequests` and
`triggerNetworkrequests` functions inside extension to either `stop()`
or `start()` the nft detection.
While doing this fixed the following scenario:

1- User imports MM wallet
2- User enables nft detection toggle
3- User closes MM tab or popup
4- We are no longuer making calls to third party because we called
stop() function on nftDetectionController.

It does not fix this scenario:
1- User imports MM wallet
2- User enabled nft detection toggle
3- User closes chrome all together
4- User reopens chrome without opening MM, and we notice that it is
making calls in the background to third party even though MM is not
open.

This behavior was happening because of a couple of reasons; the property
`disabled` being set to `true` in `defaultConfig`
```
    this.defaultConfig = {
      interval: DEFAULT_INTERVAL,
      chainId: initialChainId,
      selectedAddress: '',
      disabled: true,
    };
```
made the check on this line pass
https://github.com/MetaMask/core/blob/15518442513fbeaade83e4993851acdc1f038e4a/packages/assets-controllers/src/NftDetectionController.ts#L480
and triggered the `start()` function (because the useNftDetection is
true).

I fixed this by having extension pass the `disabled` in the contructor
rather than having the `true` value hardcoded in the controller.

I have also refactored the `onPreferencesStateChange` to have it make
this check
```
      if(selectedAddress !== previouslySelectedAddress){
          this.configure({ selectedAddress });
      }
```
Separately.
When the user first opens the selectedAddress is '' because its also set
in default config which, because of the `OR` statement triggered the
code to also go into the `start()`

## References

<!--
Are there any issues that this pull request is tied to? Are there other
links that reviewers should consult to understand these changes better?

For example:

* Related to
[#67890](https://github.com/MetaMask/metamask-extension/pull/24026/files#diff-6fbff2cfe97ac01b77296ef2122c7e0a5b3ff6a84b584b4d1a87482f35eea3d6)
-->

## Changelog

<!--
If you're making any consumer-facing changes, list those changes here as
if you were updating a changelog, using the template below as a guide.

(CATEGORY is one of BREAKING, ADDED, CHANGED, DEPRECATED, REMOVED, or
FIXED. For security-related issues, follow the Security Advisory
process.)

Please take care to name the exact pieces of the API you've added or
changed (e.g. types, interfaces, functions, or methods).

If there are any breaking changes, make sure to offer a solution for
consumers to follow once they upgrade to the changes.

Finally, if you're only making changes to development scripts or tests,
you may replace the template below with "None".
-->

### `@metamask/assets-controllers`

- **ADDED**: Added disabled inside contructor of nftDetectionController
- **CHANGED**: Pass new disabled value instead of true in the default
config
- **CHANGED**: Made the check for the selected address separate from the
check of useNftDetection value


## Checklist

- [ ] I've updated the test suite for new or updated code as appropriate
- [ ] I've updated documentation (JSDoc, Markdown, etc.) for new or
updated code as appropriate
- [ ] I've highlighted breaking changes using the "BREAKING" category
above as appropriate
@sahar-fehri sahar-fehri merged commit 2638466 into develop Apr 23, 2024
68 checks passed
@sahar-fehri sahar-fehri deleted the fix/stop-polling-nft-controller-if-client-closed-v2 branch April 23, 2024 20:19
@github-actions github-actions bot removed the needs-dev-review PR needs reviews from other engineers (in order to receive required approvals) label Apr 23, 2024
@github-actions github-actions bot locked and limited conversation to collaborators Apr 23, 2024
@metamaskbot metamaskbot added the release-11.16.0 Issue or pull request that will be included in release 11.16.0 label Apr 23, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
needs-assets-ux-review A shared label between the Assets and UX team to flag PRs ready for consolidated team review. release-11.16.0 Issue or pull request that will be included in release 11.16.0 team-assets
Projects
Archived in project
Development

Successfully merging this pull request may close these issues.

None yet

7 participants