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.
sahar-fehri added a commit to MetaMask/metamask-mobile that referenced this pull request Jun 26, 2024
)

## **Description**
This PR removes any code that triggers polling on
NftDetectionController.

Calling `detectNfts()` function is now only triggered when the user
clicks on the NFT tab.

It also Enables NFTDetection by default for new users.

>[!NOTE]
> this PR has an asset-cpntroller patch from this [core PR
#4281](MetaMask/core#4281)
> This patch keeps NFTDetection controller extending the polling
controller while the PR makes it extend BaseController but it makes sure
the polling is not triggered anymore in the client code.
> It will be fully updated in newer versions of assets-controllers
> I removed what triggers the `start()` of the polling, and now instead,
mobile calls `detectNfts()` directly.
> Then i made updates on the fct `getOwnerNfts` so that it only fetches
information for a specific cursor instead of looping through all user
nfts.
> The loop is now being done inside the `detectNfts()` fct, and we
basically fetch the first page of NFTs and save it to state so its
available to clients to view.

**New functionalities highlight:**

_1- Enable NFT autodetection by default to new users.
2- Old users who have the NFT autodetection off will see the new NFT
detection modal to encourage them to enable the modal.
3- When users click on the banner notice, they are no longer directed to
settings and instead we enable the NFT detection after they click on
“enable nft autodetection” and we show the Toast. We wanted to reduce
friction and we removed that redirection to settings.
4- The code should not do any of the 3mins polling in the background
anymore._

## **Related issues**

Related: MetaMask/core#4281

## **Manual testing steps**

1. Import MM application
2. Go to settings => Security and privacy and you should see that the
NFT detection is enabled.
3. Go back to home page and click on NFT tab; you should be able to see
your NFTs being added.
4. Switch to another account that has NFTs
5. Click on NFTs tab and you should also see your NFTs being added to
sate.
6. Go to settings => Security and privacy and turn OFF the NFT detection
toggle.
7. You should see a new modal. Clicking on "allow" button should
re-enable the NFT detection toggle.

## **Screenshots/Recordings**

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

### **Before**



https://github.com/MetaMask/metamask-mobile/assets/10994169/83e86852-3455-4b7c-bfc6-a658dc20c1b0



### **After**

Testing a fresh wallet import:


https://github.com/MetaMask/metamask-mobile/assets/10994169/331668b1-3a27-493f-a648-3568e4ba67c2

Testing an upgrade scenario:

First build on main, where you should see the NFT auto detection
disabled by default if the user did not enable it

Then build on this PR:
You should see the new NFT detection modal and clicking on `allow`
button should enable the NFT detection in the settings.


https://github.com/MetaMask/metamask-mobile/assets/10994169/6bcb77f5-13ef-429e-a1f5-4d907145ee30

NFT detection banner with new Toast:



https://github.com/MetaMask/metamask-mobile/assets/10994169/2f7465c0-4c42-409b-bfe4-ed70d9e1ca2c



## **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.

---------

Co-authored-by: MetaMask Bot <37885440+metamaskbot@users.noreply.github.com>
Co-authored-by: metamaskbot <metamaskbot@users.noreply.github.com>
Co-authored-by: sethkfman <seth.kaufman@consensys.net>
Co-authored-by: Nico MASSART <NicolasMassart@users.noreply.github.com>
Jonathansoufer pushed a commit to MetaMask/metamask-mobile that referenced this pull request Jun 27, 2024
)

This PR removes any code that triggers polling on
NftDetectionController.

Calling `detectNfts()` function is now only triggered when the user
clicks on the NFT tab.

It also Enables NFTDetection by default for new users.

>[!NOTE]
> this PR has an asset-cpntroller patch from this [core PR
> This patch keeps NFTDetection controller extending the polling
controller while the PR makes it extend BaseController but it makes sure
the polling is not triggered anymore in the client code.
> It will be fully updated in newer versions of assets-controllers
> I removed what triggers the `start()` of the polling, and now instead,
mobile calls `detectNfts()` directly.
> Then i made updates on the fct `getOwnerNfts` so that it only fetches
information for a specific cursor instead of looping through all user
nfts.
> The loop is now being done inside the `detectNfts()` fct, and we
basically fetch the first page of NFTs and save it to state so its
available to clients to view.

**New functionalities highlight:**

_1- Enable NFT autodetection by default to new users.
2- Old users who have the NFT autodetection off will see the new NFT
detection modal to encourage them to enable the modal.
3- When users click on the banner notice, they are no longer directed to
settings and instead we enable the NFT detection after they click on
“enable nft autodetection” and we show the Toast. We wanted to reduce
friction and we removed that redirection to settings.
4- The code should not do any of the 3mins polling in the background
anymore._

Related: MetaMask/core#4281

1. Import MM application
2. Go to settings => Security and privacy and you should see that the
NFT detection is enabled.
3. Go back to home page and click on NFT tab; you should be able to see
your NFTs being added.
4. Switch to another account that has NFTs
5. Click on NFTs tab and you should also see your NFTs being added to
sate.
6. Go to settings => Security and privacy and turn OFF the NFT detection
toggle.
7. You should see a new modal. Clicking on "allow" button should
re-enable the NFT detection toggle.

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

https://github.com/MetaMask/metamask-mobile/assets/10994169/83e86852-3455-4b7c-bfc6-a658dc20c1b0

Testing a fresh wallet import:

https://github.com/MetaMask/metamask-mobile/assets/10994169/331668b1-3a27-493f-a648-3568e4ba67c2

Testing an upgrade scenario:

First build on main, where you should see the NFT auto detection
disabled by default if the user did not enable it

Then build on this PR:
You should see the new NFT detection modal and clicking on `allow`
button should enable the NFT detection in the settings.

https://github.com/MetaMask/metamask-mobile/assets/10994169/6bcb77f5-13ef-429e-a1f5-4d907145ee30

NFT detection banner with new Toast:

https://github.com/MetaMask/metamask-mobile/assets/10994169/2f7465c0-4c42-409b-bfe4-ed70d9e1ca2c

- [ ] 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.

- [ ] 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.

---------

Co-authored-by: MetaMask Bot <37885440+metamaskbot@users.noreply.github.com>
Co-authored-by: metamaskbot <metamaskbot@users.noreply.github.com>
Co-authored-by: sethkfman <seth.kaufman@consensys.net>
Co-authored-by: Nico MASSART <NicolasMassart@users.noreply.github.com>
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.

4 participants