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: fetch networks with transaction activity by accounts #5551

Open
wants to merge 51 commits into
base: main
Choose a base branch
from

Conversation

vinnyhoward
Copy link

@vinnyhoward vinnyhoward commented Mar 25, 2025

Explanation

Currently, our client manually adds networks and checks user activity one network at a time. This is inefficient and doesn't scale well.

This PR introduces a more efficient approach by:

  1. Adding a new API integration that fetches active networks for multiple accounts in a single request
  2. Managing network activity state through the controller
  3. Adding type-safe methods to handle both EVM and non-EVM accounts
  4. Extracting network fetching logic into a dedicated service layer

Key additions:

  • getNetworksWithTransactionActivityByAccounts: Fetches active networks for accounts
  • MultichainNetworkService: New service layer handling network activity fetching
  • Enhanced state management for network configurations and activity
  • Improved error handling and fallback mechanisms

The new implementation:

  • Reduces API calls through batch fetching
  • Improves separation of concerns with dedicated service layer
  • Enhances type safety and error handling
  • Provides better state management with fallbacks

PRs for Client Integration
Extension
Mobile

References

Related to #4469

Changelog

@metamask/multichain-network-controller

  • ADDED: New method getNetworksWithTransactionActivityByAccounts to fetch active networks for accounts

  • ADDED: New MultichainNetworkServiceController for handling network activity fetching

  • ADDED: New types for network activity state and responses

  • CHANGED: Enhanced error handling for network requests

  • CHANGED: Improved type safety for messenger actions

  • CHANGED: Updated state management for network activity

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
  • I've prepared draft pull requests for clients and consumer packages to resolve any breaking changes

… controller, renamed variables/keys to be more descriptive
@vinnyhoward vinnyhoward force-pushed the feat-4469-active-networks-method branch from d7c63ef to a038b1f Compare March 27, 2025 23:28
@vinnyhoward vinnyhoward changed the title feat: active networks feat: networks with activity Mar 31, 2025
@vinnyhoward vinnyhoward marked this pull request as ready for review March 31, 2025 18:19
@vinnyhoward vinnyhoward requested review from a team as code owners March 31, 2025 18:19
@salimtb
Copy link
Contributor

salimtb commented Apr 2, 2025

@metamask-bot @metamaskbot publish-preview

@salimtb
Copy link
Contributor

salimtb commented Apr 2, 2025

can you pls fix the broken unit test

… and fix Solana address validation

This commit improves the test coverage and fixes issues with Solana address validation
in the multichain network controller. The changes ensure proper handling of both EVM
and non-EVM network addresses.

Key Changes:
- Fixed Solana address format in tests to use base58 instead of hex format
- Updated test expectations for mixed EVM and non-EVM network responses
- Corrected validation regex for Solana addresses
…k/core into feat-4469-active-networks-method
vinnyhoward and others added 5 commits April 9, 2025 11:32
Co-authored-by: Charly Chevalier <charly.chevalier@consensys.net>
Co-authored-by: Charly Chevalier <charly.chevalier@consensys.net>
Co-authored-by: Charly Chevalier <charly.chevalier@consensys.net>
- Extract repeated mock addresses and CAIP IDs into reusable constants
- Add proper TypeScript types for mock data and test helpers
- Create reusable createMockAccount helper function
- Remove should from test descriptions for better readability
- Update test names to be more action-oriented and specific
Copy link
Contributor

@mcmire mcmire left a comment

Choose a reason for hiding this comment

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

One more thing and then I'm good :)

…troller

- Use PublicInterface pattern for network service dependency instead of concrete implementation.
- This improves testability and reduces coupling between the controller and service implementation.
mcmire
mcmire previously approved these changes Apr 9, 2025
Copy link
Contributor

@mcmire mcmire left a comment

Choose a reason for hiding this comment

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

LGTM!

- Replace regex pattern validation with CaipAccountIdStruct from @metamask/utils
- Remove assertCaipAccountIds utility function in favor of superstruct validation
- Update error handling in MultichainNetworkService to use assert for better errors
- Fix Solana chain reference in tests to use proper mainnet identifier

This change leverages existing superstruct validation from utils package,
reducing duplicate validation logic and providing better error messages.
@vinnyhoward vinnyhoward changed the title feat: networks with activity feat: fetch networks with transaction activity Apr 10, 2025
@vinnyhoward vinnyhoward changed the title feat: fetch networks with transaction activity feat: fetch networks with transaction activity by accounts Apr 10, 2025
Copy link
Contributor

@ccharly ccharly left a comment

Choose a reason for hiding this comment

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

Final review round 💪

I did left a bunch of suggestions (mainly for tests that I didn't review during last reviews)

The code looks really great now, thanks a lot for making all those changes 😄

Comment on lines +99 to +117
it('uses default block explorer index when undefined', () => {
const network: NetworkConfiguration = {
chainId: '0x1',
name: 'Ethereum Mainnet',
nativeCurrency: 'ETH',
blockExplorerUrls: ['https://etherscan.io'],
defaultBlockExplorerUrlIndex: undefined,
rpcEndpoints: [],
defaultRpcEndpointIndex: 0,
};
expect(toMultichainNetworkConfiguration(network)).toStrictEqual({
chainId: 'eip155:1',
isEvm: true,
name: 'Ethereum Mainnet',
nativeCurrency: 'ETH',
blockExplorerUrls: ['https://etherscan.io'],
defaultBlockExplorerUrlIndex: 0,
});
});
Copy link
Contributor

Choose a reason for hiding this comment

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

Out of curiosity, was this a missing test? It doesn't feel related to this PR's scope 😄 (that's probably ok, especially this was missing 👍)

Copy link
Author

Choose a reason for hiding this comment

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

Oh I just saw a missing test case and decided to add it. Might as well add it if I'm here but I can remove since it doesn't relate to the scope

vinnyhoward and others added 9 commits April 11, 2025 12:57
…rvice/MultichainNetworkService.test.ts

Co-authored-by: Charly Chevalier <charly.chevalier@consensys.net>
…st.ts

Co-authored-by: Charly Chevalier <charly.chevalier@consensys.net>
…st.ts

Co-authored-by: Charly Chevalier <charly.chevalier@consensys.net>
…st.ts

Co-authored-by: Charly Chevalier <charly.chevalier@consensys.net>
…st.ts

Co-authored-by: Charly Chevalier <charly.chevalier@consensys.net>
Co-authored-by: Charly Chevalier <charly.chevalier@consensys.net>
…ntroller/MultichainNetworkController.test.ts

Co-authored-by: Charly Chevalier <charly.chevalier@consensys.net>
…ntroller/MultichainNetworkController.test.ts

Co-authored-by: Charly Chevalier <charly.chevalier@consensys.net>
…ntroller/MultichainNetworkController.test.ts

Co-authored-by: Charly Chevalier <charly.chevalier@consensys.net>
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.

5 participants