-
Notifications
You must be signed in to change notification settings - Fork 5k
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
refactor: move getProviderConfig
out of ducks/metamask.js
to shared/selectors/networks.ts
#27646
Changes from 12 commits
0cbbd3a
73913e1
caf7ae7
6c1b27f
9800ba0
fa8600f
48d9abd
f841340
cfa208e
785d2e4
838a9bb
3e54a1e
027ca07
d1b2e6b
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -87,6 +87,8 @@ export const NETWORK_TYPES = { | |
LINEA_MAINNET: 'linea-mainnet', | ||
} as const; | ||
|
||
export type NetworkTypes = (typeof NETWORK_TYPES)[keyof typeof NETWORK_TYPES]; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This appears to be unused. Maybe not needed anymore? Or is this used by a later PR There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Likewise for |
||
|
||
/** | ||
* An object containing shortcut names for any non-builtin network. We need | ||
* this to be able to differentiate between networks that require custom | ||
|
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 }; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This seems a bit confusing 🤔 It's only nested under I guess if you just wanted a type representing the actual NetworkController state you'd import the type from there instead though. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Whatever we do, it will change again soon when we unflatten the |
||
|
||
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 | ||
); | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -52,13 +52,6 @@ jest.mock('../../../selectors', () => { | |
getAccountType: mockGetAccountType, | ||
getSelectedInternalAccount: mockGetSelectedAccount, | ||
getCurrentChainId: jest.fn(() => '0x5'), | ||
getSelectedNetworkClientId: jest.fn(() => 'goerli'), | ||
getNetworkConfigurationsByChainId: jest.fn(() => ({ | ||
'0x5': { | ||
chainId: '0x5', | ||
rpcEndpoints: [{ networkClientId: 'goerli' }], | ||
}, | ||
})), | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. These functions were moved, and the tests pass without these being mocked. so I removed them. |
||
}; | ||
}); | ||
|
||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why was this cast necessary? It looks like it already has the type
Hex
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It is not. I think this is just due to doing so many refactors and merges. I can fix it here, or fix it in a follow up PR.