Skip to content

[Wallet/iOS] Fix #35986: Persist balance in Portfolio Assets #22029

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

Merged
merged 4 commits into from
Feb 14, 2024

Conversation

nuo-xu
Copy link
Contributor

@nuo-xu nuo-xu commented Feb 13, 2024

Resolves brave/brave-browser#35986

We are now caching asset's balance. They can be easily fetch from CD via token info or account info or both.
Cached balance will be refreshed under certain scenarios. they will be listed in the test plan.

Submitter Checklist:

  • I confirm that no security/privacy review is needed and no other type of reviews are needed, or that I have requested them
  • There is a ticket for my issue
  • Used Github auto-closing keywords in the PR description above
  • Wrote a good PR/commit description
  • Squashed any review feedback or "fixup" commits before merge, so that history is a record of what happened in the repo, not your PR
  • Added appropriate labels (QA/Yes or QA/No; release-notes/include or release-notes/exclude; OS/...) to the associated issue
  • Checked the PR locally:
    • npm run test -- brave_browser_tests, npm run test -- brave_unit_tests wiki
    • npm run presubmit wiki, npm run gn_check, npm run tslint
  • Ran git rebase master (if needed)

Reviewer Checklist:

  • A security review is not needed, or a link to one is included in the PR description
  • New files have MPL-2.0 license header
  • Adequate test coverage exists to prevent regressions
  • Major classes, functions and non-trivial code blocks are well-commented
  • Changes in component dependencies are properly reflected in gn
  • Code follows the style guide
  • Test plan is specified in PR before merging

After-merge Checklist:

Test Plan:

  • Upgrade (current target 1.63.x)
  1. install a older version of Brave (1.62.x and earlier)
  2. set up a wallet with some balance
  3. Dismiss wallet then open
  4. Notice every time wallet open triggers balance fetching
  5. upgrade Brave to the 1.63.x
  6. Open wallet you will notice there will be a loading time to fetch balance since we have never cached any
  7. Dismiss wallet and open again
  8. Notice the balance still being displayed with no loading.
  • New Wallet Set up
  1. Fresh install wallet and create a new wallet from onboarding
  2. All default user assets will appear in Portfolio with 0 balance.
  3. Dismiss the wallet then open it (without locking)
  4. There will be no loading in Portfolio.
  1. Delete Brave and fresh install Brave
  2. Restore a wallet from onboarding (Wallet A)
  3. You will see default user assets are being displayed and their balance are being fetched or in within couple seconds based on internet speed.
  4. Dismiss the wallet then open it (without locking)
  5. There will be no loading in Portfolio.
  6. Notice there is no auto-discovered assets got discovered. This is the issue mentioned earlier.
  7. Let's take advantage of this issue by checking if wallet will add and fetch balance of a token that we already know this wallet has
  8. Let's say we already know Wallet A has some balance of WETH on Polygon Mainnet
  9. Tap Edit Visible Asset to bring up the token list, and search for WETH
  10. Tap on WETH to mark it as visible asset
  11. Back to Portfolio, check WETH now appears and its balance is fetched.
  12. A workaround to let core give the rest of auto-discovery assets is go to NFTs tab, enable NFT auto-discovery in the prompt
  13. A spinner will show up indicating wallet starts discovering assets.
  14. The spinner under Assets also shows up if you navigate to Assets
  15. After a while, once the spinner disappears, some new assets will show up in Assets that this Wallet A owns with some balance. Please check if their balances were fetched correctly.
  16. repeat step 4-5
  17. Lock the wallet
  18. restore a new wallet (Wallet A)
  19. Check user assets and balance are fetched correctly,
  20. Repeat step 4-5
  • Add a primary or secondary account to wallet
  1. Group visible assets by Account in Portfolio
  2. add a primary account
  3. check the native token has been added to Portfolio and its balance is fetched "0"
  4. add a secondary account with some balance
  5. check all visible assets have the same coin type are displayed under this new account and their balances are fetched correctly
  • Add/Remove a new network
  1. Group visible assets by Group
  2. add/remove a custom network from wallet settings or a dapp
  3. Check if the new network group is added in Portfolio and its native asset is displayed in the group with its balance fetched correctly. (Or removed if the network is removed)
  • Add/Remove custom token
  1. add/remove a custom token from wallet settings or a dapp
  2. Check if the new token is displayed in Portfolio with its balance fetched correctly. (Or removed if the token is removed)
  • Check balance after a transaction is confirmed/error/dropped
  1. make a send transaction of token A from Portfolio
  2. after the transaction status becomes Confirmed
  3. check if token A balance is updated
  4. Not sure how to make a transaction error out or dropped. but Once a transaction status becomes error or dropped. Some gas fee was consumed so the native token balance is relatively decreased.

In summary, balances in Portfolio (Assets) will be refreshed when

  1. A wallet is created
  2. A wallet is restored
  3. When wallet is unlocked
  4. A account is created or added
  5. A network is added
  6. An asset is added
  7. An asset's visibility status is changed to be visible
  8. A transaction is confirmed or error out or dropped

