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: update state only when metadata changed after fetch #4143

Merged
merged 3 commits into from
May 7, 2024

Conversation

sahar-fehri
Copy link
Contributor

@sahar-fehri sahar-fehri commented Apr 11, 2024

Explanation

Updating the updateNftMetadata function in NftController, to update the state only when new fetched metadata is different from the metadata in the state.

On Mobile when Nfts tab is rendered, we are calling addNft function on all user nfts.
We also do that when a user refreshed the page (by polling down the tab).
We want to refactor the component and use the updateNftMetadata instead of addNft.

Calling the addNft everytime when the component is rendered resulted in this bug on mobile MetaMask/metamask-mobile#9196

This one was tricky to reproduce, you will have to:
1- Import an account that has multiple NFTs
2- Create a new account (click on "add new account" button on mobile)
3- Switch back and forth between these two accounts fast and at some point you will see nfts from the account that has nfts appear on the account that did not have any nfts.

Still not quite sure what caused this bug now, but i see this PR
that has been merged and is part of v7.20.0 release which updates the "switch account" behavior.

I think what happened is when you are switching to the account that has nfts, we called addNft function on all the collectibles of accountA and the addIndividualNft fct is executed. Then when you switched to accountB (which does not have any nfts) the selectedAddress has switched in the nftController but we were still trying to add an NFT to state. So we came here where existingEntry was undefined because accountB does not have nfts

const existingEntry: Nft | undefined = nfts.find(
with selectedAddress = accountB and with an nft that does not belong to accountB and it then executes the update
this.updateNestedNftState(newNfts, ALL_NFTS_STATE_KEY, {
of accountB state.

Refactoring this component on mobile by removing unecessary useEffects (I think that we had those useEffects in the first place to fix nft refresh issues, but core already made updates where its triggering nft refresh when preferenceController state changes) this will allow removing this useEffect and this one.
The last useEffect which is triggering addNft, will instead be calling updateNftMetadata function and will only update the state if necessary.
If a user pulls down the page or goes back and forth between accounts, we might fetch new nft data but we wont be updating the state.

References

Changelog

@metamask/package-a

  • FIXED: Instead of updating state automatically after fetching nft metadata, we are comparing the fetched metadata with current state and updating it only when it is different.

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 April 11, 2024 12:05
sethkfman added a commit to MetaMask/metamask-mobile that referenced this pull request Apr 11, 2024
#9206)

## **Description**

This PR fixes this issue
#9196.
Patch created considering this open PR on core
MetaMask/core#4143

When a user imports an account with Nfts then switches back and forth
betwen two accounts, at some point he will see nfts from one account
appear on another.

Link to core branch for the patch :
https://github.com/MetaMask/core/compare/patch/mobile-assets-controllers-v-12-0-0...patch/mobile-assets-controllers-v-12-0-0-nft-bug?expand=1


## **Related issues**

Fixes: #9196.
Core PR: MetaMask/core#4143

## **Manual testing steps**

1. Open app
2. Import an account that has nfts (use for example orangefox.eth)
3. Create a new account (You can either create a new account which will
be empty or use any account that you already have)
4. Switch back and forth between orangefox.eth and another account fast
and you will see nfts from orangefox.eth that appear on the other
account. (This should not happen on this PR)

## **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-mobile/assets/10994169/611bd2c5-b005-48ed-9926-6d72ff244816


### **After**

<!-- [screenshots/recordings] -->
Switching back and forth between accounts


https://github.com/MetaMask/metamask-mobile/assets/10994169/5c00c78d-f3f2-4080-b441-eaf9828aec13


## **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: sethkfman <10342624+sethkfman@users.noreply.github.com>
metamaskbot pushed a commit to MetaMask/metamask-mobile that referenced this pull request Apr 11, 2024
#9206)

## **Description**

This PR fixes this issue
#9196.
Patch created considering this open PR on core
MetaMask/core#4143

When a user imports an account with Nfts then switches back and forth
betwen two accounts, at some point he will see nfts from one account
appear on another.

Link to core branch for the patch :
https://github.com/MetaMask/core/compare/patch/mobile-assets-controllers-v-12-0-0...patch/mobile-assets-controllers-v-12-0-0-nft-bug?expand=1


## **Related issues**

Fixes: #9196.
Core PR: MetaMask/core#4143

## **Manual testing steps**

1. Open app
2. Import an account that has nfts (use for example orangefox.eth)
3. Create a new account (You can either create a new account which will
be empty or use any account that you already have)
4. Switch back and forth between orangefox.eth and another account fast
and you will see nfts from orangefox.eth that appear on the other
account. (This should not happen on this PR)

## **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-mobile/assets/10994169/611bd2c5-b005-48ed-9926-6d72ff244816


### **After**

<!-- [screenshots/recordings] -->
Switching back and forth between accounts


https://github.com/MetaMask/metamask-mobile/assets/10994169/5c00c78d-f3f2-4080-b441-eaf9828aec13


## **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: sethkfman <10342624+sethkfman@users.noreply.github.com>
@sahar-fehri sahar-fehri requested a review from a team as a code owner May 7, 2024 20:40
@sahar-fehri sahar-fehri merged commit 07f7701 into main May 7, 2024
143 checks passed
@sahar-fehri sahar-fehri deleted the fix/update-nfts-only-when-different-from-state branch May 7, 2024 20:43
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants