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: initial setup for not globally selected chain ID (useChainId hook) #11753

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

Conversation

andreahaku
Copy link
Member

@andreahaku andreahaku commented Oct 11, 2024

Description

Initial test support for not globally selected chain ID applied to assets (token and NFTs) lists.

This PR creates a React hook (useChainId) that replaces the selectChainId selector used to get the globally selected chain Id.

Currently, the network badge for assets is derived from the globally selected chain ID in the app. This global chain ID is shimmed via the new hook, effectively making it equal to the chain ID selected for the entire session or dapp. However, with the support of the hook and by adding the support of the chain ID at the single component (token and NFT) level we can move towards supporting a per-dapp chain ID.

Related issues

Fixes: https://github.com/orgs/MetaMask/projects/85/views/2?pane=issue&itemId=82943990

Related PRs

Manual testing steps

  1. Go to this page...

Screenshots/Recordings

Before

As After

After

Simulator.Screen.Recording.-.iPhone.12.Pro.-.2024-10-11.at.14.26.31.mp4

Simulator Screenshot - iPhone 12 Pro - 2024-10-11 at 14 25 55
Simulator Screenshot - iPhone 12 Pro - 2024-10-11 at 14 25 38
Simulator Screenshot - iPhone 12 Pro - 2024-10-11 at 14 25 26

Pre-merge author checklist

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.

@github-actions github-actions bot added the team-sdk SDK team label Oct 11, 2024
@andreahaku andreahaku changed the title Initial setup for not globally selected chain ID chore: initial setup for not globally selected chain ID Oct 11, 2024
@andreahaku andreahaku force-pushed the feat/preparation_for_asset_chainId branch from da78ed8 to 9c967a8 Compare October 14, 2024 08:33
@andreahaku andreahaku marked this pull request as ready for review October 14, 2024 09:37
@andreahaku andreahaku requested review from a team as code owners October 14, 2024 09:37
@andreahaku andreahaku added needs-dev-review PR needs reviews from other engineers (in order to receive required approvals) Code Impact - Low Minor code change that can safely applied to the codebase team-wallet-ux and removed team-sdk SDK team labels Oct 14, 2024
@andreahaku andreahaku self-assigned this Oct 14, 2024
@andreahaku andreahaku added Priority - Medium Task with medium priority Run Smoke E2E Triggers smoke e2e on Bitrise labels Oct 14, 2024
Copy link
Contributor

github-actions bot commented Oct 14, 2024

https://bitrise.io/ Bitrise

✅✅✅ pr_smoke_e2e_pipeline passed on Bitrise! ✅✅✅

Commit hash: 9c967a8
Build link: https://app.bitrise.io/app/be69d4368ee7e86d/pipelines/6a912a86-98f6-4469-a3d3-ab295879299f

Note

  • You can kick off another pr_smoke_e2e_pipeline on Bitrise by removing and re-applying the Run Smoke E2E label on the pull request

@sahar-fehri
Copy link
Contributor

LGTM!

sahar-fehri
sahar-fehri previously approved these changes Oct 14, 2024
@andreahaku andreahaku changed the title chore: initial setup for not globally selected chain ID feat: initial setup for not globally selected chain ID Oct 17, 2024
@andreahaku andreahaku changed the title feat: initial setup for not globally selected chain ID feat: initial setup for not globally selected chain ID (useChainId hook) Oct 21, 2024
Copy link

sonarcloud bot commented Oct 22, 2024

Quality Gate Failed Quality Gate failed

Failed conditions
57.1% Coverage on New Code (required ≥ 60%)

See analysis details on SonarCloud

import { useSelector } from 'react-redux';
import { selectChainId } from '../selectors/networkController';

export const useChainId = () => {
Copy link
Member

@matthewwalsh0 matthewwalsh0 Oct 22, 2024

Choose a reason for hiding this comment

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

I understand the convenience of having this as re-usable hook, but does this not still couple our UX to a global or semi-global chain ID at any one time?

What logic do we predict could populate this?

What if we want to render two components at once on alternate chain IDs for example?

I can't speak for the rest of the mobile UX, but the pattern we're using in confirmations is relying on the subject or data of the confirmation as a source of truth.

For example, since signatures and transactions are created on a specific chain, we can persist that in the controller state and read that chain where needed in the confirmation components.

Copy link
Member Author

Choose a reason for hiding this comment

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

this will be able to return the selected chain ID from the of permitted chain ID (and the list of permitted chain IDs if needed) on the first phase.

When per-dapp network permissions will be ready, this will return the per-dapp selected chain ID and potentially also the list of the per-dapp permitted networks if needed.

This is the logic behind the creation of this hook

Copy link
Member Author

Choose a reason for hiding this comment

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

it can return 'per-dapp' chain Id or the mobile app chain Id coming not a specific dapp but a general state

Copy link
Member Author

Choose a reason for hiding this comment

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

and in theory components (like token lists and/or NFTs list) can iterate and show assets from all the permitted networks this way as what I've been doing is to have single elements of those lists have a "chainId" prop.

Copy link
Member

@matthewwalsh0 matthewwalsh0 Oct 22, 2024

Choose a reason for hiding this comment

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

But won't that excessively couple us to a dApp and a selected chain, where as in the final form of multi-chain, there won't be dApp selected chains, only requests on alternate chains.

If we treat the chain ID as a global or context based thing, does that not hinder the readability and dynamic nature of where the chain ID is coming from?

If we're looking at a signature or transaction, let's just look at its saved chain ID.

If we're viewing a NFT or ERC-20 token, they will have persisted chain ID also?

Why store it in a context / hook at all, if we can make it explicit with props or by reading it from the relevant state as needed?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Code Impact - Low Minor code change that can safely applied to the codebase needs-dev-review PR needs reviews from other engineers (in order to receive required approvals) Priority - Medium Task with medium priority Run Smoke E2E Triggers smoke e2e on Bitrise team-wallet-ux
Projects
Status: Needs dev review
Development

Successfully merging this pull request may close these issues.

3 participants