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: remove nft detection polling #4281

Merged
merged 15 commits into from
Jun 12, 2024
Merged

Conversation

sahar-fehri
Copy link
Contributor

@sahar-fehri sahar-fehri commented May 15, 2024

Explanation

This PR removes polling logic from NftDetectionController.
Calling detectNfts function will be tied to UI components instead of preference change events.
NftDetectionController will now extend BaseController instead of StaticIntervalPollingController.

The detectNfts logic has also changed to call the NFT-API and save the nfts in state as soon as they are available instead of waiting to fetch all the user NFTs before saving to state.

References

Changelog

@metamask/assets-controllers

  • BREAKING: Changed NftDetectionController to extend BaseController.
  • REMOVED: Removed interval from contructor in NftDetectionController.

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 requested a review from a team as a code owner May 15, 2024 18:36
@sahar-fehri sahar-fehri marked this pull request as draft May 15, 2024 18:36
@sahar-fehri sahar-fehri force-pushed the feat/remove-nft-detection-polling branch from fc291e4 to e4d1afd Compare May 30, 2024 14:33
@sahar-fehri sahar-fehri force-pushed the feat/remove-nft-detection-polling branch from e4d1afd to c58add7 Compare June 3, 2024 08:36
@sahar-fehri sahar-fehri marked this pull request as ready for review June 6, 2024 20:06
*/
export class NftDetectionController extends StaticIntervalPollingController<
export class NftDetectionController extends BaseController<
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@sahar-fehri what do you think about making this a service instead as it's stateless ?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Resolving this now because we discussed that we can iterate in the future to extract api interactions into a separate service.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For future reference, I believe modeling stateless services as non-controllers is a separate concern from extracting external API integrations.

We probably want to create a ticket for the first task if it's out of scope for this PR.

bergeron
bergeron previously approved these changes Jun 11, 2024
@sahar-fehri sahar-fehri requested a review from a team June 11, 2024 17:02
Copy link
Contributor

@MajorLift MajorLift left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM!

@sahar-fehri sahar-fehri merged commit 2763df6 into main Jun 12, 2024
113 checks passed
@sahar-fehri sahar-fehri deleted the feat/remove-nft-detection-polling branch June 12, 2024 16:40
Comment on lines +561 to +562
// eslint-disable-next-line @typescript-eslint/restrict-template-expressions
const updateKey: `${Hex}:${string}` = `${chainId}:${userAddress}`;
Copy link
Contributor

@MajorLift MajorLift Jun 12, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@sahar-fehri Edit: This is for my reference, don't worry about it!

This eslint-disable directive should be removable with the next @metamask/keyring-api release.

  • Currently, userAddress is resolving to any.
  • This is because the AccountsController:getSelectedAccount action returns InternalAccount,
  • which is defined using superstruct instead of @metamask/superstruct in current the @metamask/keyring-api version.
  • Fix ESM/CJS compatibility superstruct#1

Similar issue: MetaMask/eth-snap-keyring#323 (comment)

mcmire pushed a commit that referenced this pull request Jun 12, 2024
## Explanation

This PR removes polling logic from NftDetectionController.
Calling detectNfts function will be tied to UI components instead of
preference change events.
NftDetectionController will now extend `BaseController` instead of
`StaticIntervalPollingController`.

The detectNfts logic has also changed to call the NFT-API and save the
nfts in state as soon as they are available instead of waiting to fetch
all the user NFTs before saving to state.


## References


* Related to
[67890](MetaMask/metamask-extension#24547)


## 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`

- **BREAKING**: Changed NftDetectionController to extend
`BaseController`.
- **REMOVED**: Removed `interval` from contructor in
`NftDetectionController`.


## 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 added a commit to MetaMask/metamask-extension that referenced this pull request Jun 19, 2024
## **Description**

This PR aims to:
1- Enable nft autodetection by default
2- Show a modal only once if the user disables the nft autodetection
3- Make NFT detection tied to the components that use the NFTs instead
of the 3mins polling strategy.

This PR goes with this core PR:
MetaMask/core#4281

[![Open in GitHub
Codespaces](https://github.com/codespaces/badge.svg)](https://codespaces.new/MetaMask/metamask-extension/pull/24547?quickstart=1)

## **Related issues**

Related to: MetaMask/core#4281

## **Manual testing steps**

**Test the nft auto detection modal:**

1. Do a fresh import of the wallet
2. Switch to mainnet.
3. Go to settings => Security and privacy and make sure the toggle
enable NFT auto detection is ON
4. Turn the nft auto detection toggle OFF
5. Go back to home page and you should see a new modal
6. Clicking on `not right now `button should close the modal and closing
on `allow` button should enable the nft auto detection modal.
This modal should be seen only once.

**Test the removal of the polling in the backgound:**

We have the toggle enabled by default now but this should not trigger
calls to NFT-API every 3 mins anymore.
Instead the calls should be triggered only when you click on the NFT
tab.

1. Open the background console and click on Networks tab.
2. Filter by /tokens (so you are able to see only the calls that will
fetch user nfts)
3. Notice that as long as you did not click on the NFT tab, you should
not be able to see any calls made in the backround.
(where the old logic should keep detecting your NFTs every 3 mins as
long as you have MM open)
5. Click on the NFT tab and you should be able to see the requests in
the background to fetch your nfts.
7. You can also click on Send, and click on asset picker and click on
NFT tab, you should be able to see your NFTs there too.
8. Calls should be made only when you click on the NFT tab.

**Test new notice banner behavior:**

Users should see the NFT Notice banner as long as they are on mainnet +
they have NFT detection OFF.
Regardless of whether they have NFTs in the state or not.
Clicking on the"Enable NFT autodetection" should remove the notice
banner and enable the NFT detection without redirecting the user to
settings.


## **Screenshots/Recordings**

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

### **Before**

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


https://github.com/MetaMask/metamask-extension/assets/10994169/0815ac0c-a60c-400a-9ffe-92451dfc3ded

### **After**

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


https://github.com/MetaMask/metamask-extension/assets/10994169/0b00f1c1-baf1-4205-bca2-991d11e39a2f

Notice banner with toast:


https://github.com/MetaMask/metamask-extension/assets/10994169/c1aaa168-a29f-4b38-b0c7-74aaa1945ba0



## **Pre-merge author checklist**

- [ ] I’ve followed [MetaMask Coding
Standards](https://github.com/MetaMask/metamask-extension/blob/develop/.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-extension/blob/develop/.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.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
Archived in project
Development

Successfully merging this pull request may close these issues.

None yet

4 participants