-
Notifications
You must be signed in to change notification settings - Fork 5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: move
getProviderConfig
out of ducks/metamask.js
to `sha…
…red/selectors/networks.ts` (#27646) Converts some from functions from JS to TS. Also updates some functions to match the actual expect return values. Why only these functions? I'm trying to solve circular dependency issues. `getProviderConfig` is so widely used in the codebase, and makes use of multiple selectors itself, it makes it very complicated to untangle. I've put it in a new file (`seelctors/networks.ts`) just to, hopefully temporarily, simplify untangling other circular dependency issues. <!-- Please submit this PR as a draft initially. Do not mark it as "Ready for review" until the template has been completely filled out, and PR status checks have passed at least once. ## **Description** <!-- Write a short description of the changes included in this pull request, also include relevant motivation and context. Have in mind the following questions: 1. What is the reason for the change? [![Open in GitHub Codespaces](https://github.com/codespaces/badge.svg)](https://codespaces.new/MetaMask/metamask-extension/pull/27646?quickstart=1) ## **Related issues** Fixes: ## **Manual testing steps** 1. Go to this page... 2. 3. ## **Screenshots/Recordings** ### **Before** ### **After** ## **Pre-merge author checklist** - [ ] I've followed [MetaMask Contributor Docs](https://github.com/MetaMask/contributor-docs) and [MetaMask Extension Coding Standards](https://github.com/MetaMask/metamask-extension/blob/develop/.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-extension/blob/develop/.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. -->
- Loading branch information
1 parent
e748576
commit 5116c32
Showing
98 changed files
with
462 additions
and
326 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,114 @@ | ||
import { | ||
RpcEndpointType, | ||
type NetworkConfiguration, | ||
type NetworkState as _NetworkState, | ||
} from '@metamask/network-controller'; | ||
import { createSelector } from 'reselect'; | ||
import { NetworkStatus } from '../../constants/network'; | ||
import { createDeepEqualSelector } from './util'; | ||
|
||
export type NetworkState = { metamask: _NetworkState }; | ||
|
||
export type NetworkConfigurationsState = { | ||
metamask: { | ||
networkConfigurations: Record< | ||
string, | ||
MetaMaskExtensionNetworkConfiguration | ||
>; | ||
}; | ||
}; | ||
|
||
export type SelectedNetworkClientIdState = { | ||
metamask: { | ||
selectedNetworkClientId: string; | ||
}; | ||
}; | ||
|
||
export type MetaMaskExtensionNetworkConfiguration = NetworkConfiguration; | ||
|
||
export type NetworkConfigurationsByChainIdState = { | ||
metamask: Pick<_NetworkState, 'networkConfigurationsByChainId'>; | ||
}; | ||
|
||
export type ProviderConfigState = NetworkConfigurationsByChainIdState & | ||
SelectedNetworkClientIdState; | ||
|
||
export const getNetworkConfigurationsByChainId = createDeepEqualSelector( | ||
(state: NetworkConfigurationsByChainIdState) => | ||
state.metamask.networkConfigurationsByChainId, | ||
(networkConfigurationsByChainId) => networkConfigurationsByChainId, | ||
); | ||
|
||
export function getSelectedNetworkClientId( | ||
state: SelectedNetworkClientIdState, | ||
) { | ||
return state.metamask.selectedNetworkClientId; | ||
} | ||
|
||
/** | ||
* Get the provider configuration for the current selected network. | ||
* | ||
* @param state - Redux state object. | ||
*/ | ||
export const getProviderConfig = createSelector( | ||
(state: ProviderConfigState) => getNetworkConfigurationsByChainId(state), | ||
getSelectedNetworkClientId, | ||
(networkConfigurationsByChainId, selectedNetworkClientId) => { | ||
for (const network of Object.values(networkConfigurationsByChainId)) { | ||
for (const rpcEndpoint of network.rpcEndpoints) { | ||
if (rpcEndpoint.networkClientId === selectedNetworkClientId) { | ||
const blockExplorerUrl = | ||
network.defaultBlockExplorerUrlIndex === undefined | ||
? undefined | ||
: network.blockExplorerUrls?.[ | ||
network.defaultBlockExplorerUrlIndex | ||
]; | ||
|
||
return { | ||
chainId: network.chainId, | ||
ticker: network.nativeCurrency, | ||
rpcPrefs: { ...(blockExplorerUrl && { blockExplorerUrl }) }, | ||
type: | ||
rpcEndpoint.type === RpcEndpointType.Custom | ||
? 'rpc' | ||
: rpcEndpoint.networkClientId, | ||
...(rpcEndpoint.type === RpcEndpointType.Custom && { | ||
id: rpcEndpoint.networkClientId, | ||
nickname: network.name, | ||
rpcUrl: rpcEndpoint.url, | ||
}), | ||
}; | ||
} | ||
} | ||
} | ||
return undefined; // should not be reachable | ||
}, | ||
); | ||
|
||
export function getNetworkConfigurations( | ||
state: NetworkConfigurationsState, | ||
): Record<string, MetaMaskExtensionNetworkConfiguration> { | ||
return state.metamask.networkConfigurations; | ||
} | ||
|
||
/** | ||
* Returns true if the currently selected network is inaccessible or whether no | ||
* provider has been set yet for the currently selected network. | ||
* | ||
* @param state - Redux state object. | ||
*/ | ||
export function isNetworkLoading(state: NetworkState) { | ||
const selectedNetworkClientId = getSelectedNetworkClientId(state); | ||
return ( | ||
selectedNetworkClientId && | ||
state.metamask.networksMetadata[selectedNetworkClientId].status !== | ||
NetworkStatus.Available | ||
); | ||
} | ||
|
||
export function getInfuraBlocked(state: NetworkState) { | ||
return ( | ||
state.metamask.networksMetadata[getSelectedNetworkClientId(state)] | ||
.status === NetworkStatus.Blocked | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
7 changes: 2 additions & 5 deletions
7
ui/components/app/assets/asset-list/asset-list-control-bar/asset-list-control-bar.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.