Note: Transactions that are submitted by Dapps via signing request through wallet, since there is no access from Wallet to know the transaction status that is not created by Brave Wallet, it is not possible to determine when to refresh the balance based this transaction's status until we can observe all on-chain activity for an address. In this case, to refresh balance, user has to lock and unlock the wallet.

@nuo-xu nuo-xu added CI/skip-android Do not run CI builds for Android CI/skip-macos-x64 Do not run CI builds for macOS x64 CI/skip-windows-x64 Do not run CI builds for Windows x64 labels Feb 13, 2024
@nuo-xu nuo-xu requested a review from StephenHeaps February 13, 2024 18:14
@nuo-xu nuo-xu self-assigned this Feb 13, 2024
@nuo-xu nuo-xu requested a review from a team as a code owner February 13, 2024 18:14
@nuo-xu nuo-xu force-pushed the wallet/ios-persist-balance branch from cd7aa69 to 59ac294 Compare February 13, 2024 20:52
@nuo-xu nuo-xu force-pushed the wallet/ios-persist-balance branch from 59ac294 to 597cfbc Compare February 14, 2024 16:54
Copy link
Contributor

[puLL-Merge] - brave/brave-core@22029

Description

This PR introduces modifications to various Swift source files related to the Brave Wallet feature within the iOS application. It focuses on improving the handling of user assets and their balances, along with some structural cleanups like the removal of unnecessary callback functions and the addition of new functionalities for better management of blockchain assets and NFTs.

Changes

Changes

ios/brave-ios/Sources/BraveWallet/Crypto/Portfolio/AddCustomAssetView.swift

  • Removal: Callback function onNewAssetAdded after adding a user asset successfully.

ios/brave-ios/Sources/BraveWallet/Crypto/Portfolio/EditUserAssetsView.swift

  • Removal: Callback function assetsUpdated after editing user assets.

ios/brave-ios/Sources/BraveWallet/Crypto/Portfolio/PortfolioView.swift

  • Removal: Callback functions for updating assets immediately after adding or editing assets.

ios/brave-ios/Sources/BraveWallet/Crypto/Stores/CryptoStore.swift

  • Addition: Pass additional parameters to WalletUserAssetManager to include services like keyringService, rpcService, walletService, and txService.
  • Modification: Changed the way assets are updated after being auto-discovered.
  • Addition: Observers setup for changes from userAssetManager.
  • Formatting: Code cleanup for better readability and adhering to Swift style guidelines.

ios/brave-ios/Sources/BraveWallet/Crypto/Stores/NFTStore.swift

  • Addition: Implementation of balance and asset data update observer to trigger update method which seems to be related to updating NFT display information based on changes to user asset data.

ios/brave-ios/Sources/BraveWallet/Crypto/Stores/NetworkStore.swift

  • Modification: Improved handling for removing user assets' balances upon network deletion or changes.

ios/brave-ios/Sources/BraveWallet/Crypto/Stores/PortfolioStore.swift

  • Modifications and Additions: Implement observers for user asset data updates triggering portfolio updates. Also involved code structure improvements for readability.

ios/brave-ios/Sources/BraveWallet/Crypto/Stores/UserAssetsStore.swift

  • Addition: Logic to update user asset's visibility and initiate a fetch for the asset's balance upon visibility change.

ios/brave-ios/Sources/BraveWallet/WalletUserAssetManager.swift

  • Additions and Modifications: Extensive enhancements introducing balance management, adding observers for asset updates, and refreshing balance information. It reflects a significant improvement in how asset information and its changes are handled within the application.

Various Tests and Model Updates

  • Addition of WalletUserAssetBalanceTests.swift: Introduces testing for the new balance management functionalities added.
  • Modifications to Test Files: Adjustments to account for changes in the application's wallet functionalities, including the handling of locking mechanisms in test setups and cleanup.

Security Hotspots

  1. Sensitive Data Exposure: Ensure that the new balance management functionalities do not inadvertently expose sensitive user data, especially in the context of public blockchain information and user balances.

  2. Access Control: The modifications in callback functions and the introduction of balance management features should be reviewed to ensure they do not lead to unauthorized access to user assets or manipulation of asset information.

  3. Input Validation: With the introduction of new functionalities for handling assets, especially NFTs, ensuring robust input validation to prevent injection attacks or manipulation of asset data becomes critical.

@nuo-xu nuo-xu merged commit 340813e into master Feb 14, 2024
@nuo-xu nuo-xu deleted the wallet/ios-persist-balance branch February 14, 2024 17:45
@github-actions github-actions bot added this to the 1.64.x - Nightly milestone Feb 14, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
CI/skip-android Do not run CI builds for Android CI/skip-macos-x64 Do not run CI builds for macOS x64 CI/skip-windows-x64 Do not run CI builds for Windows x64 puLL-Merge
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Cache token balances
2 participants