-
-
Notifications
You must be signed in to change notification settings - Fork 246
feat(assets): implement comprehensive balance selectors for multichain account groups and wallets #6235
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(assets): implement comprehensive balance selectors for multichain account groups and wallets #6235
Conversation
Review the following changes in direct dependencies. Learn more about Socket for GitHub.
|
@metamaskbot publish-preview |
Preview builds have been published. See these instructions for more information about preview builds. Expand for full list of packages and versions.
|
@metamaskbot publish-preview |
Preview builds have been published. See these instructions for more information about preview builds. Expand for full list of packages and versions.
|
…es and integrate reselect - Introduced new selectors to aggregate balances for account groups and wallets, supporting both EVM and Solana accounts. - Implemented memoization using `reselect` to optimize performance and reduce unnecessary re-renders. - Updated `package.json` and `yarn.lock` to include `reselect` as a dependency. - Added comprehensive tests for the new selectors to ensure accuracy and reliability. This enhancement improves the efficiency of balance calculations across multiple account types, facilitating better user experience in managing multichain assets.
- Updated `yarn.lock` to reflect the new version of `@metamask/account-tree-controller` and removed `@metamask/multichain-account-service` from dependencies. - Enhanced `package.json` to include `@metamask/account-api` and `@metamask/base-controller` as devDependencies. - Refactored selectors to utilize the `AccountTreeController` for improved account group handling and balance aggregation. - Added tests to ensure the accuracy of the new selector implementations and edge case handling. These changes optimize the performance of balance calculations and ensure better integration with the updated account management structure.
…ce test coverage - Renamed selectors for clarity, changing `selectBalancesByAccountGroup` to `selectBalanceByAccountGroup` and similar for wallet and all wallets selectors. - Improved the structure of mock state in tests to better reflect the current account and wallet hierarchy. - Added comprehensive tests for the new selector implementations, ensuring accurate balance calculations and edge case handling. - Optimized selector logic to enhance performance and maintainability, leveraging memoization where applicable. These changes improve the readability and efficiency of the selector functions, ensuring better integration with the updated account management structure.
- Updated the `@metamask/account-api` dependency in `package.json` to version 0.7.0 for improved functionality. - Added `@metamask/account-api` to `yarn.lock` to ensure consistent dependency resolution. These changes enhance the assets controller's capabilities by leveraging the latest features and fixes from the updated account API.
…urrency support - Updated `createMockState` to accept user currency, allowing for more flexible test scenarios. - Added tests to verify balance calculations for specific account groups in multiple currencies (EUR, GBP). - Implemented a new helper function `convertUsdToUserCurrency` to handle currency conversion logic. - Ensured that selectors correctly fall back to USD when no conversion rate is available. These changes improve test coverage and ensure accurate balance calculations across different currencies, enhancing the robustness of the balance selectors.
…n tsconfig.build.json - Included a reference to the `account-tree-controller` in the `tsconfig.build.json` file to improve project structure and facilitate better integration between controllers. This change enhances the modularity of the assets controller by establishing a clear dependency on the account tree controller.
… package.json and yarn.lock - Removed the direct dependency on `@metamask/account-api` from `package.json` and added it to `peerDependencies` to better reflect its usage. - Updated `yarn.lock` to ensure consistent resolution of the `@metamask/account-api` version. These changes streamline the dependency management for the assets controller, ensuring clarity in the project's structure and dependencies.
…ect dependency - Added `@metamask/account-api` back to `package.json` as a direct dependency to ensure its availability for the assets controller. - Updated `yarn.lock` to reflect this change and maintain consistent dependency resolution. This adjustment clarifies the assets controller's reliance on the account API, enhancing the project's dependency management.
…onal metadata - Updated `createMockState` to include `pinned` and `hidden` properties in account group metadata for improved test scenarios. - Added `accountGroupsMetadata` and `accountWalletsMetadata` to the mock state to better reflect the structure of account data. These changes enhance the test coverage for balance selectors by ensuring that the metadata is accurately represented, leading to more robust and reliable tests.
- Removed try-catch block and streamlined the logic for extracting wallet and group information from the account tree state. - Enhanced readability by directly returning early when wallet or group is not found, improving maintainability of the selector. These changes improve the clarity and efficiency of the `getInternalAccountsForGroup` selector, ensuring it adheres to best practices for functional programming.
- Updated the coverage thresholds for branches, lines, and statements in `jest.config.js` to 91, 98, and 98 respectively. - This change ensures that the coverage requirements are aligned with current project standards while maintaining a focus on code quality. These adjustments help streamline the testing process and clarify the expectations for code coverage in the assets controllers.
…n selectors - Enhanced the `selectBalanceByAccountGroup` selector to gracefully skip malformed balance values, preventing NaN propagation in calculations. - Added a new test case to verify that the selector correctly handles invalid balance data while returning valid results. These changes improve the robustness of the balance selectors by ensuring they can handle unexpected input without breaking functionality.
…urrency conversion - Refactored balance selectors tests to utilize direct conversion rates instead of relying on USD conversion. - Enhanced test cases to reflect accurate calculations for EUR and GBP, ensuring consistency with the new conversion logic. - Updated assertions to use `toBeCloseTo` for floating-point precision in balance comparisons. - Removed the `convertUsdToUserCurrency` helper function as it is no longer needed. These changes improve the accuracy and reliability of balance calculations in the selectors, aligning with the recent refactor of the conversion logic.
…rmalization - Introduced a new `AssetsSelectorState` type to standardize the state structure for assets controllers. - Created `extractControllerStates` function to normalize state extraction from various structures (mobile, extension, flat). - Updated balance selectors to utilize the new state adapter, improving maintainability and consistency across selectors. - Added comprehensive tests for the `extractControllerStates` function to ensure correct behavior across different state formats. These changes enhance the robustness and clarity of the state management in the assets controllers, aligning with best practices for functional programming.
…ng-api versions in yarn.lock - Added new version entries for `@metamask/account-api@npm:^0.7.0` and `@metamask/keyring-api@npm:^19.1.0` with their respective dependencies and checksums. - Updated the `@metamask/multichain-account-service` entry to specify the npm version. - These changes ensure that the project uses the latest compatible versions of the dependencies, improving stability and functionality.
….json and yarn.lock - Updated the version of `@metamask/account-api` from `^0.7.0` to `^0.9.0` in both `package.json` and `yarn.lock`. - This change ensures compatibility with the latest features and improvements in the account API, enhancing the overall functionality of the assets controllers.
6ed1531
to
037d305
Compare
@metamaskbot publish-preview |
Preview builds have been published. See these instructions for more information about preview builds. Expand for full list of packages and versions.
|
…tate access and remove state adapter - Refactored balance selectors to utilize direct state access for improved performance and stability, supporting both mobile and extension state structures. - Removed the `extractControllerStates` function and `AssetsSelectorState` type, simplifying the state management approach. - Updated tests to reflect changes in state structure and ensure compatibility across different formats. - Enhanced mock state creation functions for better clarity and maintainability. These changes streamline the balance selectors, aligning with best practices for functional programming and improving overall code quality.
… state structure - Modified balance selectors to access token balances using the updated structure: `tokenBalances[accountAddress][chainId][tokenAddress]`. - Adjusted mock state creation to align with the new structure, enhancing clarity and maintainability. - Updated tests to ensure compatibility with the revised state access patterns and to handle malformed balance values appropriately. These changes improve the performance and robustness of the balance selectors, adhering to best practices for functional programming.
@metamaskbot publish-preview |
Preview builds have been published. See these instructions for more information about preview builds. Expand for full list of packages and versions.
|
Preview builds have been published. See these instructions for more information about preview builds. Expand for full list of packages and versions.
|
…ve wallet balance calculations - Refactored balance selectors to create a comprehensive selector that aggregates balances for all wallets and groups, serving as the single source of truth for balance calculations. - Updated the `selectBalanceByAccountGroup` and `selectBalanceForAllWallets` functions to derive from the new comprehensive selector, ensuring proper memoization and improved performance. - Enhanced handling of wallet and group balances, including robust checks for malformed balance values to prevent NaN propagation. - These changes streamline balance calculations and improve the maintainability of the assets controllers, adhering to best practices for functional programming.
- Introduced a new test suite to verify the memoization behavior of balance selectors, ensuring that results are cached correctly for identical state inputs. - Added tests for `selectBalanceByAccountGroup`, `selectBalanceByWallet`, `selectBalanceForAllWallets`, and `selectBalanceForSelectedAccountGroup` to confirm that they return the same reference for the same state and different references when state values change. - These enhancements improve the reliability of the balance selectors by validating their performance optimizations and adherence to memoization best practices.
…lectors - Introduced a new test suite to cover various edge cases and error handling scenarios in balance selectors. - Added tests to ensure that selectors gracefully handle missing controller state, NaN balance values, and missing wallets or groups. - Enhanced the robustness of balance calculations by verifying that valid results are returned even when encountering malformed data or unexpected states. - These improvements ensure that the balance selectors maintain functionality and reliability under diverse conditions, adhering to best practices for error handling.
…alance selectors - Introduced new test cases to verify the handling of tokens with various decimal scenarios, including 0, undefined, null, and NaN decimals. - Ensured that balance calculations remain accurate and robust when encountering these edge cases, maintaining functionality under diverse conditions. - Updated the balance selector logic to utilize nullish coalescing for decimal handling, preventing NaN propagation and improving overall reliability. - These enhancements strengthen the error handling capabilities of the balance selectors, adhering to best practices for robust state management.
@metamaskbot publish-preview |
Preview builds have been published. See these instructions for more information about preview builds. Expand for full list of packages and versions.
|
## Explanation Bumps @metamask/assets-controllers to 73.1.0 to add comprehensive balance selectors for multichain account groups and wallets, along with dependency updates and bug fixes. ## References - [#6235](#6235) - Comprehensive balance selectors for multichain account groups and wallets - [#6248](#6248) - Bump keyring-api dependency - [#6242](#6242) - Fix DeFiPositionsController polling rate - [#6250](#6250) - Fix AccountTrackerController cache invalidation ## Checklist - [x] I've updated the test suite for new or updated code as appropriate - [x] I've updated documentation (JSDoc, Markdown, etc.) for new or updated code as appropriate - [x] I've communicated my changes to consumers by updating changelogs for packages I've changed, highlighting breaking changes as necessary - [x] I've prepared draft pull requests for clients and consumer packages to resolve any breaking changes
Explanation
This PR implements the
selectBalancesByAccountGroup
selector and related balance selectors for the assets-controllers package. The selectors provide functionality to calculate total balances for account groups, wallets, and all wallets in the user's selected currency.Mobile PR (Implementation) : MetaMask/metamask-mobile#18063
Current State and Need for Change
Currently, there's no centralized way to calculate total balances for account groups or wallets across multiple chains and account types. This makes it difficult for consumers to display aggregated balance information.
Solution
The new selectors provide:
selectBalanceByAccountGroup
- Calculate total balance for a specific account groupselectBalanceByWallet
- Calculate total balance for all groups in a walletselectBalanceForAllWallets
- Calculate total balance across all walletsselectBalanceForSelectedAccountGroup
- Calculate balance for the currently selected account groupTechnical Implementation
References
Checklist